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 "SkData.h" |
| 11 #include "SkDocument.h" |
11 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
12 #include "SkSurface.h" | 13 #include "SkSurface.h" |
13 #include "SkImage.h" | 14 #include "SkImage.h" |
14 #include "SkStream.h" | 15 #include "SkStream.h" |
15 #include "SkString.h" | 16 #include "SkString.h" |
16 | 17 |
17 DEFINE_string2(outFile, o, "skhello.png", "The filename to write the image."); | 18 DEFINE_string2(outFile, o, "skhello", "The filename to write the image."); |
18 DEFINE_string2(text, t, "Hello", "The string to write."); | 19 DEFINE_string2(text, t, "Hello", "The string to write."); |
19 | 20 |
| 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) { |
| 33 SkImage::Info info = { |
| 34 w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType |
| 35 }; |
| 36 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 37 doDraw(surface->getCanvas(), paint, text); |
| 38 |
| 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 |
20 int tool_main(int argc, char** argv); | 60 int tool_main(int argc, char** argv); |
21 int tool_main(int argc, char** argv) { | 61 int tool_main(int argc, char** argv) { |
22 SkCommandLineFlags::SetUsage(""); | 62 SkCommandLineFlags::SetUsage(""); |
23 SkCommandLineFlags::Parse(argc, argv); | 63 SkCommandLineFlags::Parse(argc, argv); |
24 | 64 |
25 SkAutoGraphics ag; | 65 SkAutoGraphics ag; |
26 SkString path("skhello.png"); | 66 SkString path("skhello"); |
27 SkString text("Hello"); | 67 SkString text("Hello"); |
28 | 68 |
29 if (!FLAGS_outFile.isEmpty()) { | 69 if (!FLAGS_outFile.isEmpty()) { |
30 path.set(FLAGS_outFile[0]); | 70 path.set(FLAGS_outFile[0]); |
31 } | 71 } |
32 if (!FLAGS_text.isEmpty()) { | 72 if (!FLAGS_text.isEmpty()) { |
33 text.set(FLAGS_text[0]); | 73 text.set(FLAGS_text[0]); |
34 } | 74 } |
35 | 75 |
36 SkPaint paint; | 76 SkPaint paint; |
37 paint.setAntiAlias(true); | 77 paint.setAntiAlias(true); |
38 paint.setTextSize(SkIntToScalar(30)); | 78 paint.setTextSize(SkIntToScalar(30)); |
39 paint.setTextAlign(SkPaint::kCenter_Align); | 79 paint.setTextAlign(SkPaint::kCenter_Align); |
40 | 80 |
41 SkScalar width = paint.measureText(text.c_str(), text.size()); | 81 SkScalar width = paint.measureText(text.c_str(), text.size()); |
42 SkScalar spacing = paint.getFontSpacing(); | 82 SkScalar spacing = paint.getFontSpacing(); |
43 | 83 |
44 int w = SkScalarRound(width) + 30; | 84 int w = SkScalarRound(width) + 30; |
45 int h = SkScalarRound(spacing) + 30; | 85 int h = SkScalarRound(spacing) + 30; |
46 | 86 |
47 SkImage::Info info = { | 87 static const struct { |
48 w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType | 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" }, |
49 }; | 94 }; |
50 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); | 95 |
51 SkCanvas* canvas = surface->getCanvas(); | 96 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
52 | 97 SkString file; |
53 canvas->drawColor(SK_ColorWHITE); | 98 file.printf("%s%s", path.c_str(), gRec[i].fSuffix); |
54 canvas->drawText(text.c_str(), text.size(), | 99 if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) { |
55 SkIntToScalar(w)/2, SkIntToScalar(h)*2/3, | 100 return -1; |
56 paint); | 101 } |
57 | |
58 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); | |
59 SkAutoDataUnref data(image->encode()); | |
60 if (NULL == data.get()) { | |
61 return -1; | |
62 } | 102 } |
63 SkFILEWStream stream(path.c_str()); | 103 return 0; |
64 return stream.write(data->data(), data->size()); | |
65 } | 104 } |
66 | 105 |
67 #if !defined SK_BUILD_FOR_IOS | 106 #if !defined SK_BUILD_FOR_IOS |
68 int main(int argc, char * const argv[]) { | 107 int main(int argc, char * const argv[]) { |
69 return tool_main(argc, (char**) argv); | 108 return tool_main(argc, (char**) argv); |
70 } | 109 } |
71 #endif | 110 #endif |
OLD | NEW |