199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com/*
2a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary * Copyright 2011 Google Inc.
399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com *
499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Use of this source code is governed by a BSD-style license that can be
599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * found in the LICENSE file.
699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */
799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#include "SkDocument.h"
9a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary#include "SkPDFCanon.h"
105867736b08d3689356b49f505bcf748c2194a0bcreed#include "SkPDFDevice.h"
11a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary#include "SkPDFFont.h"
122f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary#include "SkPDFStream.h"
13a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary#include "SkPDFTypes.h"
14f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary#include "SkPDFUtils.h"
15a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary#include "SkStream.h"
1634422610ac22adceeabb66023120f27b96cae953halcanary#include "SkPDFMetadata.h"
17a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
18f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanaryclass SkPDFDict;
19f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary
20a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanarystatic void emit_pdf_header(SkWStream* stream) {
21a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("%PDF-1.4\n%");
22a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    // The PDF spec recommends including a comment with four bytes, all
23a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    // with their high bits set.  This is "Skia" with the high bits set.
24a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->write32(0xD3EBE9E1);
25a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("\n");
26a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary}
27a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
28a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanarystatic void emit_pdf_footer(SkWStream* stream,
2937c46cad21632cfc1411b08d73af37a1fffe2944halcanary                            const SkPDFObjNumMap& objNumMap,
3037c46cad21632cfc1411b08d73af37a1fffe2944halcanary                            const SkPDFSubstituteMap& substitutes,
31a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                            SkPDFObject* docCatalog,
32a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                            int64_t objCount,
33f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary                            int32_t xRefFileOffset,
3434422610ac22adceeabb66023120f27b96cae953halcanary                            SkPDFObject* info /* take ownership */,
3534422610ac22adceeabb66023120f27b96cae953halcanary                            SkPDFObject* id /* take ownership */) {
36a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    SkPDFDict trailerDict;
3734422610ac22adceeabb66023120f27b96cae953halcanary    // TODO(http://crbug.com/80908): Linearized format will take a
3834422610ac22adceeabb66023120f27b96cae953halcanary    //                               Prev entry too.
39a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    trailerDict.insertInt("Size", int(objCount));
4072266fd1fe9bfe078239a9d9e85f479faee30281halcanary    trailerDict.insertObjRef("Root", SkRef(docCatalog));
41f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary    SkASSERT(info);
4234422610ac22adceeabb66023120f27b96cae953halcanary    trailerDict.insertObjRef("Info", info);
4334422610ac22adceeabb66023120f27b96cae953halcanary    if (id) {
4434422610ac22adceeabb66023120f27b96cae953halcanary        trailerDict.insertObject("ID", id);
4534422610ac22adceeabb66023120f27b96cae953halcanary    }
46a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("trailer\n");
4737c46cad21632cfc1411b08d73af37a1fffe2944halcanary    trailerDict.emitObject(stream, objNumMap, substitutes);
48a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("\nstartxref\n");
49a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeBigDecAsText(xRefFileOffset);
50a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("\n%%EOF");
51a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary}
52a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
532f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanarystatic void perform_font_subsetting(
542f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        const SkTDArray<const SkPDFDevice*>& pageDevices,
5537c46cad21632cfc1411b08d73af37a1fffe2944halcanary        SkPDFSubstituteMap* substituteMap) {
5637c46cad21632cfc1411b08d73af37a1fffe2944halcanary    SkASSERT(substituteMap);
57a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
58a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    SkPDFGlyphSetMap usage;
592f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    for (int i = 0; i < pageDevices.count(); ++i) {
602f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        usage.merge(pageDevices[i]->getFontGlyphUsage());
61a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
62a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    SkPDFGlyphSetMap::F2BIter iterator(usage);
63a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    const SkPDFGlyphSetMap::FontGlyphSetPair* entry = iterator.next();
64a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    while (entry) {
65a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        SkAutoTUnref<SkPDFFont> subsetFont(
66a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                entry->fFont->getFontSubset(entry->fGlyphSet));
67a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        if (subsetFont) {
6837c46cad21632cfc1411b08d73af37a1fffe2944halcanary            substituteMap->setSubstitute(entry->fFont, subsetFont.get());
69a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        }
70a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        entry = iterator.next();
71a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
72a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary}
73a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
7472266fd1fe9bfe078239a9d9e85f479faee30281halcanarystatic SkPDFObject* create_pdf_page_content(const SkPDFDevice* pageDevice) {
7572266fd1fe9bfe078239a9d9e85f479faee30281halcanary    SkAutoTDelete<SkStreamAsset> content(pageDevice->content());
76385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary    return new SkPDFStream(content.get());
7772266fd1fe9bfe078239a9d9e85f479faee30281halcanary}
7872266fd1fe9bfe078239a9d9e85f479faee30281halcanary
792f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanarystatic SkPDFDict* create_pdf_page(const SkPDFDevice* pageDevice) {
80385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary    SkAutoTUnref<SkPDFDict> page(new SkPDFDict("Page"));
8172266fd1fe9bfe078239a9d9e85f479faee30281halcanary    page->insertObject("Resources", pageDevice->createResourceDict());
8272266fd1fe9bfe078239a9d9e85f479faee30281halcanary    page->insertObject("MediaBox", pageDevice->copyMediaBox());
83ef6c50a80f77e6da84e198a34755dd42b1b0cf1ewangxianzhu    SkAutoTUnref<SkPDFArray> annotations(new SkPDFArray);
84ef6c50a80f77e6da84e198a34755dd42b1b0cf1ewangxianzhu    pageDevice->appendAnnotations(annotations);
85ef6c50a80f77e6da84e198a34755dd42b1b0cf1ewangxianzhu    if (annotations->size() > 0) {
86ef6c50a80f77e6da84e198a34755dd42b1b0cf1ewangxianzhu        page->insertObject("Annots", annotations.detach());
872f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    }
8872266fd1fe9bfe078239a9d9e85f479faee30281halcanary    page->insertObjRef("Contents", create_pdf_page_content(pageDevice));
892f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    return page.detach();
902f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary}
912f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
922f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanarystatic void generate_page_tree(const SkTDArray<SkPDFDict*>& pages,
932f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                               SkTDArray<SkPDFDict*>* pageTree,
942f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                               SkPDFDict** rootNode) {
952f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // PDF wants a tree describing all the pages in the document.  We arbitrary
962f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // choose 8 (kNodeSize) as the number of allowed children.  The internal
972f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // nodes have type "Pages" with an array of children, a parent pointer, and
982f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // the number of leaves below the node as "Count."  The leaves are passed
992f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // into the method, have type "Page" and need a parent pointer. This method
1002f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // builds the tree bottom up, skipping internal nodes that would have only
1012f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // one child.
1022f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    static const int kNodeSize = 8;
1032f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1042f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // curNodes takes a reference to its items, which it passes to pageTree.
1052f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    SkTDArray<SkPDFDict*> curNodes;
1062f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    curNodes.setReserve(pages.count());
1072f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    for (int i = 0; i < pages.count(); i++) {
1082f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        SkSafeRef(pages[i]);
1092f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        curNodes.push(pages[i]);
1102f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    }
1112f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1122f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    // nextRoundNodes passes its references to nodes on to curNodes.
1132f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    SkTDArray<SkPDFDict*> nextRoundNodes;
1142f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize);
1152f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1162f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    int treeCapacity = kNodeSize;
1172f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    do {
1182f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        for (int i = 0; i < curNodes.count(); ) {
1192f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            if (i > 0 && i + 1 == curNodes.count()) {
1202f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                nextRoundNodes.push(curNodes[i]);
1212f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                break;
1222f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            }
1232f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
12472266fd1fe9bfe078239a9d9e85f479faee30281halcanary            SkAutoTUnref<SkPDFDict> newNode(new SkPDFDict("Pages"));
1252f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            SkAutoTUnref<SkPDFArray> kids(new SkPDFArray);
1262f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            kids->reserve(kNodeSize);
1272f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1282f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            int count = 0;
1292f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            for (; i < curNodes.count() && count < kNodeSize; i++, count++) {
13072266fd1fe9bfe078239a9d9e85f479faee30281halcanary                curNodes[i]->insertObjRef("Parent", SkRef(newNode.get()));
13172266fd1fe9bfe078239a9d9e85f479faee30281halcanary                kids->appendObjRef(SkRef(curNodes[i]));
1322f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1332f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                // TODO(vandebo): put the objects in strict access order.
1342f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                // Probably doesn't matter because they are so small.
1352f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                if (curNodes[i] != pages[0]) {
1362f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                    pageTree->push(curNodes[i]);  // Transfer reference.
1372f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                } else {
1382f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                    SkSafeUnref(curNodes[i]);
1392f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                }
1402f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            }
1412f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1422f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // treeCapacity is the number of leaf nodes possible for the
1432f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // current set of subtrees being generated. (i.e. 8, 64, 512, ...).
1442f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // It is hard to count the number of leaf nodes in the current
1452f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // subtree. However, by construction, we know that unless it's the
1462f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // last subtree for the current depth, the leaf count will be
1472f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // treeCapacity, otherwise it's what ever is left over after
1482f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            // consuming treeCapacity chunks.
1492f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            int pageCount = treeCapacity;
1502f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            if (i == curNodes.count()) {
1512f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary                pageCount = ((pages.count() - 1) % treeCapacity) + 1;
1522f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary            }
15372266fd1fe9bfe078239a9d9e85f479faee30281halcanary            newNode->insertInt("Count", pageCount);
15472266fd1fe9bfe078239a9d9e85f479faee30281halcanary            newNode->insertObject("Kids", kids.detach());
15572266fd1fe9bfe078239a9d9e85f479faee30281halcanary            nextRoundNodes.push(newNode.detach());  // Transfer reference.
1562f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        }
1572f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1582f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        curNodes = nextRoundNodes;
1592f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        nextRoundNodes.rewind();
1602f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        treeCapacity *= kNodeSize;
1612f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    } while (curNodes.count() > 1);
1622f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
1632f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    pageTree->push(curNodes[0]);  // Transfer reference.
1642f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    if (rootNode) {
1652f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        *rootNode = curNodes[0];
1662f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    }
1672f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary}
1682f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary
169a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanarystatic bool emit_pdf_document(const SkTDArray<const SkPDFDevice*>& pageDevices,
17034422610ac22adceeabb66023120f27b96cae953halcanary                              const SkPDFMetadata& metadata,
171a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                              SkWStream* stream) {
172a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    if (pageDevices.isEmpty()) {
173a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        return false;
174a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
175a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
1762f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    SkTDArray<SkPDFDict*> pages;
177385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary    SkAutoTUnref<SkPDFDict> dests(new SkPDFDict);
178a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
179a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    for (int i = 0; i < pageDevices.count(); i++) {
180a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        SkASSERT(pageDevices[i]);
181a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        SkASSERT(i == 0 ||
182a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                 pageDevices[i - 1]->getCanon() == pageDevices[i]->getCanon());
1832f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        SkAutoTUnref<SkPDFDict> page(create_pdf_page(pageDevices[i]));
1842f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary        pageDevices[i]->appendDestinations(dests, page.get());
185a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        pages.push(page.detach());
186a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
187a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
188385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary    SkAutoTUnref<SkPDFDict> docCatalog(new SkPDFDict("Catalog"));
189a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
19034422610ac22adceeabb66023120f27b96cae953halcanary    SkAutoTUnref<SkPDFObject> infoDict(
19134422610ac22adceeabb66023120f27b96cae953halcanary            metadata.createDocumentInformationDict());
19234422610ac22adceeabb66023120f27b96cae953halcanary
19334422610ac22adceeabb66023120f27b96cae953halcanary    SkAutoTUnref<SkPDFObject> id, xmp;
19434422610ac22adceeabb66023120f27b96cae953halcanary#ifdef SK_PDF_GENERATE_PDFA
19534422610ac22adceeabb66023120f27b96cae953halcanary    SkPDFMetadata::UUID uuid = metadata.uuid();
19634422610ac22adceeabb66023120f27b96cae953halcanary    // We use the same UUID for Document ID and Instance ID since this
19734422610ac22adceeabb66023120f27b96cae953halcanary    // is the first revision of this document (and Skia does not
19834422610ac22adceeabb66023120f27b96cae953halcanary    // support revising existing PDF documents).
19934422610ac22adceeabb66023120f27b96cae953halcanary    // If we are not in PDF/A mode, don't use a UUID since testing
20034422610ac22adceeabb66023120f27b96cae953halcanary    // works best with reproducible outputs.
20134422610ac22adceeabb66023120f27b96cae953halcanary    id.reset(SkPDFMetadata::CreatePdfId(uuid, uuid));
20234422610ac22adceeabb66023120f27b96cae953halcanary    xmp.reset(metadata.createXMPObject(uuid, uuid));
20334422610ac22adceeabb66023120f27b96cae953halcanary    docCatalog->insertObjRef("Metadata", xmp.detach());
20434422610ac22adceeabb66023120f27b96cae953halcanary
20534422610ac22adceeabb66023120f27b96cae953halcanary    // sRGB is specified by HTML, CSS, and SVG.
20634422610ac22adceeabb66023120f27b96cae953halcanary    SkAutoTUnref<SkPDFDict> outputIntent(new SkPDFDict("OutputIntent"));
20734422610ac22adceeabb66023120f27b96cae953halcanary    outputIntent->insertName("S", "GTS_PDFA1");
20834422610ac22adceeabb66023120f27b96cae953halcanary    outputIntent->insertString("RegistryName", "http://www.color.org");
20934422610ac22adceeabb66023120f27b96cae953halcanary    outputIntent->insertString("OutputConditionIdentifier",
21034422610ac22adceeabb66023120f27b96cae953halcanary                               "sRGB IEC61966-2.1");
21134422610ac22adceeabb66023120f27b96cae953halcanary    SkAutoTUnref<SkPDFArray> intentArray(new SkPDFArray);
21234422610ac22adceeabb66023120f27b96cae953halcanary    intentArray->appendObject(outputIntent.detach());
21334422610ac22adceeabb66023120f27b96cae953halcanary    // Don't specify OutputIntents if we are not in PDF/A mode since
21434422610ac22adceeabb66023120f27b96cae953halcanary    // no one has ever asked for this feature.
21534422610ac22adceeabb66023120f27b96cae953halcanary    docCatalog->insertObject("OutputIntents", intentArray.detach());
21634422610ac22adceeabb66023120f27b96cae953halcanary#endif
21734422610ac22adceeabb66023120f27b96cae953halcanary
21834422610ac22adceeabb66023120f27b96cae953halcanary    SkTDArray<SkPDFDict*> pageTree;
219a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    SkPDFDict* pageTreeRoot;
2202f7ebcb424cd1d1acf07478157f86b0a3eafd712halcanary    generate_page_tree(pages, &pageTree, &pageTreeRoot);
22172266fd1fe9bfe078239a9d9e85f479faee30281halcanary    docCatalog->insertObjRef("Pages", SkRef(pageTreeRoot));
222a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
22372266fd1fe9bfe078239a9d9e85f479faee30281halcanary    if (dests->size() > 0) {
22472266fd1fe9bfe078239a9d9e85f479faee30281halcanary        docCatalog->insertObjRef("Dests", dests.detach());
22572266fd1fe9bfe078239a9d9e85f479faee30281halcanary    }
226a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
227a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    // Build font subsetting info before proceeding.
22837c46cad21632cfc1411b08d73af37a1fffe2944halcanary    SkPDFSubstituteMap substitutes;
22937c46cad21632cfc1411b08d73af37a1fffe2944halcanary    perform_font_subsetting(pageDevices, &substitutes);
230a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
23137c46cad21632cfc1411b08d73af37a1fffe2944halcanary    SkPDFObjNumMap objNumMap;
23234422610ac22adceeabb66023120f27b96cae953halcanary    objNumMap.addObjectRecursively(infoDict, substitutes);
23334422610ac22adceeabb66023120f27b96cae953halcanary    objNumMap.addObjectRecursively(docCatalog.get(), substitutes);
2344f2b33159b5a5da869ed605abf1d73b5cd429934halcanary    size_t baseOffset = stream->bytesWritten();
235a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    emit_pdf_header(stream);
236a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    SkTDArray<int32_t> offsets;
23737c46cad21632cfc1411b08d73af37a1fffe2944halcanary    for (int i = 0; i < objNumMap.objects().count(); ++i) {
23837c46cad21632cfc1411b08d73af37a1fffe2944halcanary        SkPDFObject* object = objNumMap.objects()[i];
2394f2b33159b5a5da869ed605abf1d73b5cd429934halcanary        size_t offset = stream->bytesWritten();
2404f2b33159b5a5da869ed605abf1d73b5cd429934halcanary        // This assert checks that size(pdf_header) > 0 and that
2414f2b33159b5a5da869ed605abf1d73b5cd429934halcanary        // the output stream correctly reports bytesWritten().
2424f2b33159b5a5da869ed605abf1d73b5cd429934halcanary        SkASSERT(offset > baseOffset);
2434f2b33159b5a5da869ed605abf1d73b5cd429934halcanary        offsets.push(SkToS32(offset - baseOffset));
24437c46cad21632cfc1411b08d73af37a1fffe2944halcanary        SkASSERT(object == substitutes.getSubstitute(object));
24537c46cad21632cfc1411b08d73af37a1fffe2944halcanary        SkASSERT(objNumMap.getObjectNumber(object) == i + 1);
246a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        stream->writeDecAsText(i + 1);
247a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        stream->writeText(" 0 obj\n");  // Generation number is always 0.
24837c46cad21632cfc1411b08d73af37a1fffe2944halcanary        object->emitObject(stream, objNumMap, substitutes);
249a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        stream->writeText("\nendobj\n");
250a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
251a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    int32_t xRefFileOffset = SkToS32(stream->bytesWritten() - baseOffset);
252a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
25341f88f0251cf48eb06f3f8d143aac8562a230889halcanary    // Include the zeroth object in the count.
254a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    int32_t objCount = SkToS32(offsets.count() + 1);
255a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
256a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("xref\n0 ");
25741f88f0251cf48eb06f3f8d143aac8562a230889halcanary    stream->writeDecAsText(objCount);
258a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    stream->writeText("\n0000000000 65535 f \n");
259a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    for (int i = 0; i < offsets.count(); i++) {
260a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        SkASSERT(offsets[i] > 0);
261a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        stream->writeBigDecAsText(offsets[i], 10);
262a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        stream->writeText(" 00000 n \n");
263a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
26437c46cad21632cfc1411b08d73af37a1fffe2944halcanary    emit_pdf_footer(stream, objNumMap, substitutes, docCatalog.get(), objCount,
26534422610ac22adceeabb66023120f27b96cae953halcanary                    xRefFileOffset, infoDict.detach(), id.detach());
266a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
267a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    // The page tree has both child and parent pointers, so it creates a
268a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    // reference cycle.  We must clear that cycle to properly reclaim memory.
269a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    for (int i = 0; i < pageTree.count(); i++) {
270a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        pageTree[i]->clear();
271a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
272a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    pageTree.safeUnrefAll();
273a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    pages.unrefAll();
274a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    return true;
275a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary}
276a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
277a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary#if 0
278a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary// TODO(halcanary): expose notEmbeddableCount in SkDocument
279a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanaryvoid GetCountOfFontTypes(
280a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        const SkTDArray<SkPDFDevice*>& pageDevices,
281a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1],
282a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        int* notSubsettableCount,
283a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        int* notEmbeddableCount) {
284a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    sk_bzero(counts, sizeof(int) *
285a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                     (SkAdvancedTypefaceMetrics::kOther_Font + 1));
286a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    SkTDArray<SkFontID> seenFonts;
287a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    int notSubsettable = 0;
288a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    int notEmbeddable = 0;
289a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
290a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    for (int pageNumber = 0; pageNumber < pageDevices.count(); pageNumber++) {
291a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        const SkTDArray<SkPDFFont*>& fontResources =
292a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                pageDevices[pageNumber]->getFontResources();
293a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        for (int font = 0; font < fontResources.count(); font++) {
294a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary            SkFontID fontID = fontResources[font]->typeface()->uniqueID();
295a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary            if (seenFonts.find(fontID) == -1) {
296a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                counts[fontResources[font]->getType()]++;
297a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                seenFonts.push(fontID);
298a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                if (!fontResources[font]->canSubset()) {
299a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                    notSubsettable++;
300a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                }
301a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                if (!fontResources[font]->canEmbed()) {
302a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                    notEmbeddable++;
303a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary                }
304a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary            }
305a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        }
306a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
307a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    if (notSubsettableCount) {
308a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        *notSubsettableCount = notSubsettable;
309a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary
310a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
311a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    if (notEmbeddableCount) {
312a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary        *notEmbeddableCount = notEmbeddable;
313a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary    }
314a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary}
315a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary#endif
316f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary
317f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanarytemplate <typename T> static T* clone(const T* o) { return o ? new T(*o) : nullptr; }
318a43b41538ad6af0f2b7742b4ade8265d344b9d64halcanary////////////////////////////////////////////////////////////////////////////////
31999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
3207a0118465a2dd1783d7b19b026bedd7e13afadf2halcanarynamespace {
32199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comclass SkDocument_PDF : public SkDocument {
32299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.compublic:
3238c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary    SkDocument_PDF(SkWStream* stream,
324792c80f5a7b66e75d42664ccb298f31962c6654chalcanary                   void (*doneProc)(SkWStream*, bool),
325712fdf7603c62820b21174da9b0a2071c174936bhalcanary                   SkScalar rasterDpi,
326712fdf7603c62820b21174da9b0a2071c174936bhalcanary                   SkPixelSerializer* jpegEncoder)
3278c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary        : SkDocument(stream, doneProc)
328712fdf7603c62820b21174da9b0a2071c174936bhalcanary        , fRasterDpi(rasterDpi) {
329712fdf7603c62820b21174da9b0a2071c174936bhalcanary        fCanon.fPixelSerializer.reset(SkSafeRef(jpegEncoder));
330712fdf7603c62820b21174da9b0a2071c174936bhalcanary    }
3316319367bdcbf5e5050632ab97973f5035d0dd8faskia.committer@gmail.com
33299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    virtual ~SkDocument_PDF() {
33399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        // subclasses must call close() in their destructors
33499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        this->close();
33599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
33699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
33799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comprotected:
338f4219ddf31b2171a5576ab98d1fd2deab4d816fdtfarina    SkCanvas* onBeginPage(SkScalar width, SkScalar height,
339f4219ddf31b2171a5576ab98d1fd2deab4d816fdtfarina                          const SkRect& trimBox) override {
340a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary        SkASSERT(!fCanvas.get());
34199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
342a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary        SkISize pageSize = SkISize::Make(
343a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary                SkScalarRoundToInt(width), SkScalarRoundToInt(height));
3447a0118465a2dd1783d7b19b026bedd7e13afadf2halcanary        SkAutoTUnref<SkPDFDevice> device(
3457a0118465a2dd1783d7b19b026bedd7e13afadf2halcanary                SkPDFDevice::Create(pageSize, fRasterDpi, &fCanon));
346385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary        fCanvas.reset(new SkCanvas(device.get()));
3477a0118465a2dd1783d7b19b026bedd7e13afadf2halcanary        fPageDevices.push(device.detach());
348be519ad718371cc40c432572173e47f851476486halcanary        fCanvas->clipRect(trimBox);
34993f816129356d6764a9402817cc36bffd8f991fbhalcanary        fCanvas->translate(trimBox.x(), trimBox.y());
350a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary        return fCanvas.get();
35199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
35299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
35336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onEndPage() override {
354a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary        SkASSERT(fCanvas.get());
35599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        fCanvas->flush();
35696fcdcc219d2a0d3579719b84b28bede76efba64halcanary        fCanvas.reset(nullptr);
35799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
35899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
35936352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    bool onClose(SkWStream* stream) override {
360a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary        SkASSERT(!fCanvas.get());
36199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
362f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary        bool success = emit_pdf_document(fPageDevices, fMetadata, stream);
3637a0118465a2dd1783d7b19b026bedd7e13afadf2halcanary        fPageDevices.unrefAll();
3642e3f9d8a9309686eeb4c76ccfde5800da87a68b3halcanary        fCanon.reset();
365b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org        return success;
366b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org    }
367b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org
36836352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onAbort() override {
3697a0118465a2dd1783d7b19b026bedd7e13afadf2halcanary        fPageDevices.unrefAll();
3702e3f9d8a9309686eeb4c76ccfde5800da87a68b3halcanary        fCanon.reset();
37199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
37299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
3737001576c7160e20c9c9db2fbe947408989027d66halcanary    void setMetadata(const SkDocument::Attribute info[],
3747001576c7160e20c9c9db2fbe947408989027d66halcanary                     int infoCount,
375f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary                     const SkTime::DateTime* creationDate,
376f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary                     const SkTime::DateTime* modifiedDate) override {
3777001576c7160e20c9c9db2fbe947408989027d66halcanary        fMetadata.fInfo.reset(info, infoCount);
378f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary        fMetadata.fCreation.reset(clone(creationDate));
379f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary        fMetadata.fModified.reset(clone(modifiedDate));
380f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary    }
381f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary
38299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comprivate:
383a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary    SkPDFCanon fCanon;
3846d622703e578eddc64ab4e3340d0ab0033268799halcanary    SkTDArray<const SkPDFDevice*> fPageDevices;
385a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary    SkAutoTUnref<SkCanvas> fCanvas;
386a1f1ee98a1f6d0770f6243270ca2f0e6c92efabahalcanary    SkScalar fRasterDpi;
38734422610ac22adceeabb66023120f27b96cae953halcanary    SkPDFMetadata fMetadata;
38899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com};
3897a0118465a2dd1783d7b19b026bedd7e13afadf2halcanary}  // namespace
39099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com///////////////////////////////////////////////////////////////////////////////
39199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
3928c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanarySkDocument* SkDocument::CreatePDF(SkWStream* stream, SkScalar dpi) {
393712fdf7603c62820b21174da9b0a2071c174936bhalcanary    return stream ? new SkDocument_PDF(stream, nullptr, dpi, nullptr) : nullptr;
394712fdf7603c62820b21174da9b0a2071c174936bhalcanary}
395712fdf7603c62820b21174da9b0a2071c174936bhalcanary
396712fdf7603c62820b21174da9b0a2071c174936bhalcanarySkDocument* SkDocument::CreatePDF(SkWStream* stream,
397712fdf7603c62820b21174da9b0a2071c174936bhalcanary                                  SkScalar dpi,
398712fdf7603c62820b21174da9b0a2071c174936bhalcanary                                  SkPixelSerializer* jpegEncoder) {
399712fdf7603c62820b21174da9b0a2071c174936bhalcanary    return stream
400712fdf7603c62820b21174da9b0a2071c174936bhalcanary        ? new SkDocument_PDF(stream, nullptr, dpi, jpegEncoder)
401712fdf7603c62820b21174da9b0a2071c174936bhalcanary        : nullptr;
40299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
40399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
4048c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanarySkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) {
405385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary    SkFILEWStream* stream = new SkFILEWStream(path);
40699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    if (!stream->isValid()) {
407385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary        delete stream;
40896fcdcc219d2a0d3579719b84b28bede76efba64halcanary        return nullptr;
40999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
410385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary    auto delete_wstream = [](SkWStream* stream, bool) { delete stream; };
411712fdf7603c62820b21174da9b0a2071c174936bhalcanary    return new SkDocument_PDF(stream, delete_wstream, dpi, nullptr);
41299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
413