Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions | Variables
shape_text.cpp File Reference
#include "include/core/SkAlphaType.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontMgr.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkStream.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/encode/SkJpegEncoder.h"
#include "include/ports/SkFontMgr_empty.h"
#include "modules/skparagraph/include/DartTypes.h"
#include "modules/skparagraph/include/FontCollection.h"
#include "modules/skparagraph/include/Paragraph.h"
#include "modules/skparagraph/include/ParagraphBuilder.h"
#include "modules/skparagraph/include/ParagraphStyle.h"
#include "modules/skunicode/include/SkUnicode_icu.h"
#include <cstdio>
#include <cstdlib>
#include <memory>

Go to the source code of this file.

Classes

class  OneFontStyleSet
 
class  OneFontMgr
 

Functions

int main (int argc, char **argv)
 

Variables

constexpr const char * story
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 116 of file shape_text.cpp.

116 {
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()
void clear(SkColor color)
Definition SkCanvas.h:1199
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Definition SkData.cpp:208
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
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SKUNICODE_API sk_sp< SkUnicode > Make()
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)

Variable Documentation

◆ story

constexpr const char* story
constexpr
Initial value:
=
"The landing port at Titan had not changed much in five years.\n"
"The ship settled down on the scarred blast shield, beside the same trio "
"of squat square buildings, and quickly disgorged its scanty quota of "
"cargo and a lone passenger into the flexible tube that linked the loading "
"hatch with the main building.\n"
"As soon as the tube was disconnected, the ship screamed off through the "
"murky atmosphere, seemingly glad to get away from Titan and head back to "
"the more comfortable and settled parts of the Solar System."

Definition at line 35 of file shape_text.cpp.