2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #include "SampleCode.h"
9 #include "SkBlurMaskFilter.h"
11 #include "SkColorShader.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
19 #include "SkColorPriv.h"
20 #include "SkColorFilter.h"
22 #include "SkTypeface.h"
23 #include "SkTextBox.h"
28 extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
30 #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
31 extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
34 static const char gText[] =
35 "When in the Course of human events it becomes necessary for one people "
36 "to dissolve the political bands which have connected them with another "
37 "and to assume among the powers of the earth, the separate and equal "
38 "station to which the Laws of Nature and of Nature's God entitle them, "
39 "a decent respect to the opinions of mankind requires that they should "
40 "declare the causes which impel them to the separation.";
42 class TextBoxView : public SampleView {
45 #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
47 sk_bzero(&lf, sizeof(lf));
49 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
51 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
52 // we assert that different sizes should not affect which face we get
60 // overrides from SkEventSink
61 virtual bool onQuery(SkEvent* evt) {
62 if (SampleCode::TitleQ(*evt)) {
63 SampleCode::TitleR(evt, "TextBox");
66 return this->INHERITED::onQuery(evt);
69 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
70 SkAutoCanvasRestore acr(canvas, true);
72 canvas->clipRect(SkRect::MakeWH(w, h));
73 canvas->drawColor(bg);
76 tbox.setMode(SkTextBox::kLineBreak_Mode);
77 tbox.setBox(margin, margin,
78 w - margin, h - margin);
79 tbox.setSpacing(SkIntToScalar(3)/3, 0);
82 paint.setAntiAlias(true);
83 paint.setLCDRenderText(true);
85 tbox.setText(gText, strlen(gText), paint);
87 for (int i = 9; i < 24; i += 2) {
88 paint.setTextSize(SkIntToScalar(i));
90 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
94 virtual void onDrawContent(SkCanvas* canvas) {
95 SkScalar width = this->width() / 3;
96 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
97 canvas->translate(width, 0);
98 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
99 canvas->translate(width, 0);
100 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
101 canvas->translate(0, this->height()/2);
102 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
106 typedef SampleView INHERITED;
109 //////////////////////////////////////////////////////////////////////////////
111 static SkView* MyFactory() { return new TextBoxView; }
112 static SkViewRegister reg(MyFactory);