| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkCommandLineFlags.h" | 9 #include "SkCommandLineFlags.h" |
| 10 #include "SkData.h" |
| 10 #include "SkGraphics.h" | 11 #include "SkGraphics.h" |
| 11 #include "SkImageEncoder.h" | 12 #include "SkSurface.h" |
| 13 #include "SkImage.h" |
| 14 #include "SkStream.h" |
| 12 #include "SkString.h" | 15 #include "SkString.h" |
| 13 | 16 |
| 14 DEFINE_string2(outFile, o, "skhello.png", "The filename to write the image."); | 17 DEFINE_string2(outFile, o, "skhello.png", "The filename to write the image."); |
| 15 DEFINE_string2(text, t, "Hello", "The string to write."); | 18 DEFINE_string2(text, t, "Hello", "The string to write."); |
| 16 | 19 |
| 17 int tool_main(int argc, char** argv); | 20 int tool_main(int argc, char** argv); |
| 18 int tool_main(int argc, char** argv) { | 21 int tool_main(int argc, char** argv) { |
| 19 SkCommandLineFlags::SetUsage(""); | 22 SkCommandLineFlags::SetUsage(""); |
| 20 SkCommandLineFlags::Parse(argc, argv); | 23 SkCommandLineFlags::Parse(argc, argv); |
| 21 | 24 |
| 22 SkAutoGraphics ag; | 25 SkAutoGraphics ag; |
| 23 SkString path("skhello.png"); | 26 SkString path("skhello.png"); |
| 24 SkString text("Hello"); | 27 SkString text("Hello"); |
| 25 | 28 |
| 26 if (!FLAGS_outFile.isEmpty()) { | 29 if (!FLAGS_outFile.isEmpty()) { |
| 27 path.set(FLAGS_outFile[0]); | 30 path.set(FLAGS_outFile[0]); |
| 28 } | 31 } |
| 29 if (!FLAGS_text.isEmpty()) { | 32 if (!FLAGS_text.isEmpty()) { |
| 30 text.set(FLAGS_text[0]); | 33 text.set(FLAGS_text[0]); |
| 31 } | 34 } |
| 32 | 35 |
| 33 SkPaint paint; | 36 SkPaint paint; |
| 34 paint.setAntiAlias(true); | 37 paint.setAntiAlias(true); |
| 35 paint.setTextSize(SkIntToScalar(30)); | 38 paint.setTextSize(SkIntToScalar(30)); |
| 39 paint.setTextAlign(SkPaint::kCenter_Align); |
| 40 |
| 36 SkScalar width = paint.measureText(text.c_str(), text.size()); | 41 SkScalar width = paint.measureText(text.c_str(), text.size()); |
| 37 SkScalar spacing = paint.getFontSpacing(); | 42 SkScalar spacing = paint.getFontSpacing(); |
| 38 | 43 |
| 39 int w = SkScalarRound(width) + 30; | 44 int w = SkScalarRound(width) + 30; |
| 40 int h = SkScalarRound(spacing) + 30; | 45 int h = SkScalarRound(spacing) + 30; |
| 41 SkBitmap bitmap; | |
| 42 bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
| 43 bitmap.allocPixels(); | |
| 44 | 46 |
| 45 SkCanvas canvas(bitmap); | 47 SkImage::Info info = { |
| 46 canvas.drawColor(SK_ColorWHITE); | 48 w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType |
| 49 }; |
| 50 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 51 SkCanvas* canvas = surface->getCanvas(); |
| 47 | 52 |
| 48 paint.setTextAlign(SkPaint::kCenter_Align); | 53 canvas->drawColor(SK_ColorWHITE); |
| 49 canvas.drawText(text.c_str(), text.size(), | 54 canvas->drawText(text.c_str(), text.size(), |
| 50 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3, | 55 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3, |
| 51 paint); | 56 paint); |
| 52 | 57 |
| 53 bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap, | 58 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 54 SkImageEncoder::kPNG_Type, 100); | 59 SkAutoDataUnref data(image->encode()); |
| 55 if (!success) { | 60 SkFILEWStream stream(path.c_str()); |
| 56 SkDebugf("--- failed to write %s\n", path.c_str()); | 61 return stream.write(data->data(), data->size()); |
| 57 } | |
| 58 return !success; | |
| 59 } | 62 } |
| 60 | 63 |
| 61 #if !defined SK_BUILD_FOR_IOS | 64 #if !defined SK_BUILD_FOR_IOS |
| 62 int main(int argc, char * const argv[]) { | 65 int main(int argc, char * const argv[]) { |
| 63 return tool_main(argc, (char**) argv); | 66 return tool_main(argc, (char**) argv); |
| 64 } | 67 } |
| 65 #endif | 68 #endif |
| OLD | NEW |