Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: experimental/PdfViewer/generate_code.py

Issue 16844017: most pdf dictionary types suported, need enabling now (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « experimental/PdfViewer/PdfReference-okular-1.txt ('k') | experimental/PdfViewer/spec2def.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys 1 import sys
2 2
3 class PdfName: 3 class PdfName:
4 def __init__(self, name, abr=''): 4 def __init__(self, name, abr=''):
5 self.fName = name 5 self.fName = name
6 self.fAbr = abr 6 self.fAbr = abr
7 7
8 def toCpp(self): 8 def toCpp(self):
9 return '\"' + self.fName + '\"' 9 return '\"' + self.fName + '\"'
10 10
(...skipping 25 matching lines...) Expand all
36 def toCpp(self): 36 def toCpp(self):
37 return self.fValue 37 return self.fValue
38 38
39 class PdfBoolean: 39 class PdfBoolean:
40 def __init__(self, value): 40 def __init__(self, value):
41 self.fValue = value 41 self.fValue = value
42 42
43 def toCpp(self): 43 def toCpp(self):
44 return self.fValue 44 return self.fValue
45 45
46 class CppNull:
47 def toCpp(self):
48 return 'NULL'
49
50
46 class PdfField: 51 class PdfField:
47 def __init__(self, parent, name, abr): 52 def __init__(self, parent, name, abr):
48 self.fParent = parent 53 self.fParent = parent
49 self.fName = name 54 self.fName = name
50 self.fAbr = abr 55 self.fAbr = abr
51 56
52 self.fDefault = '' 57 self.fDefault = ''
53 self.fType = '' 58 self.fType = ''
54 self.fCppName = '' 59 self.fCppName = ''
55 self.fCppType = '' 60 self.fCppType = ''
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 self.fType = 'string' 104 self.fType = 'string'
100 self.fCppName = name 105 self.fCppName = name
101 self.fCppType = 'std::string' 106 self.fCppType = 'std::string'
102 self.fCppReader = 'StringFromDictionary' 107 self.fCppReader = 'StringFromDictionary'
103 return self 108 return self
104 109
105 def multiple(self, validOptions): 110 def multiple(self, validOptions):
106 self.fValidOptions = validOptions 111 self.fValidOptions = validOptions
107 return self 112 return self
108 113
114 def dictionary(self, name):
115 self.fType = 'dictionary'
116 self.fCppName = name
117 self.fDictionaryType = 'Resources' # TODO(edisonn): Dictionary?
118 self.fCppReader = 'DictionaryFromDictionary'
119 self.fDefault = CppNull()
120 return self
121
122 def type(self, type):
123 # TODO (edisonn): if simple type, use it, otherwise set it to Dictionary, an d set a mask for valid types, like array or name
124 self.fType = 'dictionary'
125 self.fDictionaryType = 'Dictionary'
126 self.fCppReader = 'DictionaryFromDictionary'
127 self.fDefault = CppNull()
128 return self
129
130 def comment(self, comment):
131 return self
132
109 def done(self): 133 def done(self):
110 return self.fParent 134 return self.fParent
111 135
112 136
113 class PdfClassField: 137 class PdfClassField:
114 def __init__(self, parent, required): 138 def __init__(self, parent, required, version='', inheritable=False, comment='' ):
115 #self.fProp = '' 139 #self.fProp = ''
116 self.fParent = parent 140 self.fParent = parent
117 self.fRequired = required 141 self.fRequired = required
142 self.fVersion = version
143 self.fInheritable = inheritable
144 self.fComment = comment
118 145
119 def field(self, name, abr=''): 146 def field(self, name, abr=''):
120 self.fProp = PdfField(self, name, abr) 147 self.fProp = PdfField(self, name, abr)
121 return self.fProp 148 return self.fProp
122 149
123 def done(self): 150 def done(self):
124 return self.fParent 151 return self.fParent
125 152
126 class PdfClass: 153 class PdfClass:
127 def __init__(self, name, base): 154 def __init__(self, name, base, comment):
128 self.fFields = [] 155 self.fFields = []
129 self.fIncludes = [] 156 self.fIncludes = []
130 self.fCCPublic = [] 157 self.fCCPublic = []
131 self.fCCPrivate = [] 158 self.fCCPrivate = []
132 self.fName = name 159 self.fName = name
133 self.fBase = base 160 self.fBase = base
161 self.fComment = comment
134 162
135 self.fEnumSubclasses = [] 163 self.fEnumSubclasses = []
136 164
137 self.fEnum = '!UNDEFINED' 165 self.fEnum = '!UNDEFINED'
138 self.fEnumEnd = '!UNDEFINED' 166 self.fEnumEnd = '!UNDEFINED'
139 167
140 def required(self, badDefault): 168 def required(self, badDefault):
141 field = PdfClassField(self, True) 169 field = PdfClassField(self, True)
142 field.fBadDefault = badDefault 170 field.fBadDefault = badDefault
143 self.fFields.append(field) 171 self.fFields.append(field)
144 return field 172 return field
145 173
146 def optional(self): 174 def optional(self):
147 field = PdfClassField(self, False) 175 field = PdfClassField(self, False)
148 self.fFields.append(field) 176 self.fFields.append(field)
149 return field 177 return field
178
179 #([Required] [;] [inheritable] [;] [version]; [comments])
180 # version: PDF [d].[d]
181 # ; separate props
182 #inheritable
183 #version
184 #required, if
185 #optional, if
150 186
151 def include(self, path): 187 def include(self, path):
152 self.fIncludes.append(path) 188 self.fIncludes.append(path)
153 return self 189 return self
154 190
155 def carbonCopyPublic(self, cc): 191 def carbonCopyPublic(self, cc):
156 self.fCCPublic.append(cc) 192 self.fCCPublic.append(cc)
157 return self 193 return self
158 194
159 def carbonCopyPrivate(self, cc): 195 def carbonCopyPrivate(self, cc):
160 self.fCCPrivate.append(cc) 196 self.fCCPrivate.append(cc)
161 return self 197 return self
198
199 def done(self):
200 return
162 201
163 class PdfClassManager: 202 class PdfClassManager:
164 def __init__(self): 203 def __init__(self):
165 self.fClasses = {} 204 self.fClasses = {}
166 self.fClassesNamesInOrder = [] 205 self.fClassesNamesInOrder = []
167 206
168 def addClass(self, name, base='Object'): 207 def addClass(self, name, base='Object', comment=''):
169 if name == 'Object': 208 if name == 'Object':
170 cls = PdfClass(name, '') 209 cls = PdfClass(name, '', comment)
171 else: 210 else:
172 cls = PdfClass(name, base) 211 cls = PdfClass(name, base, comment)
173 self.fClasses[name] = cls 212 self.fClasses[name] = cls
174 self.fClassesNamesInOrder.append(name) 213 self.fClassesNamesInOrder.append(name)
175 return cls 214 return cls
176 215
177 def longName(self, name): 216 def longName(self, name):
178 ret = '' 217 ret = ''
179 while name != '': 218 while name != '':
180 cls = self.fClasses[name] 219 cls = self.fClasses[name]
181 ret = name + ret 220 ret = name + ret
182 name = cls.fBase 221 name = cls.fBase
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 if cls.fBase == '': 343 if cls.fBase == '':
305 print('protected:') 344 print('protected:')
306 print(' const PdfMemDocument* fPodofoDoc;') 345 print(' const PdfMemDocument* fPodofoDoc;')
307 print(' const PdfObject* fPodofoObj;') 346 print(' const PdfObject* fPodofoObj;')
308 print 347 print
309 print('public:') 348 print('public:')
310 print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc, const P dfObject* podofoObj) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}') 349 print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc, const P dfObject* podofoObj) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}')
311 print(' const PdfObject* podofo() const { return fPodofoObj;}') 350 print(' const PdfObject* podofo() const { return fPodofoObj;}')
312 else: 351 else:
313 print('public:') 352 print('public:')
314 print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc, const Pd fObject* podofoObj) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}') 353 print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc, const P dfObject* podofoObj) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}')
354 print
315 355
316 #check required fieds, also, there should be an internal_valid() manually wrote for complex 356 #check required fieds, also, there should be an internal_valid() manually wrote for complex
317 # situations 357 # situations
318 # right now valid return true 358 # right now valid return true
319 print(' virtual bool valid() const {return true;}') 359 print(' virtual bool valid() const {return true;}')
360 print
320 361
321 for field in cls.fFields: 362 for field in cls.fFields:
322 prop = field.fProp 363 prop = field.fProp
323 if prop.fCppName != '': 364 if prop.fCppName != '':
365 if prop.fType != 'dictionary':
324 print(' ' + prop.fCppType + ' ' + prop.fCppName + '() const {') 366 print(' ' + prop.fCppType + ' ' + prop.fCppName + '() const {')
325 print(' ' + prop.fCppType + ' ret;') 367 print(' ' + prop.fCppType + ' ret;')
326 print(' if (' + prop.fCppReader + '(fPodofoDoc, fPodofoObj->GetDi ctionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;') 368 print(' if (' + prop.fCppReader + '(fPodofoDoc, fPodofoObj->GetDi ctionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;')
327 if field.fRequired == False: 369 if field.fRequired == False:
328 print(' return ' + prop.fDefault.toCpp() + ';'); 370 print(' return ' + prop.fDefault.toCpp() + ';');
329 if field.fRequired == True: 371 if field.fRequired == True:
330 print(' // TODO(edisonn): warn about missing required field, as sert for known good pdfs') 372 print(' // TODO(edisonn): warn about missing required field, as sert for known good pdfs')
331 print(' return ' + field.fBadDefault + ';'); 373 print(' return ' + field.fBadDefault + ';');
332 print(' }') 374 print(' }')
333 print 375 print
376
377 if prop.fType == 'dictionary':
378 print(' SkPdf' + prop.fDictionaryType + '* ' + prop.fCppName + '() const {')
379 print(' SkPdfObject* dict = NULL;')
380 print(' if (' + prop.fCppReader + '(fPodofoDoc, fPodofoObj->GetDi ctionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &dict) && dict != NUL L) {')
381 print(' SkPdf' + prop.fDictionaryType + '* ret = new SkPdf' + p rop.fDictionaryType + '(fPodofoDoc, dict->podofo());')
382 print(' delete dict; dict = NULL;')
383 print(' return ret;')
384 print(' }')
385 if field.fRequired == False:
386 print(' return ' + prop.fDefault.toCpp() + ';');
387 if field.fRequired == True:
388 print(' // TODO(edisonn): warn about missing required field, as sert for known good pdfs')
389 print(' return ' + field.fBadDefault + ';');
390 print(' }')
391 print
392
393
334 394
335 print('};') 395 print('};')
336 print 396 print
337 print 397 print
338 398
339 399
340 400
341 # generate constructor when knowing the type 401 # generate constructor when knowing the type
342 # later, p2, generate constructor when not knowing the type - very similar with parsing? 402 # later, p2, generate constructor when not knowing the type - very similar with parsing?
343 403
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 457
398 all.addClass('Object') 458 all.addClass('Object')
399 all.addClass('Null') 459 all.addClass('Null')
400 all.addClass('Boolean') 460 all.addClass('Boolean')
401 all.addClass('Integer') 461 all.addClass('Integer')
402 all.addClass('Real') 462 all.addClass('Real')
403 all.addClass('Name') 463 all.addClass('Name')
404 all.addClass('Stream') 464 all.addClass('Stream')
405 all.addClass('Reference') 465 all.addClass('Reference')
406 all.addClass('Array') 466 all.addClass('Array')
407 all.addClass('Dictionary') 467 all.addClass('Dictionary').optional().field('Resources', '').dictionary("r") # .inherited_from_page_tree()
468
469 all.addClass('Resources', 'Dictionary')
408 470
409 all.addClass('XObject', 'Dictionary').required('""').field('Type').must(PdfNam e('XObject')).name('t') 471 all.addClass('XObject', 'Dictionary').required('""').field('Type').must(PdfNam e('XObject')).name('t')
410 472
411 all.addClass('Image', 'XObject').required('""').field('Type').must(PdfName('XO bject')).name('t').done()\ 473 all.addClass('Image', 'XObject').required('""').field('Type').must(PdfName('XO bject')).name('t').done()\
412 .done()\ 474 .done()\
413 .required('""').field('Subtype').must(PdfName( 'Image')).name('s').done()\ 475 .required('""').field('Subtype').must(PdfName( 'Image')).name('s').done()\
414 .done()\ 476 .done()\
415 .required('-1').field('Width', 'W').integer('w ').done()\ 477 .required('-1').field('Width', 'W').integer('w ').done()\
416 .done()\ 478 .done()\
417 .required('-1').field('Height', 'H').integer(' h').done()\ 479 .required('-1').field('Height', 'H').integer(' h').done()\
418 .done()\ 480 .done()\
419 .required('""').field('ColorSpace').name('cs') .multiple([PdfName('/DeviceRGB', '/RGB'), PdfName('/DeviceGray', '/Gray')]).done ()\ 481 .required('""').field('ColorSpace').name('cs') .multiple([PdfName('/DeviceRGB', '/RGB'), PdfName('/DeviceGray', '/Gray')]).done ()\
420 .done()\ 482 .done()\
421 .optional().field('BitsPerComponent', 'BPC').i nteger('bpc').multiple([PdfInteger(1), PdfInteger(2), PdfInteger(4), PdfInteger( 8)])\ 483 .optional().field('BitsPerComponent', 'BPC').i nteger('bpc').multiple([PdfInteger(1), PdfInteger(2), PdfInteger(4), PdfInteger( 8)])\
422 .default(PdfInteger(1)).done()\ 484 .default(PdfInteger(1)).done()\
423 .done()\ 485 .done()\
424 .carbonCopyPrivate('SkBitmap bitmap;') 486 .carbonCopyPrivate('SkBitmap bitmap;')
425 487
426 all.addClass('Form', 'XObject').required('""').field('Type').must(PdfName('XOb ject')).name('t').done()\ 488 all.addClass('Form', 'XObject').required('""').field('Type').must(PdfName('XOb ject')).name('t').done()\
427 .done()\ 489 .done()\
428 .required('""').field('Subtype').must(PdfName(' Form')).name('s').done()\ 490 .required('""').field('Subtype').must(PdfName(' Form')).name('s').done()\
429 .done()\ 491 .done()\
430 .carbonCopyPublic('void test() {}') 492 .carbonCopyPublic('void test() {}')
431 493
432 494
495
496 all.addClass('SpecificToATrapNetworkAppearanceStream', 'Dictionary', 'Addition al entries specific to a trap network appearance stream')\
497 .required('NULL')\
498 .field('PCM')\
499 .name('PCM')\
500 .type('name')\
501 .comment('(Required) The name of the process color model that was assu med when this trap network was created; equivalent to the PostScript page device parameter ProcessColorModel (see Section 6.2.5 of the PostScript Language Refer ence, Third Edition). Valid values are DeviceGray, DeviceRGB, DeviceCMYK, Device CMY, DeviceRGBK, and DeviceN.')\
502 .done().done()\
503 .optional()\
504 .field('SeparationColorNames')\
505 .name('SeparationColorNames')\
506 .type('array')\
507 .comment('(Optional) An array of names identifying the colorants that were assumed when this network was created; equivalent to the Post- Script page device parameter of the same name (see Section 6.2.5 of the PostScript Language Reference, Third Edition). Colorants im- plied by the process color model PCM ar e available automatically and need not be explicitly declared. If this entry is absent, the colorants implied by PCM are assumed.')\
508 .done().done()\
509 .optional()\
510 .field('TrapRegions')\
511 .name('TrapRegions')\
512 .type('array')\
513 .comment('(Optional) An array of indirect references to TrapRegion obj ects defining the page\'s trapping zones and the associated trapping parameters, as described in Adobe Technical Note #5620, Portable Job Ticket Format. These r eferences are to objects comprising portions of a PJTF job ticket that is embedd ed in the PDF file. When the trapping zones and parameters are defined by an ext ernal job ticket (or by some other means, such as with JDF), this entry is absen t.')\
514 .done().done()\
515 .optional()\
516 .field('TrapStyles')\
517 .name('TrapStyles')\
518 .type('text string')\
519 .comment('(Optional) A human-readable text string that applications ca n use to describe this trap network to the user (for example, to allow switching between trap networks).')\
520 .done().done()\
521 .done()
522
523 all.addClass('OpiVersionDictionary', 'Dictionary', 'Entry in an OPI version di ctionary')\
524 .required('NULL')\
525 .field('version_number')\
526 .name('version_number')\
527 .type('dictionary')\
528 .comment('(Required; PDF 1.2) An OPI dictionary specifying the attribu tes of this proxy (see Tables 9.50 and 9.51). The key for this entry must be the name 1.3 or 2.0, identifying the version of OPI to which the proxy corresponds. ')\
529 .done().done()\
530 .done()
531
532
533
433 all.write() 534 all.write()
434 535
435 return 1 536 return 1
436 537
437 if '__main__' == __name__: 538 if '__main__' == __name__:
438 sys.exit(generateCode()) 539 sys.exit(generateCode())
540
541
OLDNEW
« no previous file with comments | « experimental/PdfViewer/PdfReference-okular-1.txt ('k') | experimental/PdfViewer/spec2def.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698