static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos, const SkPoint& offset, SkTextBlobBuilder& builder, const SkRect* bounds = nullptr) { switch (pos) { case SkTextBlobRunIterator::kDefault_Positioning: { const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(), offset.y(), bounds); for (int i = 0; i < count; ++i) { rb.glyphs[i] = i; } } break; case SkTextBlobRunIterator::kHorizontal_Positioning: { const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(), bounds); for (int i = 0; i < count; ++i) { rb.glyphs[i] = i; rb.pos[i] = SkIntToScalar(i); } } break; case SkTextBlobRunIterator::kFull_Positioning: { const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds); for (int i = 0; i < count; ++i) { rb.glyphs[i] = i; rb.pos[i * 2] = SkIntToScalar(i); rb.pos[i * 2 + 1] = -SkIntToScalar(i); } } break; default: SK_ABORT("unhandled positioning value"); } }
static void drawKernText(SkCanvas* canvas, const void* text, size_t len, SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) { SkTypeface* face = font.getTypefaceOrDefault(); if (!face) { canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint); return; } SkAutoSTMalloc<128, uint16_t> glyphStorage(len); uint16_t* glyphs = glyphStorage.get(); int glyphCount = font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len); if (glyphCount < 1) { return; } SkAutoSTMalloc<128, int32_t> adjustmentStorage(glyphCount - 1); int32_t* adjustments = adjustmentStorage.get(); if (!face->getKerningPairAdjustments(glyphs, glyphCount, adjustments)) { canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint); return; } SkTextBlobBuilder builder; auto rec = builder.allocRunPos(font, glyphCount); memcpy(rec.glyphs, glyphs, glyphCount * sizeof(SkGlyphID)); getGlyphPositions(font, glyphs, glyphCount, x, y, rec.points()); applyKerning(rec.points(), adjustments, glyphCount, font); canvas->drawTextBlob(builder.make(), 0, 0, paint); }
sk_sp<SkTextBlob> makeBlob(unsigned blobIndex) { SkTextBlobBuilder builder; SkFont font; font.setSubpixel(true); font.setEdging(SkFont::Edging::kAntiAlias); font.setTypeface(fTypeface); for (unsigned l = 0; l < SK_ARRAY_COUNT(blobConfigs[blobIndex]); ++l) { unsigned currentGlyph = 0; for (unsigned c = 0; c < SK_ARRAY_COUNT(blobConfigs[blobIndex][l]); ++c) { const BlobCfg* cfg = &blobConfigs[blobIndex][l][c]; unsigned count = cfg->count; if (count > fGlyphs.count() - currentGlyph) { count = fGlyphs.count() - currentGlyph; } if (0 == count) { break; } font.setSize(kFontSize * cfg->scale); const SkScalar advanceX = font.getSize() * 0.85f; const SkScalar advanceY = font.getSize() * 1.5f; SkPoint offset = SkPoint::Make(currentGlyph * advanceX + c * advanceX, advanceY * l); switch (cfg->pos) { case kDefault_Pos: { const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, count, offset.x(), offset.y()); memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t)); } break; case kScalar_Pos: { const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPosH(font, count, offset.y()); SkTDArray<SkScalar> pos; for (unsigned i = 0; i < count; ++i) { *pos.append() = offset.x() + i * advanceX; } memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t)); memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar)); } break; case kPoint_Pos: { const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPos(font, count); SkTDArray<SkScalar> pos; for (unsigned i = 0; i < count; ++i) { *pos.append() = offset.x() + i * advanceX; *pos.append() = offset.y() + i * (advanceY / count); } memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t)); memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar) * 2); } break; default: SK_ABORT("unhandled pos value"); } currentGlyph += count; } } return builder.make(); }
// This unit test verifies blob bounds computation. static void TestBounds(skiatest::Reporter* reporter) { SkTextBlobBuilder builder; SkFont font; // Explicit bounds. { sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, !blob); } { SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); builder.allocRun(font, 16, 0, 0, &r1); sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, blob->bounds() == r1); } { SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); builder.allocRunPosH(font, 16, 0, &r1); sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, blob->bounds() == r1); } { SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); builder.allocRunPos(font, 16, &r1); sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, blob->bounds() == r1); } { SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50); SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5); builder.allocRun(font, 16, 0, 0, &r1); builder.allocRunPosH(font, 16, 0, &r2); builder.allocRunPos(font, 16, &r3); sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65)); } { sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, !blob); } // Implicit bounds { // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers // don't trigger asserts (http://crbug.com/542643). SkFont font; font.setSize(0); const char* txt = "BOOO"; const size_t txtLen = strlen(txt); const int glyphCount = font.countText(txt, txtLen, SkTextEncoding::kUTF8); const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount); font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, buffer.glyphs, glyphCount); memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2); sk_sp<SkTextBlob> blob(builder.make()); REPORTER_ASSERT(reporter, blob->bounds().isEmpty()); } }