change private alloc methods to also take fonts
Bug: skia:2664
Change-Id: I772534179ce0c948984640f9fffc22e0372fce97
Reviewed-on: https://skia-review.googlesource.com/c/174848
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/include/core/SkTextBlob.h b/include/core/SkTextBlob.h
index 3bd0d11..1e07e1a 100644
--- a/include/core/SkTextBlob.h
+++ b/include/core/SkTextBlob.h
@@ -285,17 +285,17 @@
const SkRect* bounds = nullptr);
private:
- const RunBuffer& allocRunText(const SkPaint& font,
+ const RunBuffer& allocRunText(const SkFont& font,
int count,
SkScalar x,
SkScalar y,
int textByteCount,
SkString lang,
const SkRect* bounds = nullptr);
- const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
+ const RunBuffer& allocRunTextPosH(const SkFont& font, int count, SkScalar y,
int textByteCount, SkString lang,
const SkRect* bounds = nullptr);
- const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
+ const RunBuffer& allocRunTextPos(const SkFont& font, int count,
int textByteCount, SkString lang,
const SkRect* bounds = nullptr);
void reserve(size_t size);
diff --git a/modules/skshaper/src/SkShaper_harfbuzz.cpp b/modules/skshaper/src/SkShaper_harfbuzz.cpp
index ced56ee..e5664cc 100644
--- a/modules/skshaper/src/SkShaper_harfbuzz.cpp
+++ b/modules/skshaper/src/SkShaper_harfbuzz.cpp
@@ -396,16 +396,16 @@
bool fHasVisual;
};
struct ShapedRun {
- ShapedRun(const char* utf8Start, const char* utf8End, int numGlyphs, const SkFont& paint,
+ ShapedRun(const char* utf8Start, const char* utf8End, int numGlyphs, const SkFont& font,
UBiDiLevel level, std::unique_ptr<ShapedGlyph[]> glyphs)
- : fUtf8Start(utf8Start), fUtf8End(utf8End), fNumGlyphs(numGlyphs), fPaint(paint)
+ : fUtf8Start(utf8Start), fUtf8End(utf8End), fNumGlyphs(numGlyphs), fFont(font)
, fLevel(level), fGlyphs(std::move(glyphs))
{}
const char* fUtf8Start;
const char* fUtf8End;
int fNumGlyphs;
- SkFont fPaint;
+ SkFont fFont;
UBiDiLevel fLevel;
std::unique_ptr<ShapedGlyph[]> fGlyphs;
};
@@ -416,9 +416,7 @@
static void append(SkTextBlobBuilder* b, const ShapedRun& run, int start, int end, SkPoint* p) {
unsigned len = end - start;
- SkPaint tmpPaint;
- run.fPaint.LEGACY_applyToPaint(&tmpPaint);
- auto runBuffer = SkTextBlobBuilderPriv::AllocRunTextPos(b, tmpPaint, len,
+ auto runBuffer = SkTextBlobBuilderPriv::AllocRunTextPos(b, run.fFont, len,
run.fUtf8End - run.fUtf8Start, SkString());
memcpy(runBuffer.utf8text, run.fUtf8Start, run.fUtf8End - run.fUtf8Start);
@@ -641,8 +639,8 @@
std::unique_ptr<ShapedGlyph[]>(new ShapedGlyph[len]));
int scaleX, scaleY;
hb_font_get_scale(font->currentHBFont(), &scaleX, &scaleY);
- double textSizeY = run.fPaint.getSize() / scaleY;
- double textSizeX = run.fPaint.getSize() / scaleX * run.fPaint.getScaleX();
+ double textSizeY = run.fFont.getSize() / scaleY;
+ double textSizeX = run.fFont.getSize() / scaleX * run.fFont.getScaleX();
for (unsigned i = 0; i < len; i++) {
ShapedGlyph& glyph = run.fGlyphs[i];
glyph.fID = info[i].codepoint;
@@ -732,7 +730,7 @@
if (previousRunIndex != runIndex) {
SkFontMetrics metrics;
- runs[runIndex].fPaint.getMetrics(&metrics);
+ runs[runIndex].fFont.getMetrics(&metrics);
maxAscent = SkTMin(maxAscent, metrics.fAscent);
maxDescent = SkTMax(maxDescent, metrics.fDescent);
maxLeading = SkTMax(maxLeading, metrics.fLeading);
diff --git a/modules/skshaper/src/SkShaper_primitive.cpp b/modules/skshaper/src/SkShaper_primitive.cpp
index 1dd6da6..7b798e7 100644
--- a/modules/skshaper/src/SkShaper_primitive.cpp
+++ b/modules/skshaper/src/SkShaper_primitive.cpp
@@ -33,7 +33,7 @@
}
SkPoint SkShaper::shape(SkTextBlobBuilder* builder,
- const SkFont& srcPaint,
+ const SkFont& srcFont,
const char* utf8text,
size_t textBytes,
bool leftToRight,
@@ -41,21 +41,19 @@
SkScalar width) const {
sk_ignore_unused_variable(leftToRight);
- SkFont paint(srcPaint);
- paint.setTypeface(fImpl->fTypeface);
- int glyphCount = paint.countText(utf8text, textBytes, SkTextEncoding::kUTF8);
+ SkFont font(srcFont);
+ font.setTypeface(fImpl->fTypeface);
+ int glyphCount = font.countText(utf8text, textBytes, SkTextEncoding::kUTF8);
if (glyphCount <= 0) {
return point;
}
SkRect bounds;
SkFontMetrics metrics;
- paint.getMetrics(&metrics);
+ font.getMetrics(&metrics);
point.fY -= metrics.fAscent;
- (void)paint.measureText(utf8text, textBytes, SkTextEncoding::kUTF8, &bounds);
- SkPaint tmpPaint;
- paint.LEGACY_applyToPaint(&tmpPaint);
+ (void)font.measureText(utf8text, textBytes, SkTextEncoding::kUTF8, &bounds);
const SkTextBlobBuilder::RunBuffer& runBuffer =
- builder->allocRunTextPosH(tmpPaint, glyphCount, point.y(), textBytes, SkString(), &bounds);
+ builder->allocRunTextPosH(font, glyphCount, point.y(), textBytes, SkString(), &bounds);
memcpy(runBuffer.utf8text, utf8text, textBytes);
const char* txtPtr = utf8text;
for (int i = 0; i < glyphCount; ++i) {
@@ -64,10 +62,10 @@
txtPtr += utf8_lead_byte_to_count(txtPtr);
SkASSERT(txtPtr <= utf8text + textBytes);
}
- (void)paint.textToGlyphs(utf8text, textBytes, SkTextEncoding::kUTF8,
- runBuffer.glyphs, glyphCount);
+ (void)font.textToGlyphs(utf8text, textBytes, SkTextEncoding::kUTF8,
+ runBuffer.glyphs, glyphCount);
// replace with getPos()?
- (void)paint.getWidths(runBuffer.glyphs, glyphCount, runBuffer.pos);
+ (void)font.getWidths(runBuffer.glyphs, glyphCount, runBuffer.pos);
SkScalar x = point.x();
for (int i = 0; i < glyphCount; ++i) {
SkScalar w = runBuffer.pos[i];
diff --git a/src/core/SkTextBlob.cpp b/src/core/SkTextBlob.cpp
index 6d3abe7..ab2d582 100644
--- a/src/core/SkTextBlob.cpp
+++ b/src/core/SkTextBlob.cpp
@@ -523,37 +523,30 @@
return fCurrentRunBuffer;
}
-// SkPaint versions
-
-const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunText(const SkPaint& paint, int count,
+const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunText(const SkFont& font, int count,
SkScalar x, SkScalar y,
int textByteCount,
SkString lang,
const SkRect* bounds) {
- SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
this->allocInternal(font, SkTextBlob::kDefault_Positioning, count, textByteCount, SkPoint::Make(x, y), bounds);
return fCurrentRunBuffer;
}
-const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPosH(const SkPaint& paint, int count,
+const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPosH(const SkFont& font, int count,
SkScalar y,
int textByteCount,
SkString lang,
const SkRect* bounds) {
- SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
this->allocInternal(font, SkTextBlob::kHorizontal_Positioning, count, textByteCount, SkPoint::Make(0, y),
bounds);
-
return fCurrentRunBuffer;
}
-const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPos(const SkPaint& paint, int count,
+const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPos(const SkFont& font, int count,
int textByteCount,
SkString lang,
const SkRect *bounds) {
- SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
this->allocInternal(font, SkTextBlob::kFull_Positioning, count, textByteCount, SkPoint::Make(0, 0), bounds);
-
return fCurrentRunBuffer;
}
@@ -671,8 +664,9 @@
SkPoint offset;
reader.readPoint(&offset);
- SkPaint font;
- reader.readPaint(&font);
+ SkPaint paint;
+ reader.readPaint(&paint);
+ SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
// Compute the expected size of the buffer and ensure we have enough to deserialize
// a run before allocating it.
diff --git a/src/core/SkTextBlobPriv.h b/src/core/SkTextBlobPriv.h
index e620a3d..3710afb 100644
--- a/src/core/SkTextBlobPriv.h
+++ b/src/core/SkTextBlobPriv.h
@@ -43,17 +43,17 @@
class SkTextBlobBuilderPriv {
public:
static const SkTextBlobBuilder::RunBuffer& AllocRunText(SkTextBlobBuilder* builder,
- const SkPaint& font, int count, SkScalar x, SkScalar y, int textByteCount,
+ const SkFont& font, int count, SkScalar x, SkScalar y, int textByteCount,
SkString lang, const SkRect* bounds = nullptr) {
return builder->allocRunText(font, count, x, y, textByteCount, lang, bounds);
}
static const SkTextBlobBuilder::RunBuffer& AllocRunTextPosH(SkTextBlobBuilder* builder,
- const SkPaint& font, int count, SkScalar y, int textByteCount, SkString lang,
+ const SkFont& font, int count, SkScalar y, int textByteCount, SkString lang,
const SkRect* bounds = nullptr) {
return builder->allocRunTextPosH(font, count, y, textByteCount, lang, bounds);
}
static const SkTextBlobBuilder::RunBuffer& AllocRunTextPos(SkTextBlobBuilder* builder,
- const SkPaint& font, int count, int textByteCount, SkString lang,
+ const SkFont& font, int count, int textByteCount, SkString lang,
const SkRect* bounds = nullptr) {
return builder->allocRunTextPos(font, count, textByteCount, lang, bounds);
}
@@ -237,6 +237,10 @@
SkASSERT(!this->done());
return fCurrentRun->offset();
}
+ const SkFont& font() const {
+ SkASSERT(!this->done());
+ return fCurrentRun->font();
+ }
const SkRunFont& runFont() const {
SkASSERT(!this->done());
return fCurrentRun->font();
diff --git a/tests/TextBlobTest.cpp b/tests/TextBlobTest.cpp
index 09e4bb1..51ff457 100644
--- a/tests/TextBlobTest.cpp
+++ b/tests/TextBlobTest.cpp
@@ -320,17 +320,16 @@
DEF_TEST(TextBlob_extended, reporter) {
SkTextBlobBuilder textBlobBuilder;
- SkPaint paint;
+ SkFont font;
const char text1[] = "Foo";
const char text2[] = "Bar";
- int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr);
+ int glyphCount = font.countText(text1, strlen(text1), kUTF8_SkTextEncoding);
SkAutoTMalloc<uint16_t> glyphs(glyphCount);
- (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get());
- paint.setTextEncoding(kGlyphID_SkTextEncoding);
+ (void)font.textToGlyphs(text1, strlen(text1), kUTF8_SkTextEncoding, glyphs.get(), glyphCount);
auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
- paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
+ font, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
memcpy(run.utf8text, text2, strlen(text2));
for (int i = 0; i < glyphCount; ++i) {
diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp
index 96491d7..1ba9a07 100644
--- a/tools/viewer/Viewer.cpp
+++ b/tools/viewer/Viewer.cpp
@@ -992,15 +992,17 @@
continue;
}
+ SkFont font = SkFont::LEGACY_ExtractFromPaint(*filteredPaint);
+
const SkTextBlobBuilder::RunBuffer& runBuffer
= it.positioning() == SkTextBlobRunIterator::kDefault_Positioning
- ? SkTextBlobBuilderPriv::AllocRunText(&builder, *filteredPaint,
+ ? SkTextBlobBuilderPriv::AllocRunText(&builder, font,
it.offset().x(),it.offset().y(), it.glyphCount(), it.textSize(), SkString())
: it.positioning() == SkTextBlobRunIterator::kHorizontal_Positioning
- ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, *filteredPaint,
+ ? SkTextBlobBuilderPriv::AllocRunTextPosH(&builder, font,
it.offset().y(), it.glyphCount(), it.textSize(), SkString())
: it.positioning() == SkTextBlobRunIterator::kFull_Positioning
- ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, *filteredPaint,
+ ? SkTextBlobBuilderPriv::AllocRunTextPos(&builder, font,
it.glyphCount(), it.textSize(), SkString())
: (SkASSERT_RELEASE(false), SkTextBlobBuilder::RunBuffer());
uint32_t glyphCount = it.glyphCount();