2014-08-21 22:03:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPoint.h"
|
2018-07-19 14:07:01 +00:00
|
|
|
#include "SkSerialProcs.h"
|
2018-07-27 13:47:15 +00:00
|
|
|
#include "SkTextBlobPriv.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
2015-11-17 19:39:32 +00:00
|
|
|
#include "SkTypeface.h"
|
2014-08-21 22:03:04 +00:00
|
|
|
|
|
|
|
#include "Test.h"
|
2017-12-08 16:46:09 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-08-21 22:03:04 +00:00
|
|
|
|
|
|
|
class TextBlobTester {
|
|
|
|
public:
|
2014-08-22 14:50:45 +00:00
|
|
|
// This unit test feeds an SkTextBlobBuilder various runs then checks to see if
|
|
|
|
// the result contains the provided data and merges runs when appropriate.
|
|
|
|
static void TestBuilder(skiatest::Reporter* reporter) {
|
2014-08-21 22:03:04 +00:00
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
|
|
|
// empty run set
|
2015-08-27 14:41:13 +00:00
|
|
|
RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0);
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set1[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 100 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1));
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set2[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 100 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2));
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set3[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3));
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set4[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4));
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set5[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 300, 250 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef mergedSet5[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 250 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5,
|
|
|
|
SK_ARRAY_COUNT(mergedSet5));
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set6[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 200, 200 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 300, 300 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef mergedSet6[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 384, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6,
|
|
|
|
SK_ARRAY_COUNT(mergedSet6));
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef set7[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 400, 350 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 400, 350 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 550 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 650 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 400, 750 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kFull_Positioning, 400, 850 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunDef mergedSet7[] = {
|
2018-07-27 13:47:15 +00:00
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
|
|
|
|
{ 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
|
|
|
|
{ 256, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 550 },
|
|
|
|
{ 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 650 },
|
|
|
|
{ 256, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
2014-08-22 14:50:45 +00:00
|
|
|
RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7,
|
|
|
|
SK_ARRAY_COUNT(mergedSet7));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This unit test verifies blob bounds computation.
|
|
|
|
static void TestBounds(skiatest::Reporter* reporter) {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
SkPaint font;
|
|
|
|
font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
|
|
|
|
// Explicit bounds.
|
|
|
|
{
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2017-03-13 13:03:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, !blob);
|
2014-08-22 14:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
|
|
|
|
builder.allocRun(font, 16, 0, 0, &r1);
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2014-08-22 14:50:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, blob->bounds() == r1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
|
|
|
|
builder.allocRunPosH(font, 16, 0, &r1);
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2014-08-22 14:50:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, blob->bounds() == r1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
|
|
|
|
builder.allocRunPos(font, 16, &r1);
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2014-08-22 14:50:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, blob->bounds() == r1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
|
|
|
|
SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50);
|
|
|
|
SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5);
|
|
|
|
|
|
|
|
builder.allocRun(font, 16, 0, 0, &r1);
|
|
|
|
builder.allocRunPosH(font, 16, 0, &r2);
|
|
|
|
builder.allocRunPos(font, 16, &r3);
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2014-08-22 14:50:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2017-03-13 13:03:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, !blob);
|
2014-08-22 14:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implicit bounds
|
2015-10-13 15:59:23 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers
|
|
|
|
// don't trigger asserts (http://crbug.com/542643).
|
|
|
|
SkPaint p;
|
|
|
|
p.setTextSize(0);
|
2015-10-14 15:11:40 +00:00
|
|
|
p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
2015-10-13 15:59:23 +00:00
|
|
|
|
|
|
|
const char* txt = "BOOO";
|
2015-10-14 15:11:40 +00:00
|
|
|
const size_t txtLen = strlen(txt);
|
|
|
|
const int glyphCount = p.textToGlyphs(txt, txtLen, nullptr);
|
|
|
|
|
|
|
|
p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(p, glyphCount);
|
|
|
|
|
|
|
|
p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
|
|
|
p.textToGlyphs(txt, txtLen, buffer.glyphs);
|
|
|
|
|
|
|
|
memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2015-10-13 15:59:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
|
|
|
|
}
|
2014-08-21 22:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:39:32 +00:00
|
|
|
// Verify that text-related properties are captured in run paints.
|
|
|
|
static void TestPaintProps(skiatest::Reporter* reporter) {
|
|
|
|
SkPaint font;
|
|
|
|
font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
|
|
|
|
// Kitchen sink font.
|
|
|
|
font.setTextSize(42);
|
|
|
|
font.setTextScaleX(4.2f);
|
2016-05-12 17:09:30 +00:00
|
|
|
font.setTypeface(SkTypeface::MakeDefault());
|
2015-11-17 19:39:32 +00:00
|
|
|
font.setTextSkewX(0.42f);
|
|
|
|
font.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
font.setHinting(SkPaint::kFull_Hinting);
|
|
|
|
font.setAntiAlias(true);
|
|
|
|
font.setFakeBoldText(true);
|
|
|
|
font.setLinearText(true);
|
|
|
|
font.setSubpixelText(true);
|
|
|
|
font.setDevKernText(true);
|
|
|
|
font.setLCDRenderText(true);
|
|
|
|
font.setEmbeddedBitmapText(true);
|
|
|
|
font.setAutohinted(true);
|
|
|
|
|
|
|
|
// Ensure we didn't pick default values by mistake.
|
|
|
|
SkPaint defaultPaint;
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.getTextSize() != font.getTextSize());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.getTextScaleX() != font.getTextScaleX());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.getTypeface() != font.getTypeface());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.getTextSkewX() != font.getTextSkewX());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.getTextAlign() != font.getTextAlign());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.getHinting() != font.getHinting());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.isAntiAlias() != font.isAntiAlias());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.isFakeBoldText() != font.isFakeBoldText());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.isLinearText() != font.isLinearText());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.isSubpixelText() != font.isSubpixelText());
|
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.isLCDRenderText() != font.isLCDRenderText());
|
2018-07-27 13:47:15 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
defaultPaint.isEmbeddedBitmapText() != font.isEmbeddedBitmapText());
|
2015-11-17 19:39:32 +00:00
|
|
|
REPORTER_ASSERT(reporter, defaultPaint.isAutohinted() != font.isAutohinted());
|
|
|
|
|
|
|
|
SkTextBlobBuilder builder;
|
2018-07-27 13:47:15 +00:00
|
|
|
AddRun(font, 1, SkTextBlobRunIterator::kDefault_Positioning, SkPoint::Make(0, 0), builder);
|
|
|
|
AddRun(font, 1, SkTextBlobRunIterator::kHorizontal_Positioning, SkPoint::Make(0, 0),
|
|
|
|
builder);
|
|
|
|
AddRun(font, 1, SkTextBlobRunIterator::kFull_Positioning, SkPoint::Make(0, 0), builder);
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2015-11-17 19:39:32 +00:00
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
SkTextBlobRunIterator it(blob.get());
|
2015-11-17 19:39:32 +00:00
|
|
|
while (!it.done()) {
|
|
|
|
SkPaint paint;
|
|
|
|
it.applyFontToPaint(&paint);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, paint.getTextSize() == font.getTextSize());
|
|
|
|
REPORTER_ASSERT(reporter, paint.getTextScaleX() == font.getTextScaleX());
|
|
|
|
REPORTER_ASSERT(reporter, paint.getTypeface() == font.getTypeface());
|
|
|
|
REPORTER_ASSERT(reporter, paint.getTextSkewX() == font.getTextSkewX());
|
|
|
|
REPORTER_ASSERT(reporter, paint.getTextAlign() == font.getTextAlign());
|
|
|
|
REPORTER_ASSERT(reporter, paint.getHinting() == font.getHinting());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isAntiAlias() == font.isAntiAlias());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isFakeBoldText() == font.isFakeBoldText());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isLinearText() == font.isLinearText());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isSubpixelText() == font.isSubpixelText());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isLCDRenderText() == font.isLCDRenderText());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isEmbeddedBitmapText() == font.isEmbeddedBitmapText());
|
|
|
|
REPORTER_ASSERT(reporter, paint.isAutohinted() == font.isAutohinted());
|
|
|
|
|
|
|
|
it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-21 22:03:04 +00:00
|
|
|
private:
|
|
|
|
struct RunDef {
|
2018-07-27 13:47:15 +00:00
|
|
|
unsigned count;
|
|
|
|
SkTextBlobRunIterator::GlyphPositioning pos;
|
|
|
|
SkScalar x, y;
|
2014-08-21 22:03:04 +00:00
|
|
|
};
|
|
|
|
|
2014-08-22 14:50:45 +00:00
|
|
|
static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
|
2014-08-21 22:03:04 +00:00
|
|
|
const RunDef in[], unsigned inCount,
|
|
|
|
const RunDef out[], unsigned outCount) {
|
2014-08-22 14:50:45 +00:00
|
|
|
SkPaint font;
|
|
|
|
font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
|
2014-08-21 22:03:04 +00:00
|
|
|
unsigned glyphCount = 0;
|
|
|
|
unsigned posCount = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < inCount; ++i) {
|
2014-08-22 14:50:45 +00:00
|
|
|
AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
|
2014-08-21 22:03:04 +00:00
|
|
|
glyphCount += in[i].count;
|
|
|
|
posCount += in[i].count * in[i].pos;
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(builder.make());
|
2017-03-13 13:03:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
|
|
|
|
if (!blob) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-21 22:03:04 +00:00
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
SkTextBlobRunIterator it(blob.get());
|
2014-08-21 22:03:04 +00:00
|
|
|
for (unsigned i = 0; i < outCount; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, !it.done());
|
|
|
|
REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
|
|
|
|
REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
|
2018-07-27 13:47:15 +00:00
|
|
|
if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
|
2014-08-21 22:03:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
|
|
|
|
REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
|
2018-07-27 13:47:15 +00:00
|
|
|
} else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
|
2014-08-21 22:03:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned k = 0; k < it.glyphCount(); ++k) {
|
|
|
|
REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
|
2018-07-27 13:47:15 +00:00
|
|
|
if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
|
2014-08-21 22:03:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
|
2018-07-27 13:47:15 +00:00
|
|
|
} else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
|
2014-08-21 22:03:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
|
|
|
|
REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, it.done());
|
|
|
|
}
|
|
|
|
|
2018-07-27 13:47:15 +00:00
|
|
|
static void AddRun(const SkPaint& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
|
2014-08-21 22:03:04 +00:00
|
|
|
const SkPoint& offset, SkTextBlobBuilder& builder,
|
2015-08-27 14:41:13 +00:00
|
|
|
const SkRect* bounds = nullptr) {
|
2014-08-21 22:03:04 +00:00
|
|
|
switch (pos) {
|
2018-07-27 13:47:15 +00:00
|
|
|
case SkTextBlobRunIterator::kDefault_Positioning: {
|
2014-08-21 22:03:04 +00:00
|
|
|
const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
|
|
|
|
offset.y(), bounds);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
rb.glyphs[i] = i;
|
|
|
|
}
|
|
|
|
} break;
|
2018-07-27 13:47:15 +00:00
|
|
|
case SkTextBlobRunIterator::kHorizontal_Positioning: {
|
2014-08-21 22:03:04 +00:00
|
|
|
const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
|
|
|
|
bounds);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
rb.glyphs[i] = i;
|
|
|
|
rb.pos[i] = SkIntToScalar(i);
|
|
|
|
}
|
|
|
|
} break;
|
2018-07-27 13:47:15 +00:00
|
|
|
case SkTextBlobRunIterator::kFull_Positioning: {
|
2014-08-21 22:03:04 +00:00
|
|
|
const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
rb.glyphs[i] = i;
|
|
|
|
rb.pos[i * 2] = SkIntToScalar(i);
|
|
|
|
rb.pos[i * 2 + 1] = -SkIntToScalar(i);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("unhandled positioning value");
|
2014-08-21 22:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(TextBlob_builder, reporter) {
|
2014-08-22 14:50:45 +00:00
|
|
|
TextBlobTester::TestBuilder(reporter);
|
|
|
|
TextBlobTester::TestBounds(reporter);
|
2014-08-21 22:03:04 +00:00
|
|
|
}
|
2015-11-17 19:39:32 +00:00
|
|
|
|
|
|
|
DEF_TEST(TextBlob_paint, reporter) {
|
|
|
|
TextBlobTester::TestPaintProps(reporter);
|
|
|
|
}
|
2016-08-30 18:58:33 +00:00
|
|
|
|
|
|
|
DEF_TEST(TextBlob_extended, reporter) {
|
|
|
|
SkTextBlobBuilder textBlobBuilder;
|
|
|
|
SkPaint paint;
|
|
|
|
const char text1[] = "Foo";
|
|
|
|
const char text2[] = "Bar";
|
|
|
|
|
|
|
|
int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr);
|
|
|
|
SkAutoTMalloc<uint16_t> glyphs(glyphCount);
|
|
|
|
(void)paint.textToGlyphs(text1, strlen(text1), glyphs.get());
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
|
2018-08-09 14:07:33 +00:00
|
|
|
auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
|
2016-08-30 18:58:33 +00:00
|
|
|
paint, 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) {
|
|
|
|
run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
|
|
|
|
}
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(textBlobBuilder.make());
|
2016-08-30 18:58:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, blob);
|
|
|
|
|
|
|
|
for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
|
|
|
|
REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
|
|
|
|
for (uint32_t i = 0; i < it.glyphCount(); ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
|
|
|
|
}
|
2018-07-27 13:47:15 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
|
2016-08-30 18:58:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
|
|
|
|
REPORTER_ASSERT(reporter, it.textSize() > 0);
|
|
|
|
REPORTER_ASSERT(reporter, it.clusters());
|
|
|
|
for (uint32_t i = 0; i < it.glyphCount(); ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, i == it.clusters()[i]);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 14:27:40 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkTDArray.h"
|
|
|
|
|
|
|
|
static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
|
|
|
|
sk_sp<SkTypeface> tf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(16);
|
|
|
|
paint.setTypeface(tf);
|
|
|
|
|
|
|
|
int glyphCount = paint.textToGlyphs(text, strlen(text), nullptr);
|
|
|
|
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(paint, glyphCount, x, y);
|
|
|
|
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
|
|
|
(void)paint.textToGlyphs(text, strlen(text), buffer.glyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkImage> render(const SkTextBlob* blob) {
|
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
|
|
|
|
SkScalarRoundToInt(blob->bounds().height()));
|
2017-07-11 19:07:08 +00:00
|
|
|
if (!surf) {
|
|
|
|
return nullptr; // bounds are empty?
|
|
|
|
}
|
2017-07-11 14:27:40 +00:00
|
|
|
surf->getCanvas()->clear(SK_ColorWHITE);
|
|
|
|
surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
|
|
|
|
return surf->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:07:01 +00:00
|
|
|
static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
|
|
|
|
auto array = (SkTDArray<SkTypeface*>*)ctx;
|
|
|
|
*array->append() = tf;
|
|
|
|
return sk_sp<SkData>(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
|
|
|
|
auto array = (SkTDArray<SkTypeface*>*)ctx;
|
|
|
|
for (int i = 0; i < array->count(); ++i) {
|
|
|
|
auto result = (*array)[i];
|
|
|
|
if (result) {
|
|
|
|
(*array)[i] = nullptr;
|
|
|
|
return sk_ref_sp(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkASSERT(false);
|
|
|
|
return sk_sp<SkTypeface>(nullptr);
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:27:40 +00:00
|
|
|
/*
|
|
|
|
* Build a blob with more than one typeface.
|
|
|
|
* Draw it into an offscreen,
|
|
|
|
* then serialize and deserialize,
|
|
|
|
* Then draw the new instance and assert it draws the same as the original.
|
|
|
|
*/
|
|
|
|
DEF_TEST(TextBlob_serialize, reporter) {
|
2017-12-08 16:46:09 +00:00
|
|
|
sk_sp<SkTextBlob> blob0 = []() {
|
2017-12-08 18:20:01 +00:00
|
|
|
sk_sp<SkTypeface> tf = SkTypeface::MakeDefault();
|
2017-07-11 14:27:40 +00:00
|
|
|
|
2017-12-08 16:46:09 +00:00
|
|
|
SkTextBlobBuilder builder;
|
2017-12-08 18:20:01 +00:00
|
|
|
add_run(&builder, "Hello", 10, 20, nullptr); // we don't flatten this in the paint
|
|
|
|
add_run(&builder, "World", 10, 40, tf); // we will flatten this in the paint
|
2017-12-08 16:46:09 +00:00
|
|
|
return builder.make();
|
|
|
|
}();
|
2017-07-11 14:27:40 +00:00
|
|
|
|
|
|
|
SkTDArray<SkTypeface*> array;
|
2018-07-19 14:07:01 +00:00
|
|
|
SkSerialProcs serializeProcs;
|
|
|
|
serializeProcs.fTypefaceProc = &SerializeTypeface;
|
|
|
|
serializeProcs.fTypefaceCtx = (void*) &array;
|
|
|
|
sk_sp<SkData> data = blob0->serialize(serializeProcs);
|
|
|
|
REPORTER_ASSERT(reporter, array.count() == 2);
|
|
|
|
SkDeserialProcs deserializeProcs;
|
|
|
|
deserializeProcs.fTypefaceProc = &DeserializeTypeface;
|
|
|
|
deserializeProcs.fTypefaceCtx = (void*) &array;
|
|
|
|
sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
|
2017-07-11 14:27:40 +00:00
|
|
|
|
2017-07-11 19:07:08 +00:00
|
|
|
sk_sp<SkImage> img0 = render(blob0.get());
|
|
|
|
sk_sp<SkImage> img1 = render(blob1.get());
|
|
|
|
if (img0 && img1) {
|
2017-12-08 16:46:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get()));
|
2017-07-11 14:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 20:10:30 +00:00
|
|
|
|
|
|
|
DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
|
|
|
const char text[] = "Hello";
|
2018-07-27 20:28:01 +00:00
|
|
|
auto blob = SkTextBlob::MakeFromText(text, strlen(text), paint);
|
2018-07-17 20:10:30 +00:00
|
|
|
|
|
|
|
int runs = 0;
|
|
|
|
for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
|
|
|
|
REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
|
2018-07-27 13:47:15 +00:00
|
|
|
REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
|
2018-07-17 20:10:30 +00:00
|
|
|
runs += 1;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, runs == 1);
|
|
|
|
|
|
|
|
}
|