impl breakText in SkFont

Bug: skia:
Change-Id: I449e0b29f98dc194a335be476f6dbdd69efad16b
Reviewed-on: https://skia-review.googlesource.com/c/174504
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2018-12-05 05:34:58 -05:00 committed by Skia Commit-Bot
parent ef211e600c
commit 70fbc36512
2 changed files with 20 additions and 90 deletions

View File

@ -347,59 +347,8 @@ SkScalar SkPaint::measureText(const void* textData, size_t length, SkRect* bound
size_t SkPaint::breakText(const void* textD, size_t length, SkScalar maxWidth, size_t SkPaint::breakText(const void* textD, size_t length, SkScalar maxWidth,
SkScalar* measuredWidth) const { SkScalar* measuredWidth) const {
if (0 == length || 0 >= maxWidth) { return SkFont::LEGACY_ExtractFromPaint(*this).breakText(textD, length,
if (measuredWidth) { (SkTextEncoding)this->getTextEncoding(), maxWidth, measuredWidth);
*measuredWidth = 0;
}
return 0;
}
if (0 == fTextSize) {
if (measuredWidth) {
*measuredWidth = 0;
}
return length;
}
SkASSERT(textD != nullptr);
const char* text = (const char*)textD;
const char* stop = text + length;
SkCanonicalizePaint canon(*this);
const SkPaint& paint = canon.getPaint();
SkScalar scale = canon.getScale();
// adjust max in case we changed the textSize in paint
if (scale) {
maxWidth /= scale;
}
const SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
auto cache = SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(font, paint);
SkFontPriv::GlyphCacheProc glyphCacheProc = SkFontPriv::GetGlyphCacheProc(
static_cast<SkTextEncoding>(paint.getTextEncoding()), false);
SkScalar width = 0;
while (text < stop) {
const char* curr = text;
SkScalar x = advance(glyphCacheProc(cache.get(), &text, stop));
if ((width += x) > maxWidth) {
width -= x;
text = curr;
break;
}
}
if (measuredWidth) {
if (scale) {
width *= scale;
}
*measuredWidth = width;
}
// return the number of bytes measured
return text - stop + length;
} }
SkScalar SkPaint::getFontMetrics(SkFontMetrics* metrics) const { SkScalar SkPaint::getFontMetrics(SkFontMetrics* metrics) const {

View File

@ -7,35 +7,19 @@
#include "SkAutoMalloc.h" #include "SkAutoMalloc.h"
#include "SkFont.h" #include "SkFont.h"
#include "SkPaint.h"
#include "Test.h" #include "Test.h"
static size_t break_text(const SkPaint& paint, const void* text, size_t length, SkScalar maxw, static void test_monotonic(skiatest::Reporter* reporter, const SkFont& font, const char* msg) {
SkScalar* measuredw, skiatest::Reporter* reporter) {
size_t n = paint.breakText(text, length, maxw, measuredw);
SkScalar measuredw2;
size_t n2 = SkFont::LEGACY_ExtractFromPaint(paint).breakText(text, length,
(SkTextEncoding)paint.getTextEncoding(), maxw, &measuredw2);
REPORTER_ASSERT(reporter, n == n2);
if (measuredw) {
REPORTER_ASSERT(reporter, *measuredw == measuredw2);
}
return n;
}
static void test_monotonic(skiatest::Reporter* reporter,
const SkPaint& paint,
const char* msg) {
const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj"; const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
const size_t length = strlen(text); const size_t length = strlen(text);
const SkScalar width = paint.measureText(text, length); const SkScalar width = font.measureText(text, length, kUTF8_SkTextEncoding);
SkScalar mm = 0; SkScalar mm = 0;
size_t nn = 0; size_t nn = 0;
const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1); const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1);
for (SkScalar w = 0; w <= width; w += step) { for (SkScalar w = 0; w <= width; w += step) {
SkScalar m; SkScalar m;
const size_t n = break_text(paint, text, length, w, &m, reporter); const size_t n = font.breakText(text, length, kUTF8_SkTextEncoding, w, &m);
REPORTER_ASSERT(reporter, n <= length, msg); REPORTER_ASSERT(reporter, n <= length, msg);
REPORTER_ASSERT(reporter, m <= width, msg); REPORTER_ASSERT(reporter, m <= width, msg);
@ -56,44 +40,41 @@ static void test_monotonic(skiatest::Reporter* reporter,
} }
} }
static void test_eq_measure_text(skiatest::Reporter* reporter, static void test_eq_measure_text(skiatest::Reporter* reporter, const SkFont& font,
const SkPaint& paint,
const char* msg) { const char* msg) {
const char* text = "The ultimate measure of a man is not where he stands in moments of comfort " const char* text = "The ultimate measure of a man is not where he stands in moments of comfort "
"and convenience, but where he stands at times of challenge and controversy."; "and convenience, but where he stands at times of challenge and controversy.";
const size_t length = strlen(text); const size_t length = strlen(text);
const SkScalar width = paint.measureText(text, length); const SkScalar width = font.measureText(text, length, kUTF8_SkTextEncoding);
SkScalar mm; SkScalar mm;
const size_t length2 = break_text(paint, text, length, width, &mm, reporter); const size_t length2 = font.breakText(text, length, kUTF8_SkTextEncoding, width, &mm);
REPORTER_ASSERT(reporter, length2 == length, msg); REPORTER_ASSERT(reporter, length2 == length, msg);
REPORTER_ASSERT(reporter, mm == width, msg); REPORTER_ASSERT(reporter, mm == width, msg);
} }
static void test_long_text(skiatest::Reporter* reporter, static void test_long_text(skiatest::Reporter* reporter, const SkFont& font, const char* msg) {
const SkPaint& paint,
const char* msg) {
static const int kSize = 16 * 1024; static const int kSize = 16 * 1024;
SkAutoMalloc block(kSize); SkAutoMalloc block(kSize);
memset(block.get(), 'a', kSize - 1); memset(block.get(), 'a', kSize - 1);
char* text = static_cast<char*>(block.get()); char* text = static_cast<char*>(block.get());
text[kSize - 1] = '\0'; text[kSize - 1] = '\0';
const SkScalar width = paint.measureText(text, kSize); const SkScalar width = font.measureText(text, kSize, kUTF8_SkTextEncoding);
SkScalar mm; SkScalar mm;
const size_t length = break_text(paint, text, kSize, width, &mm, reporter); const size_t length = font.breakText(text, kSize, kUTF8_SkTextEncoding, width, &mm);
REPORTER_ASSERT(reporter, length == kSize, msg); REPORTER_ASSERT(reporter, length == kSize, msg);
REPORTER_ASSERT(reporter, mm == width, msg); REPORTER_ASSERT(reporter, mm == width, msg);
} }
DEF_TEST(PaintBreakText, reporter) { DEF_TEST(PaintBreakText, reporter) {
SkPaint paint; SkFont font;
test_monotonic(reporter, paint, "default"); test_monotonic(reporter, font, "default");
test_eq_measure_text(reporter, paint, "default"); test_eq_measure_text(reporter, font, "default");
test_long_text(reporter, paint, "default"); test_long_text(reporter, font, "default");
paint.setTextSize(SkIntToScalar(1 << 17)); font.setSize(SkIntToScalar(1 << 17));
test_monotonic(reporter, paint, "huge text size"); test_monotonic(reporter, font, "huge text size");
test_eq_measure_text(reporter, paint, "huge text size"); test_eq_measure_text(reporter, font, "huge text size");
paint.setTextSize(0); font.setSize(0);
test_monotonic(reporter, paint, "zero text size"); test_monotonic(reporter, font, "zero text size");
} }