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.
15 class LcdTextGM : public skiagm::GM {
18 const int pointSize = 36;
19 textHeight = SkIntToScalar(pointSize);
24 SkString onShortName() {
25 return SkString("lcdtext");
28 SkISize onISize() { return SkISize::Make(640, 480); }
30 virtual void onDraw(SkCanvas* canvas) {
33 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
35 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
37 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
39 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
43 void drawText(SkCanvas* canvas, const SkString& string,
44 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
46 paint.setColor(SK_ColorBLACK);
47 paint.setDither(true);
48 paint.setAntiAlias(true);
49 sk_tool_utils::set_portable_typeface(&paint);
50 paint.setSubpixelText(subpixelTextEnabled);
51 paint.setLCDRenderText(lcdRenderTextEnabled);
52 paint.setTextSize(textHeight);
54 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
59 typedef skiagm::GM INHERITED;
60 SkScalar y, textHeight;
64 * Skia will automatically disable LCD requests if the total size exceeds some limit
65 * (hard coded in this test for now, as it is now avaiable as an API)
67 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
69 class LcdTextSizeGM : public skiagm::GM {
71 kLCDTextSizeLimit = 48
74 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
76 m.setScale(sx, sy, px, py);
84 SkString onShortName() {
85 return SkString("lcdtextsize");
88 SkISize onISize() { return SkISize::Make(320, 120); }
90 virtual void onDraw(SkCanvas* canvas) {
91 const char* lcd_text = "LCD";
92 const char* gray_text = "GRAY";
95 paint.setAntiAlias(true);
96 paint.setLCDRenderText(true);
104 { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
105 { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
106 { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
107 { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
110 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
111 const SkPoint loc = rec[i].fLoc;
112 SkAutoCanvasRestore acr(canvas, true);
114 paint.setTextSize(rec[i].fTextSize);
115 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
116 canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint);
121 typedef skiagm::GM INHERITED;
124 #include "SkSurface.h"
126 // ensure that we respect the SkPixelGeometry in SurfaceProps
127 class LcdTextProps : public skiagm::GM {
128 static void DrawText(SkCanvas* canvas) {
129 canvas->drawColor(SK_ColorWHITE);
131 paint.setAntiAlias(true);
132 paint.setLCDRenderText(true);
133 paint.setTextSize(30);
134 canvas->drawText("Base", 4, 4, 30, paint);
135 canvas->saveLayer(NULL, NULL);
136 canvas->drawText("Layer", 5, 4, 70, paint);
141 SkString onShortName() SK_OVERRIDE {
142 return SkString("lcdtextprops");
145 SkISize onISize() SK_OVERRIDE { return SkISize::Make(230, 120); }
147 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
148 const SkPixelGeometry geos[] = {
149 kRGB_H_SkPixelGeometry,
150 kUnknown_SkPixelGeometry,
153 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
154 for (size_t i = 0; i < SK_ARRAY_COUNT(geos); ++i) {
155 SkSurfaceProps props = SkSurfaceProps(0, geos[i]);
156 SkAutoTUnref<SkSurface> surf(canvas->newSurface(info, &props));
158 surf.reset(SkSurface::NewRaster(info, &props));
160 DrawText(surf->getCanvas());
161 surf->draw(canvas, SkIntToScalar(i * (info.width() + 10)), 0, NULL);
166 ///////////////////////////////////////////////////////////////////////////////
168 DEF_GM( return new LcdTextGM; )
169 DEF_GM( return new LcdTextSizeGM; )
170 DEF_GM( return new LcdTextProps; )