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.
9 /* Tests text rendering with LCD and subpixel rendering turned on and off.
13 #include "sk_tool_utils.h"
15 #include "SkPicture.h"
16 #include "SkPictureImageFilter.h"
17 #include "SkPictureRecorder.h"
18 #include "SkSurface.h"
21 class LcdTextGM : public skiagm::GM {
24 const int pointSize = 36;
25 textHeight = SkIntToScalar(pointSize);
30 SkString onShortName() {
31 SkString name("lcdtext");
32 name.append(sk_tool_utils::major_platform_os_name());
36 SkISize onISize() { return SkISize::Make(640, 480); }
38 virtual void onDraw(SkCanvas* canvas) {
41 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
43 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
45 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
47 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
51 void drawText(SkCanvas* canvas, const SkString& string,
52 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
54 paint.setColor(SK_ColorBLACK);
55 paint.setDither(true);
56 paint.setAntiAlias(true);
57 paint.setSubpixelText(subpixelTextEnabled);
58 paint.setLCDRenderText(lcdRenderTextEnabled);
59 paint.setTextSize(textHeight);
61 canvas->drawString(string, 0, y, paint);
66 typedef skiagm::GM INHERITED;
67 SkScalar y, textHeight;
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)
74 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
76 class LcdTextSizeGM : public skiagm::GM {
78 kLCDTextSizeLimit = 48
81 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
83 m.setScale(sx, sy, px, py);
91 SkString onShortName() {
92 return SkString("lcdtextsize");
95 SkISize onISize() { return SkISize::Make(320, 120); }
97 virtual void onDraw(SkCanvas* canvas) {
98 const char* lcd_text = "LCD";
99 const char* gray_text = "GRAY";
102 paint.setAntiAlias(true);
103 paint.setLCDRenderText(true);
111 { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
112 { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
113 { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
114 { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
117 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
118 const SkPoint loc = rec[i].fLoc;
119 SkAutoCanvasRestore acr(canvas, true);
121 paint.setTextSize(rec[i].fTextSize);
122 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
123 canvas->drawString(rec[i].fText, loc.x(), loc.y(), paint);
128 typedef skiagm::GM INHERITED;
130 DEF_GM( return new LcdTextGM; )
131 DEF_GM( return new LcdTextSizeGM; )
133 ///////////////////////////////////////////////////////////////////////////////////////////////////
135 DEF_SIMPLE_GM(savelayer_lcdtext, canvas, 620, 260) {
137 paint.setAntiAlias(true);
138 paint.setLCDRenderText(true);
139 paint.setTextSize(20);
141 canvas->drawString("Hamburgefons", 30, 30, paint);
143 const bool gPreserveLCDText[] = { false, true };
145 canvas->translate(0, 20);
146 for (auto preserve : gPreserveLCDText) {
147 preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr)
148 : canvas->saveLayer(nullptr, nullptr);
150 canvas->drawString("Hamburgefons", 30, 60, paint);
153 p.setColor(0xFFCCCCCC);
154 canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p);
155 canvas->drawString("Hamburgefons", 30, 90, paint);
158 canvas->translate(0, 80);