blob: 5198e7b0acb36452116e5d6ca69328fe5b62dcdf [file] [log] [blame]
halcanaryfb62b3d2015-01-21 09:59:14 -08001/*
2 * Copyright 2015 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#ifndef SkPDFCanon_DEFINED
8#define SkPDFCanon_DEFINED
9
halcanarybe27a112015-04-01 13:31:19 -070010#include "SkPDFGraphicState.h"
halcanaryfb62b3d2015-01-21 09:59:14 -080011#include "SkPDFShader.h"
halcanaryfb62b3d2015-01-21 09:59:14 -080012#include "SkTDArray.h"
halcanarybe27a112015-04-01 13:31:19 -070013#include "SkTHash.h"
halcanaryfb62b3d2015-01-21 09:59:14 -080014
halcanary1b5c6042015-02-18 11:29:56 -080015class SkBitmap;
halcanaryfb62b3d2015-01-21 09:59:14 -080016class SkPDFFont;
halcanary1b5c6042015-02-18 11:29:56 -080017class SkPDFBitmap;
halcanaryfb62b3d2015-01-21 09:59:14 -080018class SkPaint;
halcanaryfb62b3d2015-01-21 09:59:14 -080019
halcanary792c80f2015-02-20 07:21:05 -080020/**
21 * The SkPDFCanon canonicalizes objects across PDF pages(SkPDFDevices).
22 *
23 * The PDF backend works correctly if:
24 * - There is no more than one SkPDFCanon for each thread.
25 * - Every SkPDFDevice is given a pointer to a SkPDFCanon on creation.
26 * - All SkPDFDevices in a document share the same SkPDFCanon.
27 * The SkDocument_PDF class makes this happen by owning a single
28 * SkPDFCanon.
29 *
halcanary2e3f9d82015-02-27 12:41:03 -080030 * The addFoo() methods will ref the Foo; the canon's destructor will
31 * call foo->unref() on all of these objects.
32 *
33 * The findFoo() methods do not change the ref count of the Foo
34 * objects.
halcanary792c80f2015-02-20 07:21:05 -080035 */
halcanaryfb62b3d2015-01-21 09:59:14 -080036class SkPDFCanon : SkNoncopyable {
37public:
halcanary2e3f9d82015-02-27 12:41:03 -080038 ~SkPDFCanon() { this->reset(); }
39
40 // reset to original setting, unrefs all objects.
41 void reset();
halcanaryfb62b3d2015-01-21 09:59:14 -080042
halcanaryfb62b3d2015-01-21 09:59:14 -080043 // Returns exact match if there is one. If not, it returns NULL.
44 // If there is no exact match, but there is a related font, we
45 // still return NULL, but also set *relatedFont.
46 SkPDFFont* findFont(uint32_t fontID,
47 uint16_t glyphID,
48 SkPDFFont** relatedFont) const;
49 void addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID);
halcanaryfb62b3d2015-01-21 09:59:14 -080050
halcanary530ea8e2015-01-23 06:17:35 -080051 SkPDFFunctionShader* findFunctionShader(const SkPDFShader::State&) const;
52 void addFunctionShader(SkPDFFunctionShader*);
halcanary530ea8e2015-01-23 06:17:35 -080053
54 SkPDFAlphaFunctionShader* findAlphaShader(const SkPDFShader::State&) const;
55 void addAlphaShader(SkPDFAlphaFunctionShader*);
halcanary530ea8e2015-01-23 06:17:35 -080056
57 SkPDFImageShader* findImageShader(const SkPDFShader::State&) const;
58 void addImageShader(SkPDFImageShader*);
halcanaryfb62b3d2015-01-21 09:59:14 -080059
halcanarybe27a112015-04-01 13:31:19 -070060 const SkPDFGraphicState* findGraphicState(const SkPDFGraphicState&) const;
61 void addGraphicState(const SkPDFGraphicState*);
halcanaryfb62b3d2015-01-21 09:59:14 -080062
halcanary1b5c6042015-02-18 11:29:56 -080063 SkPDFBitmap* findBitmap(const SkBitmap&) const;
64 void addBitmap(SkPDFBitmap*);
halcanarya1f1ee92015-02-20 06:17:26 -080065
halcanaryfb62b3d2015-01-21 09:59:14 -080066private:
67 struct FontRec {
68 SkPDFFont* fFont;
69 uint32_t fFontID;
70 uint16_t fGlyphID;
71 };
72 SkTDArray<FontRec> fFontRecords;
73
halcanary530ea8e2015-01-23 06:17:35 -080074 SkTDArray<SkPDFFunctionShader*> fFunctionShaderRecords;
75
76 SkTDArray<SkPDFAlphaFunctionShader*> fAlphaShaderRecords;
77
78 SkTDArray<SkPDFImageShader*> fImageShaderRecords;
halcanaryfb62b3d2015-01-21 09:59:14 -080079
halcanarybe27a112015-04-01 13:31:19 -070080 struct WrapGS {
81 explicit WrapGS(const SkPDFGraphicState* ptr = NULL) : fPtr(ptr) {}
82 const SkPDFGraphicState* fPtr;
83 bool operator==(const WrapGS& rhs) const {
84 SkASSERT(fPtr);
85 SkASSERT(rhs.fPtr);
86 return *fPtr == *rhs.fPtr;
87 }
88 static uint32_t Hash(const WrapGS& w) {
89 SkASSERT(w.fPtr);
90 return w.fPtr->hash();
91 }
92 };
93 SkTHashSet<WrapGS, WrapGS::Hash> fGraphicStateRecords;
halcanary1b5c6042015-02-18 11:29:56 -080094
95 SkTDArray<SkPDFBitmap*> fBitmapRecords;
halcanaryfb62b3d2015-01-21 09:59:14 -080096};
97#endif // SkPDFCanon_DEFINED