blob: 9f054342a4e719fa404ccfe86242d0100b23d77f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
tfarina@chromium.orge0e71af2012-09-25 20:16:46 +00007
reed@android.com6413e792010-04-12 14:27:37 +00008#include "SkCanvas.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00009#include "SkCommandLineFlags.h"
reed@google.com4b0757b2013-05-20 16:33:41 +000010#include "SkData.h"
reed@google.com99ac02b2013-06-07 20:30:16 +000011#include "SkDocument.h"
scroggo@google.comf78cdb42013-12-04 18:34:58 +000012#include "SkForceLinking.h"
reed@android.com6413e792010-04-12 14:27:37 +000013#include "SkGraphics.h"
reed@google.com4b0757b2013-05-20 16:33:41 +000014#include "SkSurface.h"
15#include "SkImage.h"
16#include "SkStream.h"
reed@android.com6413e792010-04-12 14:27:37 +000017#include "SkString.h"
reed@android.com6413e792010-04-12 14:27:37 +000018
scroggo@google.comf78cdb42013-12-04 18:34:58 +000019__SK_FORCE_IMAGE_DECODER_LINKING;
20
reed@google.com99ac02b2013-06-07 20:30:16 +000021DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000022DEFINE_string2(text, t, "Hello", "The string to write.");
reed@android.com6413e792010-04-12 14:27:37 +000023
reed@google.com99ac02b2013-06-07 20:30:16 +000024static 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) {
reed3054be12014-12-10 07:24:28 -080036 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(w, h));
reed@google.com99ac02b2013-06-07 20:30:16 +000037 doDraw(surface->getCanvas(), paint, text);
skia.committer@gmail.com63193672013-06-08 07:01:13 +000038
reed@google.com99ac02b2013-06-07 20:30:16 +000039 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
caryclark@google.com5987f582012-10-02 18:33:14 +000060int tool_main(int argc, char** argv);
61int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000062 SkCommandLineFlags::SetUsage("");
63 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000064
reed@android.com6413e792010-04-12 14:27:37 +000065 SkAutoGraphics ag;
reed@google.com99ac02b2013-06-07 20:30:16 +000066 SkString path("skhello");
reed@android.com6413e792010-04-12 14:27:37 +000067 SkString text("Hello");
68
scroggo@google.com604e0c22013-04-09 21:25:46 +000069 if (!FLAGS_outFile.isEmpty()) {
70 path.set(FLAGS_outFile[0]);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000071 }
scroggo@google.com604e0c22013-04-09 21:25:46 +000072 if (!FLAGS_text.isEmpty()) {
73 text.set(FLAGS_text[0]);
reed@android.com6413e792010-04-12 14:27:37 +000074 }
75
76 SkPaint paint;
77 paint.setAntiAlias(true);
78 paint.setTextSize(SkIntToScalar(30));
reed@google.com4b0757b2013-05-20 16:33:41 +000079 paint.setTextAlign(SkPaint::kCenter_Align);
80
reed@android.com6413e792010-04-12 14:27:37 +000081 SkScalar width = paint.measureText(text.c_str(), text.size());
82 SkScalar spacing = paint.getFontSpacing();
83
reed@google.come1ca7052013-12-17 19:22:07 +000084 int w = SkScalarRoundToInt(width) + 30;
85 int h = SkScalarRoundToInt(spacing) + 30;
reed@android.com6413e792010-04-12 14:27:37 +000086
reed@google.com99ac02b2013-06-07 20:30:16 +000087 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" },
reed@google.com4b0757b2013-05-20 16:33:41 +000094 };
skia.committer@gmail.com63193672013-06-08 07:01:13 +000095
reed@google.com99ac02b2013-06-07 20:30:16 +000096 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 }
scroggo@google.com7def5e12013-05-31 14:00:10 +0000102 }
reed@google.com99ac02b2013-06-07 20:30:16 +0000103 return 0;
reed@android.com6413e792010-04-12 14:27:37 +0000104}
caryclark@google.com5987f582012-10-02 18:33:14 +0000105
106#if !defined SK_BUILD_FOR_IOS
107int main(int argc, char * const argv[]) {
108 return tool_main(argc, (char**) argv);
109}
110#endif