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