blob: 4da808e0ff8332717c4c873a91e49574960f7355 [file] [log] [blame]
Mike Reedb0210d22020-05-07 16:58:40 -04001/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
Kevin Lubickbca43ec2023-10-30 10:11:22 -040011#include "include/core/SkFontMgr.h"
Mike Reedb0210d22020-05-07 16:58:40 -040012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Florin Malita2b07cdb2022-07-26 11:03:23 -040014#include "include/core/SkPictureRecorder.h"
Mike Reedb0210d22020-05-07 16:58:40 -040015#include "include/core/SkSize.h"
Kevin Lubickefce17d2022-03-09 23:11:31 -050016#include "include/core/SkStream.h"
Mike Reedb0210d22020-05-07 16:58:40 -040017#include "include/core/SkString.h"
18#include "include/utils/SkCustomTypeface.h"
Kevin Lubicke5c37862023-10-16 17:21:15 -040019#include "src/core/SkFontPriv.h"
Mike Reedb0210d22020-05-07 16:58:40 -040020#include "tools/Resources.h"
Kevin Lubickbca43ec2023-10-30 10:11:22 -040021#include "tools/fonts/FontToolUtils.h"
Mike Reedb0210d22020-05-07 16:58:40 -040022
Florin Malita2b07cdb2022-07-26 11:03:23 -040023static sk_sp<SkDrawable> make_drawable(const SkPath& path) {
24 const auto bounds = path.computeTightBounds();
25
26 SkPictureRecorder recorder;
27 auto* canvas = recorder.beginRecording(bounds);
28
29 SkPaint paint;
30 paint.setColor(0xff008000);
31 paint.setAntiAlias(true);
32
33 canvas->drawPath(path, paint);
34
35 return recorder.finishRecordingAsDrawable();
36}
37
Mike Reedb0210d22020-05-07 16:58:40 -040038static sk_sp<SkTypeface> make_tf() {
Florin Malita340cd9c2020-05-14 16:21:53 -040039 SkCustomTypefaceBuilder builder;
Kevin Lubickbca43ec2023-10-30 10:11:22 -040040 SkFont font = ToolUtils::DefaultFont();
41 SkASSERT(font.getTypeface());
42 const float upem = font.getTypeface()->getUnitsPerEm();
Mike Reedd240d762020-05-19 12:50:48 -040043
44 // request a big size, to improve precision at the fontscaler level
45 font.setSize(upem);
Mike Reedb0210d22020-05-07 16:58:40 -040046 font.setHinting(SkFontHinting::kNone);
47
Mike Reedd240d762020-05-19 12:50:48 -040048 // so we can scale our paths back down to 1-point
49 const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem);
50
Mike Reed98bc22c2020-05-18 16:17:05 -040051 {
52 SkFontMetrics metrics;
53 font.getMetrics(&metrics);
Mike Reedd240d762020-05-19 12:50:48 -040054 builder.setMetrics(metrics, 1.0f/upem);
Mike Reed98bc22c2020-05-18 16:17:05 -040055 }
Kevin Lubickbca43ec2023-10-30 10:11:22 -040056 builder.setFontStyle(font.getTypeface()->fontStyle());
Mike Reed98bc22c2020-05-18 16:17:05 -040057
Mike Reedb0210d22020-05-07 16:58:40 -040058 // Steal the first 128 chars from the default font
59 for (SkGlyphID index = 0; index <= 127; ++index) {
60 SkGlyphID glyph = font.unicharToGlyph(index);
61
62 SkScalar width;
63 font.getWidths(&glyph, 1, &width);
64 SkPath path;
65 font.getPath(glyph, &path);
Florin Malita2b07cdb2022-07-26 11:03:23 -040066 path.transform(scale);
Mike Reedb0210d22020-05-07 16:58:40 -040067
68 // we use the charcode to be our glyph index, since we have no cmap table
Florin Malita2b07cdb2022-07-26 11:03:23 -040069 if (index % 2) {
70 builder.setGlyph(index, width/upem, make_drawable(path), path.computeTightBounds());
71 } else {
72 builder.setGlyph(index, width/upem, path);
73 }
Mike Reedb0210d22020-05-07 16:58:40 -040074 }
75
76 return builder.detach();
77}
78
79#include "include/core/SkTextBlob.h"
80
Mike Reed98bc22c2020-05-18 16:17:05 -040081static sk_sp<SkTypeface> round_trip(sk_sp<SkTypeface> tf) {
82 auto data = tf->serialize();
83 SkMemoryStream stream(data->data(), data->size());
Kevin Lubickbca43ec2023-10-30 10:11:22 -040084 sk_sp<SkTypeface> face = SkTypeface::MakeDeserialize(&stream, nullptr);
85 SkASSERT(face);
86 return face;
Mike Reed98bc22c2020-05-18 16:17:05 -040087}
88
Mike Reedb0210d22020-05-07 16:58:40 -040089class UserFontGM : public skiagm::GM {
90 sk_sp<SkTypeface> fTF;
Mike Reedb0210d22020-05-07 16:58:40 -040091
Mike Reedb0210d22020-05-07 16:58:40 -040092public:
93 UserFontGM() {}
94
95 void onOnceBeforeDraw() override {
96 fTF = make_tf();
Mike Reed98bc22c2020-05-18 16:17:05 -040097 // test serialization
98 fTF = round_trip(fTF);
Mike Reed2ddf7f32020-05-18 14:06:38 -040099 }
Mike Reedb0210d22020-05-07 16:58:40 -0400100
Mike Reed98bc22c2020-05-18 16:17:05 -0400101 static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size, float* spacing) {
Mike Reed2ddf7f32020-05-18 14:06:38 -0400102 SkFont font(tf);
103 font.setSize(size);
Mike Reedb0210d22020-05-07 16:58:40 -0400104 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed98bc22c2020-05-18 16:17:05 -0400105 *spacing = font.getMetrics(nullptr);
Mike Reed2ddf7f32020-05-18 14:06:38 -0400106 return SkTextBlob::MakeFromString("Typeface", font);
Mike Reedb0210d22020-05-07 16:58:40 -0400107 }
108
109 bool runAsBench() const override { return true; }
110
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000111 SkString getName() const override { return SkString("user_typeface"); }
Mike Reedb0210d22020-05-07 16:58:40 -0400112
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000113 SkISize getISize() override { return {810, 452}; }
Mike Reedb0210d22020-05-07 16:58:40 -0400114
115 void onDraw(SkCanvas* canvas) override {
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400116 auto waterfall = [&](sk_sp<SkTypeface> tf, bool defaultFace) {
Mike Reed2ddf7f32020-05-18 14:06:38 -0400117 SkPaint paint;
118 paint.setAntiAlias(true);
Mike Reedb0210d22020-05-07 16:58:40 -0400119
Mike Reed98bc22c2020-05-18 16:17:05 -0400120 float spacing;
Mike Reed2ddf7f32020-05-18 14:06:38 -0400121 float x = 20,
122 y = 16;
123 for (float size = 9; size <= 100; size *= 1.25f) {
Mike Reed98bc22c2020-05-18 16:17:05 -0400124 auto blob = make_blob(tf, size, &spacing);
125
126 // shared baseline
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400127 if (defaultFace) {
Mike Reed98bc22c2020-05-18 16:17:05 -0400128 paint.setColor(0xFFDDDDDD);
129 canvas->drawRect({0, y, 810, y+1}, paint);
130 }
Mike Reedb0210d22020-05-07 16:58:40 -0400131
Mike Reed2ddf7f32020-05-18 14:06:38 -0400132 paint.setColor(0xFFCCCCCC);
133 paint.setStyle(SkPaint::kStroke_Style);
134 canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
135
136 paint.setStyle(SkPaint::kFill_Style);
137 paint.setColor(SK_ColorBLACK);
138 canvas->drawTextBlob(blob, x, y, paint);
139
Mike Reed98bc22c2020-05-18 16:17:05 -0400140 y += SkScalarRoundToInt(spacing * 1.25f + 2);
Mike Reed2ddf7f32020-05-18 14:06:38 -0400141 }
142 };
143
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400144 waterfall(ToolUtils::DefaultTypeface(), true);
Mike Reed2ddf7f32020-05-18 14:06:38 -0400145 canvas->translate(400, 0);
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400146 waterfall(fTF, false);
Mike Reedb0210d22020-05-07 16:58:40 -0400147 }
148};
149DEF_GM(return new UserFontGM;)