blob: 5614a56f5d5d5c55684e51d093250c3e17054c3b [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) {
robertphillips702edbd2015-06-23 06:26:08 -070036 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
37 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(w, h, &props));
reed@google.com99ac02b2013-06-07 20:30:16 +000038 doDraw(surface->getCanvas(), paint, text);
skia.committer@gmail.com63193672013-06-08 07:01:13 +000039
reed@google.com99ac02b2013-06-07 20:30:16 +000040 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
41 SkAutoDataUnref data(image->encode());
halcanary96fcdcc2015-08-27 07:41:13 -070042 if (nullptr == data.get()) {
reed@google.com99ac02b2013-06-07 20:30:16 +000043 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);
halcanary96fcdcc2015-08-27 07:41:13 -070055 doDraw(doc->beginPage(width, height, nullptr), paint, text);
reed@google.com99ac02b2013-06-07 20:30:16 +000056 return true;
57 }
58 return false;
59}
60
caryclark@google.com5987f582012-10-02 18:33:14 +000061int tool_main(int argc, char** argv);
62int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000063 SkCommandLineFlags::SetUsage("");
64 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000065
reed@android.com6413e792010-04-12 14:27:37 +000066 SkAutoGraphics ag;
reed@google.com99ac02b2013-06-07 20:30:16 +000067 SkString path("skhello");
reed@android.com6413e792010-04-12 14:27:37 +000068 SkString text("Hello");
69
scroggo@google.com604e0c22013-04-09 21:25:46 +000070 if (!FLAGS_outFile.isEmpty()) {
71 path.set(FLAGS_outFile[0]);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000072 }
scroggo@google.com604e0c22013-04-09 21:25:46 +000073 if (!FLAGS_text.isEmpty()) {
74 text.set(FLAGS_text[0]);
reed@android.com6413e792010-04-12 14:27:37 +000075 }
76
77 SkPaint paint;
78 paint.setAntiAlias(true);
79 paint.setTextSize(SkIntToScalar(30));
reed@google.com4b0757b2013-05-20 16:33:41 +000080 paint.setTextAlign(SkPaint::kCenter_Align);
81
reed@android.com6413e792010-04-12 14:27:37 +000082 SkScalar width = paint.measureText(text.c_str(), text.size());
83 SkScalar spacing = paint.getFontSpacing();
84
reed@google.come1ca7052013-12-17 19:22:07 +000085 int w = SkScalarRoundToInt(width) + 30;
86 int h = SkScalarRoundToInt(spacing) + 30;
reed@android.com6413e792010-04-12 14:27:37 +000087
reed@google.com99ac02b2013-06-07 20:30:16 +000088 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" },
reed@google.com4b0757b2013-05-20 16:33:41 +000095 };
skia.committer@gmail.com63193672013-06-08 07:01:13 +000096
reed@google.com99ac02b2013-06-07 20:30:16 +000097 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 }
scroggo@google.com7def5e12013-05-31 14:00:10 +0000103 }
reed@google.com99ac02b2013-06-07 20:30:16 +0000104 return 0;
reed@android.com6413e792010-04-12 14:27:37 +0000105}
caryclark@google.com5987f582012-10-02 18:33:14 +0000106
107#if !defined SK_BUILD_FOR_IOS
108int main(int argc, char * const argv[]) {
109 return tool_main(argc, (char**) argv);
110}
111#endif