blob: d1f8cb8192bd5997432d90f74bcb8bf5bb968fab [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"
reed@android.com6413e792010-04-12 14:27:37 +000012#include "SkGraphics.h"
reed@google.com4b0757b2013-05-20 16:33:41 +000013#include "SkSurface.h"
14#include "SkImage.h"
15#include "SkStream.h"
reed@android.com6413e792010-04-12 14:27:37 +000016#include "SkString.h"
reed@android.com6413e792010-04-12 14:27:37 +000017
reed@google.com99ac02b2013-06-07 20:30:16 +000018DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000019DEFINE_string2(text, t, "Hello", "The string to write.");
reed@android.com6413e792010-04-12 14:27:37 +000020
reed@google.com99ac02b2013-06-07 20:30:16 +000021static 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) {
robertphillips702edbd2015-06-23 06:26:08 -070033 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -070034 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h, &props));
reed@google.com99ac02b2013-06-07 20:30:16 +000035 doDraw(surface->getCanvas(), paint, text);
skia.committer@gmail.com63193672013-06-08 07:01:13 +000036
reed9ce9d672016-03-17 10:51:11 -070037 sk_sp<SkImage> image(surface->makeImageSnapshot());
bungeman38d909e2016-08-02 14:40:46 -070038 sk_sp<SkData> data(image->encode());
39 if (!data) {
reed@google.com99ac02b2013-06-07 20:30:16 +000040 return false;
41 }
42 SkFILEWStream stream(path);
43 return stream.write(data->data(), data->size());
44}
45
46static bool do_document(int w, int h, const char path[], const char text[],
47 const SkPaint& paint) {
halcanary676ab682016-05-03 12:10:04 -070048 sk_sp<SkDocument> doc(SkDocument::MakePDF(path));
reed@google.com99ac02b2013-06-07 20:30:16 +000049 if (doc.get()) {
50 SkScalar width = SkIntToScalar(w);
51 SkScalar height = SkIntToScalar(h);
halcanary96fcdcc2015-08-27 07:41:13 -070052 doDraw(doc->beginPage(width, height, nullptr), paint, text);
reed@google.com99ac02b2013-06-07 20:30:16 +000053 return true;
54 }
55 return false;
56}
57
caryclark@google.com5987f582012-10-02 18:33:14 +000058int tool_main(int argc, char** argv);
59int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000060 SkCommandLineFlags::SetUsage("");
61 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000062
reed@android.com6413e792010-04-12 14:27:37 +000063 SkAutoGraphics ag;
reed@google.com99ac02b2013-06-07 20:30:16 +000064 SkString path("skhello");
reed@android.com6413e792010-04-12 14:27:37 +000065 SkString text("Hello");
66
scroggo@google.com604e0c22013-04-09 21:25:46 +000067 if (!FLAGS_outFile.isEmpty()) {
68 path.set(FLAGS_outFile[0]);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000069 }
scroggo@google.com604e0c22013-04-09 21:25:46 +000070 if (!FLAGS_text.isEmpty()) {
71 text.set(FLAGS_text[0]);
reed@android.com6413e792010-04-12 14:27:37 +000072 }
73
74 SkPaint paint;
75 paint.setAntiAlias(true);
76 paint.setTextSize(SkIntToScalar(30));
reed@google.com4b0757b2013-05-20 16:33:41 +000077 paint.setTextAlign(SkPaint::kCenter_Align);
78
reed@android.com6413e792010-04-12 14:27:37 +000079 SkScalar width = paint.measureText(text.c_str(), text.size());
80 SkScalar spacing = paint.getFontSpacing();
81
reed@google.come1ca7052013-12-17 19:22:07 +000082 int w = SkScalarRoundToInt(width) + 30;
83 int h = SkScalarRoundToInt(spacing) + 30;
reed@android.com6413e792010-04-12 14:27:37 +000084
reed@google.com99ac02b2013-06-07 20:30:16 +000085 static const struct {
86 bool (*fProc)(int w, int h, const char path[], const char text[],
87 const SkPaint&);
88 const char* fSuffix;
89 } gRec[] = {
90 { do_surface, ".png" },
91 { do_document, ".pdf" },
reed@google.com4b0757b2013-05-20 16:33:41 +000092 };
skia.committer@gmail.com63193672013-06-08 07:01:13 +000093
reed@google.com99ac02b2013-06-07 20:30:16 +000094 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
95 SkString file;
96 file.printf("%s%s", path.c_str(), gRec[i].fSuffix);
97 if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) {
98 return -1;
99 }
scroggo@google.com7def5e12013-05-31 14:00:10 +0000100 }
reed@google.com99ac02b2013-06-07 20:30:16 +0000101 return 0;
reed@android.com6413e792010-04-12 14:27:37 +0000102}
caryclark@google.com5987f582012-10-02 18:33:14 +0000103
104#if !defined SK_BUILD_FOR_IOS
105int main(int argc, char * const argv[]) {
106 return tool_main(argc, (char**) argv);
107}
108#endif