skia2/gm/glyph_pos_align.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

67 lines
2.3 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 "SkGradientShader.h"
/**
* This test exercises drawPosTextH and drawPosText with every text align.
*/
constexpr int kWidth = 480;
constexpr int kHeight = 600;
constexpr SkScalar kTextHeight = 64.0f;
constexpr int kMaxStringLength = 12;
static void drawTestCase(SkCanvas*, const char*, SkScalar, const SkPaint&);
DEF_SIMPLE_GM_BG(glyph_pos_align, canvas, kWidth, kHeight, SK_ColorBLACK) {
SkPaint paint;
paint.setTextSize(kTextHeight);
paint.setFakeBoldText(true);
const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
const SkPoint pts[] = {{0, 0}, {kWidth, kHeight}};
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
SkShader::kMirror_TileMode));
paint.setTextAlign(SkPaint::kRight_Align);
drawTestCase(canvas, "Right Align", kTextHeight, paint);
paint.setTextAlign(SkPaint::kCenter_Align);
drawTestCase(canvas, "Center Align", 4 * kTextHeight, paint);
paint.setTextAlign(SkPaint::kLeft_Align);
drawTestCase(canvas, "Left Align", 7 * kTextHeight, paint);
}
void drawTestCase(SkCanvas* canvas, const char* text, SkScalar y, const SkPaint& paint) {
SkScalar widths[kMaxStringLength];
SkScalar posX[kMaxStringLength];
SkPoint pos[kMaxStringLength];
int length = SkToInt(strlen(text));
SkASSERT(length <= kMaxStringLength);
paint.getTextWidths(text, length, widths);
float originX;
switch (paint.getTextAlign()) {
case SkPaint::kRight_Align: originX = 1; break;
case SkPaint::kCenter_Align: originX = 0.5f; break;
case SkPaint::kLeft_Align: originX = 0; break;
default: SkFAIL("Invalid paint origin"); return;
}
float x = kTextHeight;
for (int i = 0; i < length; ++i) {
posX[i] = x + originX * widths[i];
pos[i].set(posX[i], i ? pos[i - 1].y() + 3 : y + kTextHeight);
x += widths[i];
}
canvas->drawPosTextH(text, length, posX, y, paint);
canvas->drawPosText(text, length, pos, paint);
}