blob: 3eb9f45567544fc0477ea031e758ade4c488531b [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[]) {
Mike Reed918e1442017-01-23 11:39:45 -050022 SkRect bounds = canvas->getLocalClipBounds();
reed@google.com99ac02b2013-06-07 20:30:16 +000023
24 canvas->drawColor(SK_ColorWHITE);
25 canvas->drawText(text, strlen(text),
26 bounds.centerX(), bounds.centerY(),
27 paint);
28}
29
30static bool do_surface(int w, int h, const char path[], const char text[],
31 const SkPaint& paint) {
robertphillips702edbd2015-06-23 06:26:08 -070032 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -070033 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h, &props));
reed@google.com99ac02b2013-06-07 20:30:16 +000034 doDraw(surface->getCanvas(), paint, text);
skia.committer@gmail.com63193672013-06-08 07:01:13 +000035
reed9ce9d672016-03-17 10:51:11 -070036 sk_sp<SkImage> image(surface->makeImageSnapshot());
bungeman38d909e2016-08-02 14:40:46 -070037 sk_sp<SkData> data(image->encode());
38 if (!data) {
reed@google.com99ac02b2013-06-07 20:30:16 +000039 return false;
40 }
41 SkFILEWStream stream(path);
42 return stream.write(data->data(), data->size());
43}
44
45static bool do_document(int w, int h, const char path[], const char text[],
46 const SkPaint& paint) {
Hal Canary23564b92018-09-07 14:33:14 -040047 sk_sp<SkDocument> doc(SkPDF::MakeDocument(path));
reed@google.com99ac02b2013-06-07 20:30:16 +000048 if (doc.get()) {
49 SkScalar width = SkIntToScalar(w);
50 SkScalar height = SkIntToScalar(h);
halcanary96fcdcc2015-08-27 07:41:13 -070051 doDraw(doc->beginPage(width, height, nullptr), paint, text);
reed@google.com99ac02b2013-06-07 20:30:16 +000052 return true;
53 }
54 return false;
55}
56
Mike Kleinbe28ee22017-02-06 12:46:20 -050057int main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000058 SkCommandLineFlags::SetUsage("");
59 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000060
reed@android.com6413e792010-04-12 14:27:37 +000061 SkAutoGraphics ag;
reed@google.com99ac02b2013-06-07 20:30:16 +000062 SkString path("skhello");
reed@android.com6413e792010-04-12 14:27:37 +000063 SkString text("Hello");
64
scroggo@google.com604e0c22013-04-09 21:25:46 +000065 if (!FLAGS_outFile.isEmpty()) {
66 path.set(FLAGS_outFile[0]);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000067 }
scroggo@google.com604e0c22013-04-09 21:25:46 +000068 if (!FLAGS_text.isEmpty()) {
69 text.set(FLAGS_text[0]);
reed@android.com6413e792010-04-12 14:27:37 +000070 }
71
72 SkPaint paint;
73 paint.setAntiAlias(true);
74 paint.setTextSize(SkIntToScalar(30));
reed@google.com4b0757b2013-05-20 16:33:41 +000075 paint.setTextAlign(SkPaint::kCenter_Align);
76
reed@android.com6413e792010-04-12 14:27:37 +000077 SkScalar width = paint.measureText(text.c_str(), text.size());
78 SkScalar spacing = paint.getFontSpacing();
79
reed@google.come1ca7052013-12-17 19:22:07 +000080 int w = SkScalarRoundToInt(width) + 30;
81 int h = SkScalarRoundToInt(spacing) + 30;
reed@android.com6413e792010-04-12 14:27:37 +000082
reed@google.com99ac02b2013-06-07 20:30:16 +000083 static const struct {
84 bool (*fProc)(int w, int h, const char path[], const char text[],
85 const SkPaint&);
86 const char* fSuffix;
87 } gRec[] = {
88 { do_surface, ".png" },
89 { do_document, ".pdf" },
reed@google.com4b0757b2013-05-20 16:33:41 +000090 };
skia.committer@gmail.com63193672013-06-08 07:01:13 +000091
reed@google.com99ac02b2013-06-07 20:30:16 +000092 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
93 SkString file;
94 file.printf("%s%s", path.c_str(), gRec[i].fSuffix);
95 if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) {
96 return -1;
97 }
scroggo@google.com7def5e12013-05-31 14:00:10 +000098 }
reed@google.com99ac02b2013-06-07 20:30:16 +000099 return 0;
reed@android.com6413e792010-04-12 14:27:37 +0000100}