glyph_pos.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2014 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/SkFontTypes.h"
  12. #include "include/core/SkMatrix.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkTypeface.h"
  17. #include "include/core/SkTypes.h"
  18. #include "tools/ToolUtils.h"
  19. /* This test tries to define the effect of using hairline strokes on text.
  20. * Provides non-hairline images for reference and consistency checks.
  21. * glyph_pos_(h/n)_(s/f/b)
  22. * -> test hairline/non-hairline stroke/fill/stroke+fill.
  23. */
  24. constexpr SkScalar kTextHeight = 14.0f;
  25. constexpr char kText[] = "Proportional Hamburgefons #% fi";
  26. static void drawTestCase(SkCanvas* canvas,
  27. SkScalar textScale,
  28. SkScalar strokeWidth,
  29. SkPaint::Style strokeStyle);
  30. static void draw_gm(SkCanvas* canvas,
  31. SkScalar strokeWidth,
  32. SkPaint::Style strokeStyle) {
  33. // There's a black pixel at 40, 40 for reference.
  34. canvas->drawPoint(40, 40, SkPaint());
  35. // Two reference images.
  36. canvas->translate(50.0f, 50.0f);
  37. drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
  38. canvas->translate(0.0f, 50.0f);
  39. drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
  40. // Uniform scaling test.
  41. canvas->translate(0.0f, 100.0f);
  42. canvas->save();
  43. canvas->scale(3.0f, 3.0f);
  44. drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
  45. canvas->restore();
  46. // Non-uniform scaling test.
  47. canvas->translate(0.0f, 100.0f);
  48. canvas->save();
  49. canvas->scale(3.0f, 6.0f);
  50. drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
  51. canvas->restore();
  52. // Skew test.
  53. canvas->translate(0.0f, 80.0f);
  54. canvas->save();
  55. canvas->scale(3.0f, 3.0f);
  56. SkMatrix skew;
  57. skew.setIdentity();
  58. skew.setSkewX(8.0f / 25.0f);
  59. skew.setSkewY(2.0f / 25.0f);
  60. canvas->concat(skew);
  61. drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
  62. canvas->restore();
  63. // Perspective test.
  64. canvas->translate(0.0f, 80.0f);
  65. canvas->save();
  66. SkMatrix perspective;
  67. perspective.setIdentity();
  68. perspective.setPerspX(-SkScalarInvert(340));
  69. perspective.setSkewX(8.0f / 25.0f);
  70. perspective.setSkewY(2.0f / 25.0f);
  71. canvas->concat(perspective);
  72. drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
  73. canvas->restore();
  74. }
  75. static void drawTestCase(SkCanvas* canvas,
  76. SkScalar textScale,
  77. SkScalar strokeWidth,
  78. SkPaint::Style strokeStyle) {
  79. SkPaint paint;
  80. paint.setColor(SK_ColorBLACK);
  81. paint.setAntiAlias(true);
  82. paint.setStrokeWidth(strokeWidth);
  83. paint.setStyle(strokeStyle);
  84. SkFont font(ToolUtils::create_portable_typeface(), kTextHeight * textScale);
  85. // This demonstrates that we can not measure the text if
  86. // there's a device transform. The canvas total matrix will
  87. // end up being a device transform.
  88. bool drawRef = !(canvas->getTotalMatrix().getType() &
  89. ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
  90. SkRect bounds;
  91. if (drawRef) {
  92. SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
  93. &bounds, &paint);
  94. paint.setStrokeWidth(0.0f);
  95. paint.setStyle(SkPaint::kStroke_Style);
  96. // Green box is the measured text bounds.
  97. paint.setColor(SK_ColorGREEN);
  98. canvas->drawRect(bounds, paint);
  99. // Red line is the measured advance from the 0,0 of the text position.
  100. paint.setColor(SK_ColorRED);
  101. canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
  102. }
  103. // Black text is the testcase, eg. the text.
  104. paint.setColor(SK_ColorBLACK);
  105. paint.setStrokeWidth(strokeWidth);
  106. paint.setStyle(strokeStyle);
  107. canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
  108. 0.0f, 0.0f, font, paint);
  109. if (drawRef) {
  110. const size_t len = sizeof(kText) - 1;
  111. SkGlyphID glyphs[len];
  112. const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len);
  113. SkScalar widths[len]; // len is conservative. we really only need 'count'
  114. font.getWidthsBounds(glyphs, count, widths, nullptr, &paint);
  115. paint.setStrokeWidth(0.0f);
  116. paint.setStyle(SkPaint::kStroke_Style);
  117. // Magenta lines are the positions for the characters.
  118. paint.setColor(SK_ColorMAGENTA);
  119. SkScalar w = bounds.x();
  120. for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
  121. canvas->drawLine(w, 0.0f, w, 5.0f, paint);
  122. w += widths[i];
  123. }
  124. }
  125. }
  126. DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
  127. draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
  128. }
  129. DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
  130. draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
  131. }
  132. DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
  133. draw_gm(c, 0.0f, SkPaint::kStroke_Style);
  134. }
  135. DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
  136. draw_gm(c, 1.2f, SkPaint::kStroke_Style);
  137. }
  138. DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
  139. draw_gm(c, 0.0f, SkPaint::kFill_Style);
  140. }
  141. DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
  142. draw_gm(c, 1.2f, SkPaint::kFill_Style);
  143. }