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 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 const SkPDFSubstituteMap& substitutes) const = 0; | 44 const SkPDFSubstituteMap& substitutes) const = 0; |
45 | 45 |
46 /** | 46 /** |
47 * Adds all transitive dependencies of this object to the | 47 * Adds all transitive dependencies of this object to the |
48 * catalog. Implementations should respect the catalog's object | 48 * catalog. Implementations should respect the catalog's object |
49 * substitution map. | 49 * substitution map. |
50 */ | 50 */ |
51 virtual void addResources(SkPDFObjNumMap* catalog, | 51 virtual void addResources(SkPDFObjNumMap* catalog, |
52 const SkPDFSubstituteMap& substitutes) const {} | 52 const SkPDFSubstituteMap& substitutes) const {} |
53 | 53 |
| 54 /** |
| 55 * Release all resources associated with this SkPDFObject. It is |
| 56 * an error to call emitObject() or addResources() after calling |
| 57 * drop(). |
| 58 */ |
| 59 virtual void drop() {} |
| 60 |
| 61 virtual ~SkPDFObject() {} |
54 private: | 62 private: |
55 typedef SkRefCnt INHERITED; | 63 typedef SkRefCnt INHERITED; |
56 }; | 64 }; |
57 | 65 |
58 //////////////////////////////////////////////////////////////////////////////// | 66 //////////////////////////////////////////////////////////////////////////////// |
59 | 67 |
60 /** | 68 /** |
61 A SkPDFUnion is a non-virtualized implementation of the | 69 A SkPDFUnion is a non-virtualized implementation of the |
62 non-compound, non-specialized PDF Object types: Name, String, | 70 non-compound, non-specialized PDF Object types: Name, String, |
63 Number, Boolean. | 71 Number, Boolean. |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 #endif // 0 | 179 #endif // 0 |
172 | 180 |
173 //////////////////////////////////////////////////////////////////////////////// | 181 //////////////////////////////////////////////////////////////////////////////// |
174 | 182 |
175 /** \class SkPDFArray | 183 /** \class SkPDFArray |
176 | 184 |
177 An array object in a PDF. | 185 An array object in a PDF. |
178 */ | 186 */ |
179 class SkPDFArray final : public SkPDFObject { | 187 class SkPDFArray final : public SkPDFObject { |
180 public: | 188 public: |
181 static const int kMaxLen = 8191; | |
182 | |
183 /** Create a PDF array. Maximum length is 8191. | 189 /** Create a PDF array. Maximum length is 8191. |
184 */ | 190 */ |
185 SkPDFArray(); | 191 SkPDFArray(); |
186 virtual ~SkPDFArray(); | 192 virtual ~SkPDFArray(); |
187 | 193 |
188 // The SkPDFObject interface. | 194 // The SkPDFObject interface. |
189 void emitObject(SkWStream* stream, | 195 void emitObject(SkWStream* stream, |
190 const SkPDFObjNumMap& objNumMap, | 196 const SkPDFObjNumMap& objNumMap, |
191 const SkPDFSubstituteMap& substitutes) const override; | 197 const SkPDFSubstituteMap& substitutes) const override; |
192 void addResources(SkPDFObjNumMap*, | 198 void addResources(SkPDFObjNumMap*, |
193 const SkPDFSubstituteMap&) const override; | 199 const SkPDFSubstituteMap&) const override; |
| 200 void drop() override; |
194 | 201 |
195 /** The size of the array. | 202 /** The size of the array. |
196 */ | 203 */ |
197 int size() const; | 204 int size() const; |
198 | 205 |
199 /** Preallocate space for the given number of entries. | 206 /** Preallocate space for the given number of entries. |
200 * @param length The number of array slots to preallocate. | 207 * @param length The number of array slots to preallocate. |
201 */ | 208 */ |
202 void reserve(int length); | 209 void reserve(int length); |
203 | 210 |
204 /** Appends a value to the end of the array. | 211 /** Appends a value to the end of the array. |
205 * @param value The value to add to the array. | 212 * @param value The value to add to the array. |
206 */ | 213 */ |
207 void appendInt(int32_t); | 214 void appendInt(int32_t); |
208 void appendBool(bool); | 215 void appendBool(bool); |
209 void appendScalar(SkScalar); | 216 void appendScalar(SkScalar); |
210 void appendName(const char[]); | 217 void appendName(const char[]); |
211 void appendName(const SkString&); | 218 void appendName(const SkString&); |
212 void appendString(const char[]); | 219 void appendString(const char[]); |
213 void appendString(const SkString&); | 220 void appendString(const SkString&); |
214 void appendObject(sk_sp<SkPDFObject>); | 221 void appendObject(sk_sp<SkPDFObject>); |
215 void appendObjRef(sk_sp<SkPDFObject>); | 222 void appendObjRef(sk_sp<SkPDFObject>); |
216 | 223 |
217 private: | 224 private: |
218 SkTDArray<SkPDFUnion> fValues; | 225 SkTArray<SkPDFUnion> fValues; |
219 void append(SkPDFUnion&& value); | 226 void append(SkPDFUnion&& value); |
220 typedef SkPDFObject INHERITED; | 227 SkDEBUGCODE(bool fDumped;) |
221 }; | 228 }; |
222 | 229 |
223 /** \class SkPDFDict | 230 /** \class SkPDFDict |
224 | 231 |
225 A dictionary object in a PDF. | 232 A dictionary object in a PDF. |
226 */ | 233 */ |
227 class SkPDFDict : public SkPDFObject { | 234 class SkPDFDict : public SkPDFObject { |
228 public: | 235 public: |
229 /** Create a PDF dictionary. Maximum number of entries is 4095. | 236 /** Create a PDF dictionary. |
| 237 * @param type The value of the Type entry, nullptr for no type. |
230 */ | 238 */ |
231 SkPDFDict(); | 239 explicit SkPDFDict(const char type[] = nullptr); |
232 | |
233 /** Create a PDF dictionary with a Type entry. | |
234 * @param type The value of the Type entry. | |
235 */ | |
236 explicit SkPDFDict(const char type[]); | |
237 | 240 |
238 virtual ~SkPDFDict(); | 241 virtual ~SkPDFDict(); |
239 | 242 |
240 // The SkPDFObject interface. | 243 // The SkPDFObject interface. |
241 void emitObject(SkWStream* stream, | 244 void emitObject(SkWStream* stream, |
242 const SkPDFObjNumMap& objNumMap, | 245 const SkPDFObjNumMap& objNumMap, |
243 const SkPDFSubstituteMap& substitutes) const override; | 246 const SkPDFSubstituteMap& substitutes) const override; |
244 void addResources(SkPDFObjNumMap*, | 247 void addResources(SkPDFObjNumMap*, |
245 const SkPDFSubstituteMap&) const override; | 248 const SkPDFSubstituteMap&) const override; |
| 249 void drop() override; |
246 | 250 |
247 /** The size of the dictionary. | 251 /** The size of the dictionary. |
248 */ | 252 */ |
249 int size() const; | 253 int size() const; |
250 | 254 |
251 /** Add the value to the dictionary with the given key. | 255 /** Add the value to the dictionary with the given key. |
252 * @param key The text of the key for this dictionary entry. | 256 * @param key The text of the key for this dictionary entry. |
253 * @param value The value for this dictionary entry. | 257 * @param value The value for this dictionary entry. |
254 */ | 258 */ |
255 void insertObject(const char key[], sk_sp<SkPDFObject>); | 259 void insertObject(const char key[], sk_sp<SkPDFObject>); |
256 void insertObject(const SkString& key, sk_sp<SkPDFObject>); | 260 void insertObject(const SkString& key, sk_sp<SkPDFObject>); |
257 void insertObjRef(const char key[], sk_sp<SkPDFObject>); | 261 void insertObjRef(const char key[], sk_sp<SkPDFObject>); |
258 void insertObjRef(const SkString& key, sk_sp<SkPDFObject>); | 262 void insertObjRef(const SkString& key, sk_sp<SkPDFObject>); |
259 | 263 |
260 /** Add the value to the dictionary with the given key. | 264 /** Add the value to the dictionary with the given key. |
261 * @param key The text of the key for this dictionary entry. | 265 * @param key The text of the key for this dictionary entry. |
262 * @param value The value for this dictionary entry. | 266 * @param value The value for this dictionary entry. |
263 */ | 267 */ |
264 void insertBool(const char key[], bool value); | 268 void insertBool(const char key[], bool value); |
265 void insertInt(const char key[], int32_t value); | 269 void insertInt(const char key[], int32_t value); |
266 void insertInt(const char key[], size_t value); | 270 void insertInt(const char key[], size_t value); |
267 void insertScalar(const char key[], SkScalar value); | 271 void insertScalar(const char key[], SkScalar value); |
268 void insertName(const char key[], const char nameValue[]); | 272 void insertName(const char key[], const char nameValue[]); |
269 void insertName(const char key[], const SkString& nameValue); | 273 void insertName(const char key[], const SkString& nameValue); |
270 void insertString(const char key[], const char value[]); | 274 void insertString(const char key[], const char value[]); |
271 void insertString(const char key[], const SkString& value); | 275 void insertString(const char key[], const SkString& value); |
272 | 276 |
273 /** Remove all entries from the dictionary. | |
274 */ | |
275 void clear(); | |
276 | |
277 /** Emit the dictionary, without the "<<" and ">>". | 277 /** Emit the dictionary, without the "<<" and ">>". |
278 */ | 278 */ |
279 void emitAll(SkWStream* stream, | 279 void emitAll(SkWStream* stream, |
280 const SkPDFObjNumMap& objNumMap, | 280 const SkPDFObjNumMap& objNumMap, |
281 const SkPDFSubstituteMap& substitutes) const; | 281 const SkPDFSubstituteMap& substitutes) const; |
282 | 282 |
283 private: | 283 private: |
284 struct Record { | 284 struct Record { |
285 SkPDFUnion fKey; | 285 SkPDFUnion fKey; |
286 SkPDFUnion fValue; | 286 SkPDFUnion fValue; |
| 287 Record(SkPDFUnion&&, SkPDFUnion&&); |
| 288 Record(Record&&); |
| 289 Record& operator=(Record&&); |
| 290 Record(const Record&) = delete; |
| 291 Record& operator=(const Record&) = delete; |
287 }; | 292 }; |
288 SkTDArray<Record> fRecords; | 293 SkTArray<Record> fRecords; |
289 static const int kMaxLen = 4095; | 294 SkDEBUGCODE(bool fDumped;) |
290 | |
291 void set(SkPDFUnion&& name, SkPDFUnion&& value); | |
292 | |
293 typedef SkPDFObject INHERITED; | |
294 }; | 295 }; |
295 | 296 |
296 /** \class SkPDFSharedStream | 297 /** \class SkPDFSharedStream |
297 | 298 |
298 This class takes an asset and assumes that it is backed by | 299 This class takes an asset and assumes that it is backed by |
299 long-lived shared data (for example, an open file | 300 long-lived shared data (for example, an open file |
300 descriptor). That is: no memory savings can be made by holding on | 301 descriptor). That is: no memory savings can be made by holding on |
301 to a compressed version instead. | 302 to a compressed version instead. |
302 */ | 303 */ |
303 class SkPDFSharedStream final : public SkPDFObject { | 304 class SkPDFSharedStream final : public SkPDFObject { |
304 public: | 305 public: |
305 // Takes ownership of asset. | 306 // Takes ownership of asset. |
306 SkPDFSharedStream(SkStreamAsset* data) | 307 SkPDFSharedStream(SkStreamAsset* data); |
307 : fAsset(data), fDict(new SkPDFDict) { SkASSERT(data); } | 308 ~SkPDFSharedStream(); |
308 SkPDFDict* dict() { return fDict.get(); } | 309 SkPDFDict* dict() { return fDict.get(); } |
309 void emitObject(SkWStream*, | 310 void emitObject(SkWStream*, |
310 const SkPDFObjNumMap&, | 311 const SkPDFObjNumMap&, |
311 const SkPDFSubstituteMap&) const override; | 312 const SkPDFSubstituteMap&) const override; |
312 void addResources(SkPDFObjNumMap*, | 313 void addResources(SkPDFObjNumMap*, |
313 const SkPDFSubstituteMap&) const override; | 314 const SkPDFSubstituteMap&) const override; |
| 315 void drop() override; |
314 | 316 |
315 private: | 317 private: |
316 SkAutoTDelete<SkStreamAsset> fAsset; | 318 SkAutoTDelete<SkStreamAsset> fAsset; |
317 sk_sp<SkPDFDict> fDict; | 319 sk_sp<SkPDFDict> fDict; |
| 320 SkDEBUGCODE(bool fDumped;) |
318 typedef SkPDFObject INHERITED; | 321 typedef SkPDFObject INHERITED; |
319 }; | 322 }; |
320 | 323 |
321 //////////////////////////////////////////////////////////////////////////////// | 324 //////////////////////////////////////////////////////////////////////////////// |
322 | 325 |
323 /** \class SkPDFObjNumMap | 326 /** \class SkPDFObjNumMap |
324 | 327 |
325 The PDF Object Number Map manages object numbers. It is used to | 328 The PDF Object Number Map manages object numbers. It is used to |
326 create the PDF cross reference table. | 329 create the PDF cross reference table. |
327 */ | 330 */ |
328 class SkPDFObjNumMap : SkNoncopyable { | 331 class SkPDFObjNumMap : SkNoncopyable { |
329 public: | 332 public: |
330 /** Add the passed object to the catalog. | 333 /** Add the passed object to the catalog. |
331 * @param obj The object to add. | 334 * @param obj The object to add. |
332 * @return True iff the object was not already added to the catalog. | 335 * @return True iff the object was not already added to the catalog. |
333 */ | 336 */ |
334 bool addObject(SkPDFObject* obj); | 337 bool addObject(SkPDFObject* obj); |
335 | 338 |
336 /** Add the passed object to the catalog, as well as all its dependencies. | 339 /** Add the passed object to the catalog, as well as all its dependencies. |
337 * @param obj The object to add. If nullptr, this is a noop. | 340 * @param obj The object to add. If nullptr, this is a noop. |
338 * @param subs Will be passed to obj->addResources(). | 341 * @param subs Will be passed to obj->addResources(). |
339 */ | 342 */ |
340 void addObjectRecursively(SkPDFObject* obj, const SkPDFSubstituteMap& subs); | 343 void addObjectRecursively(SkPDFObject* obj, const SkPDFSubstituteMap& subs); |
341 | 344 |
342 /** Get the object number for the passed object. | 345 /** Get the object number for the passed object. |
343 * @param obj The object of interest. | 346 * @param obj The object of interest. |
344 */ | 347 */ |
345 int32_t getObjectNumber(SkPDFObject* obj) const; | 348 int32_t getObjectNumber(SkPDFObject* obj) const; |
346 | 349 |
347 const SkTDArray<SkPDFObject*>& objects() const { return fObjects; } | 350 const SkTArray<sk_sp<SkPDFObject>>& objects() const { return fObjects; } |
348 | 351 |
349 private: | 352 private: |
350 SkTDArray<SkPDFObject*> fObjects; | 353 SkTArray<sk_sp<SkPDFObject>> fObjects; |
351 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers; | 354 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers; |
352 }; | 355 }; |
353 | 356 |
354 //////////////////////////////////////////////////////////////////////////////// | 357 //////////////////////////////////////////////////////////////////////////////// |
355 | 358 |
356 /** \class SkPDFSubstituteMap | 359 /** \class SkPDFSubstituteMap |
357 | 360 |
358 The PDF Substitute Map manages substitute objects and owns the | 361 The PDF Substitute Map manages substitute objects and owns the |
359 substitutes. | 362 substitutes. |
360 */ | 363 */ |
(...skipping 19 matching lines...) Expand all Loading... |
380 }; | 383 }; |
381 | 384 |
382 #ifdef SK_PDF_IMAGE_STATS | 385 #ifdef SK_PDF_IMAGE_STATS |
383 extern SkAtomic<int> gDrawImageCalls; | 386 extern SkAtomic<int> gDrawImageCalls; |
384 extern SkAtomic<int> gJpegImageObjects; | 387 extern SkAtomic<int> gJpegImageObjects; |
385 extern SkAtomic<int> gRegularImageObjects; | 388 extern SkAtomic<int> gRegularImageObjects; |
386 extern void SkPDFImageDumpStats(); | 389 extern void SkPDFImageDumpStats(); |
387 #endif // SK_PDF_IMAGE_STATS | 390 #endif // SK_PDF_IMAGE_STATS |
388 | 391 |
389 #endif | 392 #endif |
OLD | NEW |