blob: 668c3a993d901cf1899cb0adfabc1d8e8dd21c8f [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@android.com6413e792010-04-12 14:27:37 +000011#include "SkGraphics.h"
reed@google.com4b0757b2013-05-20 16:33:41 +000012#include "SkSurface.h"
13#include "SkImage.h"
14#include "SkStream.h"
reed@android.com6413e792010-04-12 14:27:37 +000015#include "SkString.h"
reed@android.com6413e792010-04-12 14:27:37 +000016
scroggo@google.com604e0c22013-04-09 21:25:46 +000017DEFINE_string2(outFile, o, "skhello.png", "The filename to write the image.");
18DEFINE_string2(text, t, "Hello", "The string to write.");
reed@android.com6413e792010-04-12 14:27:37 +000019
caryclark@google.com5987f582012-10-02 18:33:14 +000020int tool_main(int argc, char** argv);
21int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000022 SkCommandLineFlags::SetUsage("");
23 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000024
reed@android.com6413e792010-04-12 14:27:37 +000025 SkAutoGraphics ag;
26 SkString path("skhello.png");
27 SkString text("Hello");
28
scroggo@google.com604e0c22013-04-09 21:25:46 +000029 if (!FLAGS_outFile.isEmpty()) {
30 path.set(FLAGS_outFile[0]);
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000031 }
scroggo@google.com604e0c22013-04-09 21:25:46 +000032 if (!FLAGS_text.isEmpty()) {
33 text.set(FLAGS_text[0]);
reed@android.com6413e792010-04-12 14:27:37 +000034 }
35
36 SkPaint paint;
37 paint.setAntiAlias(true);
38 paint.setTextSize(SkIntToScalar(30));
reed@google.com4b0757b2013-05-20 16:33:41 +000039 paint.setTextAlign(SkPaint::kCenter_Align);
40
reed@android.com6413e792010-04-12 14:27:37 +000041 SkScalar width = paint.measureText(text.c_str(), text.size());
42 SkScalar spacing = paint.getFontSpacing();
43
44 int w = SkScalarRound(width) + 30;
45 int h = SkScalarRound(spacing) + 30;
reed@android.com6413e792010-04-12 14:27:37 +000046
reed@google.com4b0757b2013-05-20 16:33:41 +000047 SkImage::Info info = {
48 w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType
49 };
50 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
51 SkCanvas* canvas = surface->getCanvas();
rmistry@google.comd6176b02012-08-23 18:14:13 +000052
reed@google.com4b0757b2013-05-20 16:33:41 +000053 canvas->drawColor(SK_ColorWHITE);
54 canvas->drawText(text.c_str(), text.size(),
55 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
56 paint);
reed@android.com6413e792010-04-12 14:27:37 +000057
reed@google.com4b0757b2013-05-20 16:33:41 +000058 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
59 SkAutoDataUnref data(image->encode());
scroggo@google.com7def5e12013-05-31 14:00:10 +000060 if (NULL == data.get()) {
61 return -1;
62 }
reed@google.com4b0757b2013-05-20 16:33:41 +000063 SkFILEWStream stream(path.c_str());
64 return stream.write(data->data(), data->size());
reed@android.com6413e792010-04-12 14:27:37 +000065}
caryclark@google.com5987f582012-10-02 18:33:14 +000066
67#if !defined SK_BUILD_FOR_IOS
68int main(int argc, char * const argv[]) {
69 return tool_main(argc, (char**) argv);
70}
71#endif