blob: 82abf80ba7e8ce67213171532cf92d5f15b3f200 [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
tfarina@chromium.orge0e71af2012-09-25 20:16:46 +000017int main(int argc, char* const argv[]) {
reed@android.com6413e792010-04-12 14:27:37 +000018 SkAutoGraphics ag;
19 SkString path("skhello.png");
20 SkString text("Hello");
21
22 for (int i = 1; i < argc; i++) {
23 if (!strcmp(argv[i], "--help")) {
24 show_help();
25 return 0;
26 }
27 if (!strcmp(argv[i], "-o")) {
28 if (i == argc-1) {
29 SkDebugf("ERROR: -o needs a following filename\n");
30 return -1;
31 }
32 path.set(argv[i+1]);
33 i += 1; // skip the out dir name
34 } else if (!strcmp(argv[i], "-t")) {
35 if (i == argc-1) {
36 SkDebugf("ERROR: -t needs a following string\n");
37 return -1;
38 }
39 text.set(argv[i+1]);
40 i += 1; // skip the text string
41 }
42 }
43
44 SkPaint paint;
45 paint.setAntiAlias(true);
46 paint.setTextSize(SkIntToScalar(30));
47 SkScalar width = paint.measureText(text.c_str(), text.size());
48 SkScalar spacing = paint.getFontSpacing();
49
50 int w = SkScalarRound(width) + 30;
51 int h = SkScalarRound(spacing) + 30;
52 SkBitmap bitmap;
53 bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
54 bitmap.allocPixels();
55
56 SkCanvas canvas(bitmap);
57 canvas.drawColor(SK_ColorWHITE);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
reed@android.com6413e792010-04-12 14:27:37 +000059 paint.setTextAlign(SkPaint::kCenter_Align);
60 canvas.drawText(text.c_str(), text.size(),
61 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
62 paint);
63
reed@android.comfd9714e2010-04-15 14:28:24 +000064 bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap,
reed@android.com6413e792010-04-12 14:27:37 +000065 SkImageEncoder::kPNG_Type, 100);
reed@android.comfd9714e2010-04-15 14:28:24 +000066 if (!success) {
67 SkDebugf("--- failed to write %s\n", path.c_str());
68 }
69 return !success;
reed@android.com6413e792010-04-12 14:27:37 +000070}