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" |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 11 | #include "SkDocument.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 12 | #include "SkGraphics.h" |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 13 | #include "SkSurface.h" |
| 14 | #include "SkImage.h" |
| 15 | #include "SkStream.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 16 | #include "SkString.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 17 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 18 | DEFINE_string2(outFile, o, "skhello", "The filename to write the image."); |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 19 | DEFINE_string2(text, t, "Hello", "The string to write."); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 20 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 21 | static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) { |
| 22 | SkRect bounds; |
| 23 | canvas->getClipBounds(&bounds); |
| 24 | |
| 25 | canvas->drawColor(SK_ColorWHITE); |
| 26 | canvas->drawText(text, strlen(text), |
| 27 | bounds.centerX(), bounds.centerY(), |
| 28 | paint); |
| 29 | } |
| 30 | |
| 31 | static bool do_surface(int w, int h, const char path[], const char text[], |
| 32 | const SkPaint& paint) { |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 33 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(w, h)); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 34 | doDraw(surface->getCanvas(), paint, text); |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 35 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 36 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 37 | SkAutoDataUnref data(image->encode()); |
| 38 | if (NULL == data.get()) { |
| 39 | return false; |
| 40 | } |
| 41 | SkFILEWStream stream(path); |
| 42 | return stream.write(data->data(), data->size()); |
| 43 | } |
| 44 | |
| 45 | static bool do_document(int w, int h, const char path[], const char text[], |
| 46 | const SkPaint& paint) { |
| 47 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path)); |
| 48 | if (doc.get()) { |
| 49 | SkScalar width = SkIntToScalar(w); |
| 50 | SkScalar height = SkIntToScalar(h); |
| 51 | doDraw(doc->beginPage(width, height, NULL), paint, text); |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 57 | int tool_main(int argc, char** argv); |
| 58 | int tool_main(int argc, char** argv) { |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 59 | SkCommandLineFlags::SetUsage(""); |
| 60 | SkCommandLineFlags::Parse(argc, argv); |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 61 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 62 | SkAutoGraphics ag; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 63 | SkString path("skhello"); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 64 | SkString text("Hello"); |
| 65 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 66 | if (!FLAGS_outFile.isEmpty()) { |
| 67 | path.set(FLAGS_outFile[0]); |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 68 | } |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 69 | if (!FLAGS_text.isEmpty()) { |
| 70 | text.set(FLAGS_text[0]); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | SkPaint paint; |
| 74 | paint.setAntiAlias(true); |
| 75 | paint.setTextSize(SkIntToScalar(30)); |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 76 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 77 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 78 | SkScalar width = paint.measureText(text.c_str(), text.size()); |
| 79 | SkScalar spacing = paint.getFontSpacing(); |
| 80 | |
| 81 | int w = SkScalarRound(width) + 30; |
| 82 | int h = SkScalarRound(spacing) + 30; |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 83 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 84 | static const struct { |
| 85 | bool (*fProc)(int w, int h, const char path[], const char text[], |
| 86 | const SkPaint&); |
| 87 | const char* fSuffix; |
| 88 | } gRec[] = { |
| 89 | { do_surface, ".png" }, |
| 90 | { do_document, ".pdf" }, |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 91 | }; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 92 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 93 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 94 | SkString file; |
| 95 | file.printf("%s%s", path.c_str(), gRec[i].fSuffix); |
| 96 | if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) { |
| 97 | return -1; |
| 98 | } |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 99 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 100 | return 0; |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 101 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 102 | |
| 103 | #if !defined SK_BUILD_FOR_IOS |
| 104 | int main(int argc, char * const argv[]) { |
| 105 | return tool_main(argc, (char**) argv); |
| 106 | } |
| 107 | #endif |