blob: f128e444ca224be9ee45954cef44ed6c4cdfecc9 [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'],
edisonn@google.comff278442013-06-21 21:03:15 +000023'stream': ['SkPdfStream*', 'StreamFromDictionary', datatypes.CppNull(), 'ret->podofo()->HasStream()'],
edisonn@google.com1277cf02013-06-17 23:36:45 +000024'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.comff278442013-06-21 21:03:15 +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.comff278442013-06-21 21:03:15 +0000215 def determineAllMustBe(self, cls, field, enumToCls):
216 mustBe = []
217 for sub in cls.fEnumSubclasses:
218 mustBe = mustBe + self.determineAllMustBe(enumToCls[sub], field, enumToCls)
219
220 for subField in cls.fFields:
221 if subField.fProp.fName == field.fProp.fName:
222 mustBe = mustBe + subField.fProp.fMustBe
223
224# while cls.fBase != '':
225# cls = self.fClasses[cls.fBase]
226# # TODO(edisonn): bad perf
227# for subField in cls.fFields:
228# if subField.fProp.fName == field.fProp.fName:
229# mustBe = mustBe + subField.fProp.fMustBe
230
231 return mustBe
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000232
edisonn@google.com1a191c62013-06-11 21:44:08 +0000233 def write(self):
edisonn@google.com59543d32013-06-18 22:00:40 +0000234 global fileHeaders
edisonn@google.com1277cf02013-06-17 23:36:45 +0000235 global knowTypes
236
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000237 # generate enum
238 enumsRoot = []
239
240 enumToCls = {}
241
242 for name in self.fClasses:
243 cls = self.fClasses[name]
edisonn@google.com59543d32013-06-18 22:00:40 +0000244 cls.fEnum = 'k' + name + '_SkPdfObjectType'
245 cls.fEnumEnd = 'k' + name + '__End_SkPdfObjectType'
246
247 fileHeaders.write('#include "SkPdf' + cls.fName + '_autogen.h"\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000248
249 if cls.fBase != '':
250 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
251
252 if cls.fBase == '':
253 enumsRoot.append(cls.fEnum)
254
255 enumToCls[cls.fEnum] = cls
256
257 enumsRoot.sort()
258
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000259
260 # TODO(edisonn): move each .h in it's own file
261 # write imports
262
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000263 # write enums
edisonn@google.com59543d32013-06-18 22:00:40 +0000264 fileEnums = open('SkPdfEnums_autogen.h', 'w')
265 fileEnums.write('#ifndef __DEFINED__SkPdfEnums\n')
266 fileEnums.write('#define __DEFINED__SkPdfEnums\n')
267 fileEnums.write('\n')
268
269 fileEnums.write('enum SkPdfObjectType {\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000270 for enum in enumsRoot:
edisonn@google.com59543d32013-06-18 22:00:40 +0000271 self.writeEnum(fileEnums, enum, enumToCls)
272 fileEnums.write('};\n')
273 fileEnums.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000274
275 # write forward class declaration
276 for name in self.fClassesNamesInOrder:
edisonn@google.com59543d32013-06-18 22:00:40 +0000277 fileEnums.write('class SkPdf' + name + ';\n')
278 fileEnums.write('\n')
279
280 fileEnums.write('#endif // __DEFINED__SkPdfEnums\n')
281 fileEnums.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000282
283 for name in self.fClassesNamesInOrder:
284 cls = self.fClasses[name]
285 enum = cls.fEnum
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000286
edisonn@google.com59543d32013-06-18 22:00:40 +0000287 fileClass = open('SkPdf' + cls.fName + '_autogen.h', 'w')
288 fileClass.write('#ifndef __DEFINED__SkPdf' + cls.fName + '\n')
289 fileClass.write('#define __DEFINED__SkPdf' + cls.fName + '\n')
290 fileClass.write('\n')
291
292 fileClass.write('#include "SkPdfEnums_autogen.h"\n')
293 fileClass.write('#include "SkPdfArray_autogen.h"\n')
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000294 if cls.fBase != '':
295 fileClass.write('#include "SkPdf' + cls.fBase + '_autogen.h"\n')
296 fileClass.write('\n')
297
298 if cls.fComment != '':
299 fileClass.write('// ' + cls.fComment + '\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000300
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000301 if cls.fBase == '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000302 fileClass.write('class SkPdf' + cls.fName + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000303 else:
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000304 fileClass.write('class SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000305
edisonn@google.com59543d32013-06-18 22:00:40 +0000306 fileClass.write('public:\n')
307 fileClass.write(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000308 if len(cls.fEnumSubclasses) == 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000309 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjectType)(' + cls.fEnum + ' + 1);}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000310 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000311 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000312
edisonn@google.com59543d32013-06-18 22:00:40 +0000313 self.writeAsFoo(fileClass, cls, enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000314
edisonn@google.com59543d32013-06-18 22:00:40 +0000315 fileClass.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000316 for cc in cls.fCCPublic:
edisonn@google.com59543d32013-06-18 22:00:40 +0000317 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000318
edisonn@google.com59543d32013-06-18 22:00:40 +0000319 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000320 for cc in cls.fCCPrivate:
edisonn@google.com59543d32013-06-18 22:00:40 +0000321 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000322
323 if cls.fBase == '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000324 fileClass.write('protected:\n')
325 fileClass.write(' const PdfMemDocument* fPodofoDoc;\n')
326 fileClass.write(' const PdfObject* fPodofoObj;\n')
327 fileClass.write('\n')
328 fileClass.write('public:\n')
329 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}\n')
330 fileClass.write(' const PdfMemDocument* doc() const { return fPodofoDoc;}\n')
331 fileClass.write(' const PdfObject* podofo() const { return fPodofoObj;}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000332 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000333 fileClass.write('public:\n')
334 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}\n')
335 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000336
337 #check required fieds, also, there should be an internal_valid() manually wrote for complex
338 # situations
339 # right now valid return true
edisonn@google.com59543d32013-06-18 22:00:40 +0000340 fileClass.write(' virtual bool valid() const {return true;}\n')
341 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000342
edisonn@google.com59543d32013-06-18 22:00:40 +0000343 fileClass.write(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fName + '& from) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPodofoObj; return *this;}\n')
344 fileClass.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000345
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000346 for field in cls.fFields:
347 prop = field.fProp
348 if prop.fCppName != '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000349
350 lines = prop.fComment.split('\n')
351 if prop.fComment != '' and len(lines) > 0:
352 fileClass.write('/** ' + lines[0] + '\n')
353 for line in lines[1:]:
354 fileClass.write(' * ' + line + '\n')
355 fileClass.write('**/\n')
356
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000357 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000358 fileClass.write('/*\n') # comment code of the atributes that can have any name
edisonn@google.com1277cf02013-06-17 23:36:45 +0000359
360 # TODO(edisonn): has_foo();
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000361 fileClass.write(' bool has_' + prop.fCppName + '() const {\n')
362 fileClass.write(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));\n')
363 fileClass.write(' }\n')
364 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000365
366 if len(prop.fTypes.split()) == 1:
367 t = prop.fTypes.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000368 fileClass.write(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {\n')
369 fileClass.write(' ' + knowTypes[t][0] + ' ret;\n')
370 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 +0000371 if field.fRequired == False and prop.fDefault != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000372 fileClass.write(' return ' + prop.fDefault.toCpp() + ';\n');
edisonn@google.com1277cf02013-06-17 23:36:45 +0000373 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000374 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 else:
379 for type in prop.fTypes.split():
380 t = type.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000381 fileClass.write(' bool is' + prop.fCppName + 'A' + t.title() + '() const {\n')
382 fileClass.write(' SkPdfObject* ret = NULL;\n')
383 fileClass.write(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;\n')
384 fileClass.write(' return ' + knowTypes[t][3] + ';\n')
385 fileClass.write(' }\n')
386 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000387
edisonn@google.com59543d32013-06-18 22:00:40 +0000388 fileClass.write(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {\n')
389 fileClass.write(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';\n')
390 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
391 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
392 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n')
393 fileClass.write(' }\n')
394 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000395
edisonn@google.com45327112013-06-13 20:02:29 +0000396
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000397 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000398 fileClass.write('*/\n') # comment code of the atributes that can have any name
edisonn@google.com45327112013-06-13 20:02:29 +0000399
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000400
edisonn@google.com59543d32013-06-18 22:00:40 +0000401 fileClass.write('};\n')
402 fileClass.write('\n')
403
404 fileClass.write('#endif // __DEFINED__SkPdf' + cls.fName + '\n')
405 fileClass.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000406
407
408
409 # generate constructor when knowing the type
410 # later, p2, generate constructor when not knowing the type - very similar with parsing?
411
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000412 # generate parser
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000413 # TODO(edisonn): fast recognition based on must attributes.
edisonn@google.com59543d32013-06-18 22:00:40 +0000414 fileMapper = open('SkPdfPodofoMapper_autogen.h', 'w')
415 fileMapper.write('#ifndef __DEFINED__SkPdfPodofoMapper\n')
416 fileMapper.write('#define __DEFINED__SkPdfPodofoMapper\n')
417 fileMapper.write('\n')
418
419 fileMapper.write('#include "SkPdfHeaders_autogen.h"\n')
420 fileMapper.write('class PodofoMapper {\n')
421 fileMapper.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000422 for name in self.fClassesNamesInOrder:
423 cls = self.fClasses[name]
424
edisonn@google.com68d15c82013-06-17 20:46:27 +0000425
edisonn@google.com59543d32013-06-18 22:00:40 +0000426 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) {\n')
427 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n')
428 fileMapper.write(' }\n')
429 fileMapper.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000430
edisonn@google.com59543d32013-06-18 22:00:40 +0000431 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {\n')
432 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return false;\n')
433 fileMapper.write('\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000434
435 # stream must be last one
436 hasStream = False
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000437 for sub in cls.fEnumSubclasses:
edisonn@google.comff278442013-06-21 21:03:15 +0000438 if cls.fName == 'Object' and enumToCls[sub].fName == 'Stream':
439 hasStream = True
440 else:
441 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;\n')
442
443 if hasStream:
444 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdfStream**)out)) return true;\n')
445
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000446
edisonn@google.com59543d32013-06-18 22:00:40 +0000447 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000448
edisonn@google.com59543d32013-06-18 22:00:40 +0000449 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj);\n')
450 fileMapper.write(' return true;\n')
451 fileMapper.write(' }\n')
452 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000453
454 for name in self.fClassesNamesInOrder:
455 cls = self.fClasses[name]
456
edisonn@google.com59543d32013-06-18 22:00:40 +0000457 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000458
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000459 if cls.fCheck != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000460 fileMapper.write(' return ' + cls.fCheck + ';\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000461 else:
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000462 cntMust = 0
463 for field in cls.fFields:
464 prop = field.fProp
465 if prop.fHasMust:
466 cntMust = cntMust + 1
edisonn@google.com59543d32013-06-18 22:00:40 +0000467 fileMapper.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';\n')
468 fileMapper.write(' if (!' + knowTypes[prop.fTypes.strip()][1] + '(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000469
470 eval = '';
471 # TODO(edisonn): this could get out of hand, and could have poor performance if continued on this path
472 # but if we would write our parser, then best thing would be to create a map of (key, value) -> to bits
473 # and at each (key, value) we do an and with the bits existent, then we check what bits are left, which would tell the posible types of this dictionary
474 # and for non unique posinilities (if any) based on context, or the requester of dictionry we can determine fast the dictionary type
475 mustBe = self.determineAllMustBe(cls, field, enumToCls)
476 if len(mustBe) > 0:
477 for cnd in mustBe:
478 if eval == '':
479 eval = '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')'
480 else:
481 eval = eval + ' && ' + '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')'
482 fileMapper.write(' if (' + eval + ') return false;\n')
483 fileMapper.write('\n')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000484
edisonn@google.com59543d32013-06-18 22:00:40 +0000485 fileMapper.write(' return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000486
edisonn@google.com59543d32013-06-18 22:00:40 +0000487 fileMapper.write(' }\n')
488 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000489
edisonn@google.com59543d32013-06-18 22:00:40 +0000490 fileMapper.write('};\n')
491 fileMapper.write('\n')
492
493 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n')
494 fileMapper.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000495
edisonn@google.com1a191c62013-06-11 21:44:08 +0000496 return
497
498def generateCode():
edisonn@google.com59543d32013-06-18 22:00:40 +0000499 global fileHeaders
500
501 fileHeaders = open('SkPdfHeaders_autogen.h', 'w')
502 fileHeaders.write('#ifndef __DEFINED__SkPdfHeaders\n')
503 fileHeaders.write('#define __DEFINED__SkPdfHeaders\n')
504 fileHeaders.write('\n')
505
506 fileHeaders.write('#include "SkPdfEnums_autogen.h"\n')
507
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000508 manager = PdfClassManager()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000509
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000510 manager.addClass('Object')
edisonn@google.com1a191c62013-06-11 21:44:08 +0000511
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000512 manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000513 manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Bool')\
514 .carbonCopyPublic('bool value() const {return fPodofoObj->GetBool();}')
515
edisonn@google.com59543d32013-06-18 22:00:40 +0000516 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000517 .carbonCopyPublic('long value() const {return fPodofoObj->GetNumber();}')
518
edisonn@google.com59543d32013-06-18 22:00:40 +0000519 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000520 .carbonCopyPublic('double value() const {return fPodofoObj->GetReal();}')
521
522 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')\
523 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}')
524
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000525 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_Reference')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000526
527 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array')\
528 .carbonCopyPublic('const int size() const {return fPodofoObj->GetArray().GetSize();}')\
edisonn@google.com1be794f2013-06-21 21:43:09 +0000529 .carbonCopyPublic('SkPdfObject* operator[](int i) const { SkPdfObject* ret = NULL; skpdfmap(*fPodofoDoc, fPodofoObj->GetArray()[i], &ret); return ret; }')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000530
edisonn@google.com59543d32013-06-18 22:00:40 +0000531 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_String || podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000532 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
533
edisonn@google.com59543d32013-06-18 22:00:40 +0000534 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000535 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000536
edisonn@google.com59543d32013-06-18 22:00:40 +0000537 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_Dictionary')\
538 .carbonCopyPublic('const SkPdfObject get(const char* dictionaryKeyName) const {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
539 .carbonCopyPublic('SkPdfObject get(const char* dictionaryKeyName) {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
edisonn@google.comff278442013-06-21 21:03:15 +0000540
541 manager.addClass('Stream') # attached to a dictionary in podofo
edisonn@google.com59543d32013-06-18 22:00:40 +0000542
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000543
544 # these classes are not explicitely backed by a table in the pdf spec
545 manager.addClass('XObjectDictionary', 'Dictionary')
546
547 manager.addClass('FontDictionary', 'Dictionary')
548
edisonn@google.comff278442013-06-21 21:03:15 +0000549 manager.addClass('TrueTypeFontDictionary', 'Type1FontDictionary')\
550 .required('NULL')\
551 .field('Subtype')\
552 .name('Subtype')\
553 .type('name')\
554 .comment('')\
555 .must([datatypes.PdfName('TrueType')])\
556 .done().done()\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000557
558 pdfspec_autogen.buildPdfSpec(manager)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000559
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000560 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\
561 .required('NULL')\
562 .field('Subtype')\
563 .name('Subtype')\
edisonn@google.com45327112013-06-13 20:02:29 +0000564 .type('name')\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000565 .comment('')\
edisonn@google.comff278442013-06-21 21:03:15 +0000566 .must([datatypes.PdfName('MMType1')])\
edisonn@google.com45327112013-06-13 20:02:29 +0000567 .done().done()\
edisonn@google.com45327112013-06-13 20:02:29 +0000568
569
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000570 manager.write()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000571
edisonn@google.com59543d32013-06-18 22:00:40 +0000572 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n')
573 fileHeaders.close()
574
edisonn@google.com1a191c62013-06-11 21:44:08 +0000575 return 1
576
577if '__main__' == __name__:
578 sys.exit(generateCode())
edisonn@google.com45327112013-06-13 20:02:29 +0000579