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>
This commit is contained in:
parent
32eb5ba7af
commit
a0ba714ad5
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user