2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-07-02 13:56:39 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkMaskFilter.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/private/SkTo.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
|
|
|
#include "src/core/SkPaintPriv.h"
|
|
|
|
#include "src/core/SkReadBuffer.h"
|
|
|
|
#include "src/core/SkWriteBuffer.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
|
|
|
#include "tests/Test.h"
|
2018-07-31 13:44:36 +00:00
|
|
|
#undef ASSERT
|
2013-07-02 13:56:39 +00:00
|
|
|
|
2014-03-24 23:03:11 +00:00
|
|
|
DEF_TEST(Paint_copy, reporter) {
|
2011-12-01 17:09:21 +00:00
|
|
|
SkPaint paint;
|
|
|
|
// set a few member variables
|
|
|
|
paint.setStyle(SkPaint::kStrokeAndFill_Style);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(2));
|
|
|
|
// set a few pointers
|
2018-03-14 17:01:17 +00:00
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
|
2016-04-04 17:02:58 +00:00
|
|
|
SkBlurMask::ConvertRadiusToSigma(1)));
|
2011-12-01 17:09:21 +00:00
|
|
|
|
|
|
|
// copy the paint using the copy constructor and check they are the same
|
|
|
|
SkPaint copiedPaint = paint;
|
2019-01-28 15:57:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getHash() == copiedPaint.getHash());
|
2013-08-07 22:36:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint == copiedPaint);
|
2011-12-01 17:09:21 +00:00
|
|
|
|
|
|
|
// copy the paint using the equal operator and check they are the same
|
|
|
|
copiedPaint = paint;
|
2013-08-07 22:36:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint == copiedPaint);
|
2011-12-01 17:09:21 +00:00
|
|
|
|
|
|
|
// clean the paint and check they are back to their initial states
|
|
|
|
SkPaint cleanPaint;
|
|
|
|
paint.reset();
|
|
|
|
copiedPaint.reset();
|
2013-08-07 22:36:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, cleanPaint == paint);
|
|
|
|
REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
|
2011-12-01 17:09:21 +00:00
|
|
|
}
|
2009-06-22 17:38:10 +00:00
|
|
|
|
|
|
|
// found and fixed for webkit: mishandling when we hit recursion limit on
|
|
|
|
// mostly degenerate cubic flatness test
|
2014-03-24 23:03:11 +00:00
|
|
|
DEF_TEST(Paint_regression_cubic, reporter) {
|
2009-06-22 17:38:10 +00:00
|
|
|
SkPath path, stroke;
|
|
|
|
SkPaint paint;
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
path.moveTo(460.2881309415525f,
|
|
|
|
303.250847066498f);
|
|
|
|
path.cubicTo(463.36378422175284f,
|
|
|
|
302.1169735073363f,
|
|
|
|
456.32239330810046f,
|
|
|
|
304.720354932878f,
|
|
|
|
453.15255460013304f,
|
|
|
|
305.788586869862f);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2009-06-22 17:38:10 +00:00
|
|
|
SkRect fillR, strokeR;
|
|
|
|
fillR = path.getBounds();
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(2));
|
|
|
|
paint.getFillPath(path, &stroke);
|
|
|
|
strokeR = stroke.getBounds();
|
|
|
|
|
|
|
|
SkRect maxR = fillR;
|
2020-02-05 18:34:09 +00:00
|
|
|
SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
|
2009-06-22 17:38:10 +00:00
|
|
|
SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
|
2017-02-14 15:59:19 +00:00
|
|
|
paint.getStrokeWidth() * miter :
|
2009-06-22 17:38:10 +00:00
|
|
|
paint.getStrokeWidth();
|
|
|
|
maxR.inset(-inset, -inset);
|
|
|
|
|
|
|
|
// test that our stroke didn't explode
|
|
|
|
REPORTER_ASSERT(reporter, maxR.contains(strokeR));
|
|
|
|
}
|
|
|
|
|
2014-04-16 12:58:02 +00:00
|
|
|
DEF_TEST(Paint_flattening, reporter) {
|
|
|
|
const SkPaint::Cap caps[] = {
|
|
|
|
SkPaint::kButt_Cap,
|
|
|
|
SkPaint::kRound_Cap,
|
|
|
|
SkPaint::kSquare_Cap,
|
|
|
|
};
|
|
|
|
const SkPaint::Join joins[] = {
|
|
|
|
SkPaint::kMiter_Join,
|
|
|
|
SkPaint::kRound_Join,
|
|
|
|
SkPaint::kBevel_Join,
|
|
|
|
};
|
|
|
|
const SkPaint::Style styles[] = {
|
|
|
|
SkPaint::kFill_Style,
|
|
|
|
SkPaint::kStroke_Style,
|
|
|
|
SkPaint::kStrokeAndFill_Style,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FOR_SETUP(index, array, setter) \
|
|
|
|
for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
|
2019-01-04 19:01:06 +00:00
|
|
|
paint.setter(array[index]);
|
2014-04-16 12:58:02 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2019-01-04 19:01:06 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
// we don't serialize hinting or encoding -- soon to be removed from paint
|
2014-04-16 12:58:02 +00:00
|
|
|
|
|
|
|
FOR_SETUP(l, caps, setStrokeCap)
|
|
|
|
FOR_SETUP(m, joins, setStrokeJoin)
|
|
|
|
FOR_SETUP(p, styles, setStyle)
|
|
|
|
|
2016-05-04 18:06:28 +00:00
|
|
|
SkBinaryWriteBuffer writer;
|
2018-03-06 20:09:27 +00:00
|
|
|
SkPaintPriv::Flatten(paint, writer);
|
2014-04-16 12:58:02 +00:00
|
|
|
|
2016-04-29 20:58:09 +00:00
|
|
|
SkAutoMalloc buf(writer.bytesWritten());
|
|
|
|
writer.writeToMemory(buf.get());
|
|
|
|
SkReadBuffer reader(buf.get(), writer.bytesWritten());
|
2014-04-17 03:05:10 +00:00
|
|
|
|
2014-04-16 12:58:02 +00:00
|
|
|
SkPaint paint2;
|
2019-01-14 22:36:54 +00:00
|
|
|
SkPaintPriv::Unflatten(&paint2, reader, nullptr);
|
2014-04-16 12:58:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint2 == paint);
|
|
|
|
|
2021-01-30 14:10:41 +00:00
|
|
|
}}}
|
2014-04-16 12:58:02 +00:00
|
|
|
#undef FOR_SETUP
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-04 19:47:42 +00:00
|
|
|
// found and fixed for android: not initializing rect for string's of length 0
|
2014-03-24 23:03:11 +00:00
|
|
|
DEF_TEST(Paint_regression_measureText, reporter) {
|
2013-03-04 19:47:42 +00:00
|
|
|
|
2018-12-15 18:45:33 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSize(12.0f);
|
2013-03-04 19:47:42 +00:00
|
|
|
|
|
|
|
SkRect r;
|
|
|
|
r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
|
|
|
|
|
|
|
|
// test that the rect was reset
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText("", 0, SkTextEncoding::kUTF8, &r);
|
2013-03-04 19:47:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, r.isEmpty());
|
|
|
|
}
|
|
|
|
|
2014-02-21 18:18:05 +00:00
|
|
|
#define ASSERT(expr) REPORTER_ASSERT(r, expr)
|
|
|
|
|
2014-09-25 18:57:53 +00:00
|
|
|
DEF_TEST(Paint_MoreFlattening, r) {
|
2014-02-21 18:18:05 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0x00AABBCC);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kModulate);
|
2014-02-21 18:18:05 +00:00
|
|
|
|
2016-05-04 18:06:28 +00:00
|
|
|
SkBinaryWriteBuffer writer;
|
2018-03-06 20:09:27 +00:00
|
|
|
SkPaintPriv::Flatten(paint, writer);
|
2014-02-21 18:18:05 +00:00
|
|
|
|
2016-04-29 20:58:09 +00:00
|
|
|
SkAutoMalloc buf(writer.bytesWritten());
|
|
|
|
writer.writeToMemory(buf.get());
|
|
|
|
SkReadBuffer reader(buf.get(), writer.bytesWritten());
|
|
|
|
|
2014-02-21 18:18:05 +00:00
|
|
|
SkPaint other;
|
2019-01-14 22:36:54 +00:00
|
|
|
SkPaintPriv::Unflatten(&other, reader, nullptr);
|
2014-02-21 18:18:05 +00:00
|
|
|
ASSERT(reader.offset() == writer.bytesWritten());
|
|
|
|
|
|
|
|
// No matter the encoding, these must always hold.
|
|
|
|
ASSERT(other.getColor() == paint.getColor());
|
2016-10-06 00:33:02 +00:00
|
|
|
ASSERT(other.getBlendMode() == paint.getBlendMode());
|
2014-02-21 18:18:05 +00:00
|
|
|
}
|
2014-10-07 16:26:10 +00:00
|
|
|
|
|
|
|
DEF_TEST(Paint_getHash, r) {
|
|
|
|
// Try not to inspect the actual hash values in here.
|
|
|
|
// We might want to change the hash function.
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
const uint32_t defaultHash = paint.getHash();
|
|
|
|
|
|
|
|
// Check that some arbitrary field affects the hash.
|
|
|
|
paint.setColor(0xFF00FF00);
|
|
|
|
REPORTER_ASSERT(r, paint.getHash() != defaultHash);
|
|
|
|
paint.setColor(SK_ColorBLACK); // Reset to default value.
|
|
|
|
REPORTER_ASSERT(r, paint.getHash() == defaultHash);
|
|
|
|
|
|
|
|
// This is part of fBitfields, the last field we hash.
|
2019-01-28 15:57:28 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2014-10-07 16:26:10 +00:00
|
|
|
REPORTER_ASSERT(r, paint.getHash() != defaultHash);
|
2019-01-28 15:57:28 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrcOver);
|
2014-10-07 16:26:10 +00:00
|
|
|
REPORTER_ASSERT(r, paint.getHash() == defaultHash);
|
|
|
|
}
|
2014-11-11 20:51:33 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkColorMatrixFilter.h"
|
2014-11-11 20:51:33 +00:00
|
|
|
|
|
|
|
DEF_TEST(Paint_nothingToDraw, r) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, !paint.nothingToDraw());
|
|
|
|
paint.setAlpha(0);
|
|
|
|
REPORTER_ASSERT(r, paint.nothingToDraw());
|
|
|
|
|
|
|
|
paint.setAlpha(0xFF);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kDst);
|
2014-11-11 20:51:33 +00:00
|
|
|
REPORTER_ASSERT(r, paint.nothingToDraw());
|
|
|
|
|
|
|
|
paint.setAlpha(0);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrcOver);
|
2014-11-11 20:51:33 +00:00
|
|
|
|
|
|
|
SkColorMatrix cm;
|
|
|
|
cm.setIdentity(); // does not change alpha
|
2019-04-22 02:17:03 +00:00
|
|
|
paint.setColorFilter(SkColorFilters::Matrix(cm));
|
2014-11-11 20:51:33 +00:00
|
|
|
REPORTER_ASSERT(r, paint.nothingToDraw());
|
|
|
|
|
2019-05-02 15:31:43 +00:00
|
|
|
cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
|
2019-04-22 02:17:03 +00:00
|
|
|
paint.setColorFilter(SkColorFilters::Matrix(cm));
|
2014-11-11 20:51:33 +00:00
|
|
|
REPORTER_ASSERT(r, !paint.nothingToDraw());
|
|
|
|
}
|
2018-11-12 01:37:45 +00:00
|
|
|
|
2018-11-24 18:27:27 +00:00
|
|
|
DEF_TEST(Font_getpos, r) {
|
|
|
|
SkFont font;
|
|
|
|
const char text[] = "Hamburgefons!@#!#23425,./;'[]";
|
2019-05-07 19:38:46 +00:00
|
|
|
int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
|
2018-11-24 18:27:27 +00:00
|
|
|
SkAutoTArray<uint16_t> glyphStorage(count);
|
|
|
|
uint16_t* glyphs = glyphStorage.get();
|
2019-05-07 19:38:46 +00:00
|
|
|
(void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
|
2018-11-24 18:27:27 +00:00
|
|
|
|
|
|
|
SkAutoTArray<SkScalar> widthStorage(count);
|
|
|
|
SkAutoTArray<SkScalar> xposStorage(count);
|
|
|
|
SkAutoTArray<SkPoint> posStorage(count);
|
|
|
|
|
|
|
|
SkScalar* widths = widthStorage.get();
|
|
|
|
SkScalar* xpos = xposStorage.get();
|
|
|
|
SkPoint* pos = posStorage.get();
|
|
|
|
|
|
|
|
for (bool subpix : { false, true }) {
|
|
|
|
font.setSubpixel(subpix);
|
2019-05-07 20:50:29 +00:00
|
|
|
for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) {
|
2018-11-24 18:27:27 +00:00
|
|
|
font.setHinting(hint);
|
|
|
|
for (auto size : { 1.0f, 12.0f, 100.0f }) {
|
|
|
|
font.setSize(size);
|
|
|
|
|
|
|
|
font.getWidths(glyphs, count, widths);
|
|
|
|
font.getXPos(glyphs, count, xpos, 10);
|
|
|
|
font.getPos(glyphs, count, pos, {10, 20});
|
|
|
|
|
|
|
|
auto nearly_eq = [](SkScalar a, SkScalar b) {
|
|
|
|
return SkScalarAbs(a - b) < 0.000001f;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkScalar x = 10;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
|
|
|
|
REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
|
|
|
|
REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
|
|
|
|
x += widths[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|