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 "Test.h"
|
|
|
|
|
2018-12-05 10:34:58 +00:00
|
|
|
static void test_monotonic(skiatest::Reporter* reporter, const SkFont& font, const char* msg) {
|
2016-02-25 18:28:11 +00:00
|
|
|
const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
|
|
|
|
const size_t length = strlen(text);
|
2018-12-05 10:34:58 +00:00
|
|
|
const SkScalar width = font.measureText(text, length, kUTF8_SkTextEncoding);
|
2016-02-25 18:28:11 +00:00
|
|
|
|
|
|
|
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-05 10:34:58 +00:00
|
|
|
const size_t n = font.breakText(text, length, kUTF8_SkTextEncoding, w, &m);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:34:58 +00:00
|
|
|
static void test_eq_measure_text(skiatest::Reporter* reporter, const SkFont& font,
|
2016-02-25 18:28:11 +00:00
|
|
|
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);
|
2018-12-05 10:34:58 +00:00
|
|
|
const SkScalar width = font.measureText(text, length, kUTF8_SkTextEncoding);
|
2016-02-25 18:28:11 +00:00
|
|
|
|
|
|
|
SkScalar mm;
|
2018-12-05 10:34:58 +00:00
|
|
|
const size_t length2 = font.breakText(text, length, kUTF8_SkTextEncoding, width, &mm);
|
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
|
|
|
}
|
|
|
|
|
2018-12-05 10:34:58 +00:00
|
|
|
static void test_long_text(skiatest::Reporter* reporter, const SkFont& font, const char* msg) {
|
2016-02-25 18:28:11 +00:00
|
|
|
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';
|
2018-12-05 10:34:58 +00:00
|
|
|
const SkScalar width = font.measureText(text, kSize, kUTF8_SkTextEncoding);
|
2016-02-25 18:28:11 +00:00
|
|
|
|
|
|
|
SkScalar mm;
|
2018-12-05 10:34:58 +00:00
|
|
|
const size_t length = font.breakText(text, kSize, kUTF8_SkTextEncoding, width, &mm);
|
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) {
|
2018-12-05 10:34:58 +00:00
|
|
|
SkFont font;
|
|
|
|
test_monotonic(reporter, font, "default");
|
|
|
|
test_eq_measure_text(reporter, font, "default");
|
|
|
|
test_long_text(reporter, font, "default");
|
|
|
|
font.setSize(SkIntToScalar(1 << 17));
|
|
|
|
test_monotonic(reporter, font, "huge text size");
|
|
|
|
test_eq_measure_text(reporter, font, "huge text size");
|
|
|
|
font.setSize(0);
|
|
|
|
test_monotonic(reporter, font, "zero text size");
|
2016-02-25 18:28:11 +00:00
|
|
|
}
|