epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
tfarina@chromium.org | e0e71af | 2012-09-25 20:16:46 +0000 | [diff] [blame] | 7 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 9 | #include "SkFlags.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 10 | #include "SkGraphics.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 11 | #include "SkImageEncoder.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 14 | DEFINE_string(o, "skhello.png", "The filename to write the image."); |
| 15 | DEFINE_string(t, "Hello", "The string to write."); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 16 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 17 | int tool_main(int argc, char** argv); |
| 18 | int tool_main(int argc, char** argv) { |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 19 | SkFlags::SetUsage(""); |
| 20 | SkFlags::ParseCommandLine(argc, argv); |
| 21 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 22 | SkAutoGraphics ag; |
| 23 | SkString path("skhello.png"); |
| 24 | SkString text("Hello"); |
| 25 | |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 26 | if (!FLAGS_o.isEmpty()) { |
| 27 | path.set(FLAGS_o[0]); |
| 28 | } |
| 29 | if (!FLAGS_t.isEmpty()) { |
| 30 | text.set(FLAGS_t[0]); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 31 | } |
| 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 48 | 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.com | fd9714e | 2010-04-15 14:28:24 +0000 | [diff] [blame] | 53 | bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap, |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 54 | SkImageEncoder::kPNG_Type, 100); |
reed@android.com | fd9714e | 2010-04-15 14:28:24 +0000 | [diff] [blame] | 55 | if (!success) { |
| 56 | SkDebugf("--- failed to write %s\n", path.c_str()); |
| 57 | } |
| 58 | return !success; |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 59 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 60 | |
| 61 | #if !defined SK_BUILD_FOR_IOS |
| 62 | int main(int argc, char * const argv[]) { |
| 63 | return tool_main(argc, (char**) argv); |
| 64 | } |
| 65 | #endif |