2014-06-23 13:59:15 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2014-06-23 13:59:15 +00:00
|
|
|
|
|
|
|
/* This test tries to define the effect of using hairline strokes on text.
|
|
|
|
* Provides non-hairline images for reference and consistency checks.
|
|
|
|
* glyph_pos_(h/n)_(s/f/b)
|
|
|
|
* -> test hairline/non-hairline stroke/fill/stroke+fill.
|
|
|
|
*/
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kTextHeight = 14.0f;
|
|
|
|
constexpr char kText[] = "Proportional Hamburgefons #% fi";
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
static void drawTestCase(SkCanvas* canvas,
|
|
|
|
SkScalar textScale,
|
|
|
|
SkScalar strokeWidth,
|
|
|
|
SkPaint::Style strokeStyle);
|
|
|
|
|
|
|
|
static void draw_gm(SkCanvas* canvas,
|
|
|
|
SkScalar strokeWidth,
|
|
|
|
SkPaint::Style strokeStyle) {
|
2018-02-08 21:14:48 +00:00
|
|
|
// There's a black pixel at 40, 40 for reference.
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawPoint(40, 40, SkPaint());
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
// Two reference images.
|
|
|
|
canvas->translate(50.0f, 50.0f);
|
|
|
|
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->translate(0.0f, 50.0f);
|
|
|
|
drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
// Uniform scaling test.
|
|
|
|
canvas->translate(0.0f, 100.0f);
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(3.0f, 3.0f);
|
|
|
|
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
|
|
|
|
canvas->restore();
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
// Non-uniform scaling test.
|
|
|
|
canvas->translate(0.0f, 100.0f);
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(3.0f, 6.0f);
|
|
|
|
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
|
|
|
|
canvas->restore();
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
// Skew test.
|
|
|
|
canvas->translate(0.0f, 80.0f);
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(3.0f, 3.0f);
|
|
|
|
SkMatrix skew;
|
|
|
|
skew.setIdentity();
|
|
|
|
skew.setSkewX(8.0f / 25.0f);
|
|
|
|
skew.setSkewY(2.0f / 25.0f);
|
|
|
|
canvas->concat(skew);
|
|
|
|
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
|
|
|
|
canvas->restore();
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
// Perspective test.
|
|
|
|
canvas->translate(0.0f, 80.0f);
|
|
|
|
canvas->save();
|
|
|
|
SkMatrix perspective;
|
|
|
|
perspective.setIdentity();
|
|
|
|
perspective.setPerspX(-SkScalarInvert(340));
|
|
|
|
perspective.setSkewX(8.0f / 25.0f);
|
|
|
|
perspective.setSkewY(2.0f / 25.0f);
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->concat(perspective);
|
|
|
|
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
|
|
|
|
canvas->restore();
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2014-06-23 13:59:15 +00:00
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
static void drawTestCase(SkCanvas* canvas,
|
|
|
|
SkScalar textScale,
|
|
|
|
SkScalar strokeWidth,
|
|
|
|
SkPaint::Style strokeStyle) {
|
2018-12-16 20:57:29 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStrokeWidth(strokeWidth);
|
|
|
|
paint.setStyle(strokeStyle);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), kTextHeight * textScale);
|
2018-12-16 20:57:29 +00:00
|
|
|
|
|
|
|
// This demonstrates that we can not measure the text if
|
|
|
|
// there's a device transform. The canvas total matrix will
|
|
|
|
// end up being a device transform.
|
2020-12-03 16:21:31 +00:00
|
|
|
bool drawRef = !(canvas->getLocalToDeviceAs3x3().getType() &
|
2018-12-16 20:57:29 +00:00
|
|
|
~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
|
|
|
|
|
|
|
|
SkRect bounds;
|
|
|
|
if (drawRef) {
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
|
2018-12-16 20:57:29 +00:00
|
|
|
&bounds, &paint);
|
|
|
|
|
|
|
|
paint.setStrokeWidth(0.0f);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
// Green box is the measured text bounds.
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawRect(bounds, paint);
|
|
|
|
|
|
|
|
// Red line is the measured advance from the 0,0 of the text position.
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Black text is the testcase, eg. the text.
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setStrokeWidth(strokeWidth);
|
|
|
|
paint.setStyle(strokeStyle);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
|
|
|
|
0.0f, 0.0f, font, paint);
|
2018-12-16 20:57:29 +00:00
|
|
|
|
|
|
|
if (drawRef) {
|
|
|
|
const size_t len = sizeof(kText) - 1;
|
|
|
|
SkGlyphID glyphs[len];
|
2019-05-07 19:38:46 +00:00
|
|
|
const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len);
|
2018-12-16 20:57:29 +00:00
|
|
|
SkScalar widths[len]; // len is conservative. we really only need 'count'
|
|
|
|
font.getWidthsBounds(glyphs, count, widths, nullptr, &paint);
|
|
|
|
|
|
|
|
paint.setStrokeWidth(0.0f);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
// Magenta lines are the positions for the characters.
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
SkScalar w = bounds.x();
|
|
|
|
for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
|
|
|
|
canvas->drawLine(w, 0.0f, w, 5.0f, paint);
|
|
|
|
w += widths[i];
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2018-12-16 20:57:29 +00:00
|
|
|
}
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
|
|
|
|
draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
|
|
|
|
draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
|
|
|
|
draw_gm(c, 0.0f, SkPaint::kStroke_Style);
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
|
|
|
|
draw_gm(c, 1.2f, SkPaint::kStroke_Style);
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
|
|
|
|
draw_gm(c, 0.0f, SkPaint::kFill_Style);
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
|
|
|
|
draw_gm(c, 1.2f, SkPaint::kFill_Style);
|
2014-06-23 13:59:15 +00:00
|
|
|
}
|