blob: 465806f8faf4cfd13f8688da958091661c844719 [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"
9#include "SkGraphics.h"
reed@android.com6413e792010-04-12 14:27:37 +000010#include "SkImageEncoder.h"
reed@android.com6413e792010-04-12 14:27:37 +000011#include "SkString.h"
reed@android.com6413e792010-04-12 14:27:37 +000012
13static void show_help() {
14 SkDebugf("usage: skhello [-o out-dir] [-t 'hello']\n default output: skhello.png\n");
15}
16
caryclark@google.com5987f582012-10-02 18:33:14 +000017int tool_main(int argc, char** argv);
18int tool_main(int argc, char** argv) {
reed@android.com6413e792010-04-12 14:27:37 +000019 SkAutoGraphics ag;
20 SkString path("skhello.png");
21 SkString text("Hello");
22
23 for (int i = 1; i < argc; i++) {
24 if (!strcmp(argv[i], "--help")) {
25 show_help();
26 return 0;
27 }
28 if (!strcmp(argv[i], "-o")) {
29 if (i == argc-1) {
30 SkDebugf("ERROR: -o needs a following filename\n");
31 return -1;
32 }
33 path.set(argv[i+1]);
34 i += 1; // skip the out dir name
35 } else if (!strcmp(argv[i], "-t")) {
36 if (i == argc-1) {
37 SkDebugf("ERROR: -t needs a following string\n");
38 return -1;
39 }
40 text.set(argv[i+1]);
41 i += 1; // skip the text string
42 }
43 }
44
45 SkPaint paint;
46 paint.setAntiAlias(true);
47 paint.setTextSize(SkIntToScalar(30));
48 SkScalar width = paint.measureText(text.c_str(), text.size());
49 SkScalar spacing = paint.getFontSpacing();
50
51 int w = SkScalarRound(width) + 30;
52 int h = SkScalarRound(spacing) + 30;
53 SkBitmap bitmap;
54 bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
55 bitmap.allocPixels();
56
57 SkCanvas canvas(bitmap);
58 canvas.drawColor(SK_ColorWHITE);
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
reed@android.com6413e792010-04-12 14:27:37 +000060 paint.setTextAlign(SkPaint::kCenter_Align);
61 canvas.drawText(text.c_str(), text.size(),
62 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
63 paint);
64
reed@android.comfd9714e2010-04-15 14:28:24 +000065 bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap,
reed@android.com6413e792010-04-12 14:27:37 +000066 SkImageEncoder::kPNG_Type, 100);
reed@android.comfd9714e2010-04-15 14:28:24 +000067 if (!success) {
68 SkDebugf("--- failed to write %s\n", path.c_str());
69 }
70 return !success;
reed@android.com6413e792010-04-12 14:27:37 +000071}
caryclark@google.com5987f582012-10-02 18:33:14 +000072
73#if !defined SK_BUILD_FOR_IOS
74int main(int argc, char * const argv[]) {
75 return tool_main(argc, (char**) argv);
76}
77#endif