2014-03-12 20:31:24 +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"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
2022-03-17 03:39:21 +00:00
|
|
|
#include "include/core/SkFontArguments.h"
|
|
|
|
#include "include/core/SkFontMgr.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPathEffect.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTextBlob.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkDashPathEffect.h"
|
2020-01-27 22:43:41 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2014-03-12 20:31:24 +00:00
|
|
|
|
2014-03-14 14:02:58 +00:00
|
|
|
static void test_nulldev(SkCanvas* canvas) {
|
|
|
|
SkBitmap bm;
|
2014-06-10 02:52:07 +00:00
|
|
|
bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
|
2014-03-14 14:02:58 +00:00
|
|
|
// notice: no pixels mom! be sure we don't crash
|
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=352616
|
|
|
|
SkCanvas c(bm);
|
|
|
|
|
|
|
|
SkBitmap src;
|
|
|
|
src.allocN32Pixels(10, 10);
|
|
|
|
src.eraseColor(SK_ColorRED);
|
|
|
|
|
|
|
|
// ensure we don't crash
|
|
|
|
c.writePixels(src, 0, 0);
|
|
|
|
}
|
|
|
|
|
2018-12-24 19:52:46 +00:00
|
|
|
static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
|
|
|
|
SkScalar strokeWidth) {
|
2014-03-12 20:31:24 +00:00
|
|
|
SkPaint p(paint);
|
2014-05-22 17:41:41 +00:00
|
|
|
SkPoint loc = { 20, 435 };
|
2014-03-13 03:02:23 +00:00
|
|
|
|
2014-05-22 17:41:41 +00:00
|
|
|
if (strokeWidth > 0) {
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
|
2018-12-24 19:52:46 +00:00
|
|
|
canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
|
2014-05-22 17:41:41 +00:00
|
|
|
}
|
2014-03-13 03:02:23 +00:00
|
|
|
|
2014-03-12 20:31:24 +00:00
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
2014-05-22 17:41:41 +00:00
|
|
|
p.setStrokeWidth(strokeWidth);
|
2014-03-13 03:02:23 +00:00
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
|
2018-12-24 19:52:46 +00:00
|
|
|
canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
|
2014-03-12 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 19:52:46 +00:00
|
|
|
static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
|
2014-05-22 17:41:41 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
2018-12-24 19:52:46 +00:00
|
|
|
draw_text_stroked(canvas, paint, font, 10);
|
2014-05-22 17:41:41 +00:00
|
|
|
|
|
|
|
canvas->translate(200, 0);
|
2018-12-24 19:52:46 +00:00
|
|
|
draw_text_stroked(canvas, paint, font, 0);
|
2014-05-22 17:41:41 +00:00
|
|
|
|
|
|
|
const SkScalar intervals[] = { 20, 10, 5, 10 };
|
|
|
|
const SkScalar phase = 0;
|
|
|
|
|
|
|
|
canvas->translate(200, 0);
|
|
|
|
SkPaint p(paint);
|
2016-03-18 18:22:57 +00:00
|
|
|
p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase));
|
2018-12-24 19:52:46 +00:00
|
|
|
draw_text_stroked(canvas, p, font, 10);
|
2014-05-22 17:41:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
namespace {
|
2014-03-12 20:31:24 +00:00
|
|
|
enum {
|
|
|
|
kBelowThreshold_TextSize = 255,
|
|
|
|
kAboveThreshold_TextSize = 257
|
|
|
|
};
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
2014-03-12 20:31:24 +00:00
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
|
2018-12-24 19:52:46 +00:00
|
|
|
if (true) { test_nulldev(canvas); }
|
2014-03-13 03:02:23 +00:00
|
|
|
|
2018-12-24 19:52:46 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2014-03-13 03:02:23 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize);
|
2018-12-24 19:52:46 +00:00
|
|
|
draw_text_set(canvas, paint, font);
|
|
|
|
|
|
|
|
canvas->translate(600, 0);
|
|
|
|
font.setSize(kAboveThreshold_TextSize);
|
|
|
|
draw_text_set(canvas, paint, font);
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2020-01-27 22:43:41 +00:00
|
|
|
|
2022-03-17 03:39:21 +00:00
|
|
|
DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 420) {
|
2020-01-27 22:43:41 +00:00
|
|
|
sk_sp<SkTypeface> ttf = MakeResourceAsTypeface("fonts/Stroking.ttf");
|
|
|
|
sk_sp<SkTypeface> otf = MakeResourceAsTypeface("fonts/Stroking.otf");
|
2022-03-17 03:39:21 +00:00
|
|
|
|
|
|
|
sk_sp<SkTypeface> overlap = []() -> sk_sp<SkTypeface>{
|
|
|
|
std::unique_ptr<SkStreamAsset> variableStream(GetResourceAsStream("fonts/Variable.ttf"));
|
|
|
|
if (!variableStream) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const SkFontArguments::VariationPosition::Coordinate position[] = {
|
|
|
|
{ SkSetFourByteTag('w','g','h','t'), 721.0f },
|
|
|
|
};
|
|
|
|
SkFontArguments params;
|
|
|
|
params.setVariationDesignPosition({position, SK_ARRAY_COUNT(position)});
|
|
|
|
return SkFontMgr::RefDefault()->makeFromStream(std::move(variableStream), params);
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (!ttf && !otf && !overlap) {
|
2020-01-27 22:43:41 +00:00
|
|
|
msg->append("No support for ttf or otf.");
|
|
|
|
return skiagm::DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(10);
|
|
|
|
p.setStrokeCap(SkPaint::kRound_Cap);
|
|
|
|
p.setStrokeJoin(SkPaint::kRound_Join);
|
|
|
|
p.setARGB(0xff, 0xbb, 0x00, 0x00);
|
|
|
|
|
|
|
|
if (ttf) {
|
|
|
|
/* Stroking.ttf is structured like:
|
|
|
|
nothing U+25CB ○ (nothing inside)
|
|
|
|
something U+25C9 ◉ (a tiny thing inside)
|
|
|
|
- off (point off / empty quad with implicit end) (before U+207B ⁻ / after U+208B ₋)
|
|
|
|
+ on (point on / empty line) (before U+207A ⁺ / after U+208A ₊)
|
|
|
|
0 off off (two implicit quads) (before U+2070 ⁰ / after U+2080 ₀)
|
|
|
|
1 off on (quad with implicit close around) (before U+00B9 ¹ / after U+2081 ₁)
|
|
|
|
2 on off (quad with implicit close) (before U+00B2 ² / after U+2082 ₂)
|
|
|
|
3 on on (empty line) (before U+00B3 ³ / after U+2083 ₃)
|
|
|
|
*/
|
|
|
|
SkFont font(ttf, 100);
|
|
|
|
canvas->drawString("○◉ ⁻₋⁺₊", 10, 100, font, p);
|
|
|
|
canvas->drawString("⁰₀¹₁²₂³₃", 10, 200, font, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (otf) {
|
|
|
|
/* Stroking.otf is structured like:
|
|
|
|
nothing U+25CB ○
|
|
|
|
something U+25C9 ◉
|
|
|
|
0 moveto, moveto (before U+2070 ⁰) (nothing there, FreeType ignores these)
|
|
|
|
1 moveto, empty line, moveto (before U+00B9 ¹) (degenerate lineto)
|
|
|
|
3 moveto, empty cubic, moveto (before U+00B3 ³) (degenerate cubicto)
|
|
|
|
f moveto, empty flex, moveto (before U+1DA0 ᶠ) (degenerate flex)
|
|
|
|
*/
|
|
|
|
SkFont font(otf, 100);
|
|
|
|
canvas->drawString("○◉ ⁰¹³ᶠ", 10, 300, font, p);
|
|
|
|
}
|
|
|
|
|
2022-03-17 03:39:21 +00:00
|
|
|
if (overlap) {
|
|
|
|
/* Variable.ttf is structured like:
|
|
|
|
U+74 t (glyf outline has overlap flag)
|
|
|
|
U+167 ŧ (glyf outline does not have overlap flag)
|
|
|
|
*/
|
|
|
|
SkFont font(overlap, 100);
|
|
|
|
p.setStrokeWidth(1);
|
|
|
|
canvas->drawString("tŧ", 10, 400, font, p);
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:43:41 +00:00
|
|
|
return skiagm::DrawResult::kOk;
|
|
|
|
}
|