Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
shape_text.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 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
11#include "include/core/SkFont.h"
29
30#include <cstdio>
31#include <cstdlib>
32#include <memory>
33
34// https://www.gutenberg.org/ebooks/72339
35constexpr const char* story =
36 "The landing port at Titan had not changed much in five years.\n"
37 "The ship settled down on the scarred blast shield, beside the same trio "
38 "of squat square buildings, and quickly disgorged its scanty quota of "
39 "cargo and a lone passenger into the flexible tube that linked the loading "
40 "hatch with the main building.\n"
41 "As soon as the tube was disconnected, the ship screamed off through the "
42 "murky atmosphere, seemingly glad to get away from Titan and head back to "
43 "the more comfortable and settled parts of the Solar System.";
44
46 public:
47 explicit OneFontStyleSet(sk_sp<SkTypeface> face) : face_(face) {}
48
49 protected:
50 int count() override { return 1; }
51 void getStyle(int, SkFontStyle* out_style, SkString*) override {
52 *out_style = SkFontStyle();
53 }
54 sk_sp<SkTypeface> createTypeface(int index) override { return face_; }
55 sk_sp<SkTypeface> matchStyle(const SkFontStyle&) override { return face_; }
56
57 private:
59};
60
61class OneFontMgr : public SkFontMgr {
62 public:
64 : face_(face), style_set_(sk_make_sp<OneFontStyleSet>(face)) {}
65
66 protected:
67 int onCountFamilies() const override { return 1; }
68 void onGetFamilyName(int index, SkString* familyName) const override {
69 *familyName = SkString("the-only-font-I-have");
70 }
71 sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override {
72 return style_set_;
73 }
74 sk_sp<SkFontStyleSet> onMatchFamily(const char[]) const override {
75 return style_set_;
76 }
77
79 const SkFontStyle&) const override {
80 return face_;
81 }
83 const char familyName[], const SkFontStyle& style, const char* bcp47[],
84 int bcp47Count, SkUnichar character) const override {
85 return face_;
86 }
88 SkFontStyle) const override {
89 return face_;
90 }
91
93 std::abort();
94 return nullptr;
95 }
96 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
97 int) const override {
98 std::abort();
99 return nullptr;
100 }
102 std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override {
103 std::abort();
104 return nullptr;
105 }
106 sk_sp<SkTypeface> onMakeFromFile(const char[], int) const override {
107 std::abort();
108 return nullptr;
109 }
110
111 private:
112 sk_sp<SkTypeface> face_;
113 sk_sp<SkFontStyleSet> style_set_;
114};
115
116int main(int argc, char** argv) {
117 if (argc != 3) {
118 printf("Usage: %s <font.ttf> <name.png>", argv[0]);
119 return 1;
120 }
121
122 SkFILEStream input(argv[1]);
123 if (!input.isValid()) {
124 printf("Cannot open input file %s\n", argv[1]);
125 return 1;
126 }
127 sk_sp<SkData> font_data = SkData::MakeFromStream(&input, input.getLength());
129 sk_sp<SkTypeface> face = mgr->makeFromData(font_data);
130 if (!face) {
131 printf("input font %s was not parsable by Freetype\n", argv[1]);
132 return 1;
133 }
134
135 SkFILEWStream output(argv[2]);
136 if (!output.isValid()) {
137 printf("Cannot open output file %s\n", argv[2]);
138 return 1;
139 }
140
141 auto fontCollection = sk_make_sp<skia::textlayout::FontCollection>();
142 sk_sp<SkFontMgr> one_mgr = sk_make_sp<OneFontMgr>(face);
143 fontCollection->setDefaultFontManager(one_mgr);
144
145 constexpr int width = 200;
148 SkCanvas* canvas = surface->getCanvas();
149 canvas->clear(SK_ColorWHITE);
150
152 paint.setAntiAlias(true);
153 paint.setColor(SK_ColorBLACK);
154
157 style.setFontFamilies({SkString("sans-serif")});
158 style.setFontSize(10.5);
160 paraStyle.setTextStyle(style);
162
164 if (!unicode) {
165 printf("Could not load unicode data\n");
166 return 1;
167 }
169 std::unique_ptr<ParagraphBuilder> builder =
170 ParagraphBuilder::make(paraStyle, fontCollection, unicode);
171 builder->addText(story);
172
173 std::unique_ptr<skia::textlayout::Paragraph> paragraph = builder->Build();
174 paragraph->layout(width - 20);
175 paragraph->paint(canvas, 10, 10);
176
177 SkPixmap pixmap;
178 if (surface->peekPixels(&pixmap)) {
179 if (!SkJpegEncoder::Encode(&output, pixmap, {})) {
180 printf("Cannot write output\n");
181 return 1;
182 }
183 } else {
184 printf("Cannot readback on surface\n");
185 return 1;
186 }
187
188 return 0;
189}
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
SK_API sk_sp< SkFontMgr > SkFontMgr_New_Custom_Empty()
sk_sp< T > sk_make_sp(Args &&... args)
Definition SkRefCnt.h:371
int32_t SkUnichar
Definition SkTypes.h:175
sk_sp< SkTypeface > onMakeFromData(sk_sp< SkData >, int) const override
void onGetFamilyName(int index, SkString *familyName) const override
OneFontMgr(sk_sp< SkTypeface > face)
sk_sp< SkTypeface > onLegacyMakeTypeface(const char[], SkFontStyle) const override
sk_sp< SkFontStyleSet > onCreateStyleSet(int index) const override
sk_sp< SkFontStyleSet > onMatchFamily(const char[]) const override
sk_sp< SkTypeface > onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle &style, const char *bcp47[], int bcp47Count, SkUnichar character) const override
sk_sp< SkTypeface > onMatchFamilyStyle(const char[], const SkFontStyle &) const override
int onCountFamilies() const override
sk_sp< SkTypeface > onMakeFromStreamIndex(std::unique_ptr< SkStreamAsset >, int) const override
sk_sp< SkTypeface > onMakeFromFile(const char[], int) const override
sk_sp< SkTypeface > onMakeFromStreamArgs(std::unique_ptr< SkStreamAsset >, const SkFontArguments &) const override
OneFontStyleSet(sk_sp< SkTypeface > face)
sk_sp< SkTypeface > matchStyle(const SkFontStyle &) override
int count() override
sk_sp< SkTypeface > createTypeface(int index) override
void getStyle(int, SkFontStyle *out_style, SkString *) override
void clear(SkColor color)
Definition SkCanvas.h:1199
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Definition SkData.cpp:208
bool isValid() const
Definition SkStream.h:320
size_t getLength() const override
Definition SkStream.cpp:270
void setFontFamilies(std::vector< SkString > families)
Definition TextStyle.h:253
void setFontSize(SkScalar size)
Definition TextStyle.h:250
void setForegroundColor(SkPaint paint)
Definition TextStyle.h:181
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
char ** argv
Definition library.h:9
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SKUNICODE_API sk_sp< SkUnicode > Make()
Definition main.py:1
int32_t width
constexpr const char * story
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
void setTextStyle(const TextStyle &textStyle)
void setTextAlign(TextAlign align)