• 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 
8 
9 /* Tests text rendering with LCD and subpixel rendering turned on and off.
10  */
11 
12 #include "gm.h"
13 #include "SkCanvas.h"
14 #include "SkPicture.h"
15 #include "SkPictureImageFilter.h"
16 #include "SkPictureRecorder.h"
17 #include "SkSurface.h"
18 
19 
20 class LcdTextGM : public skiagm::GM {
21 public:
LcdTextGM()22     LcdTextGM() {
23         const int pointSize = 36;
24         textHeight = SkIntToScalar(pointSize);
25     }
26 
27 protected:
28 
onShortName()29     SkString onShortName() {
30         SkString name("lcdtext");
31         name.append(sk_tool_utils::major_platform_os_name());
32         return name;
33     }
34 
onISize()35     SkISize onISize() { return SkISize::Make(640, 480); }
36 
onDraw(SkCanvas * canvas)37     virtual void onDraw(SkCanvas* canvas) {
38 
39         y = textHeight;
40         drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
41                  true,  true);
42         drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
43                  true,  false);
44         drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
45                  false, true);
46         drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
47                  false, false);
48     }
49 
drawText(SkCanvas * canvas,const SkString & string,bool subpixelTextEnabled,bool lcdRenderTextEnabled)50     void drawText(SkCanvas* canvas, const SkString& string,
51                   bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
52         SkPaint paint;
53         paint.setColor(SK_ColorBLACK);
54         paint.setDither(true);
55         paint.setAntiAlias(true);
56         paint.setSubpixelText(subpixelTextEnabled);
57         paint.setLCDRenderText(lcdRenderTextEnabled);
58         paint.setTextSize(textHeight);
59 
60         canvas->drawText(string.c_str(), string.size(), 0, y, paint);
61         y += textHeight;
62     }
63 
64 private:
65     typedef skiagm::GM INHERITED;
66     SkScalar y, textHeight;
67 };
68 
69 /*
70  *  Skia will automatically disable LCD requests if the total size exceeds some limit
71  *  (hard coded in this test for now, as it is now avaiable as an API)
72  *
73  *  Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
74  */
75 class LcdTextSizeGM : public skiagm::GM {
76     enum {
77         kLCDTextSizeLimit = 48
78     };
79 
ScaleAbout(SkCanvas * canvas,SkScalar sx,SkScalar sy,SkScalar px,SkScalar py)80     static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
81         SkMatrix m;
82         m.setScale(sx, sy, px, py);
83         canvas->concat(m);
84     }
85 
86 public:
LcdTextSizeGM()87     LcdTextSizeGM() {}
88 
89 protected:
onShortName()90     SkString onShortName() {
91         return SkString("lcdtextsize");
92     }
93 
onISize()94     SkISize onISize() { return SkISize::Make(320, 120); }
95 
onDraw(SkCanvas * canvas)96     virtual void onDraw(SkCanvas* canvas) {
97         const char* lcd_text = "LCD";
98         const char* gray_text = "GRAY";
99 
100         SkPaint paint;
101         paint.setAntiAlias(true);
102         paint.setLCDRenderText(true);
103 
104         const struct {
105             SkPoint     fLoc;
106             SkScalar    fTextSize;
107             SkScalar    fScale;
108             const char* fText;
109         } rec[] = {
110             { {  10,  50 }, kLCDTextSizeLimit - 1,     1,  lcd_text },
111             { { 160,  50 }, kLCDTextSizeLimit + 1,     1,  gray_text },
112             { {  10, 100 }, kLCDTextSizeLimit / 2, 1.99f,  lcd_text },
113             { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f,  gray_text },
114         };
115 
116         for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
117             const SkPoint loc = rec[i].fLoc;
118             SkAutoCanvasRestore acr(canvas, true);
119 
120             paint.setTextSize(rec[i].fTextSize);
121             ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
122             canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint);
123         }
124     }
125 
126 private:
127     typedef skiagm::GM INHERITED;
128 };
129 DEF_GM( return new LcdTextGM; )
DEF_GM(return new LcdTextSizeGM;)130 DEF_GM( return new LcdTextSizeGM; )
131 
132 ///////////////////////////////////////////////////////////////////////////////////////////////////
133 
134 DEF_SIMPLE_GM(savelayer_lcdtext, canvas, 620, 260) {
135     SkPaint paint;
136     paint.setAntiAlias(true);
137     paint.setLCDRenderText(true);
138     paint.setTextSize(20);
139 
140     canvas->drawText("Hamburgefons", 12, 30, 30, paint);
141 
142     const bool gPreserveLCDText[] = { false, true };
143 
144     canvas->translate(0, 20);
145     for (auto preserve : gPreserveLCDText) {
146         preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr)
147                  : canvas->saveLayer(nullptr, nullptr);
148         canvas->drawText("Hamburgefons", 12, 30, 60, paint);
149 
150         SkPaint p;
151         p.setColor(0xFFCCCCCC);
152         canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p);
153         canvas->drawText("Hamburgefons", 12, 30, 90, paint);
154 
155         canvas->restore();
156         canvas->translate(0, 80);
157     }
158 }
159