edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1 | |
| 2 | |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 3 | import sys |
| 4 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 5 | import datatypes |
| 6 | import pdfspec_autogen |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 7 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 8 | knowTypes = { |
| 9 | '(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'], |
| 10 | '(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'], |
| 11 | '(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'], |
| 12 | 'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'], |
| 13 | 'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->podofo()->GetDataType() == ePdfDataType_Bool'], |
| 14 | 'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'], |
| 15 | 'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'], |
| 16 | 'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], |
| 17 | 'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo()->GetDataType() == ePdfDataType_Number'], |
| 18 | 'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.FileSpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], |
| 19 | 'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_Name'], |
| 20 | 'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], |
| 21 | 'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podofo()->GetDataType() == ePdfDataType_Real'], |
| 22 | 'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'], |
| 23 | 'stream': ['SkPdfStream', 'StreamFromDictionary', datatypes.PdfEmptyStream(), 'ret->podofo()->HasStream()'], |
| 24 | 'string': ['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()->GetDataType() == ePdfDataType_HexString'], |
| 27 | } |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 28 | |
| 29 | |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 30 | class PdfField: |
| 31 | def __init__(self, parent, name, abr): |
| 32 | self.fParent = parent |
| 33 | self.fName = name |
| 34 | self.fAbr = abr |
| 35 | |
| 36 | self.fDefault = '' |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 37 | self.fTypes = '' |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 38 | self.fCppName = '' |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 39 | self.fEnumValues = [] |
| 40 | self.fHasMust = False |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 41 | self.fMustBe = '' |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 42 | |
| 43 | def must(self, value): |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 44 | self.fHasMust = True |
| 45 | self.fMustBe = value |
| 46 | return self |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 47 | |
| 48 | def default(self, value): |
| 49 | self.fDefault = value |
| 50 | return self |
| 51 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 52 | def multiple(self, enumValues): |
| 53 | self.fEnumValues = enumValues |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 54 | return self |
| 55 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 56 | def name(self, name): |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 57 | self.fCppName = name |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 58 | return self |
| 59 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 60 | def type(self, types): |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 61 | # TODO (edisonn): if simple type, use it, otherwise set it to Dictionary, and set a mask for valid types, like array or name |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 62 | types = types.strip() |
| 63 | types = types.replace('or', ' ') |
| 64 | types = types.replace(',', ' ') |
| 65 | types = types.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'? |
| 66 | types = types.replace('file specification', 'file_specification') |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 67 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 68 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 69 | self.fTypes = types |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 70 | return self |
| 71 | |
| 72 | def comment(self, comment): |
| 73 | return self |
| 74 | |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 75 | def done(self): |
| 76 | return self.fParent |
| 77 | |
| 78 | |
| 79 | class PdfClassField: |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 80 | def __init__(self, parent, required, version='', inheritable=False, comment=''): |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 81 | #self.fProp = '' |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 82 | self.fParent = parent |
| 83 | self.fRequired = required |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 84 | self.fVersion = version |
| 85 | self.fInheritable = inheritable |
| 86 | self.fComment = comment |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 87 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 88 | def field(self, name, abr=''): |
| 89 | self.fProp = PdfField(self, name, abr) |
| 90 | return self.fProp |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 91 | |
| 92 | def done(self): |
| 93 | return self.fParent |
| 94 | |
| 95 | class PdfClass: |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 96 | def __init__(self, name, base, comment): |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 97 | self.fFields = [] |
| 98 | self.fIncludes = [] |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 99 | self.fCCPublic = [] |
| 100 | self.fCCPrivate = [] |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 101 | self.fName = name |
| 102 | self.fBase = base |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 103 | self.fComment = comment |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 104 | |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 105 | self.fEnumSubclasses = [] |
| 106 | |
| 107 | self.fEnum = '!UNDEFINED' |
| 108 | self.fEnumEnd = '!UNDEFINED' |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 109 | self.fCheck = '' |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 110 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 111 | def check(self, ifCheck): |
| 112 | self.fCheck = ifCheck |
| 113 | return self |
| 114 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 115 | def required(self, badDefault): |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 116 | field = PdfClassField(self, True) |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 117 | field.fBadDefault = badDefault |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 118 | self.fFields.append(field) |
| 119 | return field |
| 120 | |
| 121 | def optional(self): |
| 122 | field = PdfClassField(self, False) |
| 123 | self.fFields.append(field) |
| 124 | return field |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 125 | |
| 126 | #([Required] [;] [inheritable] [;] [version]; [comments]) |
| 127 | # version: PDF [d].[d] |
| 128 | # ; separate props |
| 129 | #inheritable |
| 130 | #version |
| 131 | #required, if |
| 132 | #optional, if |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 133 | |
| 134 | def include(self, path): |
| 135 | self.fIncludes.append(path) |
| 136 | return self |
| 137 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 138 | def carbonCopyPublic(self, cc): |
| 139 | self.fCCPublic.append(cc) |
| 140 | return self |
| 141 | |
| 142 | def carbonCopyPrivate(self, cc): |
| 143 | self.fCCPrivate.append(cc) |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 144 | return self |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 145 | |
| 146 | def done(self): |
| 147 | return |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 148 | |
| 149 | class PdfClassManager: |
| 150 | def __init__(self): |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 151 | self.fClasses = {} |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 152 | self.fClassesNamesInOrder = [] |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 153 | |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 154 | def addClass(self, name, base='Object', comment=''): |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 155 | if name == 'Object': |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 156 | cls = PdfClass(name, '', comment) |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 157 | else: |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 158 | cls = PdfClass(name, base, comment) |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 159 | self.fClasses[name] = cls |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 160 | self.fClassesNamesInOrder.append(name) |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 161 | return cls |
| 162 | |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 163 | def longName(self, name): |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 164 | #return name |
| 165 | # TODO(edisonn): we need the long name to nenerate and sort enums, but we can generate them recursively |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 166 | ret = '' |
| 167 | while name != '': |
| 168 | cls = self.fClasses[name] |
| 169 | ret = name + ret |
| 170 | name = cls.fBase |
| 171 | |
| 172 | return ret |
| 173 | |
| 174 | |
| 175 | def writeEnum(self, enum, enumToCls): |
| 176 | print(' ' + enum + ',') |
| 177 | cls = enumToCls[enum] |
| 178 | cls.fEnumSubclasses.sort() |
| 179 | |
| 180 | cnt = 0 |
| 181 | for sub in cls.fEnumSubclasses: |
| 182 | self.writeEnum(cls.fEnumSubclasses[cnt], enumToCls) |
| 183 | cnt = cnt + 1 |
| 184 | |
| 185 | if cnt != 0: |
| 186 | print(' ' + cls.fEnumEnd + ',') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | def writeAsNull(self, cls, enumToCls): |
| 190 | print(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return NULL;}') |
| 191 | print(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return NULL;}') |
| 192 | print |
| 193 | |
| 194 | cnt = 0 |
| 195 | for sub in cls.fEnumSubclasses: |
| 196 | self.writeAsNull(enumToCls[cls.fEnumSubclasses[cnt]], enumToCls) |
| 197 | cnt = cnt + 1 |
| 198 | |
| 199 | |
| 200 | def writeAsFoo(self, cls, enumToCls): |
| 201 | # TODO(edisonn): add a container, with sections, public, private, default, ... |
| 202 | # the end code will be grouped |
| 203 | |
| 204 | # me |
| 205 | print('public:') |
| 206 | print(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return this;}') |
| 207 | print(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return this;}') |
| 208 | print |
| 209 | |
| 210 | if cls.fName == 'Object': |
| 211 | cnt = 0 |
| 212 | for sub in cls.fEnumSubclasses: |
| 213 | self.writeAsNull(enumToCls[cls.fEnumSubclasses[cnt]], enumToCls) |
| 214 | cnt = cnt + 1 |
| 215 | |
| 216 | if cls.fName != 'Object': |
| 217 | print('private:') |
| 218 | base = self.fClasses[cls.fBase] |
| 219 | cnt = 0 |
| 220 | for sub in base.fEnumSubclasses: |
| 221 | if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName: |
| 222 | self.writeAsNull(enumToCls[base.fEnumSubclasses[cnt]], enumToCls) |
| 223 | cnt = cnt + 1 |
| 224 | |
| 225 | |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 226 | |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 227 | def write(self): |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 228 | |
| 229 | global knowTypes |
| 230 | |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 231 | # generate enum |
| 232 | enumsRoot = [] |
| 233 | |
| 234 | enumToCls = {} |
| 235 | |
| 236 | for name in self.fClasses: |
| 237 | cls = self.fClasses[name] |
| 238 | enum = self.longName(name) |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 239 | cls.fEnum = 'k' + enum + '_SkPdfObjectType' |
| 240 | cls.fEnumEnd = 'k' + enum + '__End_SkPdfObjectType' |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 241 | |
| 242 | if cls.fBase != '': |
| 243 | self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum) |
| 244 | |
| 245 | if cls.fBase == '': |
| 246 | enumsRoot.append(cls.fEnum) |
| 247 | |
| 248 | enumToCls[cls.fEnum] = cls |
| 249 | |
| 250 | enumsRoot.sort() |
| 251 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 252 | |
| 253 | # TODO(edisonn): move each .h in it's own file |
| 254 | # write imports |
| 255 | |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 256 | # write enums |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 257 | print('enum SkPdfObjectType {') |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 258 | for enum in enumsRoot: |
| 259 | self.writeEnum(enum, enumToCls) |
| 260 | print('};') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 261 | print |
| 262 | |
| 263 | # write forward class declaration |
| 264 | for name in self.fClassesNamesInOrder: |
| 265 | print('class SkPdf' + name + ';') |
| 266 | print |
| 267 | |
| 268 | for name in self.fClassesNamesInOrder: |
| 269 | cls = self.fClasses[name] |
| 270 | enum = cls.fEnum |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 271 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 272 | if cls.fBase == '': |
| 273 | print('class SkPdf' + cls.fName + ' {') |
| 274 | else: |
| 275 | print('class SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {') |
| 276 | |
| 277 | print('public:') |
| 278 | print(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}') |
| 279 | if len(cls.fEnumSubclasses) == 0: |
| 280 | print(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjectType)(' + cls.fEnum + ' + 1);}') |
| 281 | else: |
| 282 | print(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}') |
| 283 | |
| 284 | |
| 285 | self.writeAsFoo(cls, enumToCls) |
| 286 | |
| 287 | print('public:') |
| 288 | for cc in cls.fCCPublic: |
| 289 | print(' ' + cc) |
| 290 | |
| 291 | print('private:') |
| 292 | for cc in cls.fCCPrivate: |
| 293 | print(' ' + cc) |
| 294 | |
| 295 | if cls.fBase == '': |
| 296 | print('protected:') |
| 297 | print(' const PdfMemDocument* fPodofoDoc;') |
| 298 | print(' const PdfObject* fPodofoObj;') |
| 299 | print |
| 300 | print('public:') |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 301 | print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}') |
| 302 | print(' const PdfMemDocument* doc() const { return fPodofoDoc;}') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 303 | print(' const PdfObject* podofo() const { return fPodofoObj;}') |
| 304 | else: |
| 305 | print('public:') |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 306 | print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}') |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 307 | print |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 308 | |
| 309 | #check required fieds, also, there should be an internal_valid() manually wrote for complex |
| 310 | # situations |
| 311 | # right now valid return true |
| 312 | print(' virtual bool valid() const {return true;}') |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 313 | print |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 314 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 315 | print(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fName + '& from) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPodofoObj; return *this;}') |
| 316 | print |
| 317 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 318 | for field in cls.fFields: |
| 319 | prop = field.fProp |
| 320 | if prop.fCppName != '': |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 321 | if prop.fCppName[0] == '[': |
| 322 | print('/*') # comment code of the atributes that can have any name |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 323 | |
| 324 | # TODO(edisonn): has_foo(); |
| 325 | print(' bool has_' + prop.fCppName + '() const {') |
| 326 | print(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));') |
| 327 | print(' }') |
| 328 | |
| 329 | if len(prop.fTypes.split()) == 1: |
| 330 | t = prop.fTypes.strip() |
| 331 | print(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {') |
| 332 | print(' ' + knowTypes[t][0] + ' ret;') |
| 333 | print(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;') |
| 334 | if field.fRequired == False and prop.fDefault != '': |
| 335 | print(' return ' + prop.fDefault.toCpp() + ';'); |
| 336 | else: |
| 337 | print(' // TODO(edisonn): warn about missing required field, assert for known good pdfs') |
| 338 | print(' return ' + knowTypes[t][2].toCpp() + ';'); |
| 339 | print(' }') |
| 340 | print |
| 341 | else: |
| 342 | for type in prop.fTypes.split(): |
| 343 | t = type.strip() |
| 344 | print(' bool is' + prop.fCppName + 'A' + t.title() + '() const {') |
| 345 | print(' SkPdfObject* ret = NULL;') |
| 346 | print(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;') |
| 347 | print(' return ' + knowTypes[t][3] + ';') |
| 348 | print(' }') |
| 349 | print |
| 350 | |
| 351 | print(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {') |
| 352 | print(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';') |
| 353 | print(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;') |
| 354 | print(' // TODO(edisonn): warn about missing required field, assert for known good pdfs') |
| 355 | print(' return ' + knowTypes[t][2].toCpp() + ';') |
| 356 | print(' }') |
| 357 | print |
| 358 | |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 359 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 360 | if prop.fCppName[0] == '[': |
| 361 | print('*/') # comment code of the atributes that can have any name |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 362 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 363 | |
| 364 | print('};') |
| 365 | print |
| 366 | print |
| 367 | |
| 368 | |
| 369 | |
| 370 | # generate constructor when knowing the type |
| 371 | # later, p2, generate constructor when not knowing the type - very similar with parsing? |
| 372 | |
edisonn@google.com | f7dd491 | 2013-06-11 23:06:16 +0000 | [diff] [blame] | 373 | # generate parser |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 374 | |
| 375 | # TODO(edisonn): fast recognition based on must attributes. |
| 376 | print('class PodofoMapper {') |
| 377 | print('public:') |
| 378 | for name in self.fClassesNamesInOrder: |
| 379 | cls = self.fClasses[name] |
| 380 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 381 | |
| 382 | print(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) {') |
| 383 | print(' return map(*in.doc(), *in.podofo(), out);') |
| 384 | print(' }') |
| 385 | print |
| 386 | |
| 387 | print(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {') |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 388 | print(' if (!is' + name + '(podofoDoc, podofoObj)) return false;') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 389 | print |
| 390 | |
| 391 | for sub in cls.fEnumSubclasses: |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 392 | print(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 393 | |
| 394 | print |
| 395 | |
| 396 | print(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj);') |
| 397 | print(' return true;') |
| 398 | print(' }') |
| 399 | print |
| 400 | |
| 401 | for name in self.fClassesNamesInOrder: |
| 402 | cls = self.fClasses[name] |
| 403 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 404 | print(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 405 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 406 | if cls.fCheck != '': |
| 407 | print(' return ' + cls.fCheck + ';') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 408 | else: |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 409 | cntMust = 0 |
| 410 | for field in cls.fFields: |
| 411 | prop = field.fProp |
| 412 | if prop.fHasMust: |
| 413 | cntMust = cntMust + 1 |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame] | 414 | print(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';') |
| 415 | print(' if (!' + knowTypes[prop.fTypes.strip()][1] + '(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;') |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 416 | print(' if (' + prop.fCppName + ' != ' + prop.fMustBe.toCpp() + ') return false;') |
| 417 | print |
| 418 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 419 | print(' return true;') |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 420 | |
| 421 | print(' }') |
| 422 | print |
| 423 | |
| 424 | print('};') |
| 425 | print |
| 426 | |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 427 | return |
| 428 | |
| 429 | def generateCode(): |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 430 | manager = PdfClassManager() |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 431 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 432 | manager.addClass('Object') |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 433 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 434 | manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null') |
edisonn@google.com | 60533dc | 2013-06-18 14:51:21 +0000 | [diff] [blame^] | 435 | manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Bool')\ |
| 436 | .carbonCopyPublic('bool value() const {return fPodofoObj->GetBool();}') |
| 437 | |
| 438 | manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Number')\ |
| 439 | .carbonCopyPublic('long value() const {return fPodofoObj->GetNumber();}') |
| 440 | |
| 441 | manager.addClass('Number').check('podofoObj.GetDataType() == ePdfDataType_Real')\ |
| 442 | .carbonCopyPublic('double value() const {return fPodofoObj->GetReal();}') |
| 443 | |
| 444 | manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')\ |
| 445 | .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}') |
| 446 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 447 | #manager.addClass('Stream') - attached to a dictionary |
| 448 | manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_Reference') |
edisonn@google.com | 60533dc | 2013-06-18 14:51:21 +0000 | [diff] [blame^] | 449 | |
| 450 | manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array')\ |
| 451 | .carbonCopyPublic('const int size() const {return fPodofoObj->GetArray().GetSize();}')\ |
| 452 | .carbonCopyPublic('const SkPdfObject operator[](int i) const {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\ |
| 453 | .carbonCopyPublic('SkPdfObject operator[](int i) {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}') |
| 454 | |
| 455 | manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_String')\ |
| 456 | .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}') |
| 457 | |
| 458 | manager.addClass('HexString').check('podofoObj.GetDataType() == ePdfDataType_HexString')\ |
| 459 | .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}') |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 460 | |
| 461 | manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_Dictionary') |
| 462 | |
| 463 | # these classes are not explicitely backed by a table in the pdf spec |
| 464 | manager.addClass('XObjectDictionary', 'Dictionary') |
| 465 | |
| 466 | manager.addClass('FontDictionary', 'Dictionary') |
| 467 | |
| 468 | manager.addClass('TrueTypeFontDictionary', 'FontDictionary') |
| 469 | |
| 470 | pdfspec_autogen.buildPdfSpec(manager) |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 471 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 472 | manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\ |
| 473 | .required('NULL')\ |
| 474 | .field('Subtype')\ |
| 475 | .name('Subtype')\ |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 476 | .type('name')\ |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 477 | .comment('')\ |
| 478 | .must(datatypes.PdfName('MMType1'))\ |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 479 | .done().done()\ |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 480 | |
| 481 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 482 | manager.write() |
edisonn@google.com | 1a191c6 | 2013-06-11 21:44:08 +0000 | [diff] [blame] | 483 | |
| 484 | return 1 |
| 485 | |
| 486 | if '__main__' == __name__: |
| 487 | sys.exit(generateCode()) |
edisonn@google.com | 4532711 | 2013-06-13 20:02:29 +0000 | [diff] [blame] | 488 | |
| 489 | |