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
main(int argc,char ** argv)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 }
73