OLD | NEW |
1 | 1 |
2 | 2 |
3 import sys | 3 import sys |
4 | 4 |
5 import datatypes | 5 import datatypes |
6 import pdfspec_autogen | 6 import pdfspec_autogen |
7 | 7 |
8 knowTypes = { | 8 knowTypes = { |
9 '(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'], | 9 '(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'], |
10 '(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'tr
ue'], | 10 '(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'tr
ue'], |
11 '(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true
'], | 11 '(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true
'], |
12 'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->p
odofo()->GetDataType() == ePdfDataType_Array'], | 12 'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->p
odofo()->GetDataType() == ePdfDataType_Array'], |
13 'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->p
odofo()->GetDataType() == ePdfDataType_Bool'], | 13 'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->p
odofo()->GetDataType() == ePdfDataType_Bool'], |
14 'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podo
fo()->GetDataType() == ePdfDataType_Array'], | 14 'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podo
fo()->GetDataType() == ePdfDataType_Array'], |
15 'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull
(), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'], | 15 'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull
(), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'], |
16 'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNon
e(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], | 16 'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNon
e(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], |
17 'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo(
)->GetDataType() == ePdfDataType_Number'], | 17 'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo(
)->GetDataType() == ePdfDataType_Number'], |
18 'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.File
SpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], | 18 'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.File
SpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], |
19 'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->p
odofo()->GetDataType() == ePdfDataType_Name'], | 19 'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->p
odofo()->GetDataType() == ePdfDataType_Name'], |
20 'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo()
->GetDataType() == ePdfDataType_Reference'], | 20 'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo()
->GetDataType() == ePdfDataType_Reference'], |
21 'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podof
o()->GetDataType() == ePdfDataType_Real || ret->podofo()->GetDataType() == ePdfD
ataType_Number'], | 21 'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podof
o()->GetDataType() == ePdfDataType_Real || ret->podofo()->GetDataType() == ePdfD
ataType_Number'], |
22 'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret->
podofo()->GetDataType() == ePdfDataType_Array'], | 22 'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret->
podofo()->GetDataType() == ePdfDataType_Array'], |
23 'stream': ['SkPdfStream', 'StreamFromDictionary', datatypes.PdfEmptyStream(), '
ret->podofo()->HasStream()'], | 23 'stream': ['SkPdfStream*', 'StreamFromDictionary', datatypes.CppNull(), 'ret->p
odofo()->HasStream()'], |
24 'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 're
t->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType(
) == ePdfDataType_HexString'], | 24 'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 're
t->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType(
) == ePdfDataType_HexString'], |
25 'text': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret-
>podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType()
== ePdfDataType_HexString'], | 25 'text': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret-
>podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType()
== ePdfDataType_HexString'], |
26 'text string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""')
, 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetData
Type() == ePdfDataType_HexString'], | 26 'text string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""')
, 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetData
Type() == ePdfDataType_HexString'], |
27 } | 27 } |
28 | 28 |
29 | 29 |
30 class PdfField: | 30 class PdfField: |
31 def __init__(self, parent, name, abr): | 31 def __init__(self, parent, name, abr): |
32 self.fParent = parent | 32 self.fParent = parent |
33 self.fName = name | 33 self.fName = name |
34 self.fAbr = abr | 34 self.fAbr = abr |
35 | 35 |
36 self.fDefault = '' | 36 self.fDefault = '' |
37 self.fTypes = '' | 37 self.fTypes = '' |
38 self.fCppName = '' | 38 self.fCppName = '' |
39 self.fEnumValues = [] | 39 self.fEnumValues = [] |
40 self.fHasMust = False | 40 self.fHasMust = False |
41 self.fMustBe = '' | 41 self.fMustBe = [] |
42 self.fComment = '' | 42 self.fComment = '' |
43 | 43 |
44 def must(self, value): | 44 def must(self, value): |
45 self.fHasMust = True | 45 self.fHasMust = True |
46 self.fMustBe = value | 46 self.fMustBe = value |
47 return self | 47 return self |
48 | 48 |
49 def default(self, value): | 49 def default(self, value): |
50 self.fDefault = value | 50 self.fDefault = value |
51 return self | 51 return self |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 if cls.fName != 'Object': | 205 if cls.fName != 'Object': |
206 fileClass.write('private:\n') | 206 fileClass.write('private:\n') |
207 base = self.fClasses[cls.fBase] | 207 base = self.fClasses[cls.fBase] |
208 cnt = 0 | 208 cnt = 0 |
209 for sub in base.fEnumSubclasses: | 209 for sub in base.fEnumSubclasses: |
210 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName: | 210 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName: |
211 self.writeAsNull(fileClass, enumToCls[base.fEnumSubclasses[cnt]], enum
ToCls) | 211 self.writeAsNull(fileClass, enumToCls[base.fEnumSubclasses[cnt]], enum
ToCls) |
212 cnt = cnt + 1 | 212 cnt = cnt + 1 |
213 | 213 |
214 | 214 |
| 215 def determineAllMustBe(self, cls, field, enumToCls): |
| 216 mustBe = [] |
| 217 for sub in cls.fEnumSubclasses: |
| 218 mustBe = mustBe + self.determineAllMustBe(enumToCls[sub], field, enumToCls
) |
| 219 |
| 220 for subField in cls.fFields: |
| 221 if subField.fProp.fName == field.fProp.fName: |
| 222 mustBe = mustBe + subField.fProp.fMustBe |
| 223 |
| 224 # while cls.fBase != '': |
| 225 # cls = self.fClasses[cls.fBase] |
| 226 # # TODO(edisonn): bad perf |
| 227 # for subField in cls.fFields: |
| 228 # if subField.fProp.fName == field.fProp.fName: |
| 229 # mustBe = mustBe + subField.fProp.fMustBe |
| 230 |
| 231 return mustBe |
215 | 232 |
216 def write(self): | 233 def write(self): |
217 global fileHeaders | 234 global fileHeaders |
218 global knowTypes | 235 global knowTypes |
219 | 236 |
220 # generate enum | 237 # generate enum |
221 enumsRoot = [] | 238 enumsRoot = [] |
222 | 239 |
223 enumToCls = {} | 240 enumToCls = {} |
224 | 241 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 | 424 |
408 | 425 |
409 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name +
'** out) {\n') | 426 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name +
'** out) {\n') |
410 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n') | 427 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n') |
411 fileMapper.write(' }\n') | 428 fileMapper.write(' }\n') |
412 fileMapper.write('\n') | 429 fileMapper.write('\n') |
413 | 430 |
414 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const
PdfObject& podofoObj, SkPdf' + name + '** out) {\n') | 431 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const
PdfObject& podofoObj, SkPdf' + name + '** out) {\n') |
415 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return fa
lse;\n') | 432 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return fa
lse;\n') |
416 fileMapper.write('\n') | 433 fileMapper.write('\n') |
| 434 |
| 435 # stream must be last one |
| 436 hasStream = False |
| 437 for sub in cls.fEnumSubclasses: |
| 438 if cls.fName == 'Object' and enumToCls[sub].fName == 'Stream': |
| 439 hasStream = True |
| 440 else: |
| 441 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCl
s[sub].fName + '**)out)) return true;\n') |
417 | 442 |
418 for sub in cls.fEnumSubclasses: | 443 if hasStream: |
419 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[
sub].fName + '**)out)) return true;\n') | 444 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdfStream**)out))
return true;\n') |
| 445 |
420 | 446 |
421 fileMapper.write('\n') | 447 fileMapper.write('\n') |
422 | 448 |
423 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj)
;\n') | 449 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj)
;\n') |
424 fileMapper.write(' return true;\n') | 450 fileMapper.write(' return true;\n') |
425 fileMapper.write(' }\n') | 451 fileMapper.write(' }\n') |
426 fileMapper.write('\n') | 452 fileMapper.write('\n') |
427 | 453 |
428 for name in self.fClassesNamesInOrder: | 454 for name in self.fClassesNamesInOrder: |
429 cls = self.fClasses[name] | 455 cls = self.fClasses[name] |
430 | 456 |
431 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podof
oDoc, const PdfObject& podofoObj) {\n') | 457 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podof
oDoc, const PdfObject& podofoObj) {\n') |
432 | 458 |
433 if cls.fCheck != '': | 459 if cls.fCheck != '': |
434 fileMapper.write(' return ' + cls.fCheck + ';\n') | 460 fileMapper.write(' return ' + cls.fCheck + ';\n') |
435 else: | 461 else: |
436 cntMust = 0 | 462 cntMust = 0 |
437 for field in cls.fFields: | 463 for field in cls.fFields: |
438 prop = field.fProp | 464 prop = field.fProp |
439 if prop.fHasMust: | 465 if prop.fHasMust: |
440 cntMust = cntMust + 1 | 466 cntMust = cntMust + 1 |
441 fileMapper.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' +
prop.fCppName + ';\n') | 467 fileMapper.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' +
prop.fCppName + ';\n') |
442 fileMapper.write(' if (!' + knowTypes[prop.fTypes.strip()][1] + '
(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr
+ '\", &' + prop.fCppName + ')) return false;\n') | 468 fileMapper.write(' if (!' + knowTypes[prop.fTypes.strip()][1] + '
(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr
+ '\", &' + prop.fCppName + ')) return false;\n') |
443 fileMapper.write(' if (' + prop.fCppName + ' != ' + prop.fMustBe.
toCpp() + ') return false;\n') | 469 |
444 fileMapper.write('\n') | 470 eval = ''; |
| 471 # TODO(edisonn): this could get out of hand, and could have poor per
formance if continued on this path |
| 472 # but if we would write our parser, then best thing would be to crea
te a map of (key, value) -> to bits |
| 473 # and at each (key, value) we do an and with the bits existent, then
we check what bits are left, which would tell the posible types of this diction
ary |
| 474 # and for non unique posinilities (if any) based on context, or the
requester of dictionry we can determine fast the dictionary type |
| 475 mustBe = self.determineAllMustBe(cls, field, enumToCls) |
| 476 if len(mustBe) > 0: |
| 477 for cnd in mustBe: |
| 478 if eval == '': |
| 479 eval = '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')' |
| 480 else: |
| 481 eval = eval + ' && ' + '(' + prop.fCppName + ' != ' + cnd.toCp
p() + ')' |
| 482 fileMapper.write(' if (' + eval + ') return false;\n') |
| 483 fileMapper.write('\n') |
445 | 484 |
446 fileMapper.write(' return true;\n') | 485 fileMapper.write(' return true;\n') |
447 | 486 |
448 fileMapper.write(' }\n') | 487 fileMapper.write(' }\n') |
449 fileMapper.write('\n') | 488 fileMapper.write('\n') |
450 | 489 |
451 fileMapper.write('};\n') | 490 fileMapper.write('};\n') |
452 fileMapper.write('\n') | 491 fileMapper.write('\n') |
453 | 492 |
454 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n') | 493 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n') |
(...skipping 21 matching lines...) Expand all Loading... |
476 | 515 |
477 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Num
ber || podofoObj.GetDataType() == ePdfDataType_Real')\ | 516 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Num
ber || podofoObj.GetDataType() == ePdfDataType_Real')\ |
478 .carbonCopyPublic('long value() const {return fPodo
foObj->GetNumber();}') | 517 .carbonCopyPublic('long value() const {return fPodo
foObj->GetNumber();}') |
479 | 518 |
480 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDa
taType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\ | 519 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDa
taType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\ |
481 .carbonCopyPublic('double value() const {return fPo
dofoObj->GetReal();}') | 520 .carbonCopyPublic('double value() const {return fPo
dofoObj->GetReal();}') |
482 | 521 |
483 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')
\ | 522 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')
\ |
484 .carbonCopyPublic('const std::string& value() const
{return fPodofoObj->GetName().GetName();}') | 523 .carbonCopyPublic('const std::string& value() const
{return fPodofoObj->GetName().GetName();}') |
485 | 524 |
486 #manager.addClass('Stream') - attached to a dictionary | |
487 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_R
eference') | 525 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_R
eference') |
488 | 526 |
489 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array
')\ | 527 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array
')\ |
490 .carbonCopyPublic('const int size() const {return f
PodofoObj->GetArray().GetSize();}')\ | 528 .carbonCopyPublic('const int size() const {return f
PodofoObj->GetArray().GetSize();}')\ |
491 .carbonCopyPublic('const SkPdfObject operator[](int
i) const {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\ | 529 .carbonCopyPublic('SkPdfObject* operator[](int i) c
onst {return new SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\ |
492 .carbonCopyPublic('SkPdfObject operator[](int i) {r
eturn SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}') | |
493 | 530 |
494 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_Stri
ng || podofoObj.GetDataType() == ePdfDataType_HexString')\ | 531 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_Stri
ng || podofoObj.GetDataType() == ePdfDataType_HexString')\ |
495 .carbonCopyPublic('const std::string& value() const
{return fPodofoObj->GetString().GetStringUtf8();}') | 532 .carbonCopyPublic('const std::string& value() const
{return fPodofoObj->GetString().GetStringUtf8();}') |
496 | 533 |
497 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdf
DataType_HexString')\ | 534 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdf
DataType_HexString')\ |
498 .carbonCopyPublic('const std::string& value() const
{return fPodofoObj->GetString().GetStringUtf8();}') | 535 .carbonCopyPublic('const std::string& value() const
{return fPodofoObj->GetString().GetStringUtf8();}') |
499 | 536 |
500 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_
Dictionary')\ | 537 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_
Dictionary')\ |
501 .carbonCopyPublic('const SkPdfObject get(const c
har* dictionaryKeyName) const {return SkPdfObject(fPodofoDoc, resolveReferenceOb
ject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName)))
);}')\ | 538 .carbonCopyPublic('const SkPdfObject get(const c
har* dictionaryKeyName) const {return SkPdfObject(fPodofoDoc, resolveReferenceOb
ject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName)))
);}')\ |
502 .carbonCopyPublic('SkPdfObject get(const char* d
ictionaryKeyName) {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofo
Doc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\ | 539 .carbonCopyPublic('SkPdfObject get(const char* d
ictionaryKeyName) {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofo
Doc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\ |
| 540 |
| 541 manager.addClass('Stream') # attached to a dictionary in podofo |
503 | 542 |
504 | 543 |
505 # these classes are not explicitely backed by a table in the pdf spec | 544 # these classes are not explicitely backed by a table in the pdf spec |
506 manager.addClass('XObjectDictionary', 'Dictionary') | 545 manager.addClass('XObjectDictionary', 'Dictionary') |
507 | 546 |
508 manager.addClass('FontDictionary', 'Dictionary') | 547 manager.addClass('FontDictionary', 'Dictionary') |
509 | 548 |
510 manager.addClass('TrueTypeFontDictionary', 'FontDictionary') | 549 manager.addClass('TrueTypeFontDictionary', 'Type1FontDictionary')\ |
| 550 .required('NULL')\ |
| 551 .field('Subtype')\ |
| 552 .name('Subtype')\ |
| 553 .type('name')\ |
| 554 .comment('')\ |
| 555 .must([datatypes.PdfName('TrueType')])\ |
| 556 .done().done()\ |
511 | 557 |
512 pdfspec_autogen.buildPdfSpec(manager) | 558 pdfspec_autogen.buildPdfSpec(manager) |
513 | 559 |
514 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\ | 560 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\ |
515 .required('NULL')\ | 561 .required('NULL')\ |
516 .field('Subtype')\ | 562 .field('Subtype')\ |
517 .name('Subtype')\ | 563 .name('Subtype')\ |
518 .type('name')\ | 564 .type('name')\ |
519 .comment('')\ | 565 .comment('')\ |
520 .must(datatypes.PdfName('MMType1'))\ | 566 .must([datatypes.PdfName('MMType1')])\ |
521 .done().done()\ | 567 .done().done()\ |
522 | 568 |
523 | 569 |
524 manager.write() | 570 manager.write() |
525 | 571 |
526 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n') | 572 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n') |
527 fileHeaders.close() | 573 fileHeaders.close() |
528 | 574 |
529 return 1 | 575 return 1 |
530 | 576 |
531 if '__main__' == __name__: | 577 if '__main__' == __name__: |
532 sys.exit(generateCode()) | 578 sys.exit(generateCode()) |
533 | 579 |
OLD | NEW |