• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Sample.h"
8 
9 #include "SkBlurMaskFilter.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkColorShader.h"
14 #include "SkGradientShader.h"
15 #include "SkGraphics.h"
16 #include "SkOSFile.h"
17 #include "SkPath.h"
18 #include "SkRandom.h"
19 #include "SkRegion.h"
20 #include "SkShader.h"
21 #include "SkShaper.h"
22 #include "SkStream.h"
23 #include "SkTextBlob.h"
24 #include "SkTime.h"
25 #include "SkTypeface.h"
26 #include "SkUTF.h"
27 
28 extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
29 
30 #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
31 extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
32 #endif
33 
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.";
41 
42 class TextBoxView : public Sample {
43 public:
TextBoxView()44     TextBoxView() {
45 #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
46         LOGFONT lf;
47         sk_bzero(&lf, sizeof(lf));
48         lf.lfHeight = 9;
49         SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
50         lf.lfHeight = 12;
51         SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
52         // we assert that different sizes should not affect which face we get
53         SkASSERT(tf0 == tf1);
54         tf0->unref();
55         tf1->unref();
56 #endif
57     }
58 
59 protected:
onQuery(Sample::Event * evt)60     bool onQuery(Sample::Event* evt) override {
61         if (Sample::TitleQ(*evt)) {
62             Sample::TitleR(evt, "TextBox");
63             return true;
64         }
65         return this->INHERITED::onQuery(evt);
66     }
67 
drawTest(SkCanvas * canvas,SkScalar w,SkScalar h,SkColor fg,SkColor bg)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 
74         SkShaper shaper(nullptr);
75 
76         SkScalar margin = 20;
77 
78         SkPaint paint;
79         paint.setColor(fg);
80 
81         for (int i = 9; i < 24; i += 2) {
82             SkTextBlobBuilderRunHandler builder;
83             SkFont font(nullptr, SkIntToScalar(i));
84             font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
85 
86             SkPoint end = shaper.shape(&builder, font, gText, strlen(gText), true,
87                                        { margin, margin }, w - margin);
88             canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
89 
90             canvas->translate(0, end.y());
91         }
92     }
93 
onDrawContent(SkCanvas * canvas)94     void onDrawContent(SkCanvas* canvas) override {
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);
103     }
104 
105 private:
106     typedef Sample INHERITED;
107 };
108 
109 //////////////////////////////////////////////////////////////////////////////
110 
111 DEF_SAMPLE( return new TextBoxView(); )
112