diff options
author | Herb Derby <herb@google.com> | 2018-05-29 15:53:40 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-05-30 04:04:07 +0000 |
commit | 264182c3f7d282e57a4b1d46fde3ef702b81c5c3 (patch) | |
tree | 85c68bfa138662fd60cc3ab3bcad26f83cb9b80d /modules/skshaper/src/SkShaper_primitive.cpp | |
parent | 09725b0c3fd52b008743c63feb09ec1ab8bc2eea (diff) |
Make SkShaper a module
Change-Id: I3709e49ba865f14260660cc07a762b9ac837cb3c
Reviewed-on: https://skia-review.googlesource.com/130602
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Herb Derby <herb@google.com>
Diffstat (limited to 'modules/skshaper/src/SkShaper_primitive.cpp')
-rw-r--r-- | modules/skshaper/src/SkShaper_primitive.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/modules/skshaper/src/SkShaper_primitive.cpp b/modules/skshaper/src/SkShaper_primitive.cpp new file mode 100644 index 0000000000..06a8bec41c --- /dev/null +++ b/modules/skshaper/src/SkShaper_primitive.cpp @@ -0,0 +1,76 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include "SkShaper.h" +#include "SkStream.h" +#include "SkTextBlob.h" +#include "SkTypeface.h" + +struct SkShaper::Impl { + sk_sp<SkTypeface> fTypeface; +}; + +SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { + fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); +} + +SkShaper::~SkShaper() {} + +bool SkShaper::good() const { return true; } + +// This example only uses public API, so we don't use SkUTF8_NextUnichar. +unsigned utf8_lead_byte_to_count(const char* ptr) { + uint8_t c = *(const uint8_t*)ptr; + SkASSERT(c <= 0xF7); + SkASSERT((c & 0xC0) != 0x80); + return (((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1; +} + +SkPoint SkShaper::shape(SkTextBlobBuilder* builder, + const SkPaint& srcPaint, + const char* utf8text, + size_t textBytes, + bool leftToRight, + SkPoint point, + SkScalar width) const { + sk_ignore_unused_variable(leftToRight); + + SkPaint paint(srcPaint); + paint.setTypeface(fImpl->fTypeface); + paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); + int glyphCount = paint.countText(utf8text, textBytes); + if (glyphCount <= 0) { + return point; + } + SkRect bounds; + SkPaint::FontMetrics metrics; + paint.getFontMetrics(&metrics); + point.fY -= metrics.fAscent; + (void)paint.measureText(utf8text, textBytes, &bounds); + paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + const SkTextBlobBuilder::RunBuffer& runBuffer = + builder->allocRunTextPosH(paint, glyphCount, point.y(), textBytes, SkString(), &bounds); + memcpy(runBuffer.utf8text, utf8text, textBytes); + const char* txtPtr = utf8text; + for (int i = 0; i < glyphCount; ++i) { + // Each charater maps to exactly one glyph via SkGlyphCache::unicharToGlyph(). + runBuffer.clusters[i] = SkToU32(txtPtr - utf8text); + txtPtr += utf8_lead_byte_to_count(txtPtr); + SkASSERT(txtPtr <= utf8text + textBytes); + } + paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); + (void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs); + (void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos); + SkScalar x = point.x(); + for (int i = 0; i < glyphCount; ++i) { + SkScalar w = runBuffer.pos[i]; + runBuffer.pos[i] = x; + x += w; + } + point.fY += metrics.fDescent + metrics.fLeading; + + return point; +} |