PDF example
Update https://skia.org/user/sample/pdf to use example.
https://fiddle.skia.org/c/0777df0419252e28d93c26fb5b789059
Change-Id: Ia828cd5d2ffd30d24a237ffaa3b007984d98a3ba
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/216345
Auto-Submit: Hal Canary <halcanary@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
diff --git a/docs/examples/PDF.cpp b/docs/examples/PDF.cpp
new file mode 100644
index 0000000..f9a6878
--- /dev/null
+++ b/docs/examples/PDF.cpp
@@ -0,0 +1,64 @@
+// Copyright 2019 Google LLC.
+// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
+#include "tools/fiddle/examples.h"
+REG_FIDDLE(PDF, 256, 256, true, 0) {
+
+// Here is an example of using Skia’s PDF backend (SkPDF) via the SkDocument
+// and SkCanvas APIs.
+void WritePDF(SkWStream* outputStream,
+ const char* documentTitle,
+ void (*writePage)(SkCanvas*, int page),
+ int numberOfPages,
+ SkSize pageSize) {
+ SkPDF::Metadata metadata;
+ metadata.fTitle = documentTitle;
+ metadata.fCreator = "Example WritePDF() Function";
+ metadata.fCreation = {0, 2019, 1, 4, 31, 12, 34, 56};
+ metadata.fModified = {0, 2019, 1, 4, 31, 12, 34, 56};
+ auto pdfDocument = SkPDF::MakeDocument(outputStream, metadata);
+ SkASSERT(pdfDocument);
+ for (int page = 0; page < numberOfPages; ++page) {
+ SkCanvas* pageCanvas = pdfDocument->beginPage(pageSize.width(),
+ pageSize.height());
+ writePage(pageCanvas, page);
+ pdfDocument->endPage();
+ }
+ pdfDocument->close();
+}
+
+// Print binary data to stdout as hex.
+void print_data(const SkData* data, const char* name) {
+ if (data) {
+ SkDebugf("\nxxd -r -p > %s << EOF", name);
+ size_t s = data->size();
+ const uint8_t* d = data->bytes();
+ for (size_t i = 0; i < s; ++i) {
+ if (i % 40 == 0) { SkDebugf("\n"); }
+ SkDebugf("%02x", d[i]);
+ }
+ SkDebugf("\nEOF\n\n");
+ }
+}
+
+// example function that draws on a SkCanvas.
+void write_page(SkCanvas* canvas, int) {
+ const SkScalar R = 115.2f, C = 128.0f;
+ SkPath path;
+ path.moveTo(C + R, C);
+ for (int i = 1; i < 8; ++i) {
+ SkScalar a = 2.6927937f * i;
+ path.lineTo(C + R * cos(a), C + R * sin(a));
+ }
+ SkPaint paint;
+ paint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawPath(path, paint);
+}
+
+void draw(SkCanvas*) {
+ constexpr SkSize ansiLetterSize{8.5f * 72, 11.0f * 72};
+ SkDynamicMemoryWStream buffer;
+ WritePDF(&buffer, "SkPDF Example", &write_page, 1, ansiLetterSize);
+ sk_sp<SkData> pdfData = buffer.detachAsData();
+ print_data(pdfData.get(), "skpdf_example.pdf");
+}
+} // END FIDDLE
diff --git a/site/user/sample/pdf.md b/site/user/sample/pdf.md
index b59ae07..8847a96 100644
--- a/site/user/sample/pdf.md
+++ b/site/user/sample/pdf.md
@@ -4,33 +4,8 @@
Here is an example of using Skia's PDF backend (SkPDF) via the
SkDocument and SkCanvas APIs.
-<!--?prettify lang=cc?-->
-
- #include "SkPDFDocument.h"
-
- void WritePDF(SkWStream* outputStream,
- const char* documentTitle,
- void (*writePage)(SkCanvas*, int page),
- int numberOfPages,
- SkSize pageSize) {
- SkPDF::Metadata metadata;
- metadata.fTitle = documentTitle;
- metadata.fCreator = "Example WritePDF() Function";
- SkTime::DateTime now;
- SkTime::GetDateTime(&now);
- metadata.fCreation = now;
- metadata.fModified = now;
- auto pdfDocument = SkPDF::MakeDocument(outputStream, metadata);
- assert(pdfDocument);
-
- for (int page = 0; page < numberOfPages; ++page) {
- SkCanvas* pageCanvas = pdfDocument->beginPage(pageSize.width(),
- pageSize.height());
- writePage(pageCanvas, page);
- pdfDocument->endPage();
- }
- pdfDocument->close();
- }
+<fiddle-embed name='@PDF'></fiddle-embed>
+<!-- https://fiddle.skia.org/c/@PDF docs/examples/PDF.cpp -->
* * *
diff --git a/tools/fiddle/all_examples.cpp b/tools/fiddle/all_examples.cpp
index b14f23c..b7710c2 100644
--- a/tools/fiddle/all_examples.cpp
+++ b/tools/fiddle/all_examples.cpp
@@ -529,6 +529,7 @@
#include "../../docs/examples/Modulate.cpp"
#include "../../docs/examples/Multiply.cpp"
#include "../../docs/examples/Overlay.cpp"
+#include "../../docs/examples/PDF.cpp"
#include "../../docs/examples/Paint_053.cpp"
#include "../../docs/examples/Paint_057.cpp"
#include "../../docs/examples/Paint_containsText.cpp"