aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/PdfViewer/pdfparser/native/SkPdfNativeObject.h
blob: 1fa627290fcdab320cc28c9ab907946712164663 (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
#ifndef EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_
#define EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_

#include <stdint.h>
#include <string.h>
#include <string>
#include "SkTDArray.h"
#include "SkTDict.h"
#include "SkRect.h"
#include "SkMatrix.h"
#include "SkString.h"

#include "SkPdfNYI.h"
#include "SkPdfConfig.h"
#include "SkPdfUtils.h"

#include "SkPdfNativeTokenizer.h"

class SkPdfDictionary;
class SkPdfStream;
class SkPdfAllocator;

// TODO(edisonn): macro it and move it to utils
SkMatrix SkMatrixFromPdfMatrix(double array[6]);


#define kFilteredStreamBit 0
#define kUnfilteredStreamBit 1
#define kOwnedStreamBit 2

class SkPdfNativeObject {
 public:
     enum ObjectType {
         kInvalid_PdfObjectType,

         kBoolean_PdfObjectType,
         kInteger_PdfObjectType,
         kReal_PdfObjectType,
         kString_PdfObjectType,
         kHexString_PdfObjectType,
         kName_PdfObjectType,
         kKeyword_PdfObjectType,
         //kStream_PdfObjectType,  //  attached to a Dictionary
         kArray_PdfObjectType,
         kDictionary_PdfObjectType,
         kNull_PdfObjectType,

         // TODO(edisonn): after the pdf has been loaded completely, resolve all references
         // try the same thing with delayed loaded ...
         kReference_PdfObjectType,

         kUndefined_PdfObjectType,  // per 1.4 spec, if the same key appear twice in the dictionary, the value is undefined
     };

     enum DataType {
         kEmpty_Data,
         kFont_Data,
         kBitmap_Data,
     };

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

    // TODO(edisonn): add stream start, stream end, where stream is weither the file
    // or decoded/filtered pdf stream

    // TODO(edisonn): add warning/report per object
    // TODO(edisonn): add flag fUsed, to be used once the parsing is complete,
    // so we could show what parts have been proccessed, ignored, or generated errors

    ObjectType fObjectType;

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

        // TODO(edisonn): make sure the foorprint of fArray and fMap is small, otherwise, use pointers, or classes with up to 8 bytes in footprint
        SkTDArray<SkPdfNativeObject*>* fArray;
        Reference fRef;
    };
    SkTDict<SkPdfNativeObject*>* fMap;

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


public:

    SkPdfNativeObject() : fObjectType(kInvalid_PdfObjectType), fMap(NULL), fData(NULL), fDataType(kEmpty_Data) {}


    inline bool hasData(DataType type) {
        return type == fDataType;
    }

    inline void* data(DataType type) {
        return type == fDataType ? fData : NULL;
    }

    inline void setData(void* data, DataType type) {
        releaseData();
        fDataType = type;
        fData = data;
    }

    void releaseData();

//    ~SkPdfNativeObject() {
//        //reset();  must be called manually!
//    }

    void reset() {
        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();
    }

    ObjectType type() { return fObjectType; }

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

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

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

            default:
                // TODO(edisonn): report/warning
                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;
    }

    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;
    }

    static void makeInteger(int64_t value, SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

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

    static void makeReal(double value, SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

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

    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;
    }

    static SkPdfNativeObject kNull;

    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);
        }
    }

    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;
    }


    static void makeString(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kString_PdfObjectType);
    }

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

    static void makeString(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kString_PdfObjectType);
    }


    static void makeHexString(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kHexString_PdfObjectType);
    }

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

    static void makeHexString(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kHexString_PdfObjectType);
    }


    static void makeName(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kName_PdfObjectType);
    }

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

    static void makeName(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kName_PdfObjectType);
    }


    static void makeKeyword(const unsigned char* start, SkPdfNativeObject* obj) {
        makeStringCore(start, strlen((const char*)start), obj, kKeyword_PdfObjectType);
    }

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

    static void makeKeyword(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj) {
        makeStringCore(start, bytes, obj, kKeyword_PdfObjectType);
    }



    // TODO(edisonn): make the functions to return SkPdfArray, move these functions in SkPdfArray
    static void makeEmptyArray(SkPdfNativeObject* obj) {
        SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);

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

    bool appendInArray(SkPdfNativeObject* obj) {
        SkASSERT(fObjectType == kArray_PdfObjectType);
        if (fObjectType != kArray_PdfObjectType) {
            // TODO(edisonn): report err
            return false;
        }

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

    size_t size() const {
        SkASSERT(fObjectType == kArray_PdfObjectType);

        return fArray->count();
    }

    SkPdfNativeObject* objAtAIndex(int i) {
        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    SkPdfNativeObject* removeLastInArray() {
        SkASSERT(fObjectType == kArray_PdfObjectType);

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

        return ret;
    }


    const SkPdfNativeObject* objAtAIndex(int i) const {
        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    SkPdfNativeObject* operator[](int i) {
        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }

    const SkPdfNativeObject* operator[](int i) const {
        SkASSERT(fObjectType == kArray_PdfObjectType);

        return (*fArray)[i];
    }


    // TODO(edisonn): make the functions to return SkPdfDictionary, move these functions in SkPdfDictionary
    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): 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 twike, will make the value undefined!
    bool set(const SkPdfNativeObject* key, SkPdfNativeObject* value) {
        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key->fObjectType == kName_PdfObjectType);

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

        //// we rewrite all delimiters and white spaces with '\0', so we expect the end of name to be '\0'
        //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');

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

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

    bool set(const unsigned char* key, size_t len, SkPdfNativeObject* value) {
        SkASSERT(fObjectType == kDictionary_PdfObjectType);

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

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

    SkPdfNativeObject* get(const SkPdfNativeObject* key) {
        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key->fObjectType == kName_PdfObjectType);

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

        //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');

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

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

    SkPdfNativeObject* get(const unsigned char* key, size_t len) {
        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key);
        if (fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report err
            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;
    }

    const SkPdfNativeObject* get(const SkPdfNativeObject* key) const {
        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key->fObjectType == kName_PdfObjectType);

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

        //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');

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

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

    const SkPdfNativeObject* get(const unsigned char* key, size_t len) const {
        SkASSERT(fObjectType == kDictionary_PdfObjectType);
        SkASSERT(key);
        if (fObjectType != kDictionary_PdfObjectType) {
            // TODO(edisonn): report err
            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;
    }

    const SkPdfNativeObject* get(const char* key, const char* abr) const {
        const SkPdfNativeObject* ret = get(key);
        // TODO(edisonn): / is a valid name, and it might be an abreviation, so "" should not be like NULL
        // make this distiontion in generator, and remove "" from condition
        if (ret != NULL || abr == NULL || *abr == '\0') {
            return ret;
        }
        return get(abr);
    }

    SkPdfNativeObject* get(const char* key, const char* abr) {
        SkPdfNativeObject* ret = get(key);
        // TODO(edisonn): / is a valid name, and it might be an abreviation, so "" should not be like NULL
        // make this distiontion in generator, and remove "" from condition
        if (ret != NULL || abr == NULL || *abr == '\0') {
            return ret;
        }
        return get(abr);
    }

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

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


    bool isReference() const {
        return fObjectType == kReference_PdfObjectType;
    }

    bool isBoolean() const {
        return fObjectType == kBoolean_PdfObjectType;
    }

    bool isInteger() const {
        return fObjectType == kInteger_PdfObjectType;
    }
private:
    bool isReal() const {
        return fObjectType == kReal_PdfObjectType;
    }
public:
    bool isNumber() const {
        return fObjectType == kInteger_PdfObjectType || fObjectType == kReal_PdfObjectType;
    }

    bool isKeywordReference() const {
        return fObjectType == kKeyword_PdfObjectType && fStr.fBytes == 1 && fStr.fBuffer[0] == 'R';
    }

    bool isKeyword() const {
        return fObjectType == kKeyword_PdfObjectType;
    }

    bool isKeyword(const char* keyword) const {
        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;
    }

    bool isName() const {
        return fObjectType == kName_PdfObjectType;
    }

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

    bool isArray() const {
        return fObjectType == kArray_PdfObjectType;
    }

    bool isDate() const {
        return fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType;
    }

    bool isDictionary() const {
        return fObjectType == kDictionary_PdfObjectType;
    }

    bool isFunction() const {
        return false;  // NYI
    }

    bool isRectangle() const {
        return fObjectType == kArray_PdfObjectType && fArray->count() == 4; // NYI + and elems are numbers
    }

    // TODO(edisonn): has stream .. or is stream ... TBD
    bool hasStream() const {
        return isDictionary() && fStr.fBuffer != NULL;
    }

    // TODO(edisonn): has stream .. or is stream ... TBD
    const SkPdfStream* getStream() const {
        return hasStream() ? (const SkPdfStream*)this : NULL;
    }

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

    bool isAnyString() const {
        return fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType;
    }

    bool isHexString() const {
        return fObjectType == kHexString_PdfObjectType;
    }

    bool isMatrix() const {
        return fObjectType == kArray_PdfObjectType && fArray->count() == 6; // NYI + and elems are numbers
    }

    inline int64_t intValue() const {
        SkASSERT(fObjectType == kInteger_PdfObjectType);

        if (fObjectType != kInteger_PdfObjectType) {
            // TODO(edisonn): log err
            return 0;
        }
        return fIntegerValue;
    }
private:
    inline double realValue() const {
        SkASSERT(fObjectType == kReal_PdfObjectType);

        if (fObjectType != kReal_PdfObjectType) {
            // TODO(edisonn): log err
            return 0;
        }
        return fRealValue;
    }
public:
    inline double numberValue() const {
        SkASSERT(isNumber());

        if (!isNumber()) {
            // TODO(edisonn): log err
            return 0;
        }
        return fObjectType == kReal_PdfObjectType ? fRealValue : fIntegerValue;
    }

    inline SkScalar scalarValue() const {
        SkASSERT(isNumber());

        if (!isNumber()) {
            // TODO(edisonn): log err
            return SkIntToScalar(0);
        }
        return fObjectType == kReal_PdfObjectType ? SkDoubleToScalar(fRealValue) :
                                                    SkIntToScalar(fIntegerValue);
    }

    int referenceId() const {
        SkASSERT(fObjectType == kReference_PdfObjectType);
        return fRef.fId;
    }

    int referenceGeneration() const {
        SkASSERT(fObjectType == kReference_PdfObjectType);
        return fRef.fGen;
    }

    inline const char* nameValue() const {
        SkASSERT(fObjectType == kName_PdfObjectType);

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

    inline const char* stringValue() const {
        SkASSERT(fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType);

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

    inline NotOwnedString strRef() {
        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 std::string nameValue2() const {
        SkASSERT(fObjectType == kName_PdfObjectType);

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

    inline std::string stringValue2() const {
        SkASSERT(fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType);

        if (fObjectType != kString_PdfObjectType && fObjectType != kHexString_PdfObjectType) {
            // TODO(edisonn): log err
            return "";
        }
        return std::string((const char*)fStr.fBuffer, fStr.fBytes);
    }

    inline bool boolValue() const {
        SkASSERT(fObjectType == kBoolean_PdfObjectType);

        if (fObjectType != kBoolean_PdfObjectType) {
            // TODO(edisonn): log err
            return false;
        }
        return fBooleanValue;
    }

    SkRect rectangleValue() const {
        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 error
                return SkRect::MakeEmpty();
            }
            array[i] = elem->numberValue();
        }

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

    SkMatrix matrixValue() const {
        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 error
                return SkMatrix::I();
            }
            array[i] = elem->numberValue();
        }

        return SkMatrixFromPdfMatrix(array);
    }

    bool filterStream();


    bool GetFilteredStreamRef(unsigned char const** buffer, size_t* len) {
        // TODO(edisonn): add params that couls 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
        }

        return true;
    }

    bool isStreamFiltered() const {
        return hasStream() && ((fStr.fBytes & 1) == kFilteredStreamBit);
    }

    bool isStreamOwned() const {
        return hasStream() && ((fStr.fBytes & 2) == kOwnedStreamBit);
    }

    bool GetUnfilteredStreamRef(unsigned char const** buffer, size_t* len) const {
        if (isStreamFiltered()) {
            return false;
        }

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

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

        if (len) {
            *len = fStr.fBytes >> 2;  // remove last 2 bits
        }

        return true;
    }

    bool addStream(const unsigned char* buffer, size_t len) {
        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);
            }
        }
    }

    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();
};

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  // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_