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