Home | History | Annotate | Download | only in sample
      1 Using Skia's PDF Backend
      2 ========================
      3 
      4 Here is an example of using Skia's PDF backend (SkPDF) via the
      5 SkDocument and SkCanvas APIs.
      6 
      7 <!--?prettify lang=cc?-->
      8 
      9     #include "SkDocument.h"
     10 
     11     void WritePDF(SkWStream* outputStream,
     12                   const char* documentTitle,
     13                   void (*writePage)(SkCanvas*, int page),
     14                   int numberOfPages,
     15                   SkSize pageSize) {
     16         SkDocument::PDFMetadata metadata;
     17         metadata.fTitle = documentTitle;
     18         metadata.fCreator = "Example WritePDF() Function";
     19         SkTime::DateTime now;
     20         SkTime::GetDateTime(&now);
     21         metadata.fCreation.fEnabled  = true;
     22         metadata.fCreation.fDateTime = now;
     23         metadata.fModified.fEnabled  = true;
     24         metadata.fModified.fDateTime = now;
     25         sk_sp<SkDocument> pdfDocument = SkDocument::MakePDF(
     26                 outputStream, SK_ScalarDefaultRasterDPI, metadata,
     27                 nullptr, true);
     28         assert(pdfDocument);
     29 
     30         for (int page = 0; page < numberOfPages; ++page) {
     31             SkCanvas* pageCanvas =
     32                     pdfDocument->beginPage(pageSize.width(),
     33                                            pageSize.height());
     34             writePage(pageCanvas, page);
     35             pdfDocument->endPage();
     36         }
     37         pdfDocument->close();
     38     }
     39 
     40 * * *
     41 
     42 <span id="limits">SkPDF Limitations</span>
     43 ------------------------------------------
     44 
     45 There are several corners of Skia's public API that SkPDF currently
     46 does not handle because either no known client uses the feature or
     47 there is no simple PDF-ish way to handle it.
     48 
     49 In this document:
     50 
     51   + **drop** means to draw nothing.
     52 
     53   + **ignore** mean to draw without the effect
     54 
     55   + **expand** means to implement something in a non-PDF-ish way.
     56     This may mean to rasterize vector graphics, to expand paths with
     57     path effects into many individual paths, or to convert text to
     58     paths.
     59 
     60 <style scoped><!--
     61 #pdftable {border-collapse:collapse;}
     62 #pdftable tr th, #pdftable tr td {border:#888888 2px solid;padding: 5px;}
     63 --></style>
     64 <table id="pdftable">
     65 <tr><th>Effect</th>                  <th>text</th>   <th>images</th> <th>everything
     66                                                                          else</th></tr>
     67 <tr><th>SkMaskFilter</th>            <td>drop</td>   <td>ignore</td> <td>ignore</td></tr>
     68 <tr><th>SkPathEffect</th>            <td>ignore</td> <td>n/a</td>    <td>expand</td></tr>
     69 <tr><th>SkColorFilter</th>           <td>ignore</td> <td>expand</td> <td>ignore</td></tr>
     70 <tr><th>SkImageFilter</th>           <td>expand</td> <td>expand</td> <td>expand</td></tr>
     71 <tr><th>unsupported SkXferModes</th> <td>ignore</td> <td>ignore</td> <td>ignore</td></tr>
     72 <tr><th>non-gradient SkShader</th>   <td>expand</td> <td>n/a</td>    <td>expand</td></tr>
     73 </table>
     74 
     75 Notes:
     76 
     77   - *SkImageFilter*: When SkImageFilter is expanded, text-as-text is lost.
     78 
     79   - *SkXferMode*: The following transfer modes are not natively
     80     supported by PDF: DstOver, SrcIn, DstIn, SrcOut, DstOut, SrcATop,
     81     DstATop, and Modulate.
     82 
     83 Other limitations:
     84 
     85   - *drawText with VerticalText*  drop. No known clients seem to make use
     86     of the VerticalText flag.
     87 
     88   - *drawTextOnPath*  expand. (Text-as-text is lost.)
     89 
     90   - *drawVertices*  drop.
     91 
     92   - *drawPatch*  drop.
     93 
     94 * * *
     95