2015-01-20 18:39:06 +00:00
|
|
|
Using Skia's PDF Backend
|
|
|
|
========================
|
|
|
|
|
2016-09-29 15:43:41 +00:00
|
|
|
Here is an example of using Skia's PDF backend (SkPDF) via the
|
|
|
|
SkDocument and SkCanvas APIs.
|
2015-01-20 18:39:06 +00:00
|
|
|
|
2016-03-20 14:07:11 +00:00
|
|
|
<!--?prettify lang=cc?-->
|
2015-01-20 18:39:06 +00:00
|
|
|
|
2018-09-07 18:33:14 +00:00
|
|
|
#include "SkPDFDocument.h"
|
2015-01-20 18:39:06 +00:00
|
|
|
|
2017-01-09 20:58:56 +00:00
|
|
|
void WritePDF(SkWStream* outputStream,
|
|
|
|
const char* documentTitle,
|
|
|
|
void (*writePage)(SkCanvas*, int page),
|
|
|
|
int numberOfPages,
|
|
|
|
SkSize pageSize) {
|
2018-09-07 18:33:14 +00:00
|
|
|
SkPDF::Metadata metadata;
|
2017-01-09 20:58:56 +00:00
|
|
|
metadata.fTitle = documentTitle;
|
|
|
|
metadata.fCreator = "Example WritePDF() Function";
|
|
|
|
SkTime::DateTime now;
|
|
|
|
SkTime::GetDateTime(&now);
|
2018-09-07 18:33:14 +00:00
|
|
|
metadata.fCreation = now;
|
|
|
|
metadata.fModified = now;
|
2019-01-07 15:00:48 +00:00
|
|
|
auto pdfDocument = SkPDF::MakeDocument(outputStream, metadata);
|
2016-05-03 19:10:04 +00:00
|
|
|
assert(pdfDocument);
|
2015-01-20 18:39:06 +00:00
|
|
|
|
|
|
|
for (int page = 0; page < numberOfPages; ++page) {
|
2018-09-21 19:53:03 +00:00
|
|
|
SkCanvas* pageCanvas = pdfDocument->beginPage(pageSize.width(),
|
|
|
|
pageSize.height());
|
2017-01-09 20:58:56 +00:00
|
|
|
writePage(pageCanvas, page);
|
2015-01-20 18:39:06 +00:00
|
|
|
pdfDocument->endPage();
|
|
|
|
}
|
2017-01-09 20:58:56 +00:00
|
|
|
pdfDocument->close();
|
2015-01-20 18:39:06 +00:00
|
|
|
}
|
2016-09-29 15:43:41 +00:00
|
|
|
|
|
|
|
* * *
|
|
|
|
|
|
|
|
<span id="limits">SkPDF Limitations</span>
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
There are several corners of Skia's public API that SkPDF currently
|
|
|
|
does not handle because either no known client uses the feature or
|
|
|
|
there is no simple PDF-ish way to handle it.
|
|
|
|
|
|
|
|
In this document:
|
|
|
|
|
|
|
|
+ **drop** means to draw nothing.
|
|
|
|
|
2018-01-10 14:55:05 +00:00
|
|
|
+ **ignore** means to draw without the effect
|
2016-09-29 15:43:41 +00:00
|
|
|
|
|
|
|
+ **expand** means to implement something in a non-PDF-ish way.
|
|
|
|
This may mean to rasterize vector graphics, to expand paths with
|
|
|
|
path effects into many individual paths, or to convert text to
|
|
|
|
paths.
|
|
|
|
|
|
|
|
<style scoped><!--
|
|
|
|
#pdftable {border-collapse:collapse;}
|
|
|
|
#pdftable tr th, #pdftable tr td {border:#888888 2px solid;padding: 5px;}
|
|
|
|
--></style>
|
|
|
|
<table id="pdftable">
|
|
|
|
<tr><th>Effect</th> <th>text</th> <th>images</th> <th>everything
|
|
|
|
else</th></tr>
|
|
|
|
<tr><th>SkMaskFilter</th> <td>drop</td> <td>ignore</td> <td>ignore</td></tr>
|
|
|
|
<tr><th>SkPathEffect</th> <td>ignore</td> <td>n/a</td> <td>expand</td></tr>
|
|
|
|
<tr><th>SkColorFilter</th> <td>ignore</td> <td>expand</td> <td>ignore</td></tr>
|
|
|
|
<tr><th>SkImageFilter</th> <td>expand</td> <td>expand</td> <td>expand</td></tr>
|
|
|
|
<tr><th>unsupported SkXferModes</th> <td>ignore</td> <td>ignore</td> <td>ignore</td></tr>
|
|
|
|
<tr><th>non-gradient SkShader</th> <td>expand</td> <td>n/a</td> <td>expand</td></tr>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
- *SkImageFilter*: When SkImageFilter is expanded, text-as-text is lost.
|
|
|
|
|
|
|
|
- *SkXferMode*: The following transfer modes are not natively
|
|
|
|
supported by PDF: DstOver, SrcIn, DstIn, SrcOut, DstOut, SrcATop,
|
|
|
|
DstATop, and Modulate.
|
|
|
|
|
|
|
|
Other limitations:
|
|
|
|
|
|
|
|
- *drawText with VerticalText* — drop. No known clients seem to make use
|
|
|
|
of the VerticalText flag.
|
|
|
|
|
|
|
|
- *drawTextOnPath* — expand. (Text-as-text is lost.)
|
|
|
|
|
|
|
|
- *drawVertices* — drop.
|
|
|
|
|
|
|
|
- *drawPatch* — drop.
|
|
|
|
|
|
|
|
* * *
|