skhello.cpp revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkCanvas.h"
9#include "SkGraphics.h"
10//#include "SkImageDecoder.h"
11#include "SkImageEncoder.h"
12//#include "SkStream.h"
13#include "SkString.h"
14#include "SkTemplates.h"
15
16static void show_help() {
17    SkDebugf("usage: skhello [-o out-dir] [-t 'hello']\n  default output: skhello.png\n");
18}
19
20int main (int argc, char * const argv[]) {
21    SkAutoGraphics ag;
22    SkString path("skhello.png");
23    SkString text("Hello");
24
25    for (int i = 1; i < argc; i++) {
26        if (!strcmp(argv[i], "--help")) {
27            show_help();
28            return 0;
29        }
30        if (!strcmp(argv[i], "-o")) {
31            if (i == argc-1) {
32                SkDebugf("ERROR: -o needs a following filename\n");
33                return -1;
34            }
35            path.set(argv[i+1]);
36            i += 1; // skip the out dir name
37        } else if (!strcmp(argv[i], "-t")) {
38            if (i == argc-1) {
39                SkDebugf("ERROR: -t needs a following string\n");
40                return -1;
41            }
42            text.set(argv[i+1]);
43            i += 1; // skip the text string
44        }
45    }
46
47    SkPaint paint;
48    paint.setAntiAlias(true);
49    paint.setTextSize(SkIntToScalar(30));
50    SkScalar width = paint.measureText(text.c_str(), text.size());
51    SkScalar spacing = paint.getFontSpacing();
52
53    int w = SkScalarRound(width) + 30;
54    int h = SkScalarRound(spacing) + 30;
55    SkBitmap bitmap;
56    bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
57    bitmap.allocPixels();
58
59    SkCanvas canvas(bitmap);
60    canvas.drawColor(SK_ColorWHITE);
61
62    paint.setTextAlign(SkPaint::kCenter_Align);
63    canvas.drawText(text.c_str(), text.size(),
64                    SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
65                    paint);
66
67    bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap,
68                               SkImageEncoder::kPNG_Type, 100);
69    if (!success) {
70        SkDebugf("--- failed to write %s\n", path.c_str());
71    }
72    return !success;
73}
74
75