lcdtext.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTypeface.h"
  19. #include "include/core/SkTypes.h"
  20. class LcdTextGM : public skiagm::GM {
  21. static constexpr SkScalar kTextHeight = 36;
  22. SkScalar fY = kTextHeight;
  23. SkString onShortName() override { return SkString("lcdtext"); }
  24. SkISize onISize() override { return {640, 480}; }
  25. void onDraw(SkCanvas* canvas) override {
  26. fY = kTextHeight;
  27. drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
  28. true, true);
  29. drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
  30. true, false);
  31. drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
  32. false, true);
  33. drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
  34. false, false);
  35. }
  36. void drawText(SkCanvas* canvas, const SkString& string,
  37. bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
  38. SkPaint paint;
  39. paint.setColor(SK_ColorBLACK);
  40. paint.setDither(true);
  41. SkFont font(nullptr, kTextHeight);
  42. if (subpixelTextEnabled) {
  43. font.setSubpixel(true);
  44. }
  45. if (lcdRenderTextEnabled) {
  46. font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  47. }
  48. canvas->drawString(string, 0, fY, font, paint);
  49. fY += kTextHeight;
  50. }
  51. };
  52. /*
  53. * Skia will automatically disable LCD requests if the total size exceeds some limit
  54. * (hard coded in this test for now, as it is now avaiable as an API)
  55. *
  56. * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
  57. */
  58. class LcdTextSizeGM : public skiagm::GM {
  59. static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
  60. SkMatrix m;
  61. m.setScale(sx, sy, px, py);
  62. canvas->concat(m);
  63. }
  64. SkString onShortName() override { return SkString("lcdtextsize"); }
  65. SkISize onISize() override { return {320, 120}; }
  66. void onDraw(SkCanvas* canvas) override {
  67. const char* lcd_text = "LCD";
  68. const char* gray_text = "GRAY";
  69. constexpr static float kLCDTextSizeLimit = 48;
  70. const struct {
  71. SkPoint fLoc;
  72. SkScalar fTextSize;
  73. SkScalar fScale;
  74. const char* fText;
  75. } rec[] = {
  76. { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
  77. { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
  78. { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
  79. { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
  80. };
  81. for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
  82. const SkPoint loc = rec[i].fLoc;
  83. SkAutoCanvasRestore acr(canvas, true);
  84. SkFont font(nullptr, rec[i].fTextSize);
  85. font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  86. ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
  87. canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint());
  88. }
  89. }
  90. };
  91. DEF_GM( return new LcdTextGM; )
  92. DEF_GM( return new LcdTextSizeGM; )