texteffects.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/SkFont.h"
  10. #include "include/core/SkFontStyle.h"
  11. #include "include/core/SkFontTypes.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkTextBlob.h"
  19. #include "include/core/SkTypeface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "include/private/SkTDArray.h"
  22. #include "include/private/SkTemplates.h"
  23. #include "include/private/SkTo.h"
  24. #include "tools/ToolUtils.h"
  25. #include <string.h>
  26. static SkPath create_underline(const SkTDArray<SkScalar>& intersections,
  27. SkScalar last, SkScalar finalPos,
  28. SkScalar uPos, SkScalar uWidth, SkScalar textSize) {
  29. SkPath underline;
  30. SkScalar end = last;
  31. for (int index = 0; index < intersections.count(); index += 2) {
  32. SkScalar start = intersections[index] - uWidth;
  33. end = intersections[index + 1] + uWidth;
  34. if (start > last && last + textSize / 12 < start) {
  35. underline.moveTo(last, uPos);
  36. underline.lineTo(start, uPos);
  37. }
  38. last = end;
  39. }
  40. if (end < finalPos) {
  41. underline.moveTo(end, uPos);
  42. underline.lineTo(finalPos, uPos);
  43. }
  44. return underline;
  45. }
  46. namespace {
  47. sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const SkFont& font, const char* text) {
  48. const size_t textLen = strlen(text);
  49. const int glyphCount = font.countText(text, textLen, SkTextEncoding::kUTF8);
  50. SkAutoTArray<SkGlyphID> glyphs(glyphCount);
  51. font.textToGlyphs(text, textLen, SkTextEncoding::kUTF8, glyphs.get(), glyphCount);
  52. SkAutoTArray<SkScalar> widths(glyphCount);
  53. font.getWidths(glyphs.get(), glyphCount, widths.get());
  54. SkTextBlobBuilder blobBuilder;
  55. int glyphIndex = 0;
  56. SkScalar advance = 0;
  57. // Default-positioned run.
  58. {
  59. const int defaultRunLen = glyphCount / 3;
  60. const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRun(font,
  61. defaultRunLen,
  62. advance, 0);
  63. memcpy(buf.glyphs, glyphs.get(), SkTo<uint32_t>(defaultRunLen) * sizeof(SkGlyphID));
  64. for (int i = 0; i < defaultRunLen; ++i) {
  65. advance += widths[glyphIndex++];
  66. }
  67. }
  68. // Horizontal-positioned run.
  69. {
  70. const int horizontalRunLen = glyphCount / 3;
  71. const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPosH(font,
  72. horizontalRunLen,
  73. 0);
  74. memcpy(buf.glyphs, glyphs.get() + glyphIndex,
  75. SkTo<uint32_t>(horizontalRunLen) * sizeof(SkGlyphID));
  76. for (int i = 0; i < horizontalRunLen; ++i) {
  77. buf.pos[i] = advance;
  78. advance += widths[glyphIndex++];
  79. }
  80. }
  81. // Full-positioned run.
  82. {
  83. const int fullRunLen = glyphCount - glyphIndex;
  84. const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPos(font, fullRunLen);
  85. memcpy(buf.glyphs, glyphs.get() + glyphIndex,
  86. SkTo<uint32_t>(fullRunLen) * sizeof(SkGlyphID));
  87. for (int i = 0; i < fullRunLen; ++i) {
  88. buf.pos[i * 2 + 0] = advance; // x offset
  89. buf.pos[i * 2 + 1] = 0; // y offset
  90. advance += widths[glyphIndex++];
  91. }
  92. }
  93. return blobBuilder.make();
  94. }
  95. } // anonymous ns
  96. DEF_SIMPLE_GM(fancyblobunderline, canvas, 1480, 1380) {
  97. SkPaint paint;
  98. paint.setAntiAlias(true);
  99. const char* fam[] = { "sans-serif", "serif", "monospace" };
  100. const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
  101. const SkPoint blobOffset = { 10, 80 };
  102. for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
  103. for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
  104. SkFont skFont(ToolUtils::create_portable_typeface(fam[font], SkFontStyle()), textSize);
  105. const SkScalar uWidth = textSize / 15;
  106. paint.setStrokeWidth(uWidth);
  107. paint.setStyle(SkPaint::kFill_Style);
  108. sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, skFont, test);
  109. canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint);
  110. const SkScalar uPos = uWidth;
  111. const SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
  112. const int interceptCount = blob->getIntercepts(bounds, nullptr, &paint);
  113. SkASSERT(!(interceptCount % 2));
  114. SkTDArray<SkScalar> intercepts;
  115. intercepts.setCount(interceptCount);
  116. blob->getIntercepts(bounds, intercepts.begin(), &paint);
  117. const SkScalar start = blob->bounds().left();
  118. const SkScalar end = blob->bounds().right();
  119. SkPath underline = create_underline(intercepts, start, end, uPos, uWidth, textSize);
  120. underline.offset(blobOffset.x(), blobOffset.y());
  121. paint.setStyle(SkPaint::kStroke_Style);
  122. canvas->drawPath(underline, paint);
  123. canvas->translate(0, textSize * 1.3f);
  124. }
  125. canvas->translate(0, 60);
  126. }
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////////////////////////
  129. static sk_sp<SkTextBlob> make_text(const SkFont& font, const SkGlyphID glyphs[], int count) {
  130. return SkTextBlob::MakeFromText(glyphs, count * sizeof(SkGlyphID), font,
  131. SkTextEncoding::kGlyphID);
  132. }
  133. static sk_sp<SkTextBlob> make_posh(const SkFont& font, const SkGlyphID glyphs[], int count,
  134. SkScalar spacing) {
  135. SkAutoTArray<SkScalar> xpos(count);
  136. font.getXPos(glyphs, count, xpos.get());
  137. for (int i = 1; i < count; ++i) {
  138. xpos[i] += spacing * i;
  139. }
  140. return SkTextBlob::MakeFromPosTextH(glyphs, count * sizeof(SkGlyphID), xpos.get(), 0, font,
  141. SkTextEncoding::kGlyphID);
  142. }
  143. static sk_sp<SkTextBlob> make_pos(const SkFont& font, const SkGlyphID glyphs[], int count,
  144. SkScalar spacing) {
  145. SkAutoTArray<SkPoint> pos(count);
  146. font.getPos(glyphs, count, pos.get());
  147. for (int i = 1; i < count; ++i) {
  148. pos[i].fX += spacing * i;
  149. }
  150. return SkTextBlob::MakeFromPosText(glyphs, count * sizeof(SkGlyphID), pos.get(), font,
  151. SkTextEncoding::kGlyphID);
  152. }
  153. // widen the gaps with a margin (on each side of the gap), elimnating segments that go away
  154. static int trim_with_halo(SkScalar intervals[], int count, SkScalar margin) {
  155. SkASSERT(count > 0 && (count & 1) == 0);
  156. int n = count;
  157. SkScalar* stop = intervals + count;
  158. *intervals++ -= margin;
  159. while (intervals < stop - 1) {
  160. intervals[0] += margin;
  161. intervals[1] -= margin;
  162. if (intervals[0] >= intervals[1]) { // went away
  163. int remaining = stop - intervals - 2;
  164. SkASSERT(remaining >= 0 && (remaining & 1) == 1);
  165. if (remaining > 0) {
  166. memmove(intervals, intervals + 2, remaining * sizeof(SkScalar));
  167. }
  168. stop -= 2;
  169. n -= 2;
  170. } else {
  171. intervals += 2;
  172. }
  173. }
  174. *intervals += margin;
  175. return n;
  176. }
  177. static void draw_blob_adorned(SkCanvas* canvas, sk_sp<SkTextBlob> blob) {
  178. SkPaint paint;
  179. canvas->drawTextBlob(blob.get(), 0, 0, paint);
  180. const SkScalar yminmax[] = { 8, 16 };
  181. int count = blob->getIntercepts(yminmax, nullptr);
  182. if (!count) {
  183. return;
  184. }
  185. SkAutoTArray<SkScalar> intervals(count);
  186. blob->getIntercepts(yminmax, intervals.get());
  187. count = trim_with_halo(intervals.get(), count, SkScalarHalf(yminmax[1] - yminmax[0]) * 1.5f);
  188. SkASSERT(count >= 2);
  189. const SkScalar y = SkScalarAve(yminmax[0], yminmax[1]);
  190. SkScalar end = 900;
  191. SkPath path;
  192. path.moveTo({0, y});
  193. for (int i = 0; i < count; i += 2) {
  194. path.lineTo(intervals[i], y).moveTo(intervals[i+1], y);
  195. }
  196. if (intervals[count - 1] < end) {
  197. path.lineTo(end, y);
  198. }
  199. paint.setAntiAlias(true);
  200. paint.setStyle(SkPaint::kStroke_Style);
  201. paint.setStrokeWidth(yminmax[1] - yminmax[0]);
  202. canvas->drawPath(path, paint);
  203. }
  204. DEF_SIMPLE_GM(textblob_intercepts, canvas, 940, 800) {
  205. const char text[] = "Hyjay {worlp}.";
  206. const size_t length = strlen(text);
  207. SkFont font;
  208. font.setTypeface(ToolUtils::create_portable_typeface());
  209. font.setSize(100);
  210. font.setEdging(SkFont::Edging::kAntiAlias);
  211. const int count = font.countText(text, length, SkTextEncoding::kUTF8);
  212. SkAutoTArray<SkGlyphID> glyphs(count);
  213. font.textToGlyphs(text, length, SkTextEncoding::kUTF8, glyphs.get(), count);
  214. auto b0 = make_text(font, glyphs.get(), count);
  215. canvas->translate(20, 120);
  216. draw_blob_adorned(canvas, b0);
  217. for (SkScalar spacing = 0; spacing < 30; spacing += 20) {
  218. auto b1 = make_posh(font, glyphs.get(), count, spacing);
  219. auto b2 = make_pos( font, glyphs.get(), count, spacing);
  220. canvas->translate(0, 150);
  221. draw_blob_adorned(canvas, b1);
  222. canvas->translate(0, 150);
  223. draw_blob_adorned(canvas, b2);
  224. }
  225. }