blob: 8cea1d16403902b0d40925de05e832a7c870589f [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"
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +00009#include "SkFlags.h"
reed@android.com6413e792010-04-12 14:27:37 +000010#include "SkGraphics.h"
reed@android.com6413e792010-04-12 14:27:37 +000011#include "SkImageEncoder.h"
reed@android.com6413e792010-04-12 14:27:37 +000012#include "SkString.h"
reed@android.com6413e792010-04-12 14:27:37 +000013
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000014DEFINE_string(o, "skhello.png", "The filename to write the image.");
15DEFINE_string(t, "Hello", "The string to write.");
reed@android.com6413e792010-04-12 14:27:37 +000016
caryclark@google.com5987f582012-10-02 18:33:14 +000017int tool_main(int argc, char** argv);
18int tool_main(int argc, char** argv) {
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000019 SkFlags::SetUsage("");
20 SkFlags::ParseCommandLine(argc, argv);
21
reed@android.com6413e792010-04-12 14:27:37 +000022 SkAutoGraphics ag;
23 SkString path("skhello.png");
24 SkString text("Hello");
25
commit-bot@chromium.org31ea3392013-03-04 20:58:01 +000026 if (!FLAGS_o.isEmpty()) {
27 path.set(FLAGS_o[0]);
28 }
29 if (!FLAGS_t.isEmpty()) {
30 text.set(FLAGS_t[0]);
reed@android.com6413e792010-04-12 14:27:37 +000031 }
32
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setTextSize(SkIntToScalar(30));
36 SkScalar width = paint.measureText(text.c_str(), text.size());
37 SkScalar spacing = paint.getFontSpacing();
38
39 int w = SkScalarRound(width) + 30;
40 int h = SkScalarRound(spacing) + 30;
41 SkBitmap bitmap;
42 bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
43 bitmap.allocPixels();
44
45 SkCanvas canvas(bitmap);
46 canvas.drawColor(SK_ColorWHITE);
rmistry@google.comd6176b02012-08-23 18:14:13 +000047
reed@android.com6413e792010-04-12 14:27:37 +000048 paint.setTextAlign(SkPaint::kCenter_Align);
49 canvas.drawText(text.c_str(), text.size(),
50 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3,
51 paint);
52
reed@android.comfd9714e2010-04-15 14:28:24 +000053 bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap,
reed@android.com6413e792010-04-12 14:27:37 +000054 SkImageEncoder::kPNG_Type, 100);
reed@android.comfd9714e2010-04-15 14:28:24 +000055 if (!success) {
56 SkDebugf("--- failed to write %s\n", path.c_str());
57 }
58 return !success;
reed@android.com6413e792010-04-12 14:27:37 +000059}
caryclark@google.com5987f582012-10-02 18:33:14 +000060
61#if !defined SK_BUILD_FOR_IOS
62int main(int argc, char * const argv[]) {
63 return tool_main(argc, (char**) argv);
64}
65#endif