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