2016-02-25 18:28:11 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-01-11 17:44:43 +00:00
|
|
|
#include "SkAutoMalloc.h"
|
2018-12-03 03:21:14 +00:00
|
|
|
#include "SkFont.h"
|
2016-02-25 18:28:11 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
2018-12-03 03:21:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:28:11 +00:00
|
|
|
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;
|
2018-12-03 03:21:14 +00:00
|
|
|
const size_t n = break_text(paint, text, length, w, &m, reporter);
|
2016-02-25 18:28:11 +00:00
|
|
|
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, n <= length, msg);
|
|
|
|
REPORTER_ASSERT(reporter, m <= width, msg);
|
2016-02-25 18:28:11 +00:00
|
|
|
|
|
|
|
if (n == 0) {
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, m == 0, msg);
|
2016-02-25 18:28:11 +00:00
|
|
|
} else {
|
|
|
|
// now assert that we're monotonic
|
|
|
|
if (n == nn) {
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, m == mm, msg);
|
2016-02-25 18:28:11 +00:00
|
|
|
} else {
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, n > nn, msg);
|
|
|
|
REPORTER_ASSERT(reporter, m > mm, msg);
|
2016-02-25 18:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2018-12-03 03:21:14 +00:00
|
|
|
const size_t length2 = break_text(paint, text, length, width, &mm, reporter);
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, length2 == length, msg);
|
|
|
|
REPORTER_ASSERT(reporter, mm == width, msg);
|
2016-02-25 18:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-12-03 03:21:14 +00:00
|
|
|
const size_t length = break_text(paint, text, kSize, width, &mm, reporter);
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, length == kSize, msg);
|
|
|
|
REPORTER_ASSERT(reporter, mm == width, msg);
|
2016-02-25 18:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|