• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
24 #include "include/ports/SkFontMgr_fontconfig.h"
25 #endif
26 
27 #if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
28 #include "include/ports/SkFontMgr_mac_ct.h"
29 #endif
30 
31 #include <cstdio>
32 
main(int argc,char ** argv)33 int main(int argc, char** argv) {
34     if (argc != 2) {
35         printf("Usage: %s <name.pdf>", argv[0]);
36         return 1;
37     }
38     SkFILEWStream output(argv[1]);
39     if (!output.isValid()) {
40         printf("Cannot open output file %s\n", argv[1]);
41         return 1;
42     }
43 
44     SkPDF::Metadata metadata;
45     metadata.fTitle = "Test PDF";
46     metadata.fAuthor = "Skia Demo Writer";
47     metadata.fLang = "eng";
48     metadata.fEncodingQuality = 90;
49 
50     sk_sp<SkDocument> pdf = SkPDF::MakeDocument(&output, metadata);
51     SkCanvas* canvas = pdf->beginPage(100, 50);
52 
53 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
54     sk_sp<SkFontMgr> mgr = SkFontMgr_New_FontConfig(nullptr);
55 #endif
56 #if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
57     sk_sp<SkFontMgr> mgr = SkFontMgr_New_CoreText(nullptr);
58 #endif
59 
60     sk_sp<SkTypeface> face = mgr->matchFamilyStyle("Roboto", SkFontStyle());
61     if (!face) {
62       printf("Cannot open typeface\n");
63       return 1;
64     }
65     SkFont font(face, 14);
66     SkPaint paint;
67     paint.setColor(SK_ColorGREEN);
68     canvas->clear(SK_ColorYELLOW);
69     canvas->drawString("Hello world!", 10, 25, font, paint);
70 
71     pdf->endPage();
72     pdf->close();
73     return 0;
74 }
75