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