2020-05-07 20:58:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/utils/SkCustomTypeface.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
|
|
|
|
static sk_sp<SkTypeface> make_tf() {
|
2020-05-14 20:21:53 +00:00
|
|
|
SkCustomTypefaceBuilder builder;
|
2020-05-07 20:58:40 +00:00
|
|
|
SkFont font;
|
2020-05-19 16:50:48 +00:00
|
|
|
const float upem = font.getTypefaceOrDefault()->getUnitsPerEm();
|
|
|
|
|
|
|
|
// request a big size, to improve precision at the fontscaler level
|
|
|
|
font.setSize(upem);
|
2020-05-07 20:58:40 +00:00
|
|
|
font.setHinting(SkFontHinting::kNone);
|
|
|
|
|
2020-05-19 16:50:48 +00:00
|
|
|
// so we can scale our paths back down to 1-point
|
|
|
|
const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem);
|
|
|
|
|
2020-05-18 20:17:05 +00:00
|
|
|
{
|
|
|
|
SkFontMetrics metrics;
|
|
|
|
font.getMetrics(&metrics);
|
2020-05-19 16:50:48 +00:00
|
|
|
builder.setMetrics(metrics, 1.0f/upem);
|
2020-05-18 20:17:05 +00:00
|
|
|
}
|
2020-08-18 17:17:09 +00:00
|
|
|
builder.setFontStyle(font.getTypefaceOrDefault()->fontStyle());
|
2020-05-18 20:17:05 +00:00
|
|
|
|
2020-05-07 20:58:40 +00:00
|
|
|
// Steal the first 128 chars from the default font
|
|
|
|
for (SkGlyphID index = 0; index <= 127; ++index) {
|
|
|
|
SkGlyphID glyph = font.unicharToGlyph(index);
|
|
|
|
|
|
|
|
SkScalar width;
|
|
|
|
font.getWidths(&glyph, 1, &width);
|
|
|
|
SkPath path;
|
|
|
|
font.getPath(glyph, &path);
|
|
|
|
|
|
|
|
// we use the charcode to be our glyph index, since we have no cmap table
|
2020-05-19 16:50:48 +00:00
|
|
|
builder.setGlyph(index, width/upem, path.makeTransform(scale));
|
2020-05-07 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder.detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
|
2020-05-18 20:17:05 +00:00
|
|
|
static sk_sp<SkTypeface> round_trip(sk_sp<SkTypeface> tf) {
|
|
|
|
auto data = tf->serialize();
|
|
|
|
SkMemoryStream stream(data->data(), data->size());
|
|
|
|
return SkTypeface::MakeDeserialize(&stream);
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:58:40 +00:00
|
|
|
class UserFontGM : public skiagm::GM {
|
|
|
|
sk_sp<SkTypeface> fTF;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UserFontGM() {}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fTF = make_tf();
|
2020-05-18 20:17:05 +00:00
|
|
|
// test serialization
|
|
|
|
fTF = round_trip(fTF);
|
2020-05-18 18:06:38 +00:00
|
|
|
}
|
2020-05-07 20:58:40 +00:00
|
|
|
|
2020-05-18 20:17:05 +00:00
|
|
|
static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size, float* spacing) {
|
2020-05-18 18:06:38 +00:00
|
|
|
SkFont font(tf);
|
|
|
|
font.setSize(size);
|
2020-05-07 20:58:40 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2020-05-18 20:17:05 +00:00
|
|
|
*spacing = font.getMetrics(nullptr);
|
2020-05-18 18:06:38 +00:00
|
|
|
return SkTextBlob::MakeFromString("Typeface", font);
|
2020-05-07 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool runAsBench() const override { return true; }
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("user_typeface"); }
|
|
|
|
|
2020-05-18 20:17:05 +00:00
|
|
|
SkISize onISize() override { return {810, 452}; }
|
2020-05-07 20:58:40 +00:00
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2020-05-18 18:06:38 +00:00
|
|
|
auto waterfall = [&](sk_sp<SkTypeface> tf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2020-05-18 20:17:05 +00:00
|
|
|
float spacing;
|
2020-05-18 18:06:38 +00:00
|
|
|
float x = 20,
|
|
|
|
y = 16;
|
|
|
|
for (float size = 9; size <= 100; size *= 1.25f) {
|
2020-05-18 20:17:05 +00:00
|
|
|
auto blob = make_blob(tf, size, &spacing);
|
|
|
|
|
|
|
|
// shared baseline
|
|
|
|
if (tf == nullptr) {
|
|
|
|
paint.setColor(0xFFDDDDDD);
|
|
|
|
canvas->drawRect({0, y, 810, y+1}, paint);
|
|
|
|
}
|
2020-05-18 18:06:38 +00:00
|
|
|
|
|
|
|
paint.setColor(0xFFCCCCCC);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawTextBlob(blob, x, y, paint);
|
|
|
|
|
2020-05-18 20:17:05 +00:00
|
|
|
y += SkScalarRoundToInt(spacing * 1.25f + 2);
|
2020-05-18 18:06:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
waterfall(nullptr);
|
|
|
|
canvas->translate(400, 0);
|
|
|
|
waterfall(fTF);
|
2020-05-07 20:58:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM(return new UserFontGM;)
|