aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/PdfViewer/pdfparser/native/SkPdfNativeObject.h
blob: 6ce5622fee6fd5c75aeff0caf7f8a896222d2a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkPdfNativeObject_DEFINED
#define SkPdfNativeObject_DEFINED

#include <stdint.h>
#include <string.h>

#include "SkMatrix.h"
#include "SkPdfConfig.h"
#include "SkPdfNativeTokenizer.h"
#include "SkPdfNYI.h"
#include "SkPdfUtils.h"
#include "SkRect.h"
#include "SkString.h"
#include "SkTDArray.h"
#include "SkTDict.h"

class SkPdfDictionary;
class SkPdfStream;
class SkPdfAllocator;

// TODO(edisonn): remove these constants and clean up the code.
#define kFilteredStreamBit 0
#define kUnfilteredStreamBit 1
#define kOwnedStreamBit 2

/** \class SkPdfNativeObject
 *
 *  The SkPdfNativeObject class is used to store a pdf object. Classes that inherit it are not
 *  allowed to add fields.
 *
 *  SkPdfAllocator will allocate them in chunks and will free them in destructor.
 *
 *  You can allocate one on the stack, as long as you call reset() at the end, and any objects it
 *  points to in an allocator. But if your object is a simple one, like number, then
 *  putting it on stack will be just fine.
 *
 */
class SkPdfNativeObject {
 public:
     enum ObjectType {
         // The type will have only one of these values, but for error reporting, we make it an enum
         // so it can easily report that something was expected to be one of a few types
         kInvalid_PdfObjectType = 1 << 1,

         kBoolean_PdfObjectType = 1 << 2,
         kInteger_PdfObjectType = 1 << 3,
         kReal_PdfObjectType = 1 << 4,
         _kNumber_PdfObjectType = kInteger_PdfObjectType | kReal_PdfObjectType,
         kString_PdfObjectType = 1 << 5,
         kHexString_PdfObjectType = 1 << 6,
         _kAnyString_PdfObjectType = kString_PdfObjectType | kHexString_PdfObjectType,
         kName_PdfObjectType = 1 << 7,
         kKeyword_PdfObjectType = 1 << 8,
         _kStream_PdfObjectType = 1 << 9,  //  attached to a Dictionary, do not use
         kArray_PdfObjectType = 1 << 10,
         kDictionary_PdfObjectType = 1 << 11,
         kNull_PdfObjectType = 1 << 12,

         kReference_PdfObjectType = 1 << 13,

         kUndefined_PdfObjectType = 1 << 14,  // per 1.4 spec, if the same key appear twice in the
                                              // dictionary, the value is undefined.

         _kObject_PdfObjectType = -1,
     };

     enum DataType {
         kEmpty_Data,
         kFont_Data,
         kBitmap_Data,
     };

private:
    // TODO(edisonn): assert reset operations while in rendering! The objects should be reset
    // only when rendering is completed.
    uint32_t fInRendering : 1;
    uint32_t fUnused : 31;

    struct Reference {
        unsigned int fId;
        unsigned int fGen;
    };

    ObjectType fObjectType;

    union {
        bool fBooleanValue;
        int64_t fIntegerValue;
        // TODO(edisonn): double, float, SkScalar?
        double fRealValue;
        NotOwnedString fStr;

        SkTDArray<SkPdfNativeObject*>* fArray;
        Reference fRef;
    };
    SkTDict<SkPdfNativeObject*>* fMap;

    // TODO(edisonn): rename data with cache
    void* fData;
    DataType fDataType;

#ifdef PDF_TRACK_OBJECT_USAGE
    // Records if the object was used during rendering/proccessing. It can be used to track
    // what features are only partially implemented, by looking at what objects have not been
    // accessed.
    mutable bool fUsed;
#endif   // PDF_TRACK_OBJECT_USAGE

#ifdef PDF_TRACK_STREAM_OFFSETS
public:
    // TODO(edisonn): replace them with char* start, end - and a mechanism to register streams.
    int fStreamId;
    int fOffsetStart;
    int fOffsetEnd;
#endif  // PDF_TRACK_STREAM_OFFSETS

public:

#ifdef PDF_TRACK_STREAM_OFFSETS
    // TODO(edisonn): remove these ones.
    int streamId() const { return fStreamId; }
    int offsetStart() const { return fOffsetStart; }
    int offsetEnd() const { return fOffsetEnd; }
#endif  // PDF_TRACK_STREAM_OFFSETS

    SkPdfNativeObject() : fInRendering(0)
                        , fObjectType(kInvalid_PdfObjectType)
                        , fMap(NULL)
                        , fData(NULL)
                        , fDataType(kEmpty_Data)
#ifdef PDF_TRACK_OBJECT_USAGE
                        , fUsed(false)
#endif   // PDF_TRACK_OBJECT_USAGE

#ifdef PDF_TRACK_STREAM_OFFSETS
                        , fStreamId(-1)
                        , fOffsetStart(-1)
                        , fOffsetEnd(-1)
#endif  // PDF_TRACK_STREAM_OFFSETS
    {}

    // Used to verify if a form is used in rendering, to check for infinite loops.
    bool inRendering() const { return fInRendering != 0; }
    void startRendering() {fInRendering = 1;}
    void doneRendering() {fInRendering = 0;}

    // Each object can cache one entry associated with it.
    // for example a SkPdfImage could cache an SkBitmap, of a SkPdfFont, could cache a SkTypeface.
    inline bool hasData(DataType type) {
        return type == fDataType;
    }

    // returns the cached value
    inline void* data(DataType type) {
        return type == fDataType ? fData : NULL;
    }

    // Stores something in the cache
    inline void setData(void* data, DataType type) {
        releaseData();
        fDataType = type;
        fData = data;
    }

    // destroys the cache
    void releaseData();

    // TODO(edisonn): add an assert that reset was called
//    ~SkPdfNativeObject() {
//        //reset();  must be called manually! Normally, will be called by allocator destructor.
//    }

    // Resets a pdf object, deleting all resources directly referenced.
    // It will not reset/delete indirect resources.
    // (e.g. it deletes only the array holding pointers to objects, but does not del the objects)
    void reset() {
        SkPdfMarkObjectUnused();

        switch (fObjectType) {
            case kArray_PdfObjectType:
                delete fArray;
                break;

            case kDictionary_PdfObjectType:
                delete fMap;
                if (isStreamOwned()) {
                    delete[] fStr.fBuffer;
                    fStr.fBuffer = NULL;
                    fStr.fBytes = 0;
                }
                break;

            default:
                break;
        }
        fObjectType = kInvalid_PdfObjectType;
        releaseData();
    }

    // returns the object type (Null, Integer, String, Dictionary, ... )
    // It does not specify what type of dictionary we have.
    ObjectType type() {
        SkPdfMarkObjectUsed();

        return fObjectType;
    }

    // Gives quick access to the buffer's address of a string/keyword/name
    const char* c_str() const {
        SkPdfMarkObjectUsed();

        switch (fObjectType) {
            case kString_PdfObjectType:
            case kHexString_PdfObjectType:
            case kKeyword_PdfObjectType:
            case kName_PdfObjectType:
                return (const char*)fStr.fBuffer;

            default:
                // TODO(edisonn): report/warning/assert?
                return NULL;
        }
    }

    // Gives quick access to the length of a string/keyword/name
    size_t lenstr() const {
        SkPdfMarkObjectUsed();

        switch (fObjectType) {
            case kString_PdfObjectType:
            case kHexString_PdfObjectType:
            case kKeyword_PdfObjectType:
            case kName_PdfObjectType:
                return fStr.fBytes;

            default:
                // TODO(edisonn): report/warning/assert?
                return 0;
        }
    }


    // TODO(edisonn): NYI
    SkPdfDate& dateValue() const {
        static SkPdfDate nyi;
        return nyi;
    }

    // TODO(edisonn): NYI
    SkPdfFunction& functionValue() const {
        static SkPdfFunction nyi;
        return nyi;
    }

    // TODO(edisonn): NYI
    SkPdfFileSpec& fileSpecValue() const {
        static SkPdfFileSpec nyi;
        return nyi;
    }

    // TODO(edisonn): NYI
    SkPdfTree& treeValue() const {
        static SkPdfTree nyi;
        return nyi;
    }

    // Creates a Boolean object. Assumes and asserts that it was never initialized.
    static void makeBoolean(bool value, SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kBoolean_PdfObjectType;
        obj->fBooleanValue = value;
    }

    static SkPdfNativeObject makeBoolean(bool value) {
        SkPdfNativeObject obj;

        obj.fObjectType = kBoolean_PdfObjectType;
        obj.fBooleanValue = value;
        return obj;
    }

    // Creates an Integer object. Assumes and asserts that it was never initialized.
    static void makeInteger(int64_t value, SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kInteger_PdfObjectType;
        obj->fIntegerValue = value;
    }

    // Creates a Real object. Assumes and asserts that it was never initialized.
    static void makeReal(double value, SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kReal_PdfObjectType;
        obj->fRealValue = value;
    }

    // Creates a Null object. Assumes and asserts that it was never initialized.
    static void makeNull(SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kNull_PdfObjectType;
    }

    static SkPdfNativeObject makeNull() {
        SkPdfNativeObject obj;

        obj.fObjectType = kNull_PdfObjectType;
        return obj;
    }

    // TODO(edisonn): this might not woirk well in Chrome
    static SkPdfNativeObject kNull;

    // Creates a Numeric object from a string. Assumes and asserts that it was never initialized.
    static void makeNumeric(const unsigned char* start, const unsigned char* end,
                            SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        // TODO(edisonn): NYI properly
        // if has dot (impl), or exceeds max int, is real, otherwise is int
        bool isInt = true;
        for (const unsigned char* current = start; current < end; current++) {
            if (*current == '.') {
                isInt = false;
                break;
            }
            // TODO(edisonn): report parse issue with numbers like "24asdasd123"
        }
        if (isInt) {
            makeInteger(atol((const char*)start), obj);
        } else {
            makeReal(atof((const char*)start), obj);
        }
    }

    // Creates a Reference object. Assumes and asserts that it was never initialized.
    static void makeReference(unsigned int id, unsigned int gen, SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kReference_PdfObjectType;
        obj->fRef.fId = id;
        obj->fRef.fGen = gen;
    }

    // Creates a Reference object. Resets the object before use.
    static void resetAndMakeReference(unsigned int id, unsigned int gen, SkPdfNativeObject* obj) {
        obj->reset();
        makeReference(id, gen, obj);
    }

    // Creates a String object. Assumes and asserts that it was never initialized.
    static void makeString(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kString_PdfObjectType);
    }

    // Creates a String object. Assumes and asserts that it was never initialized.
    static void makeString(const unsigned char* start, const unsigned char* end,
                           SkPdfNativeObject* obj) {
        makeStringCore(start, end - start, obj, kString_PdfObjectType);
    }

    // Creates a String object. Assumes and asserts that it was never initialized.
    static void makeString(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kString_PdfObjectType);
    }

    // Creates a HexString object. Assumes and asserts that it was never initialized.
    static void makeHexString(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kHexString_PdfObjectType);
    }

    // Creates a HexString object. Assumes and asserts that it was never initialized.
    static void makeHexString(const unsigned char* start, const unsigned char* end,
                              SkPdfNativeObject* obj) {
        makeStringCore(start, end - start, obj, kHexString_PdfObjectType);
    }

    // Creates a HexString object. Assumes and asserts that it was never initialized.
    static void makeHexString(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kHexString_PdfObjectType);
    }

    // Creates a Name object. Assumes and asserts that it was never initialized.
    static void makeName(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kName_PdfObjectType);
    }

    // Creates a Name object. Assumes and asserts that it was never initialized.
    static void makeName(const unsigned char* start, const unsigned char* end,
                         SkPdfNativeObject* obj) {
        makeStringCore(start, end - start, obj, kName_PdfObjectType);
    }

    // Creates a Name object. Assumes and asserts that it was never initialized.
    static void makeName(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kName_PdfObjectType);
    }

    // Creates a Keyword object. Assumes and asserts that it was never initialized.
    static void makeKeyword(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kKeyword_PdfObjectType);
    }

    // Creates a Keyword object. Assumes and asserts that it was never initialized.
    static void makeKeyword(const unsigned char* start, const unsigned char* end,
                            SkPdfNativeObject* obj) {
        makeStringCore(start, end - start, obj, kKeyword_PdfObjectType);
    }

    // Creates a Keyword object. Assumes and asserts that it was never initialized.
    static void makeKeyword(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kKeyword_PdfObjectType);
    }

    // Creates an empty Array object. Assumes and asserts that it was never initialized.
    static void makeEmptyArray(SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kArray_PdfObjectType;
        obj->fArray = new SkTDArray<SkPdfNativeObject*>();
    }

    // Appends an object into the array. Assumes <this> is an array.
    bool appendInArray(SkPdfNativeObject* obj) {
        SkASSERT(fObjectType == kArray_PdfObjectType);
        if (fObjectType != kArray_PdfObjectType) {
            // TODO(edisonn): report/warning/assert?
            return false;
        }

        fArray->push(obj);
        return true;
    }

    // Returns the size of an array.
    size_t size() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kArray_PdfObjectType);

        return fArray->count();
    }

    // Returns one object of an array, by index.
    SkPdfNativeObject* objAtAIndex(int i) {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    // Returns one object of an array, by index.
    const SkPdfNativeObject* objAtAIndex(int i) const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    // Returns one object of an array, by index.
    SkPdfNativeObject* operator[](int i) {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    const SkPdfNativeObject* operator[](int i) const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    // Removes the last object in the array.
    SkPdfNativeObject* removeLastInArray() {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kArray_PdfObjectType);

        SkPdfNativeObject* ret = NULL;
        fArray->pop(&ret);

        return ret;
    }

    // Creates an empty Dictionary object. Assumes and asserts that it was never initialized.
    static void makeEmptyDictionary(SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = kDictionary_PdfObjectType;
        obj->fMap = new SkTDict<SkPdfNativeObject*>(1);
        obj->fStr.fBuffer = NULL;
        obj->fStr.fBytes = 0;
    }

    // TODO(edisonn): perf: get all the possible names from spec, and compute a hash function
    // that would create no overlaps in the same dictionary
    // or build a tree of chars that when followed goes to a unique id/index/hash
    // TODO(edisonn): generate constants like kDictFoo, kNameDict_name
    // which will be used in code
    // add function SkPdfFastNameKey key(const char* key);
    // TODO(edisonn): setting the same key twice, will make the value undefined!

    // this[key] = value;
    bool set(const SkPdfNativeObject* key, SkPdfNativeObject* value) {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key->fObjectType == kName_PdfObjectType);

        if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report/warn/assert?
            return false;
        }

        return set(key->fStr.fBuffer, key->fStr.fBytes, value);
    }

    // this[key] = value;
    bool set(const char* key, SkPdfNativeObject* value) {
        SkPdfMarkObjectUsed();

        return set((const unsigned char*)key, strlen(key), value);
    }

    // this[key] = value;
    bool set(const unsigned char* key, size_t len, SkPdfNativeObject* value) {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kDictionary_PdfObjectType);

        if (fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return false;
        }

        return fMap->set((const char*)key, len, value);
    }

    // Returns an object from a Dictionary, identified by it's name.
    SkPdfNativeObject* get(const SkPdfNativeObject* key) {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key->fObjectType == kName_PdfObjectType);

        if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return NULL;
        }

        return get(key->fStr.fBuffer, key->fStr.fBytes);
    }

    // Returns an object from a Dictionary, identified by it's name.
    SkPdfNativeObject* get(const char* key) {
        SkPdfMarkObjectUsed();

        return get((const unsigned char*)key, strlen(key));
    }

    // Returns an object from a Dictionary, identified by it's name.
    SkPdfNativeObject* get(const unsigned char* key, size_t len) {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key);
        if (fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return NULL;
        }
        SkPdfNativeObject* ret = NULL;
        fMap->find((const char*)key, len, &ret);

#ifdef PDF_TRACE
        SkString _key;
        _key.append((const char*)key, len);
        printf("\nget(/%s) = %s\n", _key.c_str(),
               ret ? ret->toString(0, len + 9).c_str() : "_NOT_FOUND");
#endif

        return ret;
    }

    // Returns an object from a Dictionary, identified by it's name.
    const SkPdfNativeObject* get(const SkPdfNativeObject* key) const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key->fObjectType == kName_PdfObjectType);

        if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return NULL;
        }

        return get(key->fStr.fBuffer, key->fStr.fBytes);
    }

    // Returns an object from a Dictionary, identified by it's name.
    const SkPdfNativeObject* get(const char* key) const {
        SkPdfMarkObjectUsed();

        return get((const unsigned char*)key, strlen(key));
    }

    // Returns an object from a Dictionary, identified by it's name.
    const SkPdfNativeObject* get(const unsigned char* key, size_t len) const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key);
        if (fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return NULL;
        }
        SkPdfNativeObject* ret = NULL;
        fMap->find((const char*)key, len, &ret);

#ifdef PDF_TRACE
        SkString _key;
        _key.append((const char*)key, len);
        printf("\nget(/%s) = %s\n", _key.c_str(),
               ret ? ret->toString(0, len + 9).c_str() : "_NOT_FOUND");
#endif

        return ret;
    }

    // Returns an object from a Dictionary, identified by it's name.
    const SkPdfNativeObject* get(const char* key, const char* abr) const {
        SkPdfMarkObjectUsed();

        const SkPdfNativeObject* ret = get(key);
        // TODO(edisonn): remove  || *abr == '\0' and pass NULL in the _autogen files instead.
        if (ret != NULL || abr == NULL || *abr == '\0') {
            return ret;
        }
        return get(abr);
    }

    // Returns an object from a Dictionary, identified by it's name.
    SkPdfNativeObject* get(const char* key, const char* abr) {
        SkPdfMarkObjectUsed();

        SkPdfNativeObject* ret = get(key);
        // TODO(edisonn): remove  || *abr == '\0' and pass NULL in the _autogen files instead.
        if (ret != NULL || abr == NULL || *abr == '\0') {
            return ret;
        }
        return get(abr);
    }

    // Casts the object to a Dictionary. Asserts if the object is not a Dictionary.
    SkPdfDictionary* asDictionary() {
        SkPdfMarkObjectUsed();

        SkASSERT(isDictionary());
        if (!isDictionary()) {
            return NULL;
        }
        return (SkPdfDictionary*) this;
    }

    // Casts the object to a Dictionary. Asserts if the object is not a Dictionary.
    const SkPdfDictionary* asDictionary() const {
        SkPdfMarkObjectUsed();

        SkASSERT(isDictionary());
        if (!isDictionary()) {
            return NULL;
        }
        return (SkPdfDictionary*) this;
    }


    // Returns true if the object is a Reference.
    bool isReference() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kReference_PdfObjectType;
    }

    // Returns true if the object is a Boolean.
    bool isBoolean() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kBoolean_PdfObjectType;
    }

    // Returns true if the object is an Integer.
    bool isInteger() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kInteger_PdfObjectType;
    }

private:
    // Returns true if the object is a Real number.
    bool isReal() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kReal_PdfObjectType;
    }

public:
    // Returns true if the object is a Number (either Integer or Real).
    bool isNumber() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kInteger_PdfObjectType || fObjectType == kReal_PdfObjectType;
    }

    // Returns true if the object is a R keyword (used to identify references, e.g. "10 3 R".
    bool isKeywordReference() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kKeyword_PdfObjectType && fStr.fBytes == 1 && fStr.fBuffer[0] == 'R';
    }

    // Returns true if the object is a Keyword.
    bool isKeyword() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kKeyword_PdfObjectType;
    }

    // Returns true if the object is a given Keyword.
    bool isKeyword(const char* keyword) const {
        SkPdfMarkObjectUsed();

        if (!isKeyword()) {
            return false;
        }

        if (strlen(keyword) != fStr.fBytes) {
            return false;
        }

        if (strncmp(keyword, (const char*)fStr.fBuffer, fStr.fBytes) != 0) {
            return false;
        }

        return true;
    }

    // Returns true if the object is a Name.
    bool isName() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kName_PdfObjectType;
    }

    // Returns true if the object is a given Name.
    bool isName(const char* name) const {
        SkPdfMarkObjectUsed();

        return fObjectType == kName_PdfObjectType &&
                fStr.fBytes == strlen(name) &&
                strncmp((const char*)fStr.fBuffer, name, fStr.fBytes) == 0;
    }

    // Returns true if the object is an Array.
    bool isArray() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kArray_PdfObjectType;
    }

    // Returns true if the object is a Date.
    // TODO(edisonn): NYI
    bool isDate() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType;
    }

    // Returns true if the object is a Dictionary.
    bool isDictionary() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kDictionary_PdfObjectType;
    }

    // Returns true if the object is a Date.
    // TODO(edisonn): NYI
    bool isFunction() const {
        SkPdfMarkObjectUsed();

        return false;  // NYI
    }

    // Returns true if the object is a Rectangle.
    bool isRectangle() const {
        SkPdfMarkObjectUsed();

        // TODO(edisonn): add also that each of these 4 objects are numbers.
        return fObjectType == kArray_PdfObjectType && fArray->count() == 4;
    }

    // TODO(edisonn): Design: decide if we should use hasStream or isStream
    // Returns true if the object has a stream associated with it.
    bool hasStream() const {
        SkPdfMarkObjectUsed();

        return isDictionary() && fStr.fBuffer != NULL;
    }

    // Returns the stream associated with the dictionary. As of now, it casts this to Stream.
    const SkPdfStream* getStream() const {
        SkPdfMarkObjectUsed();

        return hasStream() ? (const SkPdfStream*)this : NULL;
    }

    // Returns the stream associated with the dictionary. As of now, it casts this to Stream.
    SkPdfStream* getStream() {
        SkPdfMarkObjectUsed();

        return hasStream() ? (SkPdfStream*)this : NULL;
    }

    // Returns true if the object is a String or HexString.
    bool isAnyString() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType;
    }

    // Returns true if the object is a HexString.
    bool isHexString() const {
        SkPdfMarkObjectUsed();

        return fObjectType == kHexString_PdfObjectType;
    }

    // Returns true if the object is a Matrix.
    bool isMatrix() const {
        SkPdfMarkObjectUsed();

        // TODO(edisonn): add also that each of these 6 objects are numbers.
        return fObjectType == kArray_PdfObjectType && fArray->count() == 6;
    }

    // Returns the int value stored in the object. Assert if the object is not an Integer.
    inline int64_t intValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kInteger_PdfObjectType);

        if (fObjectType != kInteger_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return 0;
        }
        return fIntegerValue;
    }

private:
    // Returns the real value stored in the object. Assert if the object is not a Real.
    inline double realValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kReal_PdfObjectType);

        if (fObjectType != kReal_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return 0;
        }
        return fRealValue;
    }

public:
    // Returns the numeric value stored in the object. Assert if the object is not a Real
    // or an Integer.
    inline double numberValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(isNumber());

        if (!isNumber()) {
            // TODO(edisonn): report/warn/assert.
            return 0;
        }
        return fObjectType == kReal_PdfObjectType ? fRealValue : fIntegerValue;
    }

    // Returns the numeric value stored in the object as a scalar. Assert if the object is not
    // a Realor an Integer.
    inline SkScalar scalarValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(isNumber());

        if (!isNumber()) {
            // TODO(edisonn): report/warn/assert.
            return SkIntToScalar(0);
        }
        return fObjectType == kReal_PdfObjectType ? SkDoubleToScalar(fRealValue) :
                                                    SkIntToScalar(fIntegerValue);
    }

    // Returns the id of the referenced object. Assert if the object is not a Reference.
    int referenceId() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kReference_PdfObjectType);
        return fRef.fId;
    }

    // Returns the generation of the referenced object. Assert if the object is not a Reference.
    int referenceGeneration() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kReference_PdfObjectType);
        return fRef.fGen;
    }

    // Returns the buffer of a Name object. Assert if the object is not a Name.
    inline const char* nameValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kName_PdfObjectType);

        if (fObjectType != kName_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return "";
        }
        return (const char*)fStr.fBuffer;
    }

    // Returns the buffer of a (Hex)String object. Assert if the object is not a (Hex)String.
    inline const char* stringValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType);

        if (fObjectType != kString_PdfObjectType && fObjectType != kHexString_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return "";
        }
        return (const char*)fStr.fBuffer;
    }

    // Returns the storage of any type that can hold a form of string.
    inline NotOwnedString strRef() {
        SkPdfMarkObjectUsed();

        switch (fObjectType) {
            case kString_PdfObjectType:
            case kHexString_PdfObjectType:
            case kKeyword_PdfObjectType:
            case kName_PdfObjectType:
                return fStr;

            default:
                // TODO(edisonn): report/warning
                return NotOwnedString();
        }
    }

    // TODO(edisonn): nameValue2 and stringValue2 are used to make code generation easy,
    // but it is not a performat way to do it, since it will create an extra copy
    // remove these functions and make code generated faster
    inline SkString nameValue2() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kName_PdfObjectType);

        if (fObjectType != kName_PdfObjectType) {
            // TODO(edisonn): log err
            return SkString();
        }
        return SkString((const char*)fStr.fBuffer, fStr.fBytes);
    }

    // Returns an SkString with the value of the (Hex)String object.
    inline SkString stringValue2() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType);

        if (fObjectType != kString_PdfObjectType && fObjectType != kHexString_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return SkString();
        }
        return SkString((const char*)fStr.fBuffer, fStr.fBytes);
    }

    // Returns the boolean of the Bool object. Assert if the object is not a Bool.
    inline bool boolValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(fObjectType == kBoolean_PdfObjectType);

        if (fObjectType != kBoolean_PdfObjectType) {
            // TODO(edisonn): report/warn/assert.
            return false;
        }
        return fBooleanValue;
    }

    // Returns the rectangle of the Rectangle object. Assert if the object is not a Rectangle.
    SkRect rectangleValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(isRectangle());
        if (!isRectangle()) {
            return SkRect::MakeEmpty();
        }

        double array[4];
        for (int i = 0; i < 4; i++) {
            // TODO(edisonn): version where we could resolve references?
            const SkPdfNativeObject* elem = objAtAIndex(i);
            if (elem == NULL || !elem->isNumber()) {
                // TODO(edisonn): report/warn/assert.
                return SkRect::MakeEmpty();
            }
            array[i] = elem->numberValue();
        }

        return SkRect::MakeLTRB(SkDoubleToScalar(array[0]),
                                SkDoubleToScalar(array[1]),
                                SkDoubleToScalar(array[2]),
                                SkDoubleToScalar(array[3]));
    }

    // Returns the matrix of the Matrix object. Assert if the object is not a Matrix.
    SkMatrix matrixValue() const {
        SkPdfMarkObjectUsed();

        SkASSERT(isMatrix());
        if (!isMatrix()) {
            return SkMatrix::I();
        }

        double array[6];
        for (int i = 0; i < 6; i++) {
            // TODO(edisonn): version where we could resolve references?
            const SkPdfNativeObject* elem = objAtAIndex(i);
            if (elem == NULL || !elem->isNumber()) {
                // TODO(edisonn): report/warn/assert.
                return SkMatrix::I();
            }
            array[i] = elem->numberValue();
        }

        return SkMatrixFromPdfMatrix(array);
    }

    // Runs all the filters of this stream, except the last one, if it is a DCT.
    // Returns false on failure.
    bool filterStream();

    // Runs all the filters of this stream, except the last one, if it is a DCT, a gives back
    // the buffer and the length. The object continues to own the buffer.
    // Returns false on failure.
    bool GetFilteredStreamRef(unsigned char const** buffer, size_t* len) {
        SkPdfMarkObjectUsed();

        // TODO(edisonn): add params that could let the last filter in place
        // if it is jpeg or png to fast load images.
        if (!hasStream()) {
            return false;
        }

        filterStream();

        if (buffer) {
            *buffer = fStr.fBuffer;
        }

        if (len) {
            *len = fStr.fBytes >> 2;  // last 2 bits - TODO(edisonn): clean up.
        }

        return true;
    }

    // Returns true if the stream is already filtered.
    bool isStreamFiltered() const {
        SkPdfMarkObjectUsed();

        return hasStream() && ((fStr.fBytes & 1) == kFilteredStreamBit);
    }

    // Returns true if this object own the buffer, or false if an Allocator own it.
    bool isStreamOwned() const {
        SkPdfMarkObjectUsed();

        return hasStream() && ((fStr.fBytes & 2) == kOwnedStreamBit);
    }

    // Gives back the original buffer and the length. The object continues to own the buffer.
    // Returns false if the stream is already filtered.
    bool GetUnfilteredStreamRef(unsigned char const** buffer, size_t* len) const {
        SkPdfMarkObjectUsed();

        if (isStreamFiltered()) {
            return false;
        }

        if (!hasStream()) {
            return false;
        }

        if (buffer) {
            *buffer = fStr.fBuffer;
        }

        if (len) {
            *len = fStr.fBytes >> 2;  // remove last 2 bits - TODO(edisonn): clean up.
        }

        return true;
    }

    // Add a stream to this Dictionarry. Asserts we do not have yet a stream.
    bool addStream(const unsigned char* buffer, size_t len) {
        SkPdfMarkObjectUsed();

        SkASSERT(!hasStream());
        SkASSERT(isDictionary());

        if (!isDictionary() || hasStream()) {
            return false;
        }

        fStr.fBuffer = buffer;
        fStr.fBytes = (len << 2) + kUnfilteredStreamBit;

        return true;
    }

    static void appendSpaces(SkString* str, int level) {
        for (int i = 0 ; i < level; i++) {
            str->append(" ");
        }
    }

    static void append(SkString* str, const char* data, size_t len, const char* prefix = "\\x") {
        for (unsigned int i = 0 ; i < len; i++) {
            if (data[i] == kNUL_PdfWhiteSpace) {
                str->append(prefix);
                str->append("00");
            } else if (data[i] == kHT_PdfWhiteSpace) {
                str->append(prefix);
                str->append("09");
            } else if (data[i] == kLF_PdfWhiteSpace) {
                str->append(prefix);
                str->append("0A");
            } else if (data[i] == kFF_PdfWhiteSpace) {
                str->append(prefix);
                str->append("0C");
            } else if (data[i] == kCR_PdfWhiteSpace) {
                str->append(prefix);
                str->append("0D");
            } else {
                str->append(data + i, 1);
            }
        }
    }

    // Returns the string representation of the object value.
    SkString toString(int firstRowLevel = 0, int level = 0) {
        SkString str;
        appendSpaces(&str, firstRowLevel);
        switch (fObjectType) {
            case kInvalid_PdfObjectType:
                str.append("__Invalid");
                break;

            case kBoolean_PdfObjectType:
                str.appendf("%s", fBooleanValue ? "true" : "false");
                break;

            case kInteger_PdfObjectType:
                str.appendf("%i", (int)fIntegerValue);
                break;

            case kReal_PdfObjectType:
                str.appendf("%f", fRealValue);
                break;

            case kString_PdfObjectType:
                str.append("\"");
                append(&str, (const char*)fStr.fBuffer, fStr.fBytes);
                str.append("\"");
                break;

            case kHexString_PdfObjectType:
                str.append("<");
                for (unsigned int i = 0 ; i < fStr.fBytes; i++) {
                    str.appendf("%02x", (unsigned int)fStr.fBuffer[i]);
                }
                str.append(">");
                break;

            case kName_PdfObjectType:
                str.append("/");
                append(&str, (const char*)fStr.fBuffer, fStr.fBytes, "#");
                break;

            case kKeyword_PdfObjectType:
                append(&str, (const char*)fStr.fBuffer, fStr.fBytes);
                break;

            case kArray_PdfObjectType:
                str.append("[\n");
                for (unsigned int i = 0; i < size(); i++) {
                    str.append(objAtAIndex(i)->toString(level + 1, level + 1));
                    if (i < size() - 1) {
                        str.append(",");
                    }
                    str.append("\n");
                }
                appendSpaces(&str, level);
                str.append("]");
                break;

            case kDictionary_PdfObjectType: {
                    SkTDict<SkPdfNativeObject*>::Iter iter(*fMap);
                    SkPdfNativeObject* obj = NULL;
                    const char* key = NULL;
                    str.append("<<\n");
                    while ((key = iter.next(&obj)) != NULL) {
                        appendSpaces(&str, level + 2);
                        str.appendf("/%s %s\n", key,
                                    obj->toString(0, level + strlen(key) + 4).c_str());
                    }
                    appendSpaces(&str, level);
                    str.append(">>");
                    if (hasStream()) {
                        const unsigned char* stream = NULL;
                        size_t length = 0;
                        if (GetFilteredStreamRef(&stream, &length)) {
                            str.append("stream\n");
                            append(&str, (const char*)stream, length > 256 ? 256 : length);
                            str.append("\nendstream");
                        } else {
                            str.append("stream STREAM_ERROR endstream");
                        }
                    }
                }
                break;

            case kNull_PdfObjectType:
                str = "NULL";
                break;

            case kReference_PdfObjectType:
                str.appendf("%i %i R", fRef.fId, fRef.fGen);
                break;

            case kUndefined_PdfObjectType:
                str = "Undefined";
                break;

            default:
                str = "Error";
                break;
        }

        return str;
    }

private:
    static void makeStringCore(const unsigned char* start, SkPdfNativeObject* obj,
                               ObjectType type) {
        makeStringCore(start, strlen((const char*)start), obj, type);
    }

    static void makeStringCore(const unsigned char* start, const unsigned char* end,
                               SkPdfNativeObject* obj, ObjectType type) {
        makeStringCore(start, end - start, obj, type);
    }

    static void makeStringCore(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj,
                               ObjectType type) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

        obj->fObjectType = type;
        obj->fStr.fBuffer = start;
        obj->fStr.fBytes = bytes;
    }

    bool applyFilter(const char* name);
    bool applyFlateDecodeFilter();
    bool applyDCTDecodeFilter();
};

// These classes are provided for convenience. You still have to make sure an SkPdfInteger
// is indeed an Integer.
class SkPdfStream : public SkPdfNativeObject {};
class SkPdfArray : public SkPdfNativeObject {};
class SkPdfString : public SkPdfNativeObject {};
class SkPdfHexString : public SkPdfNativeObject {};
class SkPdfInteger : public SkPdfNativeObject {};
class SkPdfReal : public SkPdfNativeObject {};
class SkPdfNumber : public SkPdfNativeObject {};

class SkPdfName : public SkPdfNativeObject {
    SkPdfName() : SkPdfNativeObject() {
        SkPdfNativeObject::makeName((const unsigned char*)"", this);
    }
public:
    SkPdfName(char* name) : SkPdfNativeObject() {
        this->makeName((const unsigned char*)name, this);
    }
};

#endif  // SkPdfNativeObject