• Home
  • History
  • Annotate
  • 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 "sk_tool_utils.h"
14 #include "SkCanvas.h"
15 #include "SkPicture.h"
16 #include "SkPictureImageFilter.h"
17 #include "SkPictureRecorder.h"
18 #include "SkSurface.h"
19 
20 
21 class LcdTextGM : public skiagm::GM {
22 public:
LcdTextGM()23     LcdTextGM() {
24         const int pointSize = 36;
25         textHeight = SkIntToScalar(pointSize);
26     }
27 
28 protected:
29 
onShortName()30     SkString onShortName() {
31         return SkString("lcdtext");
32     }
33 
onISize()34     SkISize onISize() { return SkISize::Make(640, 480); }
35 
onDraw(SkCanvas * canvas)36     virtual void onDraw(SkCanvas* canvas) {
37 
38         y = textHeight;
39         drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
40                  true,  true);
41         drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
42                  true,  false);
43         drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
44                  false, true);
45         drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
46                  false, false);
47     }
48 
drawText(SkCanvas * canvas,const SkString & string,bool subpixelTextEnabled,bool lcdRenderTextEnabled)49     void drawText(SkCanvas* canvas, const SkString& string,
50                   bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
51         SkPaint paint;
52         paint.setColor(SK_ColorBLACK);
53         paint.setDither(true);
54         SkFont font(nullptr, textHeight);
55         if (subpixelTextEnabled) {
56             font.setSubpixel(true);
57         }
58         if (lcdRenderTextEnabled) {
59             font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
60         }
61         canvas->drawString(string, 0, y, font, paint);
62         y += textHeight;
63     }
64 
65 private:
66     typedef skiagm::GM INHERITED;
67     SkScalar y, textHeight;
68 };
69 
70 /*
71  *  Skia will automatically disable LCD requests if the total size exceeds some limit
72  *  (hard coded in this test for now, as it is now avaiable as an API)
73  *
74  *  Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
75  */
76 class LcdTextSizeGM : public skiagm::GM {
77     enum {
78         kLCDTextSizeLimit = 48
79     };
80 
ScaleAbout(SkCanvas * canvas,SkScalar sx,SkScalar sy,SkScalar px,SkScalar py)81     static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
82         SkMatrix m;
83         m.setScale(sx, sy, px, py);
84         canvas->concat(m);
85     }
86 
87 public:
LcdTextSizeGM()88     LcdTextSizeGM() {}
89 
90 protected:
onShortName()91     SkString onShortName() {
92         return SkString("lcdtextsize");
93     }
94 
onISize()95     SkISize onISize() { return SkISize::Make(320, 120); }
96 
onDraw(SkCanvas * canvas)97     virtual void onDraw(SkCanvas* canvas) {
98         const char* lcd_text = "LCD";
99         const char* gray_text = "GRAY";
100 
101         const struct {
102             SkPoint     fLoc;
103             SkScalar    fTextSize;
104             SkScalar    fScale;
105             const char* fText;
106         } rec[] = {
107             { {  10,  50 }, kLCDTextSizeLimit - 1,     1,  lcd_text },
108             { { 160,  50 }, kLCDTextSizeLimit + 1,     1,  gray_text },
109             { {  10, 100 }, kLCDTextSizeLimit / 2, 1.99f,  lcd_text },
110             { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f,  gray_text },
111         };
112 
113         for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
114             const SkPoint loc = rec[i].fLoc;
115             SkAutoCanvasRestore acr(canvas, true);
116 
117             SkFont font(nullptr, rec[i].fTextSize);
118             font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
119 
120             ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
121             canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint());
122         }
123     }
124 
125 private:
126     typedef skiagm::GM INHERITED;
127 };
128 DEF_GM( return new LcdTextGM; )
129 DEF_GM( return new LcdTextSizeGM; )
130