Debian Codegraph @

example/external_client/src/write_to_pdf.cpp

raw
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/SkDocument.h"
9 #include "include/core/SkAlphaType.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkTypeface.h"
15 #include "include/core/SkFont.h"
16 #include "include/core/SkFontStyle.h"
17 #include "include/core/SkFontMgr.h"
18 #include "include/core/SkPixmap.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkStream.h"
21 #include "include/docs/SkPDFDocument.h"
22 #include "include/docs/SkPDFJpegHelpers.h"
23
24 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) && defined(SK_TYPEFACE_FACTORY_FREETYPE)
25 #include "include/ports/SkFontMgr_fontconfig.h"
26 #include "include/ports/SkFontScanner_FreeType.h"
27 #endif
28
29 #if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
30 #include "include/ports/SkFontMgr_mac_ct.h"
31 #endif
32
33 #include <cstdio>
34
35 int main(int argc, char** argv) {
36 if (argc != 2) {
37 printf("Usage: %s <name.pdf>", argv[0]);
38 return 1;
39 }
40 SkFILEWStream output(argv[1]);
41 if (!output.isValid()) {
42 printf("Cannot open output file %s\n", argv[1]);
43 return 1;
44 }
45
46 SkPDF::Metadata metadata;
47 metadata.fTitle = "Test PDF";
48 metadata.fAuthor = "Skia Demo Writer";
49 metadata.fLang = "eng";
50 metadata.fEncodingQuality = 90;
51 metadata.jpegDecoder = SkPDF::JPEG::Decode;
52 metadata.jpegEncoder = SkPDF::JPEG::Encode;
53
54 sk_sp<SkDocument> pdf = SkPDF::MakeDocument(&output, metadata);
55 SkCanvas* canvas = pdf->beginPage(100, 50);
56
57 sk_sp<SkFontMgr> mgr;
58 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) && defined(SK_TYPEFACE_FACTORY_FREETYPE)
59 mgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType());
60 #elif defined(SK_FONTMGR_CORETEXT_AVAILABLE)
61 mgr = SkFontMgr_New_CoreText(nullptr);
62 #endif
63 if (!mgr) {
64 printf("No Font Manager configured\n");
65 return 1;
66 }
67 sk_sp<SkTypeface> face = mgr->matchFamilyStyle("Roboto", SkFontStyle());
68 if (!face) {
69 printf("Cannot open typeface\n");
70 return 1;
71 }
72 SkFont font(face, 14);
73 SkPaint paint;
74 paint.setColor(SK_ColorGREEN);
75 canvas->clear(SK_ColorYELLOW);
76 canvas->drawString("Hello world!", 10, 25, font, paint);
77
78 pdf->endPage();
79 pdf->close();
80 return 0;
81 }