skia2/gm/glyph_pos.cpp
mtklein dbfd7ab108 Replace a lot of 'static const' with 'constexpr' or 'const'.
'static const' means, there must be at most one of these, and initialize it at
compile time if possible or runtime if necessary.  This leads to unexpected
code execution, and TSAN* will complain about races on the guard variables.

Generally 'constexpr' or 'const' are better choices.  Neither can cause races:
they're either intialized at compile time (constexpr) or intialized each time
independently (const).

This CL prefers constexpr where possible, and uses const where not.  It even
prefers constexpr over const where they don't make a difference... I want to have
lots of examples of constexpr for people to see and mimic.

The scoped-to-class static has nothing to do with any of this, and is not changed.

* Not yet on the bots, which use an older TSAN.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2300623005

Review-Url: https://codereview.chromium.org/2300623005
2016-09-01 11:24:54 -07:00

153 lines
5.1 KiB
C++

/*
* 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 "gm.h"
#include "SkCanvas.h"
#include "SkTypeface.h"
/* 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.
*/
constexpr SkScalar kTextHeight = 14.0f;
constexpr char kText[] = "Proportional Hamburgefons #% fi";
static void drawTestCase(SkCanvas* canvas,
SkScalar textScale,
SkScalar strokeWidth,
SkPaint::Style strokeStyle);
static void draw_gm(SkCanvas* canvas,
SkScalar strokeWidth,
SkPaint::Style strokeStyle) {
// There's a black pixel at 40, 40 for reference.
canvas->drawPoint(40.0f, 40.0f, SK_ColorBLACK);
// Two reference images.
canvas->translate(50.0f, 50.0f);
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
canvas->translate(0.0f, 50.0f);
drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
// 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();
// 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();
// 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();
// 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);
canvas->concat(perspective);
drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
canvas->restore();
}
static void drawTestCase(SkCanvas* canvas,
SkScalar textScale,
SkScalar strokeWidth,
SkPaint::Style strokeStyle) {
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setAntiAlias(true);
paint.setTextSize(kTextHeight * textScale);
sk_tool_utils::set_portable_typeface(&paint);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(strokeStyle);
// 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.
bool drawRef = !(canvas->getTotalMatrix().getType() &
~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
SkRect bounds;
if (drawRef) {
SkScalar advance = paint.measureText(kText, sizeof(kText) - 1, &bounds);
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);
canvas->drawText(kText, sizeof(kText) - 1, 0.0f, 0.0f, paint);
if (drawRef) {
SkScalar widths[sizeof(kText) - 1];
paint.getTextWidths(kText, sizeof(kText) - 1, widths, nullptr);
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];
}
}
}
DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
}
DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
}
DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
draw_gm(c, 0.0f, SkPaint::kStroke_Style);
}
DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
draw_gm(c, 1.2f, SkPaint::kStroke_Style);
}
DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
draw_gm(c, 0.0f, SkPaint::kFill_Style);
}
DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
draw_gm(c, 1.2f, SkPaint::kFill_Style);
}