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