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
18DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");
19DEFINE_string2(text, t, "Hello", "The string to write.");
20
21static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
22    SkRect bounds;
23    canvas->getClipBounds(&bounds);
24
25    canvas->drawColor(SK_ColorWHITE);
26    canvas->drawText(text, strlen(text),
27                     bounds.centerX(), bounds.centerY(),
28                     paint);
29}
30
31static bool do_surface(int w, int h, const char path[], const char text[],
32                       const SkPaint& paint) {
33    SkImage::Info info = {
34        w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType
35    };
36    SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
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
48static 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
60int tool_main(int argc, char** argv);
61int 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 = SkScalarRound(width) + 30;
85    int h = SkScalarRound(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
107int main(int argc, char * const argv[]) {
108    return tool_main(argc, (char**) argv);
109}
110#endif
111