1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SampleCode.h"
8#include "SkView.h"
9#include "SkBlurMaskFilter.h"
10#include "SkCanvas.h"
11#include "SkColorShader.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkPath.h"
15#include "SkRandom.h"
16#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
19#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
23#include "SkTextBox.h"
24#include "SkOSFile.h"
25#include "SkStream.h"
26
27extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
28
29#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
30extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
31#endif
32
33static const char gText[] =
34    "When in the Course of human events it becomes necessary for one people "
35    "to dissolve the political bands which have connected them with another "
36    "and to assume among the powers of the earth, the separate and equal "
37    "station to which the Laws of Nature and of Nature's God entitle them, "
38    "a decent respect to the opinions of mankind requires that they should "
39    "declare the causes which impel them to the separation.";
40
41class TextBoxView : public SampleView {
42public:
43    TextBoxView() {
44#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
45        LOGFONT lf;
46        sk_bzero(&lf, sizeof(lf));
47        lf.lfHeight = 9;
48        SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
49        lf.lfHeight = 12;
50        SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
51        // we assert that different sizes should not affect which face we get
52        SkASSERT(tf0 == tf1);
53        tf0->unref();
54        tf1->unref();
55#endif
56    }
57
58protected:
59    // overrides from SkEventSink
60    virtual bool onQuery(SkEvent* evt)  {
61        if (SampleCode::TitleQ(*evt)) {
62            SampleCode::TitleR(evt, "TextBox");
63            return true;
64        }
65        return this->INHERITED::onQuery(evt);
66    }
67
68    void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
69        SkAutoCanvasRestore acr(canvas, true);
70
71        canvas->clipRect(SkRect::MakeWH(w, h));
72        canvas->drawColor(bg);
73        SkScalar margin = 20;
74        SkTextBox tbox;
75        tbox.setMode(SkTextBox::kLineBreak_Mode);
76        tbox.setBox(margin, margin,
77                    w - margin, h - margin);
78        tbox.setSpacing(SkIntToScalar(3)/3, 0);
79
80        SkPaint paint;
81        paint.setAntiAlias(true);
82        paint.setLCDRenderText(true);
83        paint.setColor(fg);
84        tbox.setText(gText, strlen(gText), paint);
85
86        for (int i = 9; i < 24; i += 2) {
87            paint.setTextSize(SkIntToScalar(i));
88            tbox.draw(canvas);
89            canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
90        }
91    }
92
93    virtual void onDrawContent(SkCanvas* canvas) {
94        SkScalar width = this->width() / 3;
95        drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
96        canvas->translate(width, 0);
97        drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
98        canvas->translate(width, 0);
99        drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
100        canvas->translate(0, this->height()/2);
101        drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
102    }
103
104private:
105    typedef SampleView INHERITED;
106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110static SkView* MyFactory() { return new TextBoxView; }
111static SkViewRegister reg(MyFactory);
112