blob: 6f56d4c0e9eeffb4742918b0b3031a648166cc5a [file] [log] [blame]
reed@android.com6413e792010-04-12 14:27:37 +00001#include "SkCanvas.h"
2#include "SkGraphics.h"
3//#include "SkImageDecoder.h"
4#include "SkImageEncoder.h"
5//#include "SkStream.h"
6#include "SkString.h"
7#include "SkTemplates.h"
8
9static void show_help() {
10 SkDebugf("usage: skhello [-o out-dir] [-t 'hello']\n default output: skhello.png\n");
11}
12
13int main (int argc, char * const argv[]) {
14 SkAutoGraphics ag;
15 SkString path("skhello.png");
16 SkString text("Hello");
17
18 for (int i = 1; i < argc; i++) {
19 if (!strcmp(argv[i], "--help")) {
20 show_help();
21 return 0;
22 }
23 if (!strcmp(argv[i], "-o")) {
24 if (i == argc-1) {
25 SkDebugf("ERROR: -o needs a following filename\n");
26 return -1;
27 }
28 path.set(argv[i+1]);
29 i += 1; // skip the out dir name
30 } else if (!strcmp(argv[i], "-t")) {
31 if (i == argc-1) {
32 SkDebugf("ERROR: -t needs a following string\n");
33 return -1;
34 }
35 text.set(argv[i+1]);
36 i += 1; // skip the text string
37 }
38 }
39
40 SkPaint paint;
41 paint.setAntiAlias(true);
42 paint.setTextSize(SkIntToScalar(30));
43 SkScalar width = paint.measureText(text.c_str(), text.size());
44 SkScalar spacing = paint.getFontSpacing();
45
46 int w = SkScalarRound(width) + 30;
47 int h = SkScalarRound(spacing) + 30;
48 SkBitmap bitmap;
49 bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
50 bitmap.allocPixels();
51
52 SkCanvas canvas(bitmap);
53 canvas.drawColor(SK_ColorWHITE);
54
55 paint.setTextAlign(SkPaint::kCenter_Align);
56 canvas.drawText(text.c_str(), text.size(),
57 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
58 paint);
59
60 SkImageEncoder::EncodeFile(path.c_str(), bitmap,
61 SkImageEncoder::kPNG_Type, 100);
62
63 return 0;
64}
65