1 /*
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 */
7
8 #include "SkCanvas.h"
9 #include "SkGraphics.h"
10 #include "SkImageEncoder.h"
11 #include "SkString.h"
12
show_help()13 static void show_help() {
14 SkDebugf("usage: skhello [-o out-dir] [-t 'hello']\n default output: skhello.png\n");
15 }
16
17 int tool_main(int argc, char** argv);
tool_main(int argc,char ** argv)18 int tool_main(int argc, char** argv) {
19 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);
59
60 paint.setTextAlign(SkPaint::kCenter_Align);
61 canvas.drawText(text.c_str(), text.size(),
62 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
63 paint);
64
65 bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap,
66 SkImageEncoder::kPNG_Type, 100);
67 if (!success) {
68 SkDebugf("--- failed to write %s\n", path.c_str());
69 }
70 return !success;
71 }
72
73 #if !defined SK_BUILD_FOR_IOS
main(int argc,char * const argv[])74 int main(int argc, char * const argv[]) {
75 return tool_main(argc, (char**) argv);
76 }
77 #endif
78