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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkData.h" |
| 10 | #include "include/core/SkDocument.h" |
| 11 | #include "include/core/SkGraphics.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/core/SkSurface.h" |
| 16 | #include "tools/flags/CommandLineFlags.h" |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 17 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 18 | static DEFINE_string2(outFile, o, "skhello", "The filename to write the image."); |
| 19 | static 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[]) { |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 22 | SkRect bounds = canvas->getLocalClipBounds(); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 23 | |
| 24 | canvas->drawColor(SK_ColorWHITE); |
| 25 | canvas->drawText(text, strlen(text), |
| 26 | bounds.centerX(), bounds.centerY(), |
| 27 | paint); |
| 28 | } |
| 29 | |
| 30 | static bool do_surface(int w, int h, const char path[], const char text[], |
| 31 | const SkPaint& paint) { |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 32 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 33 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h, &props)); |
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 | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 36 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 37 | sk_sp<SkData> data(image->encode()); |
| 38 | if (!data) { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 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) { |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 47 | auto doc = SkPDF::MakeDocument(path); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 48 | if (doc.get()) { |
| 49 | SkScalar width = SkIntToScalar(w); |
| 50 | SkScalar height = SkIntToScalar(h); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | doDraw(doc->beginPage(width, height, nullptr), paint, text); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
Mike Klein | be28ee2 | 2017-02-06 12:46:20 -0500 | [diff] [blame] | 57 | int main(int argc, char** argv) { |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 58 | CommandLineFlags::SetUsage(""); |
| 59 | CommandLineFlags::Parse(argc, argv); |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 60 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 61 | SkAutoGraphics ag; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 62 | SkString path("skhello"); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 63 | SkString text("Hello"); |
| 64 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 65 | if (!FLAGS_outFile.isEmpty()) { |
| 66 | path.set(FLAGS_outFile[0]); |
commit-bot@chromium.org | 31ea339 | 2013-03-04 20:58:01 +0000 | [diff] [blame] | 67 | } |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 68 | if (!FLAGS_text.isEmpty()) { |
| 69 | text.set(FLAGS_text[0]); |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | SkPaint paint; |
| 73 | paint.setAntiAlias(true); |
| 74 | paint.setTextSize(SkIntToScalar(30)); |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 75 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 76 | |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 77 | SkScalar width = paint.measureText(text.c_str(), text.size()); |
| 78 | SkScalar spacing = paint.getFontSpacing(); |
| 79 | |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 80 | int w = SkScalarRoundToInt(width) + 30; |
| 81 | int h = SkScalarRoundToInt(spacing) + 30; |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 82 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 83 | static const struct { |
| 84 | bool (*fProc)(int w, int h, const char path[], const char text[], |
| 85 | const SkPaint&); |
| 86 | const char* fSuffix; |
| 87 | } gRec[] = { |
| 88 | { do_surface, ".png" }, |
| 89 | { do_document, ".pdf" }, |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 90 | }; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 91 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 92 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 93 | SkString file; |
| 94 | file.printf("%s%s", path.c_str(), gRec[i].fSuffix); |
| 95 | if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) { |
| 96 | return -1; |
| 97 | } |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 98 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 99 | return 0; |
reed@android.com | 6413e79 | 2010-04-12 14:27:37 +0000 | [diff] [blame] | 100 | } |