skia2/tests/PaintBreakTextTest.cpp
Mike Reed da1b99f7f5 breakText on font
Bug: skia:
Change-Id: Iebf65b158a0b08ed8e65b77d9d0aeef8c159d5db
Reviewed-on: https://skia-review.googlesource.com/c/173770
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
2018-12-03 03:49:02 +00:00

100 lines
3.5 KiB
C++

/*
* Copyright 2011-2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAutoMalloc.h"
#include "SkFont.h"
#include "SkPaint.h"
#include "Test.h"
static size_t break_text(const SkPaint& paint, const void* text, size_t length, SkScalar maxw,
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 size_t length = strlen(text);
const SkScalar width = paint.measureText(text, length);
SkScalar mm = 0;
size_t nn = 0;
const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1);
for (SkScalar w = 0; w <= width; w += step) {
SkScalar m;
const size_t n = break_text(paint, text, length, w, &m, reporter);
REPORTER_ASSERT(reporter, n <= length, msg);
REPORTER_ASSERT(reporter, m <= width, msg);
if (n == 0) {
REPORTER_ASSERT(reporter, m == 0, msg);
} else {
// now assert that we're monotonic
if (n == nn) {
REPORTER_ASSERT(reporter, m == mm, msg);
} else {
REPORTER_ASSERT(reporter, n > nn, msg);
REPORTER_ASSERT(reporter, m > mm, msg);
}
}
nn = n;
mm = m;
}
}
static void test_eq_measure_text(skiatest::Reporter* reporter,
const SkPaint& paint,
const char* msg) {
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.";
const size_t length = strlen(text);
const SkScalar width = paint.measureText(text, length);
SkScalar mm;
const size_t length2 = break_text(paint, text, length, width, &mm, reporter);
REPORTER_ASSERT(reporter, length2 == length, msg);
REPORTER_ASSERT(reporter, mm == width, msg);
}
static void test_long_text(skiatest::Reporter* reporter,
const SkPaint& paint,
const char* msg) {
static const int kSize = 16 * 1024;
SkAutoMalloc block(kSize);
memset(block.get(), 'a', kSize - 1);
char* text = static_cast<char*>(block.get());
text[kSize - 1] = '\0';
const SkScalar width = paint.measureText(text, kSize);
SkScalar mm;
const size_t length = break_text(paint, text, kSize, width, &mm, reporter);
REPORTER_ASSERT(reporter, length == kSize, msg);
REPORTER_ASSERT(reporter, mm == width, msg);
}
DEF_TEST(PaintBreakText, reporter) {
SkPaint paint;
test_monotonic(reporter, paint, "default");
test_eq_measure_text(reporter, paint, "default");
test_long_text(reporter, paint, "default");
paint.setTextSize(SkIntToScalar(1 << 17));
test_monotonic(reporter, paint, "huge text size");
test_eq_measure_text(reporter, paint, "huge text size");
paint.setTextSize(0);
test_monotonic(reporter, paint, "zero text size");
}