• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
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 "SkCanvas.h"
9 #include "SkCommandLineFlags.h"
10 #include "SkData.h"
11 #include "SkDocument.h"
12 #include "SkGraphics.h"
13 #include "SkSurface.h"
14 #include "SkImage.h"
15 #include "SkStream.h"
16 #include "SkString.h"
17 
18 DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");
19 DEFINE_string2(text, t, "Hello", "The string to write.");
20 
doDraw(SkCanvas * canvas,const SkPaint & paint,const char text[])21 static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
22     SkRect bounds = canvas->getLocalClipBounds();
23 
24     canvas->drawColor(SK_ColorWHITE);
25     canvas->drawText(text, strlen(text),
26                      bounds.centerX(), bounds.centerY(),
27                      paint);
28 }
29 
do_surface(int w,int h,const char path[],const char text[],const SkPaint & paint)30 static bool do_surface(int w, int h, const char path[], const char text[],
31                        const SkPaint& paint) {
32     SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
33     sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h, &props));
34     doDraw(surface->getCanvas(), paint, text);
35 
36     sk_sp<SkImage> image(surface->makeImageSnapshot());
37     sk_sp<SkData> data(image->encode());
38     if (!data) {
39         return false;
40     }
41     SkFILEWStream stream(path);
42     return stream.write(data->data(), data->size());
43 }
44 
do_document(int w,int h,const char path[],const char text[],const SkPaint & paint)45 static bool do_document(int w, int h, const char path[], const char text[],
46                         const SkPaint& paint) {
47     auto doc = SkPDF::MakeDocument(path);
48     if (doc.get()) {
49         SkScalar width = SkIntToScalar(w);
50         SkScalar height = SkIntToScalar(h);
51         doDraw(doc->beginPage(width, height, nullptr), paint, text);
52         return true;
53     }
54     return false;
55 }
56 
main(int argc,char ** argv)57 int main(int argc, char** argv) {
58     SkCommandLineFlags::SetUsage("");
59     SkCommandLineFlags::Parse(argc, argv);
60 
61     SkAutoGraphics ag;
62     SkString path("skhello");
63     SkString text("Hello");
64 
65     if (!FLAGS_outFile.isEmpty()) {
66         path.set(FLAGS_outFile[0]);
67     }
68     if (!FLAGS_text.isEmpty()) {
69         text.set(FLAGS_text[0]);
70     }
71 
72     SkPaint paint;
73     paint.setAntiAlias(true);
74     paint.setTextSize(SkIntToScalar(30));
75     paint.setTextAlign(SkPaint::kCenter_Align);
76 
77     SkScalar width = paint.measureText(text.c_str(), text.size());
78     SkScalar spacing = paint.getFontSpacing();
79 
80     int w = SkScalarRoundToInt(width) + 30;
81     int h = SkScalarRoundToInt(spacing) + 30;
82 
83     static const struct {
84         bool (*fProc)(int w, int h, const char path[], const char text[],
85                       const SkPaint&);
86         const char* fSuffix;
87     } gRec[] = {
88         { do_surface, ".png" },
89         { do_document, ".pdf" },
90     };
91 
92     for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
93         SkString file;
94         file.printf("%s%s", path.c_str(), gRec[i].fSuffix);
95         if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) {
96             return -1;
97         }
98     }
99     return 0;
100 }
101