Flutter Engine
The Flutter Engine
third_party
skia
example
external_client
src
write_text_to_png.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
8
#include "
include/core/SkAlphaType.h
"
9
#include "
include/core/SkCanvas.h
"
10
#include "
include/core/SkColor.h
"
11
#include "
include/core/SkImageInfo.h
"
12
#include "
include/core/SkRefCnt.h
"
13
#include "
include/core/SkTypeface.h
"
14
#include "
include/core/SkFont.h
"
15
#include "
include/core/SkFontStyle.h
"
16
#include "
include/core/SkFontMgr.h
"
17
#include "
include/core/SkPixmap.h
"
18
#include "
include/core/SkSurface.h
"
19
#include "
include/core/SkStream.h
"
20
#include "
include/encode/SkPngEncoder.h
"
21
22
#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
23
#include "
include/ports/SkFontMgr_fontconfig.h
"
24
#endif
25
26
#if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
27
#include "
include/ports/SkFontMgr_mac_ct.h
"
28
#endif
29
30
#include <cstdio>
31
32
int
main
(
int
argc,
char
**
argv
) {
33
if
(argc != 2) {
34
printf
(
"Usage: %s <name.png>"
,
argv
[0]);
35
return
1;
36
}
37
SkFILEWStream
output
(
argv
[1]);
38
if
(!
output
.isValid()) {
39
printf
(
"Cannot open output file %s\n"
,
argv
[1]);
40
return
1;
41
}
42
sk_sp<SkSurface>
surface
=
SkSurfaces::Raster
(
SkImageInfo::MakeN32
(100, 50,
kOpaque_SkAlphaType
));
43
SkCanvas
* canvas =
surface
->getCanvas();
44
#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
45
sk_sp<SkFontMgr>
mgr =
SkFontMgr_New_FontConfig
(
nullptr
);
46
#endif
47
#if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
48
sk_sp<SkFontMgr>
mgr =
SkFontMgr_New_CoreText
(
nullptr
);
49
#endif
50
51
sk_sp<SkTypeface>
face = mgr->
matchFamilyStyle
(
"Roboto"
,
SkFontStyle
());
52
if
(!face) {
53
printf
(
"Cannot open typeface\n"
);
54
return
1;
55
}
56
SkFont
font
(face, 14);
57
SkPaint
paint
;
58
paint
.setColor(
SK_ColorGREEN
);
59
canvas->
clear
(
SK_ColorYELLOW
);
60
canvas->
drawString
(
"Hello world!"
, 10, 25,
font
,
paint
);
61
SkPixmap
pixmap;
62
if
(
surface
->peekPixels(&pixmap)) {
63
if
(!
SkPngEncoder::Encode
(&
output
, pixmap, {})) {
64
printf
(
"Cannot write output\n"
);
65
return
1;
66
}
67
}
else
{
68
printf
(
"Cannot readback on surface\n"
);
69
return
1;
70
}
71
return
0;
72
}
SkAlphaType.h
kOpaque_SkAlphaType
@ kOpaque_SkAlphaType
pixel is opaque
Definition:
SkAlphaType.h:28
SkCanvas.h
SkColor.h
SK_ColorYELLOW
constexpr SkColor SK_ColorYELLOW
Definition:
SkColor.h:139
SK_ColorGREEN
constexpr SkColor SK_ColorGREEN
Definition:
SkColor.h:131
SkFontMgr.h
SkFontMgr_fontconfig.h
SkFontMgr_New_FontConfig
SK_API sk_sp< SkFontMgr > SkFontMgr_New_FontConfig(FcConfig *fc)
Definition:
SkFontMgr_fontconfig.cpp:970
SkFontMgr_mac_ct.h
SkFontMgr_New_CoreText
SK_API sk_sp< SkFontMgr > SkFontMgr_New_CoreText(CTFontCollectionRef)
SkFontStyle.h
SkFont.h
SkImageInfo.h
SkPixmap.h
SkPngEncoder.h
SkRefCnt.h
SkStream.h
SkSurface.h
SkTypeface.h
SkCanvas
Definition:
SkCanvas.h:106
SkCanvas::clear
void clear(SkColor color)
Definition:
SkCanvas.h:1199
SkCanvas::drawString
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition:
SkCanvas.h:1803
SkFILEWStream
Definition:
SkStream.h:435
SkFontMgr::matchFamilyStyle
sk_sp< SkTypeface > matchFamilyStyle(const char familyName[], const SkFontStyle &) const
Definition:
SkFontMgr.cpp:109
SkFontStyle
Definition:
SkFontStyle.h:16
SkFont
Definition:
SkFont.h:35
SkPaint
Definition:
SkPaint.h:44
SkPixmap
Definition:
SkPixmap.h:40
sk_sp< SkSurface >
paint
const Paint & paint
Definition:
color_source.cc:38
surface
VkSurfaceKHR surface
Definition:
main.cc:49
argv
char ** argv
Definition:
library.h:9
SkPngEncoder::Encode
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
Definition:
SkPngEncoder_none.cpp:20
SkSL::String::printf
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
Definition:
SkSLString.cpp:83
SkSurfaces::Raster
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
Definition:
SkSurface_Raster.cpp:186
dart_profiler_symbols.output
output
Definition:
dart_profiler_symbols.py:51
gen_test_font.font
font
Font Metadata and Metrics.
Definition:
gen_test_font.py:26
SkImageInfo::MakeN32
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
Definition:
SkImageInfo.cpp:145
main
int main(int argc, char **argv)
Definition:
write_text_to_png.cpp:32
Generated on Sun Jun 23 2024 21:55:56 for Flutter Engine by
1.9.4