blob: 9cc3e22cd0fe996f4f1732fac3259685c8611ee0 [file] [log] [blame]
edisonn@google.coma2fab9d2013-06-14 19:22:19 +00001
2
edisonn@google.com1a191c62013-06-11 21:44:08 +00003import sys
4
edisonn@google.coma2fab9d2013-06-14 19:22:19 +00005import datatypes
6import pdfspec_autogen
edisonn@google.comaf3daa02013-06-12 19:07:45 +00007
edisonn@google.com1277cf02013-06-17 23:36:45 +00008knowTypes = {
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'],
edisonn@google.com59543d32013-06-18 22:00:40 +000021'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podofo()->GetDataType() == ePdfDataType_Real || ret->podofo()->GetDataType() == ePdfDataType_Number'],
edisonn@google.com1277cf02013-06-17 23:36:45 +000022'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.com45327112013-06-13 20:02:29 +000028
29
edisonn@google.com1a191c62013-06-11 21:44:08 +000030class 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.com1277cf02013-06-17 23:36:45 +000037 self.fTypes = ''
edisonn@google.comaf3daa02013-06-12 19:07:45 +000038 self.fCppName = ''
edisonn@google.com1277cf02013-06-17 23:36:45 +000039 self.fEnumValues = []
40 self.fHasMust = False
edisonn@google.comaf3daa02013-06-12 19:07:45 +000041 self.fMustBe = ''
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000042 self.fComment = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000043
44 def must(self, value):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000045 self.fHasMust = True
46 self.fMustBe = value
47 return self
edisonn@google.com1a191c62013-06-11 21:44:08 +000048
49 def default(self, value):
50 self.fDefault = value
51 return self
52
edisonn@google.com1277cf02013-06-17 23:36:45 +000053 def multiple(self, enumValues):
54 self.fEnumValues = enumValues
edisonn@google.com1a191c62013-06-11 21:44:08 +000055 return self
56
edisonn@google.comaf3daa02013-06-12 19:07:45 +000057 def name(self, name):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000058 self.fCppName = name
edisonn@google.com1a191c62013-06-11 21:44:08 +000059 return self
60
edisonn@google.com1277cf02013-06-17 23:36:45 +000061 def type(self, types):
edisonn@google.com45327112013-06-13 20:02:29 +000062 # 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.com1277cf02013-06-17 23:36:45 +000063 types = types.strip()
64 types = types.replace('or', ' ')
65 types = types.replace(',', ' ')
66 types = types.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'?
67 types = types.replace('file specification', 'file_specification')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000068
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000069
edisonn@google.com1277cf02013-06-17 23:36:45 +000070 self.fTypes = types
edisonn@google.com45327112013-06-13 20:02:29 +000071 return self
72
73 def comment(self, comment):
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000074 self.fComment = comment
edisonn@google.com45327112013-06-13 20:02:29 +000075 return self
76
edisonn@google.com1a191c62013-06-11 21:44:08 +000077 def done(self):
78 return self.fParent
79
80
81class PdfClassField:
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000082 def __init__(self, parent, required, version='', inheritable=False):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000083 #self.fProp = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000084 self.fParent = parent
85 self.fRequired = required
edisonn@google.com45327112013-06-13 20:02:29 +000086 self.fVersion = version
87 self.fInheritable = inheritable
edisonn@google.com1a191c62013-06-11 21:44:08 +000088
edisonn@google.comaf3daa02013-06-12 19:07:45 +000089 def field(self, name, abr=''):
90 self.fProp = PdfField(self, name, abr)
91 return self.fProp
edisonn@google.com1a191c62013-06-11 21:44:08 +000092
93 def done(self):
94 return self.fParent
95
96class PdfClass:
edisonn@google.com45327112013-06-13 20:02:29 +000097 def __init__(self, name, base, comment):
edisonn@google.com1a191c62013-06-11 21:44:08 +000098 self.fFields = []
99 self.fIncludes = []
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000100 self.fCCPublic = []
101 self.fCCPrivate = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000102 self.fName = name
103 self.fBase = base
edisonn@google.com45327112013-06-13 20:02:29 +0000104 self.fComment = comment
edisonn@google.com1a191c62013-06-11 21:44:08 +0000105
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000106 self.fEnumSubclasses = []
107
108 self.fEnum = '!UNDEFINED'
109 self.fEnumEnd = '!UNDEFINED'
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000110 self.fCheck = ''
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000111
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000112 def check(self, ifCheck):
113 self.fCheck = ifCheck
114 return self
115
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000116 def required(self, badDefault):
edisonn@google.com1a191c62013-06-11 21:44:08 +0000117 field = PdfClassField(self, True)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000118 field.fBadDefault = badDefault
edisonn@google.com1a191c62013-06-11 21:44:08 +0000119 self.fFields.append(field)
120 return field
121
122 def optional(self):
123 field = PdfClassField(self, False)
124 self.fFields.append(field)
125 return field
edisonn@google.com45327112013-06-13 20:02:29 +0000126
127 #([Required] [;] [inheritable] [;] [version]; [comments])
128 # version: PDF [d].[d]
129 # ; separate props
130 #inheritable
131 #version
132 #required, if
133 #optional, if
edisonn@google.com1a191c62013-06-11 21:44:08 +0000134
135 def include(self, path):
136 self.fIncludes.append(path)
137 return self
138
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000139 def carbonCopyPublic(self, cc):
140 self.fCCPublic.append(cc)
141 return self
142
143 def carbonCopyPrivate(self, cc):
144 self.fCCPrivate.append(cc)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000145 return self
edisonn@google.com45327112013-06-13 20:02:29 +0000146
147 def done(self):
148 return
edisonn@google.com1a191c62013-06-11 21:44:08 +0000149
150class PdfClassManager:
151 def __init__(self):
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000152 self.fClasses = {}
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000153 self.fClassesNamesInOrder = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000154
edisonn@google.com45327112013-06-13 20:02:29 +0000155 def addClass(self, name, base='Object', comment=''):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000156 if name == 'Object':
edisonn@google.com45327112013-06-13 20:02:29 +0000157 cls = PdfClass(name, '', comment)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000158 else:
edisonn@google.com45327112013-06-13 20:02:29 +0000159 cls = PdfClass(name, base, comment)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000160 self.fClasses[name] = cls
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000161 self.fClassesNamesInOrder.append(name)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000162 return cls
163
edisonn@google.com59543d32013-06-18 22:00:40 +0000164 def writeEnum(self, fileEnums, enum, enumToCls):
165 fileEnums.write(' ' + enum + ',\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000166 cls = enumToCls[enum]
167 cls.fEnumSubclasses.sort()
168
169 cnt = 0
170 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000171 self.writeEnum(fileEnums, cls.fEnumSubclasses[cnt], enumToCls)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000172 cnt = cnt + 1
173
174 if cnt != 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000175 fileEnums.write(' ' + cls.fEnumEnd + ',\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000176
177
edisonn@google.com59543d32013-06-18 22:00:40 +0000178 def writeAsNull(self, fileClass, cls, enumToCls):
179 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return NULL;}\n')
180 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return NULL;}\n')
181 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000182
183 cnt = 0
184 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000185 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000186 cnt = cnt + 1
187
188
edisonn@google.com59543d32013-06-18 22:00:40 +0000189 def writeAsFoo(self, fileClass, cls, enumToCls):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000190 # TODO(edisonn): add a container, with sections, public, private, default, ...
191 # the end code will be grouped
192
193 # me
edisonn@google.com59543d32013-06-18 22:00:40 +0000194 fileClass.write('public:\n')
195 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return this;}\n')
196 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return this;}\n')
197 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000198
199 if cls.fName == 'Object':
200 cnt = 0
201 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000202 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000203 cnt = cnt + 1
204
205 if cls.fName != 'Object':
edisonn@google.com59543d32013-06-18 22:00:40 +0000206 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000207 base = self.fClasses[cls.fBase]
208 cnt = 0
209 for sub in base.fEnumSubclasses:
210 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName:
edisonn@google.com59543d32013-06-18 22:00:40 +0000211 self.writeAsNull(fileClass, enumToCls[base.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000212 cnt = cnt + 1
213
214
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000215
edisonn@google.com1a191c62013-06-11 21:44:08 +0000216 def write(self):
edisonn@google.com59543d32013-06-18 22:00:40 +0000217 global fileHeaders
edisonn@google.com1277cf02013-06-17 23:36:45 +0000218 global knowTypes
219
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000220 # generate enum
221 enumsRoot = []
222
223 enumToCls = {}
224
225 for name in self.fClasses:
226 cls = self.fClasses[name]
edisonn@google.com59543d32013-06-18 22:00:40 +0000227 cls.fEnum = 'k' + name + '_SkPdfObjectType'
228 cls.fEnumEnd = 'k' + name + '__End_SkPdfObjectType'
229
230 fileHeaders.write('#include "SkPdf' + cls.fName + '_autogen.h"\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000231
232 if cls.fBase != '':
233 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
234
235 if cls.fBase == '':
236 enumsRoot.append(cls.fEnum)
237
238 enumToCls[cls.fEnum] = cls
239
240 enumsRoot.sort()
241
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000242
243 # TODO(edisonn): move each .h in it's own file
244 # write imports
245
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000246 # write enums
edisonn@google.com59543d32013-06-18 22:00:40 +0000247 fileEnums = open('SkPdfEnums_autogen.h', 'w')
248 fileEnums.write('#ifndef __DEFINED__SkPdfEnums\n')
249 fileEnums.write('#define __DEFINED__SkPdfEnums\n')
250 fileEnums.write('\n')
251
252 fileEnums.write('enum SkPdfObjectType {\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000253 for enum in enumsRoot:
edisonn@google.com59543d32013-06-18 22:00:40 +0000254 self.writeEnum(fileEnums, enum, enumToCls)
255 fileEnums.write('};\n')
256 fileEnums.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000257
258 # write forward class declaration
259 for name in self.fClassesNamesInOrder:
edisonn@google.com59543d32013-06-18 22:00:40 +0000260 fileEnums.write('class SkPdf' + name + ';\n')
261 fileEnums.write('\n')
262
263 fileEnums.write('#endif // __DEFINED__SkPdfEnums\n')
264 fileEnums.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000265
266 for name in self.fClassesNamesInOrder:
267 cls = self.fClasses[name]
268 enum = cls.fEnum
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000269
edisonn@google.com59543d32013-06-18 22:00:40 +0000270 fileClass = open('SkPdf' + cls.fName + '_autogen.h', 'w')
271 fileClass.write('#ifndef __DEFINED__SkPdf' + cls.fName + '\n')
272 fileClass.write('#define __DEFINED__SkPdf' + cls.fName + '\n')
273 fileClass.write('\n')
274
275 fileClass.write('#include "SkPdfEnums_autogen.h"\n')
276 fileClass.write('#include "SkPdfArray_autogen.h"\n')
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000277 if cls.fBase != '':
278 fileClass.write('#include "SkPdf' + cls.fBase + '_autogen.h"\n')
279 fileClass.write('\n')
280
281 if cls.fComment != '':
282 fileClass.write('// ' + cls.fComment + '\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000283
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000284 if cls.fBase == '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000285 fileClass.write('class SkPdf' + cls.fName + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000286 else:
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000287 fileClass.write('class SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000288
edisonn@google.com59543d32013-06-18 22:00:40 +0000289 fileClass.write('public:\n')
290 fileClass.write(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000291 if len(cls.fEnumSubclasses) == 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000292 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjectType)(' + cls.fEnum + ' + 1);}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000293 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000294 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000295
edisonn@google.com59543d32013-06-18 22:00:40 +0000296 self.writeAsFoo(fileClass, cls, enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000297
edisonn@google.com59543d32013-06-18 22:00:40 +0000298 fileClass.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000299 for cc in cls.fCCPublic:
edisonn@google.com59543d32013-06-18 22:00:40 +0000300 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000301
edisonn@google.com59543d32013-06-18 22:00:40 +0000302 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000303 for cc in cls.fCCPrivate:
edisonn@google.com59543d32013-06-18 22:00:40 +0000304 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000305
306 if cls.fBase == '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000307 fileClass.write('protected:\n')
308 fileClass.write(' const PdfMemDocument* fPodofoDoc;\n')
309 fileClass.write(' const PdfObject* fPodofoObj;\n')
310 fileClass.write('\n')
311 fileClass.write('public:\n')
312 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}\n')
313 fileClass.write(' const PdfMemDocument* doc() const { return fPodofoDoc;}\n')
314 fileClass.write(' const PdfObject* podofo() const { return fPodofoObj;}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000315 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000316 fileClass.write('public:\n')
317 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}\n')
318 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000319
320 #check required fieds, also, there should be an internal_valid() manually wrote for complex
321 # situations
322 # right now valid return true
edisonn@google.com59543d32013-06-18 22:00:40 +0000323 fileClass.write(' virtual bool valid() const {return true;}\n')
324 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000325
edisonn@google.com59543d32013-06-18 22:00:40 +0000326 fileClass.write(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fName + '& from) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPodofoObj; return *this;}\n')
327 fileClass.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000328
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000329 for field in cls.fFields:
330 prop = field.fProp
331 if prop.fCppName != '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000332
333 lines = prop.fComment.split('\n')
334 if prop.fComment != '' and len(lines) > 0:
335 fileClass.write('/** ' + lines[0] + '\n')
336 for line in lines[1:]:
337 fileClass.write(' * ' + line + '\n')
338 fileClass.write('**/\n')
339
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000340 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000341 fileClass.write('/*\n') # comment code of the atributes that can have any name
edisonn@google.com1277cf02013-06-17 23:36:45 +0000342
343 # TODO(edisonn): has_foo();
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000344 fileClass.write(' bool has_' + prop.fCppName + '() const {\n')
345 fileClass.write(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));\n')
346 fileClass.write(' }\n')
347 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000348
349 if len(prop.fTypes.split()) == 1:
350 t = prop.fTypes.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000351 fileClass.write(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {\n')
352 fileClass.write(' ' + knowTypes[t][0] + ' ret;\n')
353 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000354 if field.fRequired == False and prop.fDefault != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000355 fileClass.write(' return ' + prop.fDefault.toCpp() + ';\n');
edisonn@google.com1277cf02013-06-17 23:36:45 +0000356 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000357 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
358 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n');
359 fileClass.write(' }\n')
360 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000361 else:
362 for type in prop.fTypes.split():
363 t = type.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000364 fileClass.write(' bool is' + prop.fCppName + 'A' + t.title() + '() const {\n')
365 fileClass.write(' SkPdfObject* ret = NULL;\n')
366 fileClass.write(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;\n')
367 fileClass.write(' return ' + knowTypes[t][3] + ';\n')
368 fileClass.write(' }\n')
369 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000370
edisonn@google.com59543d32013-06-18 22:00:40 +0000371 fileClass.write(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {\n')
372 fileClass.write(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';\n')
373 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
374 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
375 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n')
376 fileClass.write(' }\n')
377 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000378
edisonn@google.com45327112013-06-13 20:02:29 +0000379
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000380 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000381 fileClass.write('*/\n') # comment code of the atributes that can have any name
edisonn@google.com45327112013-06-13 20:02:29 +0000382
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000383
edisonn@google.com59543d32013-06-18 22:00:40 +0000384 fileClass.write('};\n')
385 fileClass.write('\n')
386
387 fileClass.write('#endif // __DEFINED__SkPdf' + cls.fName + '\n')
388 fileClass.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000389
390
391
392 # generate constructor when knowing the type
393 # later, p2, generate constructor when not knowing the type - very similar with parsing?
394
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000395 # generate parser
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000396 # TODO(edisonn): fast recognition based on must attributes.
edisonn@google.com59543d32013-06-18 22:00:40 +0000397 fileMapper = open('SkPdfPodofoMapper_autogen.h', 'w')
398 fileMapper.write('#ifndef __DEFINED__SkPdfPodofoMapper\n')
399 fileMapper.write('#define __DEFINED__SkPdfPodofoMapper\n')
400 fileMapper.write('\n')
401
402 fileMapper.write('#include "SkPdfHeaders_autogen.h"\n')
403 fileMapper.write('class PodofoMapper {\n')
404 fileMapper.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000405 for name in self.fClassesNamesInOrder:
406 cls = self.fClasses[name]
407
edisonn@google.com68d15c82013-06-17 20:46:27 +0000408
edisonn@google.com59543d32013-06-18 22:00:40 +0000409 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) {\n')
410 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n')
411 fileMapper.write(' }\n')
412 fileMapper.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000413
edisonn@google.com59543d32013-06-18 22:00:40 +0000414 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {\n')
415 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return false;\n')
416 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000417
418 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000419 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000420
edisonn@google.com59543d32013-06-18 22:00:40 +0000421 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000422
edisonn@google.com59543d32013-06-18 22:00:40 +0000423 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj);\n')
424 fileMapper.write(' return true;\n')
425 fileMapper.write(' }\n')
426 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000427
428 for name in self.fClassesNamesInOrder:
429 cls = self.fClasses[name]
430
edisonn@google.com59543d32013-06-18 22:00:40 +0000431 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000432
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000433 if cls.fCheck != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000434 fileMapper.write(' return ' + cls.fCheck + ';\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000435 else:
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000436 cntMust = 0
437 for field in cls.fFields:
438 prop = field.fProp
439 if prop.fHasMust:
440 cntMust = cntMust + 1
edisonn@google.com59543d32013-06-18 22:00:40 +0000441 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')
443 fileMapper.write(' if (' + prop.fCppName + ' != ' + prop.fMustBe.toCpp() + ') return false;\n')
444 fileMapper.write('\n')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000445
edisonn@google.com59543d32013-06-18 22:00:40 +0000446 fileMapper.write(' return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000447
edisonn@google.com59543d32013-06-18 22:00:40 +0000448 fileMapper.write(' }\n')
449 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000450
edisonn@google.com59543d32013-06-18 22:00:40 +0000451 fileMapper.write('};\n')
452 fileMapper.write('\n')
453
454 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n')
455 fileMapper.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000456
edisonn@google.com1a191c62013-06-11 21:44:08 +0000457 return
458
459def generateCode():
edisonn@google.com59543d32013-06-18 22:00:40 +0000460 global fileHeaders
461
462 fileHeaders = open('SkPdfHeaders_autogen.h', 'w')
463 fileHeaders.write('#ifndef __DEFINED__SkPdfHeaders\n')
464 fileHeaders.write('#define __DEFINED__SkPdfHeaders\n')
465 fileHeaders.write('\n')
466
467 fileHeaders.write('#include "SkPdfEnums_autogen.h"\n')
468
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000469 manager = PdfClassManager()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000470
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000471 manager.addClass('Object')
edisonn@google.com1a191c62013-06-11 21:44:08 +0000472
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000473 manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000474 manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Bool')\
475 .carbonCopyPublic('bool value() const {return fPodofoObj->GetBool();}')
476
edisonn@google.com59543d32013-06-18 22:00:40 +0000477 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000478 .carbonCopyPublic('long value() const {return fPodofoObj->GetNumber();}')
479
edisonn@google.com59543d32013-06-18 22:00:40 +0000480 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000481 .carbonCopyPublic('double value() const {return fPodofoObj->GetReal();}')
482
483 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')\
484 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}')
485
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000486 #manager.addClass('Stream') - attached to a dictionary
487 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_Reference')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000488
489 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array')\
490 .carbonCopyPublic('const int size() const {return fPodofoObj->GetArray().GetSize();}')\
491 .carbonCopyPublic('const SkPdfObject operator[](int i) const {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\
492 .carbonCopyPublic('SkPdfObject operator[](int i) {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')
493
edisonn@google.com59543d32013-06-18 22:00:40 +0000494 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_String || podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000495 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
496
edisonn@google.com59543d32013-06-18 22:00:40 +0000497 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000498 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000499
edisonn@google.com59543d32013-06-18 22:00:40 +0000500 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_Dictionary')\
501 .carbonCopyPublic('const SkPdfObject get(const char* dictionaryKeyName) const {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
502 .carbonCopyPublic('SkPdfObject get(const char* dictionaryKeyName) {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
503
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000504
505 # these classes are not explicitely backed by a table in the pdf spec
506 manager.addClass('XObjectDictionary', 'Dictionary')
507
508 manager.addClass('FontDictionary', 'Dictionary')
509
510 manager.addClass('TrueTypeFontDictionary', 'FontDictionary')
511
512 pdfspec_autogen.buildPdfSpec(manager)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000513
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000514 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\
515 .required('NULL')\
516 .field('Subtype')\
517 .name('Subtype')\
edisonn@google.com45327112013-06-13 20:02:29 +0000518 .type('name')\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000519 .comment('')\
520 .must(datatypes.PdfName('MMType1'))\
edisonn@google.com45327112013-06-13 20:02:29 +0000521 .done().done()\
edisonn@google.com45327112013-06-13 20:02:29 +0000522
523
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000524 manager.write()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000525
edisonn@google.com59543d32013-06-18 22:00:40 +0000526 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n')
527 fileHeaders.close()
528
edisonn@google.com1a191c62013-06-11 21:44:08 +0000529 return 1
530
531if '__main__' == __name__:
532 sys.exit(generateCode())
edisonn@google.com45327112013-06-13 20:02:29 +0000533