skhello.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. */
  7. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkData.h"
  9. #include "include/core/SkDocument.h"
  10. #include "include/core/SkGraphics.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkStream.h"
  13. #include "include/core/SkString.h"
  14. #include "include/core/SkSurface.h"
  15. #include "tools/flags/CommandLineFlags.h"
  16. static DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");
  17. static DEFINE_string2(text, t, "Hello", "The string to write.");
  18. static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
  19. SkRect bounds = canvas->getLocalClipBounds();
  20. canvas->drawColor(SK_ColorWHITE);
  21. canvas->drawText(text, strlen(text),
  22. bounds.centerX(), bounds.centerY(),
  23. paint);
  24. }
  25. static bool do_surface(int w, int h, const char path[], const char text[],
  26. const SkPaint& paint) {
  27. SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
  28. sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h, &props));
  29. doDraw(surface->getCanvas(), paint, text);
  30. sk_sp<SkImage> image(surface->makeImageSnapshot());
  31. sk_sp<SkData> data(image->encode());
  32. if (!data) {
  33. return false;
  34. }
  35. SkFILEWStream stream(path);
  36. return stream.write(data->data(), data->size());
  37. }
  38. static bool do_document(int w, int h, const char path[], const char text[],
  39. const SkPaint& paint) {
  40. auto doc = SkPDF::MakeDocument(path);
  41. if (doc.get()) {
  42. SkScalar width = SkIntToScalar(w);
  43. SkScalar height = SkIntToScalar(h);
  44. doDraw(doc->beginPage(width, height, nullptr), paint, text);
  45. return true;
  46. }
  47. return false;
  48. }
  49. int main(int argc, char** argv) {
  50. CommandLineFlags::SetUsage("");
  51. CommandLineFlags::Parse(argc, argv);
  52. SkAutoGraphics ag;
  53. SkString path("skhello");
  54. SkString text("Hello");
  55. if (!FLAGS_outFile.isEmpty()) {
  56. path.set(FLAGS_outFile[0]);
  57. }
  58. if (!FLAGS_text.isEmpty()) {
  59. text.set(FLAGS_text[0]);
  60. }
  61. SkPaint paint;
  62. paint.setAntiAlias(true);
  63. paint.setTextSize(SkIntToScalar(30));
  64. paint.setTextAlign(SkPaint::kCenter_Align);
  65. SkScalar width = paint.measureText(text.c_str(), text.size());
  66. SkScalar spacing = paint.getFontSpacing();
  67. int w = SkScalarRoundToInt(width) + 30;
  68. int h = SkScalarRoundToInt(spacing) + 30;
  69. static const struct {
  70. bool (*fProc)(int w, int h, const char path[], const char text[],
  71. const SkPaint&);
  72. const char* fSuffix;
  73. } gRec[] = {
  74. { do_surface, ".png" },
  75. { do_document, ".pdf" },
  76. };
  77. for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
  78. SkString file;
  79. file.printf("%s%s", path.c_str(), gRec[i].fSuffix);
  80. if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) {
  81. return -1;
  82. }
  83. }
  84. return 0;
  85. }