OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The Android Open Source Project | 2 * Copyright 2010 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #ifndef SkPDFTypes_DEFINED | 9 #ifndef SkPDFTypes_DEFINED |
10 #define SkPDFTypes_DEFINED | 10 #define SkPDFTypes_DEFINED |
11 | 11 |
12 #include "SkRefCnt.h" | 12 #include "SkRefCnt.h" |
13 #include "SkScalar.h" | 13 #include "SkScalar.h" |
14 #include "SkTHash.h" | 14 #include "SkTHash.h" |
15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
16 | 16 |
17 class SkData; | 17 class SkData; |
18 class SkPDFObjNumMap; | 18 class SkPDFObjNumMap; |
19 class SkPDFObject; | 19 class SkPDFObject; |
20 class SkPDFSubstituteMap; | |
21 class SkStreamAsset; | 20 class SkStreamAsset; |
22 class SkString; | 21 class SkString; |
23 class SkWStream; | 22 class SkWStream; |
24 | 23 |
25 #ifdef SK_PDF_IMAGE_STATS | 24 #ifdef SK_PDF_IMAGE_STATS |
26 #include "SkAtomics.h" | 25 #include "SkAtomics.h" |
27 #endif | 26 #endif |
28 | 27 |
29 /** \class SkPDFObject | 28 /** \class SkPDFObject |
30 | 29 |
31 A PDF Object is the base class for primitive elements in a PDF file. A | 30 A PDF Object is the base class for primitive elements in a PDF file. A |
32 common subtype is used to ease the use of indirect object references, | 31 common subtype is used to ease the use of indirect object references, |
33 which are common in the PDF format. | 32 which are common in the PDF format. |
34 | 33 |
35 */ | 34 */ |
36 class SkPDFObject : public SkRefCnt { | 35 class SkPDFObject : public SkRefCnt { |
37 public: | 36 public: |
38 /** Subclasses must implement this method to print the object to the | 37 /** Subclasses must implement this method to print the object to the |
39 * PDF file. | 38 * PDF file. |
40 * @param catalog The object catalog to use. | 39 * @param catalog The object catalog to use. |
41 * @param stream The writable output stream to send the output to. | 40 * @param stream The writable output stream to send the output to. |
42 */ | 41 */ |
43 virtual void emitObject(SkWStream* stream, | 42 virtual void emitObject(SkWStream* stream, |
44 const SkPDFObjNumMap& objNumMap, | 43 const SkPDFObjNumMap& objNumMap) const = 0; |
45 const SkPDFSubstituteMap& substitutes) const = 0; | |
46 | 44 |
47 /** | 45 /** |
48 * Adds all transitive dependencies of this object to the | 46 * Adds all transitive dependencies of this object to the |
49 * catalog. Implementations should respect the catalog's object | 47 * catalog. Implementations should respect the catalog's object |
50 * substitution map. | 48 * substitution map. |
51 */ | 49 */ |
52 virtual void addResources(SkPDFObjNumMap* catalog, | 50 virtual void addResources(SkPDFObjNumMap* catalog) const {} |
53 const SkPDFSubstituteMap& substitutes) const {} | |
54 | 51 |
55 /** | 52 /** |
56 * Release all resources associated with this SkPDFObject. It is | 53 * Release all resources associated with this SkPDFObject. It is |
57 * an error to call emitObject() or addResources() after calling | 54 * an error to call emitObject() or addResources() after calling |
58 * drop(). | 55 * drop(). |
59 */ | 56 */ |
60 virtual void drop() {} | 57 virtual void drop() {} |
61 | 58 |
62 virtual ~SkPDFObject() {} | 59 virtual ~SkPDFObject() {} |
63 private: | 60 private: |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 static SkPDFUnion Name(const SkString&); | 111 static SkPDFUnion Name(const SkString&); |
115 | 112 |
116 /** SkPDFUnion::String will encode the passed string. */ | 113 /** SkPDFUnion::String will encode the passed string. */ |
117 static SkPDFUnion String(const SkString&); | 114 static SkPDFUnion String(const SkString&); |
118 | 115 |
119 static SkPDFUnion Object(sk_sp<SkPDFObject>); | 116 static SkPDFUnion Object(sk_sp<SkPDFObject>); |
120 static SkPDFUnion ObjRef(sk_sp<SkPDFObject>); | 117 static SkPDFUnion ObjRef(sk_sp<SkPDFObject>); |
121 | 118 |
122 /** These two non-virtual methods mirror SkPDFObject's | 119 /** These two non-virtual methods mirror SkPDFObject's |
123 corresponding virtuals. */ | 120 corresponding virtuals. */ |
124 void emitObject(SkWStream*, | 121 void emitObject(SkWStream*, const SkPDFObjNumMap&) const; |
125 const SkPDFObjNumMap&, | 122 void addResources(SkPDFObjNumMap*) const; |
126 const SkPDFSubstituteMap&) const; | |
127 void addResources(SkPDFObjNumMap*, const SkPDFSubstituteMap&) const; | |
128 | 123 |
129 bool isName() const; | 124 bool isName() const; |
130 | 125 |
131 private: | 126 private: |
132 union { | 127 union { |
133 int32_t fIntValue; | 128 int32_t fIntValue; |
134 bool fBoolValue; | 129 bool fBoolValue; |
135 SkScalar fScalarValue; | 130 SkScalar fScalarValue; |
136 const char* fStaticString; | 131 const char* fStaticString; |
137 char fSkString[sizeof(SkString)]; | 132 char fSkString[sizeof(SkString)]; |
(...skipping 26 matching lines...) Expand all Loading... |
164 | 159 |
165 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
166 | 161 |
167 #if 0 // Enable if needed. | 162 #if 0 // Enable if needed. |
168 /** This class is a SkPDFUnion with SkPDFObject virtuals attached. | 163 /** This class is a SkPDFUnion with SkPDFObject virtuals attached. |
169 The only use case of this is when a non-compound PDF object is | 164 The only use case of this is when a non-compound PDF object is |
170 referenced indirectly. */ | 165 referenced indirectly. */ |
171 class SkPDFAtom final : public SkPDFObject { | 166 class SkPDFAtom final : public SkPDFObject { |
172 public: | 167 public: |
173 void emitObject(SkWStream* stream, | 168 void emitObject(SkWStream* stream, |
174 const SkPDFObjNumMap& objNumMap, | 169 const SkPDFObjNumMap& objNumMap) final; |
175 const SkPDFSubstituteMap& substitutes) final; | 170 void addResources(SkPDFObjNumMap* const final; |
176 void addResources(SkPDFObjNumMap*, const SkPDFSubstituteMap&) const final; | |
177 SkPDFAtom(SkPDFUnion&& v) : fValue(std::move(v) {} | 171 SkPDFAtom(SkPDFUnion&& v) : fValue(std::move(v) {} |
178 | 172 |
179 private: | 173 private: |
180 const SkPDFUnion fValue; | 174 const SkPDFUnion fValue; |
181 typedef SkPDFObject INHERITED; | 175 typedef SkPDFObject INHERITED; |
182 }; | 176 }; |
183 #endif // 0 | 177 #endif // 0 |
184 | 178 |
185 //////////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////////// |
186 | 180 |
187 /** \class SkPDFArray | 181 /** \class SkPDFArray |
188 | 182 |
189 An array object in a PDF. | 183 An array object in a PDF. |
190 */ | 184 */ |
191 class SkPDFArray final : public SkPDFObject { | 185 class SkPDFArray final : public SkPDFObject { |
192 public: | 186 public: |
193 /** Create a PDF array. Maximum length is 8191. | 187 /** Create a PDF array. Maximum length is 8191. |
194 */ | 188 */ |
195 SkPDFArray(); | 189 SkPDFArray(); |
196 virtual ~SkPDFArray(); | 190 virtual ~SkPDFArray(); |
197 | 191 |
198 // The SkPDFObject interface. | 192 // The SkPDFObject interface. |
199 void emitObject(SkWStream* stream, | 193 void emitObject(SkWStream* stream, |
200 const SkPDFObjNumMap& objNumMap, | 194 const SkPDFObjNumMap& objNumMap) const override; |
201 const SkPDFSubstituteMap& substitutes) const override; | 195 void addResources(SkPDFObjNumMap*) const override; |
202 void addResources(SkPDFObjNumMap*, | |
203 const SkPDFSubstituteMap&) const override; | |
204 void drop() override; | 196 void drop() override; |
205 | 197 |
206 /** The size of the array. | 198 /** The size of the array. |
207 */ | 199 */ |
208 int size() const; | 200 int size() const; |
209 | 201 |
210 /** Preallocate space for the given number of entries. | 202 /** Preallocate space for the given number of entries. |
211 * @param length The number of array slots to preallocate. | 203 * @param length The number of array slots to preallocate. |
212 */ | 204 */ |
213 void reserve(int length); | 205 void reserve(int length); |
(...skipping 26 matching lines...) Expand all Loading... |
240 public: | 232 public: |
241 /** Create a PDF dictionary. | 233 /** Create a PDF dictionary. |
242 * @param type The value of the Type entry, nullptr for no type. | 234 * @param type The value of the Type entry, nullptr for no type. |
243 */ | 235 */ |
244 explicit SkPDFDict(const char type[] = nullptr); | 236 explicit SkPDFDict(const char type[] = nullptr); |
245 | 237 |
246 virtual ~SkPDFDict(); | 238 virtual ~SkPDFDict(); |
247 | 239 |
248 // The SkPDFObject interface. | 240 // The SkPDFObject interface. |
249 void emitObject(SkWStream* stream, | 241 void emitObject(SkWStream* stream, |
250 const SkPDFObjNumMap& objNumMap, | 242 const SkPDFObjNumMap& objNumMap) const override; |
251 const SkPDFSubstituteMap& substitutes) const override; | 243 void addResources(SkPDFObjNumMap*) const override; |
252 void addResources(SkPDFObjNumMap*, | |
253 const SkPDFSubstituteMap&) const override; | |
254 void drop() override; | 244 void drop() override; |
255 | 245 |
256 /** The size of the dictionary. | 246 /** The size of the dictionary. |
257 */ | 247 */ |
258 int size() const; | 248 int size() const; |
259 | 249 |
260 /** Add the value to the dictionary with the given key. | 250 /** Add the value to the dictionary with the given key. |
261 * @param key The text of the key for this dictionary entry. | 251 * @param key The text of the key for this dictionary entry. |
262 * @param value The value for this dictionary entry. | 252 * @param value The value for this dictionary entry. |
263 */ | 253 */ |
(...skipping 11 matching lines...) Expand all Loading... |
275 void insertInt(const char key[], size_t value); | 265 void insertInt(const char key[], size_t value); |
276 void insertScalar(const char key[], SkScalar value); | 266 void insertScalar(const char key[], SkScalar value); |
277 void insertName(const char key[], const char nameValue[]); | 267 void insertName(const char key[], const char nameValue[]); |
278 void insertName(const char key[], const SkString& nameValue); | 268 void insertName(const char key[], const SkString& nameValue); |
279 void insertString(const char key[], const char value[]); | 269 void insertString(const char key[], const char value[]); |
280 void insertString(const char key[], const SkString& value); | 270 void insertString(const char key[], const SkString& value); |
281 | 271 |
282 /** Emit the dictionary, without the "<<" and ">>". | 272 /** Emit the dictionary, without the "<<" and ">>". |
283 */ | 273 */ |
284 void emitAll(SkWStream* stream, | 274 void emitAll(SkWStream* stream, |
285 const SkPDFObjNumMap& objNumMap, | 275 const SkPDFObjNumMap& objNumMap) const; |
286 const SkPDFSubstituteMap& substitutes) const; | |
287 | 276 |
288 private: | 277 private: |
289 struct Record { | 278 struct Record { |
290 SkPDFUnion fKey; | 279 SkPDFUnion fKey; |
291 SkPDFUnion fValue; | 280 SkPDFUnion fValue; |
292 Record(SkPDFUnion&&, SkPDFUnion&&); | 281 Record(SkPDFUnion&&, SkPDFUnion&&); |
293 Record(Record&&) = default; | 282 Record(Record&&) = default; |
294 Record& operator=(Record&&) = default; | 283 Record& operator=(Record&&) = default; |
295 Record(const Record&) = delete; | 284 Record(const Record&) = delete; |
296 Record& operator=(const Record&) = delete; | 285 Record& operator=(const Record&) = delete; |
297 }; | 286 }; |
298 SkTArray<Record> fRecords; | 287 SkTArray<Record> fRecords; |
299 SkDEBUGCODE(bool fDumped;) | 288 SkDEBUGCODE(bool fDumped;) |
300 }; | 289 }; |
301 | 290 |
302 /** \class SkPDFSharedStream | 291 /** \class SkPDFSharedStream |
303 | 292 |
304 This class takes an asset and assumes that it is backed by | 293 This class takes an asset and assumes that it is backed by |
305 long-lived shared data (for example, an open file | 294 long-lived shared data (for example, an open file |
306 descriptor). That is: no memory savings can be made by holding on | 295 descriptor). That is: no memory savings can be made by holding on |
307 to a compressed version instead. | 296 to a compressed version instead. |
308 */ | 297 */ |
309 class SkPDFSharedStream final : public SkPDFObject { | 298 class SkPDFSharedStream final : public SkPDFObject { |
310 public: | 299 public: |
311 SkPDFSharedStream(std::unique_ptr<SkStreamAsset> data); | 300 SkPDFSharedStream(std::unique_ptr<SkStreamAsset> data); |
312 ~SkPDFSharedStream(); | 301 ~SkPDFSharedStream(); |
313 SkPDFDict* dict() { return &fDict; } | 302 SkPDFDict* dict() { return &fDict; } |
314 void emitObject(SkWStream*, | 303 void emitObject(SkWStream*, |
315 const SkPDFObjNumMap&, | 304 const SkPDFObjNumMap&) const override; |
316 const SkPDFSubstituteMap&) const override; | 305 void addResources(SkPDFObjNumMap*) const override; |
317 void addResources(SkPDFObjNumMap*, | |
318 const SkPDFSubstituteMap&) const override; | |
319 void drop() override; | 306 void drop() override; |
320 | 307 |
321 private: | 308 private: |
322 std::unique_ptr<SkStreamAsset> fAsset; | 309 std::unique_ptr<SkStreamAsset> fAsset; |
323 SkPDFDict fDict; | 310 SkPDFDict fDict; |
324 typedef SkPDFObject INHERITED; | 311 typedef SkPDFObject INHERITED; |
325 }; | 312 }; |
326 | 313 |
327 /** \class SkPDFStream | 314 /** \class SkPDFStream |
328 | 315 |
(...skipping 10 matching lines...) Expand all Loading... |
339 * @param data The data part of the stream. | 326 * @param data The data part of the stream. |
340 * @param stream The data part of the stream. */ | 327 * @param stream The data part of the stream. */ |
341 explicit SkPDFStream(sk_sp<SkData> data); | 328 explicit SkPDFStream(sk_sp<SkData> data); |
342 explicit SkPDFStream(std::unique_ptr<SkStreamAsset> stream); | 329 explicit SkPDFStream(std::unique_ptr<SkStreamAsset> stream); |
343 virtual ~SkPDFStream(); | 330 virtual ~SkPDFStream(); |
344 | 331 |
345 SkPDFDict* dict() { return &fDict; } | 332 SkPDFDict* dict() { return &fDict; } |
346 | 333 |
347 // The SkPDFObject interface. | 334 // The SkPDFObject interface. |
348 void emitObject(SkWStream* stream, | 335 void emitObject(SkWStream* stream, |
349 const SkPDFObjNumMap& objNumMap, | 336 const SkPDFObjNumMap& objNumMap) const override; |
350 const SkPDFSubstituteMap& substitutes) const override; | 337 void addResources(SkPDFObjNumMap*) const final; |
351 void addResources(SkPDFObjNumMap*, const SkPDFSubstituteMap&) const final; | |
352 void drop() override; | 338 void drop() override; |
353 | 339 |
354 protected: | 340 protected: |
355 /* Create a PDF stream with no data. The setData method must be called to | 341 /* Create a PDF stream with no data. The setData method must be called to |
356 * set the data. */ | 342 * set the data. */ |
357 SkPDFStream(); | 343 SkPDFStream(); |
358 | 344 |
359 /** Only call this function once. */ | 345 /** Only call this function once. */ |
360 void setData(std::unique_ptr<SkStreamAsset> stream); | 346 void setData(std::unique_ptr<SkStreamAsset> stream); |
361 | 347 |
(...skipping 14 matching lines...) Expand all Loading... |
376 class SkPDFObjNumMap : SkNoncopyable { | 362 class SkPDFObjNumMap : SkNoncopyable { |
377 public: | 363 public: |
378 /** Add the passed object to the catalog. | 364 /** Add the passed object to the catalog. |
379 * @param obj The object to add. | 365 * @param obj The object to add. |
380 * @return True iff the object was not already added to the catalog. | 366 * @return True iff the object was not already added to the catalog. |
381 */ | 367 */ |
382 bool addObject(SkPDFObject* obj); | 368 bool addObject(SkPDFObject* obj); |
383 | 369 |
384 /** Add the passed object to the catalog, as well as all its dependencies. | 370 /** Add the passed object to the catalog, as well as all its dependencies. |
385 * @param obj The object to add. If nullptr, this is a noop. | 371 * @param obj The object to add. If nullptr, this is a noop. |
386 * @param subs Will be passed to obj->addResources(). | |
387 */ | 372 */ |
388 void addObjectRecursively(SkPDFObject* obj, const SkPDFSubstituteMap& subs); | 373 void addObjectRecursively(SkPDFObject* obj); |
389 | 374 |
390 /** Get the object number for the passed object. | 375 /** Get the object number for the passed object. |
391 * @param obj The object of interest. | 376 * @param obj The object of interest. |
392 */ | 377 */ |
393 int32_t getObjectNumber(SkPDFObject* obj) const; | 378 int32_t getObjectNumber(SkPDFObject* obj) const; |
394 | 379 |
395 const SkTArray<sk_sp<SkPDFObject>>& objects() const { return fObjects; } | 380 const SkTArray<sk_sp<SkPDFObject>>& objects() const { return fObjects; } |
396 | 381 |
397 private: | 382 private: |
398 SkTArray<sk_sp<SkPDFObject>> fObjects; | 383 SkTArray<sk_sp<SkPDFObject>> fObjects; |
399 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers; | 384 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers; |
400 }; | 385 }; |
401 | 386 |
402 //////////////////////////////////////////////////////////////////////////////// | 387 //////////////////////////////////////////////////////////////////////////////// |
403 | 388 |
404 /** \class SkPDFSubstituteMap | |
405 | |
406 The PDF Substitute Map manages substitute objects and owns the | |
407 substitutes. | |
408 */ | |
409 class SkPDFSubstituteMap : SkNoncopyable { | |
410 public: | |
411 ~SkPDFSubstituteMap(); | |
412 /** Set substitute object for the passed object. | |
413 Refs substitute. | |
414 */ | |
415 void setSubstitute(SkPDFObject* original, SkPDFObject* substitute); | |
416 | |
417 /** Find and return any substitute object set for the passed object. If | |
418 * there is none, return the passed object. | |
419 */ | |
420 SkPDFObject* getSubstitute(SkPDFObject* object) const; | |
421 | |
422 SkPDFObject* operator()(SkPDFObject* o) const { | |
423 return this->getSubstitute(o); | |
424 } | |
425 | |
426 private: | |
427 SkTHashMap<SkPDFObject*, SkPDFObject*> fSubstituteMap; | |
428 }; | |
429 | |
430 #ifdef SK_PDF_IMAGE_STATS | 389 #ifdef SK_PDF_IMAGE_STATS |
431 extern SkAtomic<int> gDrawImageCalls; | 390 extern SkAtomic<int> gDrawImageCalls; |
432 extern SkAtomic<int> gJpegImageObjects; | 391 extern SkAtomic<int> gJpegImageObjects; |
433 extern SkAtomic<int> gRegularImageObjects; | 392 extern SkAtomic<int> gRegularImageObjects; |
434 extern void SkPDFImageDumpStats(); | 393 extern void SkPDFImageDumpStats(); |
435 #endif // SK_PDF_IMAGE_STATS | 394 #endif // SK_PDF_IMAGE_STATS |
436 | 395 |
437 #endif | 396 #endif |
OLD | NEW |