skia2/gm/getpostextpath.cpp
Mike Reed 38810f3a09 use font for measuring/paths
add get_text_path to sk_tool_utils

Bug: skia:
Change-Id: I45426bc018cd8a5a0309fd6a73adb30755e8155a
Reviewed-on: https://skia-review.googlesource.com/c/179847
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
2018-12-21 17:36:02 +00:00

66 lines
1.9 KiB
C++

/*
* Copyright 2012 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 "sk_tool_utils.h"
#include "SkCanvas.h"
#include "SkFontPriv.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkRandom.h"
#include "SkTemplates.h"
#include "SkTextBlob.h"
static void strokePath(SkCanvas* canvas, const SkPath& path) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(SK_ColorRED);
paint.setStyle(SkPaint::kStroke_Style);
canvas->drawPath(path, paint);
}
DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
// explicitly add spaces, to test a prev. bug
const char* text = "Ham bur ge fons";
size_t len = strlen(text);
SkPath path;
SkFont font;
font.setTypeface(sk_tool_utils::create_portable_typeface());
font.setSize(48);
SkPaint paint;
paint.setAntiAlias(true);
canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
canvas->drawSimpleText(text, len, kUTF8_SkTextEncoding, 0, 0, font, paint);
sk_tool_utils::get_text_path(font, text, len, kUTF8_SkTextEncoding, &path, nullptr);
strokePath(canvas, path);
path.reset();
SkAutoToGlyphs atg(font, text, len, kUTF8_SkTextEncoding);
const int count = atg.count();
SkAutoTArray<SkPoint> pos(count);
SkAutoTArray<SkScalar> widths(count);
font.getWidths(atg.glyphs(), count, &widths[0]);
SkRandom rand;
SkScalar x = SkIntToScalar(20);
SkScalar y = SkIntToScalar(100);
for (int i = 0; i < count; ++i) {
pos[i].set(x, y + rand.nextSScalar1() * 24);
x += widths[i];
}
canvas->translate(0, SkIntToScalar(64));
canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
sk_tool_utils::get_text_path(font, text, len, kUTF8_SkTextEncoding, &path, &pos[0]);
strokePath(canvas, path);
}