migrate to passing paint/ctm for bounds

Some of this is left in for migrating callers (see docs)

- want getWidths to just take widths (and no paint)
- want getBounds to just take bounds AND paint
- want getWidthsBounds to take it all

Bug: skia:
Change-Id: I498cd8295b90995c45237d3cf39fc097252f485e
Reviewed-on: https://skia-review.googlesource.com/c/172868
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2018-11-26 13:48:15 -05:00 committed by Skia Commit-Bot
parent 2c669e77c1
commit d5ed3dafb2
5 changed files with 81 additions and 73 deletions

View File

@ -330,7 +330,8 @@ public:
SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
SkRect* bounds = nullptr) const;
/** Retrieves the advance and bounds for each glyph in glyphs.
/** DEPRECATED
Retrieves the advance and bounds for each glyph in glyphs.
Both widths and bounds may be nullptr.
If widths is not nullptr, widths must be an array of count entries.
if bounds is not nullptr, bounds must be an array of count entries.
@ -340,8 +341,58 @@ public:
@param widths returns text advances for each glyph; may be nullptr
@param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
*/
void getWidths(const uint16_t glyphs[], int count, SkScalar widths[],
SkRect bounds[] = nullptr) const;
void getWidths(const uint16_t glyphs[], int count, SkScalar widths[], SkRect bounds[]) const {
this->getWidthsBounds(glyphs, count, widths, bounds, nullptr);
}
// DEPRECATED
void getWidths(const uint16_t glyphs[], int count, SkScalar widths[], std::nullptr_t) const {
this->getWidths(glyphs, count, widths);
}
/** Experimental
Retrieves the advance and bounds for each glyph in glyphs.
Both widths and bounds may be nullptr.
If widths is not nullptr, widths must be an array of count entries.
if bounds is not nullptr, bounds must be an array of count entries.
@param glyphs array of glyph indices to be measured
@param count number of glyphs
@param widths returns text advances for each glyph
*/
void getWidths(const uint16_t glyphs[], int count, SkScalar widths[]) const {
this->getWidthsBounds(glyphs, count, widths, nullptr, nullptr);
}
/** Experimental.
Retrieves the advance and bounds for each glyph in glyphs.
Both widths and bounds may be nullptr.
If widths is not nullptr, widths must be an array of count entries.
if bounds is not nullptr, bounds must be an array of count entries.
@param glyphs array of glyph indices to be measured
@param count number of glyphs
@param widths returns text advances for each glyph; may be nullptr
@param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
@param paint optional, specifies stroking, patheffect and maskfilter
*/
void getWidthsBounds(const uint16_t glyphs[], int count, SkScalar widths[], SkRect bounds[],
const SkPaint* paint) const;
/** Experimental.
Retrieves the bounds for each glyph in glyphs.
bounds must be an array of count entries.
If paint is not nullptr, its stroking, patheffect and maskfilter fields will be respected.
@param glyphs array of glyph indices to be measured
@param count number of glyphs
@param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
@param paint optional, specifies stroking, patheffect and maskfilter
*/
void getBounds(const uint16_t glyphs[], int count, SkRect bounds[], const SkPaint* paint) const {
this->getWidthsBounds(glyphs, count, nullptr, bounds, paint);
}
/** Experimental
Retrieves the positions for each glyph, beginning at the specified origin. The caller

View File

@ -302,7 +302,8 @@ static SkRect make_bounds(const SkGlyph& g, SkScalar scale) {
}
template <typename HANDLER>
void VisitGlyphs(const SkFont& origFont, const uint16_t glyphs[], int count, HANDLER handler) {
void VisitGlyphs(const SkFont& origFont, const SkPaint* paint, const uint16_t glyphs[], int count,
HANDLER handler) {
if (count <= 0) {
return;
}
@ -314,12 +315,14 @@ void VisitGlyphs(const SkFont& origFont, const uint16_t glyphs[], int count, HAN
scale = 1;
}
auto cache = SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(font);
auto cache = SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(font,
paint ? *paint : SkPaint());
handler(cache.get(), glyphs, count, scale);
}
void SkFont::getWidths(const uint16_t glyphs[], int count, SkScalar widths[], SkRect bounds[]) const {
VisitGlyphs(*this, glyphs, count, [widths, bounds]
void SkFont::getWidthsBounds(const uint16_t glyphs[], int count, SkScalar widths[], SkRect bounds[],
const SkPaint* paint) const {
VisitGlyphs(*this, paint, glyphs, count, [widths, bounds]
(SkGlyphCache* cache, const uint16_t glyphs[], int count, SkScalar scale) {
for (int i = 0; i < count; ++i) {
const SkGlyph* g;
@ -337,7 +340,7 @@ void SkFont::getWidths(const uint16_t glyphs[], int count, SkScalar widths[], Sk
}
void SkFont::getPos(const uint16_t glyphs[], int count, SkPoint pos[], SkPoint origin) const {
VisitGlyphs(*this, glyphs, count, [pos, origin]
VisitGlyphs(*this, nullptr, glyphs, count, [pos, origin]
(SkGlyphCache* cache, const uint16_t glyphs[], int count, SkScalar scale) {
SkPoint loc = origin;
for (int i = 0; i < count; ++i) {
@ -348,7 +351,7 @@ void SkFont::getPos(const uint16_t glyphs[], int count, SkPoint pos[], SkPoint o
}
void SkFont::getXPos(const uint16_t glyphs[], int count, SkScalar xpos[], SkScalar origin) const {
VisitGlyphs(*this, glyphs, count, [xpos, origin]
VisitGlyphs(*this, nullptr, glyphs, count, [xpos, origin]
(SkGlyphCache* cache, const uint16_t glyphs[], int count, SkScalar scale) {
SkScalar x = origin;
for (int i = 0; i < count; ++i) {

View File

@ -405,63 +405,11 @@ SkScalar SkPaint::getFontMetrics(SkFontMetrics* metrics) const {
///////////////////////////////////////////////////////////////////////////////
static void set_bounds(const SkGlyph& g, SkRect* bounds, SkScalar scale) {
bounds->set(g.fLeft * scale,
g.fTop * scale,
(g.fLeft + g.fWidth) * scale,
(g.fTop + g.fHeight) * scale);
}
int SkPaint::getTextWidths(const void* textData, size_t byteLength,
SkScalar widths[], SkRect bounds[]) const {
if (0 == byteLength) {
return 0;
}
SkASSERT(textData);
if (nullptr == widths && nullptr == bounds) {
return this->countText(textData, byteLength);
}
SkCanonicalizePaint canon(*this);
const SkPaint& paint = canon.getPaint();
SkScalar scale = canon.getScale();
auto cache = SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(paint);
SkFontPriv::GlyphCacheProc glyphCacheProc = SkFontPriv::GetGlyphCacheProc(
static_cast<SkTextEncoding>(paint.getTextEncoding()), nullptr != bounds);
const char* text = (const char*)textData;
const char* stop = text + byteLength;
int count = 0;
if (scale) {
while (text < stop) {
const SkGlyph& g = glyphCacheProc(cache.get(), &text, stop);
if (widths) {
*widths++ = advance(g) * scale;
}
if (bounds) {
set_bounds(g, bounds++, scale);
}
++count;
}
} else {
while (text < stop) {
const SkGlyph& g = glyphCacheProc(cache.get(), &text, stop);
if (widths) {
*widths++ = advance(g);
}
if (bounds) {
set_bounds(g, bounds++);
}
++count;
}
}
SkASSERT(text == stop);
return count;
int SkPaint::getTextWidths(const void* text, size_t len, SkScalar widths[], SkRect bounds[]) const {
const SkFont font = SkFont::LEGACY_ExtractFromPaint(*this);
SkAutoToGlyphs gly(font, text, len, (SkTextEncoding)this->getTextEncoding());
font.getWidthsBounds(gly.glyphs(), gly.count(), widths, bounds, this);
return gly.count();
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -196,18 +196,21 @@ auto SkStrikeCache::findOrCreateStrike(
}
SkExclusiveStrikePtr SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(const SkPaint& paint) {
return FindOrCreateStrikeExclusive(
paint, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType),
kFakeGammaAndBoostContrast, SkMatrix::I());
return FindOrCreateStrikeWithNoDeviceExclusive(SkFont::LEGACY_ExtractFromPaint(paint),
SkPaint());
}
SkExclusiveStrikePtr SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(const SkFont& font) {
return FindOrCreateStrikeWithNoDeviceExclusive(font, SkPaint());
}
SkExclusiveStrikePtr SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(const SkFont& font,
const SkPaint& paint) {
SkAutoDescriptor ad;
SkScalerContextEffects effects;
auto desc = SkScalerContext::CreateDescriptorAndEffectsUsingPaint(font,
SkPaint(),
SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType),
kFakeGammaAndBoostContrast, SkMatrix::I(), &ad, &effects);
auto desc = SkScalerContext::CreateDescriptorAndEffectsUsingPaint(font, paint,
SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType),
kFakeGammaAndBoostContrast, SkMatrix::I(), &ad, &effects);
auto typeface = SkFontPriv::GetTypefaceOrDefault(font);
return SkStrikeCache::FindOrCreateStrikeExclusive(*desc, effects, *typeface);
}

View File

@ -136,6 +136,9 @@ public:
static ExclusiveStrikePtr FindOrCreateStrikeWithNoDeviceExclusive(const SkFont&);
static ExclusiveStrikePtr FindOrCreateStrikeWithNoDeviceExclusive(const SkFont& font,
const SkPaint& paint);
static std::unique_ptr<SkScalerContext> CreateScalerContext(
const SkDescriptor&, const SkScalerContextEffects&, const SkTypeface&);