blob: baefab39bad49e7dde9e22b74524e136c0ab944c [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
edisonn@google.comb857a0c2013-06-25 20:45:40 +00006from autogen.pdfspec_autogen import *
edisonn@google.comaf3daa02013-06-12 19:07:45 +00007
edisonn@google.com1277cf02013-06-17 23:36:45 +00008knowTypes = {
edisonn@google.com3aac1f92013-07-02 22:42:53 +00009'(any)': ['SkPdfObject*', 'SkPdfObjectFromDictionary', datatypes.CppNull(), 'true', 'use a mapper'],
10'(undefined)': ['SkPdfObject*', 'SkPdfObjectFromDictionary', datatypes.CppNull(), 'true', 'use a mapper'],
11'(various)': ['SkPdfObject*', 'SkPdfObjectFromDictionary', datatypes.CppNull(), 'true', 'use a mapper'],
12'array': ['SkPdfArray*', 'ArrayFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Array'],
13'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Bool'],
14'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Array'],
15'dictionary': ['SkPdfDictionary*', 'SkPdfDictionaryFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Dictionary', 'use a mapper'],
16'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNone(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Reference'],
17'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Number'],
18'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.FileSpecNone(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Reference'],
19'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Name'],
20'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Reference'],
21'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Real || ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Number'],
22'rectangle': ['SkRect*', 'SkRectFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Array && ret->podofo()->GetArray().GetLength() == 4'],
edisonn@google.comff278442013-06-21 21:03:15 +000023'stream': ['SkPdfStream*', 'StreamFromDictionary', datatypes.CppNull(), 'ret->podofo()->HasStream()'],
edisonn@google.com3aac1f92013-07-02 22:42:53 +000024'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_String || ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_HexString'],
25'text': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_String || ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_HexString'],
26'text string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_String || ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_HexString'],
27'matrix': ['SkMatrix*', 'SkMatrixFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == PoDoFo::ePdfDataType_Array && ret->podofo()->GetArray().GetLength() == 4'],
edisonn@google.com1277cf02013-06-17 23:36:45 +000028}
edisonn@google.com45327112013-06-13 20:02:29 +000029
30
edisonn@google.com1a191c62013-06-11 21:44:08 +000031class PdfField:
32 def __init__(self, parent, name, abr):
33 self.fParent = parent
34 self.fName = name
35 self.fAbr = abr
36
37 self.fDefault = ''
edisonn@google.com1277cf02013-06-17 23:36:45 +000038 self.fTypes = ''
edisonn@google.comaf3daa02013-06-12 19:07:45 +000039 self.fCppName = ''
edisonn@google.com1277cf02013-06-17 23:36:45 +000040 self.fEnumValues = []
41 self.fHasMust = False
edisonn@google.comff278442013-06-21 21:03:15 +000042 self.fMustBe = []
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000043 self.fComment = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000044
45 def must(self, value):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000046 self.fHasMust = True
47 self.fMustBe = value
48 return self
edisonn@google.com1a191c62013-06-11 21:44:08 +000049
50 def default(self, value):
51 self.fDefault = value
52 return self
53
edisonn@google.com1277cf02013-06-17 23:36:45 +000054 def multiple(self, enumValues):
55 self.fEnumValues = enumValues
edisonn@google.com1a191c62013-06-11 21:44:08 +000056 return self
57
edisonn@google.comaf3daa02013-06-12 19:07:45 +000058 def name(self, name):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000059 self.fCppName = name
edisonn@google.com1a191c62013-06-11 21:44:08 +000060 return self
61
edisonn@google.com1277cf02013-06-17 23:36:45 +000062 def type(self, types):
edisonn@google.com45327112013-06-13 20:02:29 +000063 # 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 +000064 types = types.strip()
edisonn@google.com6e49c342013-06-27 20:03:43 +000065 types = types.replace(' or ', ' ')
66 types = types.replace(' or,', ' ')
67 types = types.replace(',or ', ' ')
68 types = types.replace(',or,', ' ')
edisonn@google.com1277cf02013-06-17 23:36:45 +000069 types = types.replace(',', ' ')
70 types = types.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'?
71 types = types.replace('file specification', 'file_specification')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000072
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000073
edisonn@google.com1277cf02013-06-17 23:36:45 +000074 self.fTypes = types
edisonn@google.com45327112013-06-13 20:02:29 +000075 return self
76
77 def comment(self, comment):
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000078 self.fComment = comment
edisonn@google.com45327112013-06-13 20:02:29 +000079 return self
80
edisonn@google.com1a191c62013-06-11 21:44:08 +000081 def done(self):
82 return self.fParent
83
84
85class PdfClassField:
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000086 def __init__(self, parent, required, version='', inheritable=False):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000087 #self.fProp = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000088 self.fParent = parent
89 self.fRequired = required
edisonn@google.com45327112013-06-13 20:02:29 +000090 self.fVersion = version
91 self.fInheritable = inheritable
edisonn@google.com1a191c62013-06-11 21:44:08 +000092
edisonn@google.comaf3daa02013-06-12 19:07:45 +000093 def field(self, name, abr=''):
94 self.fProp = PdfField(self, name, abr)
95 return self.fProp
edisonn@google.com1a191c62013-06-11 21:44:08 +000096
97 def done(self):
98 return self.fParent
99
100class PdfClass:
edisonn@google.com45327112013-06-13 20:02:29 +0000101 def __init__(self, name, base, comment):
edisonn@google.com1a191c62013-06-11 21:44:08 +0000102 self.fFields = []
103 self.fIncludes = []
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000104 self.fCCPublicPodofo = []
105 self.fCCPublicPodofoCpp = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000106 self.fName = name
107 self.fBase = base
edisonn@google.com45327112013-06-13 20:02:29 +0000108 self.fComment = comment
edisonn@google.com1a191c62013-06-11 21:44:08 +0000109
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000110 self.fEnumSubclasses = []
111
112 self.fEnum = '!UNDEFINED'
113 self.fEnumEnd = '!UNDEFINED'
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000114 self.fCheck = ''
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000115
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000116 def check(self, ifCheck):
117 self.fCheck = ifCheck
118 return self
119
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000120 def required(self, badDefault):
edisonn@google.com1a191c62013-06-11 21:44:08 +0000121 field = PdfClassField(self, True)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000122 field.fBadDefault = badDefault
edisonn@google.com1a191c62013-06-11 21:44:08 +0000123 self.fFields.append(field)
124 return field
125
126 def optional(self):
127 field = PdfClassField(self, False)
128 self.fFields.append(field)
129 return field
edisonn@google.com45327112013-06-13 20:02:29 +0000130
131 #([Required] [;] [inheritable] [;] [version]; [comments])
132 # version: PDF [d].[d]
133 # ; separate props
134 #inheritable
135 #version
136 #required, if
137 #optional, if
edisonn@google.com1a191c62013-06-11 21:44:08 +0000138
139 def include(self, path):
140 self.fIncludes.append(path)
141 return self
142
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000143 def carbonCopyPublicPodofo(self, cc):
144 self.fCCPublicPodofo.append(cc)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000145 return self
146
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000147 def carbonCopyPublicPodofoCpp(self, cc):
148 self.fCCPublicPodofoCpp.append(cc)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000149 return self
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000150
edisonn@google.com45327112013-06-13 20:02:29 +0000151 def done(self):
152 return
edisonn@google.com1a191c62013-06-11 21:44:08 +0000153
154class PdfClassManager:
155 def __init__(self):
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000156 self.fClasses = {}
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000157 self.fClassesNamesInOrder = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000158
edisonn@google.com45327112013-06-13 20:02:29 +0000159 def addClass(self, name, base='Object', comment=''):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000160 if name == 'Object':
edisonn@google.com45327112013-06-13 20:02:29 +0000161 cls = PdfClass(name, '', comment)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000162 else:
edisonn@google.com45327112013-06-13 20:02:29 +0000163 cls = PdfClass(name, base, comment)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000164 self.fClasses[name] = cls
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000165 self.fClassesNamesInOrder.append(name)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000166 return cls
167
edisonn@google.com59543d32013-06-18 22:00:40 +0000168 def writeEnum(self, fileEnums, enum, enumToCls):
169 fileEnums.write(' ' + enum + ',\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000170 cls = enumToCls[enum]
171 cls.fEnumSubclasses.sort()
172
173 cnt = 0
174 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000175 self.writeEnum(fileEnums, cls.fEnumSubclasses[cnt], enumToCls)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000176 cnt = cnt + 1
177
178 if cnt != 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000179 fileEnums.write(' ' + cls.fEnumEnd + ',\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000180
181
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000182 def writeAsNull(self, podofoFileClass, cls, enumToCls):
183 podofoFileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return NULL;}\n')
184 podofoFileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return NULL;}\n')
185 podofoFileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000186
187 cnt = 0
188 for sub in cls.fEnumSubclasses:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000189 self.writeAsNull(podofoFileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000190 cnt = cnt + 1
191
192
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000193 def writeAsFoo(self, podofoFileClass, cls, enumToCls):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000194 # TODO(edisonn): add a container, with sections, public, private, default, ...
195 # the end code will be grouped
196
197 # me
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000198 podofoFileClass.write('public:\n')
199
200 podofoFileClass.write('public:\n')
201 podofoFileClass.write(' SkPdf' + cls.fName +'* as' + cls.fName + '() {return this;}\n')
202 podofoFileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return this;}\n')
203 podofoFileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000204
205 if cls.fName == 'Object':
206 cnt = 0
207 for sub in cls.fEnumSubclasses:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000208 self.writeAsNull(podofoFileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000209 cnt = cnt + 1
210
211 if cls.fName != 'Object':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000212 podofoFileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000213 base = self.fClasses[cls.fBase]
214 cnt = 0
215 for sub in base.fEnumSubclasses:
216 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000217 self.writeAsNull(podofoFileClass, enumToCls[base.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000218 cnt = cnt + 1
219
220
edisonn@google.comff278442013-06-21 21:03:15 +0000221 def determineAllMustBe(self, cls, field, enumToCls):
222 mustBe = []
223 for sub in cls.fEnumSubclasses:
224 mustBe = mustBe + self.determineAllMustBe(enumToCls[sub], field, enumToCls)
225
226 for subField in cls.fFields:
227 if subField.fProp.fName == field.fProp.fName:
228 mustBe = mustBe + subField.fProp.fMustBe
229
230# while cls.fBase != '':
231# cls = self.fClasses[cls.fBase]
232# # TODO(edisonn): bad perf
233# for subField in cls.fFields:
234# if subField.fProp.fName == field.fProp.fName:
235# mustBe = mustBe + subField.fProp.fMustBe
236
237 return mustBe
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000238
edisonn@google.com1a191c62013-06-11 21:44:08 +0000239 def write(self):
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000240 global fileHeadersPodofo
241 global fileHeadersPodofoCpp
edisonn@google.com1277cf02013-06-17 23:36:45 +0000242 global knowTypes
243
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000244 # generate enum
245 enumsRoot = []
246
247 enumToCls = {}
248
249 for name in self.fClasses:
250 cls = self.fClasses[name]
edisonn@google.com59543d32013-06-18 22:00:40 +0000251 cls.fEnum = 'k' + name + '_SkPdfObjectType'
252 cls.fEnumEnd = 'k' + name + '__End_SkPdfObjectType'
253
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000254 fileHeadersPodofo.write('#include "SkPdf' + cls.fName + '_autogen.h"\n')
255 fileHeadersPodofoCpp.write('#include "SkPdf' + cls.fName + '_autogen.cpp"\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000256
257 if cls.fBase != '':
258 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
259
260 if cls.fBase == '':
261 enumsRoot.append(cls.fEnum)
262
263 enumToCls[cls.fEnum] = cls
264
265 enumsRoot.sort()
266
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000267
268 # TODO(edisonn): move each .h in it's own file
269 # write imports
270
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000271 # write enums
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000272 fileEnums = open(sys.argv[1] + 'autogen/SkPdfEnums_autogen.h', 'w')
edisonn@google.com59543d32013-06-18 22:00:40 +0000273 fileEnums.write('#ifndef __DEFINED__SkPdfEnums\n')
274 fileEnums.write('#define __DEFINED__SkPdfEnums\n')
275 fileEnums.write('\n')
276
277 fileEnums.write('enum SkPdfObjectType {\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000278 for enum in enumsRoot:
edisonn@google.com59543d32013-06-18 22:00:40 +0000279 self.writeEnum(fileEnums, enum, enumToCls)
280 fileEnums.write('};\n')
281 fileEnums.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000282
283 # write forward class declaration
284 for name in self.fClassesNamesInOrder:
edisonn@google.com59543d32013-06-18 22:00:40 +0000285 fileEnums.write('class SkPdf' + name + ';\n')
286 fileEnums.write('\n')
287
288 fileEnums.write('#endif // __DEFINED__SkPdfEnums\n')
289 fileEnums.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000290
291 for name in self.fClassesNamesInOrder:
292 cls = self.fClasses[name]
293 enum = cls.fEnum
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000294
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000295 podofoFileClass = open(sys.argv[1] + 'podofo/autogen/SkPdf' + cls.fName + '_autogen.h', 'w')
296 podofoFileClassCpp = open(sys.argv[1] + 'podofo/autogen/SkPdf' + cls.fName + '_autogen.cpp', 'w')
edisonn@google.com59543d32013-06-18 22:00:40 +0000297
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000298 podofoFileClass.write('#ifndef __DEFINED__SkPdf' + cls.fName + '\n')
299 podofoFileClass.write('#define __DEFINED__SkPdf' + cls.fName + '\n')
300 podofoFileClass.write('\n')
301
302 podofoFileClassCpp.write('#include "SkPdf' + cls.fName + '_autogen.h"\n\n')
303 podofoFileClassCpp.write('#include "podofo.h"\n')
304 podofoFileClassCpp.write('#include "SkPodofoUtils.h"\n')
305 podofoFileClassCpp.write('#include "SkPdfMapper_autogen.h"\n')
306 podofoFileClassCpp.write('\n')
307
308
309 if cls.fBase == '':
310 podofoFileClass.write('#include "stddef.h"\n')
311 podofoFileClass.write('#include <string>\n')
312 podofoFileClass.write('#include "SkPdfEnums_autogen.h"\n')
313 podofoFileClass.write('#include "SkPdfNYI.h"\n')
314 podofoFileClass.write('#include "SkPodofoUtils.h"\n')
315
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000316 if cls.fBase != '':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000317 podofoFileClass.write('#include "SkPdf' + cls.fBase + '_autogen.h"\n')
318
319 if cls.fBase == '':
320 podofoFileClass.write('#include "SkPodofoParsedPDF.h"\n')
321
322 podofoFileClass.write('\n')
323
324 if cls.fBase == '':
325 podofoFileClass.write('namespace PoDoFo {\n')
326 podofoFileClass.write('class PdfMemDocument;\n')
327 podofoFileClass.write('class PdfObject;\n')
328 podofoFileClass.write('}\n')
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000329
330 if cls.fComment != '':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000331 podofoFileClass.write('// ' + cls.fComment + '\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000332
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000333 if cls.fBase == '':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000334 podofoFileClass.write('class SkPdf' + cls.fName + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000335 else:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000336 podofoFileClass.write('class SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000337
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000338 podofoFileClass.write('public:\n')
339 podofoFileClass.write(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000340 if len(cls.fEnumSubclasses) == 0:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000341 podofoFileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjectType)(' + cls.fEnum + ' + 1);}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000342 else:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000343 podofoFileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000344
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000345 self.writeAsFoo(podofoFileClass, cls, enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000346
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000347 podofoFileClass.write('public:\n')
348
349 for cc in cls.fCCPublicPodofo:
350 podofoFileClass.write(' ' + cc + '\n')
351
352 for cc in cls.fCCPublicPodofoCpp:
353 podofoFileClassCpp.write(cc + '\n\n')
354
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000355
356 if cls.fBase == '':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000357 podofoFileClass.write('protected:\n')
358 podofoFileClass.write(' const PoDoFo::PdfMemDocument* fPodofoDoc;\n')
359 podofoFileClass.write(' const SkPodofoParsedPDF* fParsedDoc;\n')
360 podofoFileClass.write(' const PoDoFo::PdfObject* fPodofoObj;\n')
361 podofoFileClass.write('\n')
362
363 podofoFileClass.write('public:\n')
364
365 podofoFileClass.write(' SkPdf' + cls.fName + '(const SkPodofoParsedPDF* doc = NULL, const PoDoFo::PdfObject* podofoObj = NULL) : fPodofoDoc(doc->podofo()), fParsedDoc(doc), fPodofoObj(podofoObj) {}\n')
366 podofoFileClass.write('\n')
367 podofoFileClass.write(' const SkPodofoParsedPDF* doc() const { return fParsedDoc;}\n')
368 podofoFileClass.write(' const void* data() const {return fPodofoObj;}\n')
369 podofoFileClass.write(' const PoDoFo::PdfObject* podofo() const {return fPodofoObj;}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000370 else:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000371 podofoFileClass.write('public:\n')
372 podofoFileClass.write(' SkPdf' + cls.fName + '(const SkPodofoParsedPDF* doc = NULL, const PoDoFo::PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(doc, podofoObj) {}\n')
373 podofoFileClass.write('\n')
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000374
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000375
376 # TODO(edisonn): add is valid ?
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000377 #check required fieds, also, there should be an internal_valid() manually wrote for complex
378 # situations
379 # right now valid return true
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000380 #podofoFileClass.write(' virtual bool valid() const {return true;}\n')
381 #podofoFileClass.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000382
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000383 for field in cls.fFields:
384 prop = field.fProp
385 if prop.fCppName != '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000386
387 lines = prop.fComment.split('\n')
388 if prop.fComment != '' and len(lines) > 0:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000389 podofoFileClass.write('/** ' + lines[0] + '\n')
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000390 for line in lines[1:]:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000391 podofoFileClass.write(' * ' + line + '\n')
392 podofoFileClass.write('**/\n')
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000393
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000394 if prop.fCppName[0] == '[':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000395 podofoFileClass.write('/*\n') # comment code of the atributes that can have any name
396 podofoFileClassCpp.write('/*\n') # comment code of the atributes that can have any name
edisonn@google.com1277cf02013-06-17 23:36:45 +0000397
edisonn@google.com1277cf02013-06-17 23:36:45 +0000398
399 if len(prop.fTypes.split()) == 1:
400 t = prop.fTypes.strip()
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000401
402 podofoFileClass.write(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const;\n')
403 podofoFileClassCpp.write('' + knowTypes[t][0] + ' SkPdf' + cls.fName + '::' + prop.fCppName + '() const {\n')
404 podofoFileClassCpp.write(' ' + knowTypes[t][0] + ' ret;\n')
405
406 #hack, find out if it is dict, they have an extra entry in the array
407 if len(knowTypes[t]) == 5:
408 podofoFileClassCpp.write(' if (fParsedDoc->mapper()->' + knowTypes[t][1] + '(podofo()->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000409 else:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000410 podofoFileClassCpp.write(' if (' + knowTypes[t][1] + '(fParsedDoc, podofo()->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
411
412 if field.fRequired == False and prop.fDefault != '':
413 podofoFileClassCpp.write(' return ' + prop.fDefault.toCpp() + ';\n');
414 else:
415 podofoFileClassCpp.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
416 podofoFileClassCpp.write(' return ' + knowTypes[t][2].toCpp() + ';\n');
417 podofoFileClassCpp.write('}\n')
418 podofoFileClassCpp.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000419 else:
420 for type in prop.fTypes.split():
421 t = type.strip()
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000422
423 podofoFileClass.write(' bool is' + prop.fCppName + 'A' + t.title() + '() const;\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000424
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000425 podofoFileClassCpp.write('bool SkPdf' + cls.fName + '::is' + prop.fCppName + 'A' + t.title() + '() const {\n')
426 podofoFileClassCpp.write(' SkPdfObject* ret = NULL;\n')
427 podofoFileClassCpp.write(' if (!fParsedDoc->mapper()->SkPdfObjectFromDictionary(podofo()->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;\n')
428 podofoFileClassCpp.write(' return ' + knowTypes[t][3] + ';\n')
429 podofoFileClassCpp.write('}\n')
430 podofoFileClassCpp.write('\n')
431
432 podofoFileClass.write(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const;\n')
433 podofoFileClassCpp.write('' + knowTypes[t][0] + ' SkPdf' + cls.fName + '::get' + prop.fCppName + 'As' + t.title() + '() const {\n')
434 podofoFileClassCpp.write(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';\n')
435
436 # hack
437 if len(knowTypes[t]) == 5:
438 podofoFileClassCpp.write(' if (fParsedDoc->mapper()->' + knowTypes[t][1] + '(podofo()->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
439 else:
440 podofoFileClassCpp.write(' if (' + knowTypes[t][1] + '(fParsedDoc, podofo()->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
441
442 podofoFileClassCpp.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
443 podofoFileClassCpp.write(' return ' + knowTypes[t][2].toCpp() + ';\n')
444 podofoFileClassCpp.write('}\n')
445 podofoFileClassCpp.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000446
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000447 podofoFileClass.write(' bool has_' + prop.fCppName + '() const;\n')
448 podofoFileClassCpp.write('bool SkPdf' + cls.fName + '::has_' + prop.fCppName + '() const {\n')
449 podofoFileClassCpp.write(' return (ObjectFromDictionary(fParsedDoc, podofo()->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));\n')
450 podofoFileClassCpp.write('}\n')
451 podofoFileClassCpp.write('\n')
edisonn@google.com45327112013-06-13 20:02:29 +0000452
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000453 if prop.fCppName[0] == '[':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000454 podofoFileClass.write('*/\n') # comment code of the atributes that can have any name
455 podofoFileClassCpp.write('*/\n') # comment code of the atributes that can have any name
edisonn@google.com45327112013-06-13 20:02:29 +0000456
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000457
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000458 podofoFileClass.write('};\n')
459 podofoFileClass.write('\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000460
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000461 podofoFileClass.write('#endif // __DEFINED__PODOFO_SkPdf' + cls.fName + '\n')
462
463 podofoFileClass.close()
464 podofoFileClassCpp.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000465
466 # generate constructor when knowing the type
467 # later, p2, generate constructor when not knowing the type - very similar with parsing?
468
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000469 # generate parser
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000470 # TODO(edisonn): fast recognition based on must attributes.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000471 fileMapperPodofo = open(sys.argv[1] + 'podofo/autogen/SkPdfMapper_autogen.h', 'w')
472 fileMapperPodofoCpp = open(sys.argv[1] + 'podofo/autogen/SkPdfMapper_autogen.cpp', 'w')
edisonn@google.com59543d32013-06-18 22:00:40 +0000473
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000474 fileMapperPodofo.write('#ifndef __DEFINED__SkPdfMapper\n')
475 fileMapperPodofo.write('#define __DEFINED__SkPdfMapper\n')
476 fileMapperPodofo.write('\n')
477
478 fileMapperPodofo.write('#include "SkPdfHeaders_autogen.h"\n')
479
480
481 fileMapperPodofo.write('namespace PoDoFo {\n')
482 fileMapperPodofo.write('class PdfDictionary;\n')
483 fileMapperPodofo.write('class PdfMemDocument;\n')
484 fileMapperPodofo.write('class PdfObject;\n')
485 fileMapperPodofo.write('}\n')
486
487 fileMapperPodofoCpp.write('#include "SkPdfMapper_autogen.h"\n')
488 fileMapperPodofoCpp.write('#include "SkPdfUtils.h"\n')
489 fileMapperPodofoCpp.write('#include "podofo.h"\n')
490 fileMapperPodofoCpp.write('\n')
491
492 fileMapperPodofo.write('class SkPdfMapper {\n')
493
494 fileMapperPodofo.write(' const SkPodofoParsedPDF* fParsedDoc;\n')
495 fileMapperPodofo.write(' const PoDoFo::PdfMemDocument* fPodofoDoc;\n')
496
497 fileMapperPodofo.write('public:\n')
498
499 fileMapperPodofo.write(' SkPdfMapper(const SkPodofoParsedPDF* doc) : fParsedDoc(doc), fPodofoDoc(doc ? doc->podofo() : NULL) {}\n')
500 fileMapperPodofo.write('\n')
501
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000502 for name in self.fClassesNamesInOrder:
503 cls = self.fClasses[name]
504
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000505 fileMapperPodofo.write(' bool map' + name + '(const SkPdfObject* in, SkPdf' + name + '** out) const;\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000506
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000507 fileMapperPodofoCpp.write('bool SkPdfMapper::map' + name + '(const SkPdfObject* in, SkPdf' + name + '** out) const {\n')
508 fileMapperPodofoCpp.write(' return map' + name + '((const PoDoFo::PdfObject*)in->data(), (SkPdf' + name + '**)out);\n')
509 fileMapperPodofoCpp.write('}\n')
510 fileMapperPodofoCpp.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000511
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000512 fileMapperPodofo.write(' bool map' + name + '(const PoDoFo::PdfObject* podofoObj, SkPdf' + name + '** out) const ;\n')
513 fileMapperPodofoCpp.write('bool SkPdfMapper::map' + name + '(const PoDoFo::PdfObject* podofoObj, SkPdf' + name + '** out) const {\n')
514 fileMapperPodofoCpp.write(' if (!is' + name + '(podofoObj)) return false;\n')
515 fileMapperPodofoCpp.write('\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000516
517 # stream must be last one
518 hasStream = False
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000519 for sub in cls.fEnumSubclasses:
edisonn@google.comff278442013-06-21 21:03:15 +0000520 if cls.fName == 'Object' and enumToCls[sub].fName == 'Stream':
521 hasStream = True
522 else:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000523 fileMapperPodofoCpp.write(' if (map' + enumToCls[sub].fName + '(podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000524
525 if hasStream:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000526 fileMapperPodofoCpp.write(' if (mapStream(podofoObj, (SkPdfStream**)out)) return true;\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000527
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000528
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000529 fileMapperPodofoCpp.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000530
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000531 fileMapperPodofoCpp.write(' *out = new SkPdf' + name + '(fParsedDoc, podofoObj);\n')
532 fileMapperPodofoCpp.write(' return true;\n')
533 fileMapperPodofoCpp.write('}\n')
534 fileMapperPodofoCpp.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000535
536 for name in self.fClassesNamesInOrder:
537 cls = self.fClasses[name]
538
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000539 fileMapperPodofo.write(' bool is' + name + '(const PoDoFo::PdfObject* podofoObj) const ;\n')
540 fileMapperPodofoCpp.write('bool SkPdfMapper::is' + name + '(const PoDoFo::PdfObject* podofoObj) const {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000541
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000542 if cls.fCheck != '':
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000543 fileMapperPodofoCpp.write(' return ' + cls.fCheck + ';\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000544 else:
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000545 cntMust = 0
546 for field in cls.fFields:
547 prop = field.fProp
548 if prop.fHasMust:
549 cntMust = cntMust + 1
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000550 fileMapperPodofoCpp.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';\n')
551 fileMapperPodofoCpp.write(' if (!podofoObj->IsDictionary()) return false;\n')
552 fileMapperPodofoCpp.write(' if (!' + knowTypes[prop.fTypes.strip()][1] + '(fParsedDoc, podofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000553
554 eval = '';
555 # TODO(edisonn): this could get out of hand, and could have poor performance if continued on this path
556 # but if we would write our parser, then best thing would be to create a map of (key, value) -> to bits
557 # 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 dictionary
558 # and for non unique posinilities (if any) based on context, or the requester of dictionry we can determine fast the dictionary type
559 mustBe = self.determineAllMustBe(cls, field, enumToCls)
560 if len(mustBe) > 0:
561 for cnd in mustBe:
562 if eval == '':
563 eval = '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')'
564 else:
565 eval = eval + ' && ' + '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')'
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000566 fileMapperPodofoCpp.write(' if (' + eval + ') return false;\n')
567 fileMapperPodofoCpp.write('\n')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000568
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000569 fileMapperPodofoCpp.write(' return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000570
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000571 fileMapperPodofoCpp.write('}\n')
572 fileMapperPodofoCpp.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000573
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000574 fileMapperPodofo.write(' bool SkPdf' + name + 'FromDictionary(const PoDoFo::PdfDictionary& dict, const char* key, SkPdf' + name + '** data) const ;\n')
575 fileMapperPodofoCpp.write('bool SkPdfMapper::SkPdf' + name + 'FromDictionary(const PoDoFo::PdfDictionary& dict, const char* key, SkPdf' + name + '** data) const {\n')
576 fileMapperPodofoCpp.write(' const PoDoFo::PdfObject* value = resolveReferenceObject(fParsedDoc, dict.GetKey(PoDoFo::PdfName(key)), true);\n')
577 fileMapperPodofoCpp.write(' if (value == NULL) { return false; }\n')
578 fileMapperPodofoCpp.write(' if (data == NULL) { return true; }\n')
579 fileMapperPodofoCpp.write(' return map' + name + '(value, (SkPdf' + name + '**)data);\n')
580 fileMapperPodofoCpp.write('}\n')
581 fileMapperPodofoCpp.write('\n')
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000582
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000583 fileMapperPodofo.write(' bool SkPdf' + name + 'FromDictionary(const PoDoFo::PdfDictionary& dict, const char* key, const char* abr, SkPdf' + name + '** data) const ;\n')
584 fileMapperPodofoCpp.write('bool SkPdfMapper::SkPdf' + name + 'FromDictionary(const PoDoFo::PdfDictionary& dict, const char* key, const char* abr, SkPdf' + name + '** data) const {\n')
585 fileMapperPodofoCpp.write(' if (SkPdf' + name + 'FromDictionary(dict, key, data)) return true;\n')
586 fileMapperPodofoCpp.write(' if (abr == NULL || *abr == \'\\0\') return false;\n')
587 fileMapperPodofoCpp.write(' return SkPdf' + name + 'FromDictionary(dict, abr, data);\n')
588 fileMapperPodofoCpp.write('}\n')
589 fileMapperPodofoCpp.write('\n')
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000590
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000591 fileMapperPodofo.write('};\n')
592 fileMapperPodofo.write('\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000593
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000594 fileMapperPodofo.write('#endif // __DEFINED__SkPdfMapper\n')
595
596 fileMapperPodofo.close()
597 fileMapperPodofoCpp.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000598
edisonn@google.com1a191c62013-06-11 21:44:08 +0000599 return
600
601def generateCode():
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000602 global fileHeadersPodofo
603 global fileHeadersPodofoCpp
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000604 global knowTypes
edisonn@google.com59543d32013-06-18 22:00:40 +0000605
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000606 fileHeadersPodofo = open(sys.argv[1] + 'podofo/autogen/SkPdfHeaders_autogen.h', 'w')
607 fileHeadersPodofoCpp = open(sys.argv[1] + 'podofo/autogen/SkPdfHeaders_autogen.cpp', 'w')
edisonn@google.com59543d32013-06-18 22:00:40 +0000608
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000609 fileHeadersPodofo.write('#ifndef __DEFINED__SkPdfHeaders\n')
610 fileHeadersPodofo.write('#define __DEFINED__SkPdfHeaders\n')
611 fileHeadersPodofo.write('\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000612
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000613 fileHeadersPodofoCpp.write('#include "SkPdfHeaders_autogen.h"\n')
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000614
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000615 manager = PdfClassManager()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000616
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000617 manager.addClass('Object')
edisonn@google.com1a191c62013-06-11 21:44:08 +0000618
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000619 # TODO(edisonn): perf, instead of virtual functions, store data in field and reurn it.
620 # maybe in constructor load it, or laizy load it
621
622 manager.addClass('Null').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Null')
623 manager.addClass('Boolean').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Bool')\
624 .carbonCopyPublicPodofo('bool value() const;')\
625 .carbonCopyPublicPodofoCpp('bool SkPdfBoolean::value() const {return podofo()->GetBool();}')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000626
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000627 manager.addClass('Integer').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Number || podofoObj->GetDataType() == PoDoFo::ePdfDataType_Real')\
628 .carbonCopyPublicPodofo('long value() const;')\
629 .carbonCopyPublicPodofoCpp('long SkPdfInteger::value() const {return podofo()->GetNumber();}')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000630
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000631 manager.addClass('Number', 'Integer').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Number || podofoObj->GetDataType() == PoDoFo::ePdfDataType_Real')\
632 .carbonCopyPublicPodofo('double value() const;')\
633 .carbonCopyPublicPodofoCpp('double SkPdfNumber::value() const {return podofo()->GetReal();}')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000634
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000635 manager.addClass('Name').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Name')\
636 .carbonCopyPublicPodofo('const std::string& value() const;')\
637 .carbonCopyPublicPodofoCpp('const std::string& SkPdfName::value() const {return podofo()->GetName().GetName();}')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000638
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000639 manager.addClass('Reference').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Reference')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000640
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000641 manager.addClass('Array').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Array')\
642 .carbonCopyPublicPodofo('const int size() const;')\
643 .carbonCopyPublicPodofoCpp('const int SkPdfArray::size() const {return podofo()->GetArray().GetSize();}')\
644 .carbonCopyPublicPodofo('SkPdfObject* operator[](int i) const;')\
645 .carbonCopyPublicPodofoCpp('SkPdfObject* SkPdfArray::operator[](int i) const { SkPdfObject* ret = NULL; fParsedDoc->mapper()->mapObject(&podofo()->GetArray()[i], &ret); return ret; }')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000646
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000647 manager.addClass('String').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_String || podofoObj->GetDataType() == PoDoFo::ePdfDataType_HexString')\
648 .carbonCopyPublicPodofo('const std::string& value() const;')\
649 .carbonCopyPublicPodofoCpp('const std::string& SkPdfString::value() const {return podofo()->GetString().GetStringUtf8();}')\
650 .carbonCopyPublicPodofo('const char* c_str() const;')\
651 .carbonCopyPublicPodofoCpp('const char* SkPdfString::c_str() const {return podofo()->GetString().GetString();}')\
652 .carbonCopyPublicPodofo('size_t len() const;')\
653 .carbonCopyPublicPodofoCpp('size_t SkPdfString::len() const {return podofo()->GetString().GetLength();}')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000654
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000655 manager.addClass('HexString', 'String').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_HexString')\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000656
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000657 manager.addClass('Dictionary').check('podofoObj->GetDataType() == PoDoFo::ePdfDataType_Dictionary')\
658 .carbonCopyPublicPodofo('SkPdfObject* get(const char* dictionaryKeyName) const;')\
659 .carbonCopyPublicPodofoCpp('SkPdfObject* SkPdfDictionary::get(const char* dictionaryKeyName) const {SkPdfObject* ret = NULL; fParsedDoc->mapper()->mapObject(resolveReferenceObject(fParsedDoc, podofo()->GetDictionary().GetKey(PoDoFo::PdfName(dictionaryKeyName))), &ret); return ret;}')\
edisonn@google.comff278442013-06-21 21:03:15 +0000660
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000661 # attached to a dictionary in podofo
662 manager.addClass('Stream')\
663 .carbonCopyPublicPodofo('bool GetFilteredCopy(char** buffer, long* len) const;')\
664 .carbonCopyPublicPodofoCpp('bool SkPdfStream::GetFilteredCopy(char** buffer, long* len) const {try {PoDoFo::pdf_long podofoLen = 0; *buffer = NULL; *len = 0;podofo()->GetStream()->GetFilteredCopy(buffer, &podofoLen); *len = (long)podofoLen;} catch (PoDoFo::PdfError& e) { return false; } return true;}')
edisonn@google.com59543d32013-06-18 22:00:40 +0000665
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000666
667 # these classes are not explicitely backed by a table in the pdf spec
668 manager.addClass('XObjectDictionary', 'Dictionary')
669
670 manager.addClass('FontDictionary', 'Dictionary')
671
edisonn@google.comff278442013-06-21 21:03:15 +0000672 manager.addClass('TrueTypeFontDictionary', 'Type1FontDictionary')\
673 .required('NULL')\
674 .field('Subtype')\
675 .name('Subtype')\
676 .type('name')\
677 .comment('')\
678 .must([datatypes.PdfName('TrueType')])\
679 .done().done()\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000680
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000681
682 addDictionaryTypesTo(knowTypes)
683 buildPdfSpec(manager)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000684
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000685 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\
686 .required('NULL')\
687 .field('Subtype')\
688 .name('Subtype')\
edisonn@google.com45327112013-06-13 20:02:29 +0000689 .type('name')\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000690 .comment('')\
edisonn@google.comff278442013-06-21 21:03:15 +0000691 .must([datatypes.PdfName('MMType1')])\
edisonn@google.com45327112013-06-13 20:02:29 +0000692 .done().done()\
edisonn@google.com45327112013-06-13 20:02:29 +0000693
694
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000695 manager.write()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000696
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000697 fileHeadersPodofo.write('#endif // __DEFINED__SkPdfHeaders\n')
698
699 fileHeadersPodofo.close()
700 fileHeadersPodofoCpp.close()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000701
702if '__main__' == __name__:
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000703 #print sys.argv
edisonn@google.com1a191c62013-06-11 21:44:08 +0000704 sys.exit(generateCode())
edisonn@google.com45327112013-06-13 20:02:29 +0000705