Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / gm / lcdtext.cpp
1
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10 /* Tests text rendering with LCD and subpixel rendering turned on and off.
11  */
12
13 #include "gm.h"
14 #include "SkCanvas.h"
15
16 class LcdTextGM : public skiagm::GM {
17 public:
18     LcdTextGM() {
19         const int pointSize = 36;
20         textHeight = SkIntToScalar(pointSize);
21     }
22     
23 protected:
24     
25     SkString onShortName() {
26         return SkString("lcdtext");
27     }
28     
29     SkISize onISize() { return SkISize::Make(640, 480); }
30     
31     virtual void onDraw(SkCanvas* canvas) {
32         
33         y = textHeight;
34         drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
35                  true,  true);
36         drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
37                  true,  false);
38         drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
39                  false, true);
40         drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
41                  false, false);
42     }
43     
44     void drawText(SkCanvas* canvas, const SkString& string,
45                   bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
46         SkPaint paint;
47         paint.setColor(SK_ColorBLACK);
48         paint.setDither(true);
49         paint.setAntiAlias(true);
50         sk_tool_utils::set_portable_typeface(&paint);
51         paint.setSubpixelText(subpixelTextEnabled);
52         paint.setLCDRenderText(lcdRenderTextEnabled);
53         paint.setTextSize(textHeight);
54         
55         canvas->drawText(string.c_str(), string.size(), 0, y, paint);
56         y += textHeight;
57     }
58     
59 private:
60     typedef skiagm::GM INHERITED;
61     SkScalar y, textHeight;
62 };
63
64 /*
65  *  Skia will automatically disable LCD requests if the total size exceeds some limit
66  *  (hard coded in this test for now, as it is now avaiable as an API)
67  *
68  *  Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
69  */
70 class LcdTextSizeGM : public skiagm::GM {
71     enum {
72         kLCDTextSizeLimit = 48
73     };
74
75     static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
76         SkMatrix m;
77         m.setScale(sx, sy, px, py);
78         canvas->concat(m);
79     }
80
81 public:
82     LcdTextSizeGM() {}
83     
84 protected:
85     SkString onShortName() {
86         return SkString("lcdtextsize");
87     }
88     
89     SkISize onISize() { return SkISize::Make(320, 120); }
90     
91     virtual void onDraw(SkCanvas* canvas) {
92         const char* lcd_text = "LCD";
93         const char* gray_text = "GRAY";
94
95         SkPaint paint;
96         paint.setAntiAlias(true);
97         paint.setLCDRenderText(true);
98
99         const struct {
100             SkPoint     fLoc;
101             SkScalar    fTextSize;
102             SkScalar    fScale;
103             const char* fText;
104         } rec[] = {
105             { {  10,  50 }, kLCDTextSizeLimit - 1,     1,  lcd_text },
106             { { 160,  50 }, kLCDTextSizeLimit + 1,     1,  gray_text },
107             { {  10, 100 }, kLCDTextSizeLimit / 2, 1.99f,  lcd_text },
108             { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f,  gray_text },
109         };
110
111         for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
112             const SkPoint loc = rec[i].fLoc;
113             SkAutoCanvasRestore acr(canvas, true);
114
115             paint.setTextSize(rec[i].fTextSize);
116             ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
117             canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint);
118         }
119     }
120
121 private:
122     typedef skiagm::GM INHERITED;
123 };
124
125 ///////////////////////////////////////////////////////////////////////////////
126
127 DEF_GM( return new LcdTextGM; )
128 DEF_GM( return new LcdTextSizeGM; )