OLD | NEW |
(Empty) | |
| 1 import sys |
| 2 |
| 3 class PdfName: |
| 4 def __init__(self, name, abr=''): |
| 5 self.fName = name |
| 6 self.fAbr = abr |
| 7 |
| 8 class PdfInteger: |
| 9 def __init__(self, value): |
| 10 self.fValue = value |
| 11 |
| 12 class PdfReal: |
| 13 def __init__(self, value): |
| 14 self.fValue = value |
| 15 |
| 16 class PdfString: |
| 17 def __init__(self, value): |
| 18 self.fValue = value |
| 19 |
| 20 class PdfBoolean: |
| 21 def __init__(self, value): |
| 22 self.fValue = value |
| 23 |
| 24 class PdfField: |
| 25 def __init__(self, parent, name, abr): |
| 26 self.fParent = parent |
| 27 self.fName = name |
| 28 self.fAbr = abr |
| 29 |
| 30 self.fDefault = '' |
| 31 self.fType = '' |
| 32 |
| 33 def must(self, value): |
| 34 return self.fParent |
| 35 |
| 36 def default(self, value): |
| 37 self.fDefault = value |
| 38 return self |
| 39 |
| 40 def number(self): |
| 41 self.fType = 'number' |
| 42 return self |
| 43 |
| 44 def integer(self): |
| 45 self.fType = 'integer' |
| 46 return self |
| 47 |
| 48 def real(self): |
| 49 self.fType = 'real' |
| 50 return self |
| 51 |
| 52 def name(self): |
| 53 self.fType = 'name' |
| 54 return self |
| 55 |
| 56 def string(self): |
| 57 self.fType = 'string' |
| 58 return self |
| 59 |
| 60 def multiple(self, options): |
| 61 self.fType = 'multiple' |
| 62 self.fOptions = options |
| 63 return self |
| 64 |
| 65 def done(self): |
| 66 return self.fParent |
| 67 |
| 68 |
| 69 class PdfClassField: |
| 70 def __init__(self, parent, required): |
| 71 self.fFields = [] |
| 72 self.fIncludes = [] |
| 73 self.fCC = [] |
| 74 self.fParent = parent |
| 75 self.fRequired = required |
| 76 |
| 77 def hasField(self, name, abr=''): |
| 78 return PdfField(self, name, abr) |
| 79 |
| 80 def done(self): |
| 81 return self.fParent |
| 82 |
| 83 class PdfClass: |
| 84 def __init__(self, name, base): |
| 85 self.fFields = [] |
| 86 self.fIncludes = [] |
| 87 self.fCC = [] |
| 88 self.fName = name |
| 89 self.fBase = base |
| 90 |
| 91 def required(self): |
| 92 field = PdfClassField(self, True) |
| 93 self.fFields.append(field) |
| 94 return field |
| 95 |
| 96 def optional(self): |
| 97 field = PdfClassField(self, False) |
| 98 self.fFields.append(field) |
| 99 return field |
| 100 |
| 101 def include(self, path): |
| 102 self.fIncludes.append(path) |
| 103 return self |
| 104 |
| 105 def carbonCopy(self, cc): |
| 106 self.fCC.append(cc) |
| 107 return self |
| 108 |
| 109 class PdfClassManager: |
| 110 def __init__(self): |
| 111 self.fClasses = [] |
| 112 |
| 113 def addClass(self, name, base=''): |
| 114 cls = PdfClass(name, base) |
| 115 self.fClasses.append(cls) |
| 116 return cls |
| 117 |
| 118 def write(self): |
| 119 return |
| 120 |
| 121 def generateCode(): |
| 122 all = PdfClassManager() |
| 123 |
| 124 all.addClass('Object') |
| 125 all.addClass('Null') |
| 126 all.addClass('Boolean') |
| 127 all.addClass('Integer') |
| 128 all.addClass('Real') |
| 129 all.addClass('Name') |
| 130 all.addClass('Stream') |
| 131 all.addClass('Reference') |
| 132 all.addClass('Array') |
| 133 all.addClass('Dictionary') |
| 134 |
| 135 all.addClass('XObject', 'Dictionary').required().hasField('/Type').must('/XObj
ect') |
| 136 |
| 137 all.addClass('Image', 'XObject').required().hasField('/Type').must('/XObject')
.done()\ |
| 138 .required().hasField('/Subtype').must('/Image'
).done()\ |
| 139 .required().hasField('/Width', '/W').integer()
.done().done()\ |
| 140 .required().hasField('/Height', '/H').integer(
).done().done()\ |
| 141 .required().hasField('/ColorSpace').multiple([
PdfName('/DeviceRGB', '/RGB'), PdfName('/DeviceGray', '/Gray')])\ |
| 142
.done()\ |
| 143 .done()\ |
| 144 .optional().hasField('/BitsPerComponent', '/BP
C').multiple([PdfInteger(1), PdfInteger(2), PdfInteger(4), PdfInteger(8)])\ |
| 145
.default(PdfInteger(1))\ |
| 146
.done().done()\ |
| 147 .carbonCopy('SkBitmap bitmap;') |
| 148 |
| 149 all.addClass('Form', 'XObject').required().hasField('/Type').must('/XObject').
done()\ |
| 150 .required().hasField('/Subtype').must('/Form').
done() |
| 151 |
| 152 |
| 153 all.write() |
| 154 |
| 155 return 1 |
| 156 |
| 157 if '__main__' == __name__: |
| 158 sys.exit(generateCode()) |
OLD | NEW |