aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Brian Salomon <bsalomon@google.com>2017-11-20 13:17:43 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-11-20 19:43:18 +0000
commita0ba714ad5ee26b3f1929aa572eb77cc71809e64 (patch)
treeef549bf038a299f1c9f6466b85aba976d4ca056c
parent32eb5ba7af4da511df2c7ec9e560018c3060f914 (diff)
Require glyph positions in SkAtlasTextTarget::drawText
Change-Id: Idf0977befc8ed4fd9eb3b733db5e945457b2164c Reviewed-on: https://skia-review.googlesource.com/73701 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
-rw-r--r--gm/atlastext.cpp22
-rw-r--r--include/atlastext/SkAtlasTextTarget.h10
-rw-r--r--src/atlastext/SkAtlasTextTarget.cpp18
3 files changed, 32 insertions, 18 deletions
diff --git a/gm/atlastext.cpp b/gm/atlastext.cpp
index 8270306e5e..6ee092b49f 100644
--- a/gm/atlastext.cpp
+++ b/gm/atlastext.cpp
@@ -25,19 +25,31 @@
static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y,
uint32_t color, sk_sp<SkTypeface> typeface, float size) {
+ if (!text.size()) {
+ return x;
+ }
auto font = SkAtlasTextFont::Make(typeface, size);
int cnt = SkUTF8_CountUnichars(text.c_str());
std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[cnt]);
typeface->charsToGlyphs(text.c_str(), SkTypeface::Encoding::kUTF8_Encoding, glyphs.get(), cnt);
- target->drawText(glyphs.get(), cnt, x, y, color, *font);
-
- // Using a paint just to perform a measure to let the caller know how much space to skip in x.
+ // Using a paint to get the positions for each glyph.
SkPaint paint;
paint.setTextSize(size);
paint.setTypeface(std::move(typeface));
- paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
- return x + paint.measureText(text.c_str(), text.size());
+ paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
+ std::unique_ptr<SkScalar[]> widths(new SkScalar[cnt]);
+ paint.getTextWidths(glyphs.get(), cnt * sizeof(SkGlyphID), widths.get(), nullptr);
+
+ std::unique_ptr<SkPoint[]> positions(new SkPoint[cnt]);
+ positions[0] = {x, y};
+ for (int i = 1; i < cnt; ++i) {
+ positions[i] = {positions[i - 1].fX + widths[i - 1], y};
+ }
+
+ target->drawText(glyphs.get(), positions.get(), cnt, color, *font);
+
+ return positions[cnt - 1].fX + widths[cnt - 1];
}
class AtlasTextGM : public skiagm::GM {
diff --git a/include/atlastext/SkAtlasTextTarget.h b/include/atlastext/SkAtlasTextTarget.h
index a9d5dd5dca..ff879617d8 100644
--- a/include/atlastext/SkAtlasTextTarget.h
+++ b/include/atlastext/SkAtlasTextTarget.h
@@ -14,6 +14,7 @@
class SkAtlasTextContext;
class SkAtlasTextFont;
+struct SkPoint;
/** Represents a client-created renderable surface and is used to draw text into the surface. */
class SK_API SkAtlasTextTarget {
@@ -28,11 +29,12 @@ public:
void* handle);
/**
- * Enqueues a text draw in the target. The meaning of 'color' here is interpreted by the
- * client's SkAtlasTextRenderer when it actually renders the text.
+ * Enqueues a text draw in the target. The caller provides an array of glyphs and their
+ * positions. The meaning of 'color' here is interpreted by the client's SkAtlasTextRenderer
+ * when it actually renders the text.
*/
- virtual void drawText(const SkGlyphID*, int glyphCnt, SkScalar x, SkScalar y, uint32_t color,
- const SkAtlasTextFont& font) = 0;
+ virtual void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
+ const SkAtlasTextFont&) = 0;
/** Issues all queued text draws to SkAtlasTextRenderer. */
virtual void flush() = 0;
diff --git a/src/atlastext/SkAtlasTextTarget.cpp b/src/atlastext/SkAtlasTextTarget.cpp
index fa756fe327..89e1ad8b4c 100644
--- a/src/atlastext/SkAtlasTextTarget.cpp
+++ b/src/atlastext/SkAtlasTextTarget.cpp
@@ -7,6 +7,8 @@
#include "SkAtlasTextTarget.h"
#include "GrClip.h"
+#include "GrContextPriv.h"
+#include "GrDrawingManager.h"
#include "SkAtlasTextContext.h"
#include "SkAtlasTextFont.h"
#include "SkAtlasTextRenderer.h"
@@ -50,7 +52,7 @@ public:
/** SkAtlasTextTarget overrides */
- void drawText(const SkGlyphID*, int glyphCnt, SkScalar x, SkScalar y, uint32_t color,
+ void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
const SkAtlasTextFont&) override;
void flush() override;
@@ -75,11 +77,9 @@ std::unique_ptr<SkAtlasTextTarget> SkAtlasTextTarget::Make(sk_sp<SkAtlasTextCont
//////////////////////////////////////////////////////////////////////////////
-#include "GrContextPriv.h"
-#include "GrDrawingManager.h"
-
-void SkInternalAtlasTextTarget::drawText(const SkGlyphID* glyphs, int glyphCnt, SkScalar x,
- SkScalar y, uint32_t color, const SkAtlasTextFont& font) {
+void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[],
+ int glyphCnt, uint32_t color,
+ const SkAtlasTextFont& font) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setTypeface(font.refTypeface());
@@ -91,14 +91,14 @@ void SkInternalAtlasTextTarget::drawText(const SkGlyphID* glyphs, int glyphCnt,
// and the context will write it into the final vertices given to the client's renderer.
fColor = color;
- // The pixel geometry here is arbitrary. We don't draw LCD text.
SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry);
auto* grContext = this->context()->internal().grContext();
auto bounds = SkIRect::MakeWH(fWidth, fHeight);
auto atlasTextContext = grContext->contextPriv().drawingManager()->getAtlasTextContext();
size_t byteLength = sizeof(SkGlyphID) * glyphCnt;
- atlasTextContext->drawText(grContext, this, GrNoClip(), paint, SkMatrix::I(), props,
- (const char*)glyphs, byteLength, x, y, bounds);
+ const SkScalar* pos = &positions->fX;
+ atlasTextContext->drawPosText(grContext, this, GrNoClip(), paint, SkMatrix::I(), props,
+ (const char*)glyphs, byteLength, pos, 2, {0, 0}, bounds);
}
void SkInternalAtlasTextTarget::addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) {