blob: 02736d38490b3e9f5c62615f1f7a1fe28b960c25 [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 = {
9'(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
10'(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
11'(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
edisonn@google.comb857a0c2013-06-25 20:45:40 +000012'array': ['SkPdfArray*', 'ArrayFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
edisonn@google.com1277cf02013-06-17 23:36:45 +000013'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.comb857a0c2013-06-25 20:45:40 +000022'rectangle': ['SkRect*', 'SkRectFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == 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.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'],
edisonn@google.comb857a0c2013-06-25 20:45:40 +000027'matrix': ['SkMatrix*', 'SkMatrixFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == 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()
65 types = types.replace('or', ' ')
66 types = types.replace(',', ' ')
67 types = types.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'?
68 types = types.replace('file specification', 'file_specification')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000069
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000070
edisonn@google.com1277cf02013-06-17 23:36:45 +000071 self.fTypes = types
edisonn@google.com45327112013-06-13 20:02:29 +000072 return self
73
74 def comment(self, comment):
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000075 self.fComment = comment
edisonn@google.com45327112013-06-13 20:02:29 +000076 return self
77
edisonn@google.com1a191c62013-06-11 21:44:08 +000078 def done(self):
79 return self.fParent
80
81
82class PdfClassField:
edisonn@google.comafe5e9e2013-06-19 17:42:17 +000083 def __init__(self, parent, required, version='', inheritable=False):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000084 #self.fProp = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000085 self.fParent = parent
86 self.fRequired = required
edisonn@google.com45327112013-06-13 20:02:29 +000087 self.fVersion = version
88 self.fInheritable = inheritable
edisonn@google.com1a191c62013-06-11 21:44:08 +000089
edisonn@google.comaf3daa02013-06-12 19:07:45 +000090 def field(self, name, abr=''):
91 self.fProp = PdfField(self, name, abr)
92 return self.fProp
edisonn@google.com1a191c62013-06-11 21:44:08 +000093
94 def done(self):
95 return self.fParent
96
97class PdfClass:
edisonn@google.com45327112013-06-13 20:02:29 +000098 def __init__(self, name, base, comment):
edisonn@google.com1a191c62013-06-11 21:44:08 +000099 self.fFields = []
100 self.fIncludes = []
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000101 self.fCCPublic = []
102 self.fCCPrivate = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000103 self.fName = name
104 self.fBase = base
edisonn@google.com45327112013-06-13 20:02:29 +0000105 self.fComment = comment
edisonn@google.com1a191c62013-06-11 21:44:08 +0000106
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000107 self.fEnumSubclasses = []
108
109 self.fEnum = '!UNDEFINED'
110 self.fEnumEnd = '!UNDEFINED'
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000111 self.fCheck = ''
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000112
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000113 def check(self, ifCheck):
114 self.fCheck = ifCheck
115 return self
116
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000117 def required(self, badDefault):
edisonn@google.com1a191c62013-06-11 21:44:08 +0000118 field = PdfClassField(self, True)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000119 field.fBadDefault = badDefault
edisonn@google.com1a191c62013-06-11 21:44:08 +0000120 self.fFields.append(field)
121 return field
122
123 def optional(self):
124 field = PdfClassField(self, False)
125 self.fFields.append(field)
126 return field
edisonn@google.com45327112013-06-13 20:02:29 +0000127
128 #([Required] [;] [inheritable] [;] [version]; [comments])
129 # version: PDF [d].[d]
130 # ; separate props
131 #inheritable
132 #version
133 #required, if
134 #optional, if
edisonn@google.com1a191c62013-06-11 21:44:08 +0000135
136 def include(self, path):
137 self.fIncludes.append(path)
138 return self
139
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000140 def carbonCopyPublic(self, cc):
141 self.fCCPublic.append(cc)
142 return self
143
144 def carbonCopyPrivate(self, cc):
145 self.fCCPrivate.append(cc)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000146 return self
edisonn@google.com45327112013-06-13 20:02:29 +0000147
148 def done(self):
149 return
edisonn@google.com1a191c62013-06-11 21:44:08 +0000150
151class PdfClassManager:
152 def __init__(self):
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000153 self.fClasses = {}
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000154 self.fClassesNamesInOrder = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000155
edisonn@google.com45327112013-06-13 20:02:29 +0000156 def addClass(self, name, base='Object', comment=''):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000157 if name == 'Object':
edisonn@google.com45327112013-06-13 20:02:29 +0000158 cls = PdfClass(name, '', comment)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000159 else:
edisonn@google.com45327112013-06-13 20:02:29 +0000160 cls = PdfClass(name, base, comment)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000161 self.fClasses[name] = cls
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000162 self.fClassesNamesInOrder.append(name)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000163 return cls
164
edisonn@google.com59543d32013-06-18 22:00:40 +0000165 def writeEnum(self, fileEnums, enum, enumToCls):
166 fileEnums.write(' ' + enum + ',\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000167 cls = enumToCls[enum]
168 cls.fEnumSubclasses.sort()
169
170 cnt = 0
171 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000172 self.writeEnum(fileEnums, cls.fEnumSubclasses[cnt], enumToCls)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000173 cnt = cnt + 1
174
175 if cnt != 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000176 fileEnums.write(' ' + cls.fEnumEnd + ',\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000177
178
edisonn@google.com59543d32013-06-18 22:00:40 +0000179 def writeAsNull(self, fileClass, cls, enumToCls):
180 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return NULL;}\n')
181 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return NULL;}\n')
182 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000183
184 cnt = 0
185 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000186 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000187 cnt = cnt + 1
188
189
edisonn@google.com59543d32013-06-18 22:00:40 +0000190 def writeAsFoo(self, fileClass, cls, enumToCls):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000191 # TODO(edisonn): add a container, with sections, public, private, default, ...
192 # the end code will be grouped
193
194 # me
edisonn@google.com59543d32013-06-18 22:00:40 +0000195 fileClass.write('public:\n')
196 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return this;}\n')
197 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return this;}\n')
198 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000199
200 if cls.fName == 'Object':
201 cnt = 0
202 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000203 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000204 cnt = cnt + 1
205
206 if cls.fName != 'Object':
edisonn@google.com59543d32013-06-18 22:00:40 +0000207 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000208 base = self.fClasses[cls.fBase]
209 cnt = 0
210 for sub in base.fEnumSubclasses:
211 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName:
edisonn@google.com59543d32013-06-18 22:00:40 +0000212 self.writeAsNull(fileClass, enumToCls[base.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000213 cnt = cnt + 1
214
215
edisonn@google.comff278442013-06-21 21:03:15 +0000216 def determineAllMustBe(self, cls, field, enumToCls):
217 mustBe = []
218 for sub in cls.fEnumSubclasses:
219 mustBe = mustBe + self.determineAllMustBe(enumToCls[sub], field, enumToCls)
220
221 for subField in cls.fFields:
222 if subField.fProp.fName == field.fProp.fName:
223 mustBe = mustBe + subField.fProp.fMustBe
224
225# while cls.fBase != '':
226# cls = self.fClasses[cls.fBase]
227# # TODO(edisonn): bad perf
228# for subField in cls.fFields:
229# if subField.fProp.fName == field.fProp.fName:
230# mustBe = mustBe + subField.fProp.fMustBe
231
232 return mustBe
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000233
edisonn@google.com1a191c62013-06-11 21:44:08 +0000234 def write(self):
edisonn@google.com59543d32013-06-18 22:00:40 +0000235 global fileHeaders
edisonn@google.com1277cf02013-06-17 23:36:45 +0000236 global knowTypes
237
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000238 # generate enum
239 enumsRoot = []
240
241 enumToCls = {}
242
243 for name in self.fClasses:
244 cls = self.fClasses[name]
edisonn@google.com59543d32013-06-18 22:00:40 +0000245 cls.fEnum = 'k' + name + '_SkPdfObjectType'
246 cls.fEnumEnd = 'k' + name + '__End_SkPdfObjectType'
247
248 fileHeaders.write('#include "SkPdf' + cls.fName + '_autogen.h"\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000249
250 if cls.fBase != '':
251 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
252
253 if cls.fBase == '':
254 enumsRoot.append(cls.fEnum)
255
256 enumToCls[cls.fEnum] = cls
257
258 enumsRoot.sort()
259
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000260
261 # TODO(edisonn): move each .h in it's own file
262 # write imports
263
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000264 # write enums
edisonn@google.com59543d32013-06-18 22:00:40 +0000265 fileEnums = open('SkPdfEnums_autogen.h', 'w')
266 fileEnums.write('#ifndef __DEFINED__SkPdfEnums\n')
267 fileEnums.write('#define __DEFINED__SkPdfEnums\n')
268 fileEnums.write('\n')
269
270 fileEnums.write('enum SkPdfObjectType {\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000271 for enum in enumsRoot:
edisonn@google.com59543d32013-06-18 22:00:40 +0000272 self.writeEnum(fileEnums, enum, enumToCls)
273 fileEnums.write('};\n')
274 fileEnums.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000275
276 # write forward class declaration
277 for name in self.fClassesNamesInOrder:
edisonn@google.com59543d32013-06-18 22:00:40 +0000278 fileEnums.write('class SkPdf' + name + ';\n')
279 fileEnums.write('\n')
280
281 fileEnums.write('#endif // __DEFINED__SkPdfEnums\n')
282 fileEnums.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000283
284 for name in self.fClassesNamesInOrder:
285 cls = self.fClasses[name]
286 enum = cls.fEnum
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000287
edisonn@google.com59543d32013-06-18 22:00:40 +0000288 fileClass = open('SkPdf' + cls.fName + '_autogen.h', 'w')
289 fileClass.write('#ifndef __DEFINED__SkPdf' + cls.fName + '\n')
290 fileClass.write('#define __DEFINED__SkPdf' + cls.fName + '\n')
291 fileClass.write('\n')
292
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000293 fileClass.write('#include "SkPdfUtils.h"\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000294 fileClass.write('#include "SkPdfEnums_autogen.h"\n')
295 fileClass.write('#include "SkPdfArray_autogen.h"\n')
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000296 if cls.fBase != '':
297 fileClass.write('#include "SkPdf' + cls.fBase + '_autogen.h"\n')
298 fileClass.write('\n')
299
300 if cls.fComment != '':
301 fileClass.write('// ' + cls.fComment + '\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000302
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000303 if cls.fBase == '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000304 fileClass.write('class SkPdf' + cls.fName + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000305 else:
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000306 fileClass.write('class SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000307
edisonn@google.com59543d32013-06-18 22:00:40 +0000308 fileClass.write('public:\n')
309 fileClass.write(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000310 if len(cls.fEnumSubclasses) == 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000311 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjectType)(' + cls.fEnum + ' + 1);}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000312 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000313 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000314
edisonn@google.com59543d32013-06-18 22:00:40 +0000315 self.writeAsFoo(fileClass, cls, enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000316
edisonn@google.com59543d32013-06-18 22:00:40 +0000317 fileClass.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000318 for cc in cls.fCCPublic:
edisonn@google.com59543d32013-06-18 22:00:40 +0000319 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000320
edisonn@google.com59543d32013-06-18 22:00:40 +0000321 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000322 for cc in cls.fCCPrivate:
edisonn@google.com59543d32013-06-18 22:00:40 +0000323 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000324
325 if cls.fBase == '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000326 fileClass.write('protected:\n')
327 fileClass.write(' const PdfMemDocument* fPodofoDoc;\n')
328 fileClass.write(' const PdfObject* fPodofoObj;\n')
329 fileClass.write('\n')
330 fileClass.write('public:\n')
331 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}\n')
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000332 fileClass.write(' SkPdf' + cls.fName + '(const SkPdf' + cls.fName + '& from) : fPodofoDoc(from.fPodofoDoc), fPodofoObj(from.fPodofoObj) {}\n')
333 fileClass.write('\n')
edisonn@google.com59543d32013-06-18 22:00:40 +0000334 fileClass.write(' const PdfMemDocument* doc() const { return fPodofoDoc;}\n')
335 fileClass.write(' const PdfObject* podofo() const { return fPodofoObj;}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000336 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000337 fileClass.write('public:\n')
338 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}\n')
339 fileClass.write('\n')
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000340 fileClass.write(' SkPdf' + cls.fName + '(const SkPdf' + cls.fName + '& from) : SkPdf' + cls.fBase + '(from.fPodofoDoc, from.fPodofoObj) {}\n')
341 fileClass.write('\n')
342
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000343
344 #check required fieds, also, there should be an internal_valid() manually wrote for complex
345 # situations
346 # right now valid return true
edisonn@google.com59543d32013-06-18 22:00:40 +0000347 fileClass.write(' virtual bool valid() const {return true;}\n')
348 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000349
edisonn@google.com59543d32013-06-18 22:00:40 +0000350 fileClass.write(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fName + '& from) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPodofoObj; return *this;}\n')
351 fileClass.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000352
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000353 for field in cls.fFields:
354 prop = field.fProp
355 if prop.fCppName != '':
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000356
357 lines = prop.fComment.split('\n')
358 if prop.fComment != '' and len(lines) > 0:
359 fileClass.write('/** ' + lines[0] + '\n')
360 for line in lines[1:]:
361 fileClass.write(' * ' + line + '\n')
362 fileClass.write('**/\n')
363
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000364 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000365 fileClass.write('/*\n') # comment code of the atributes that can have any name
edisonn@google.com1277cf02013-06-17 23:36:45 +0000366
367 # TODO(edisonn): has_foo();
edisonn@google.comafe5e9e2013-06-19 17:42:17 +0000368 fileClass.write(' bool has_' + prop.fCppName + '() const {\n')
369 fileClass.write(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));\n')
370 fileClass.write(' }\n')
371 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000372
373 if len(prop.fTypes.split()) == 1:
374 t = prop.fTypes.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000375 fileClass.write(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {\n')
376 fileClass.write(' ' + knowTypes[t][0] + ' ret;\n')
377 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 +0000378 if field.fRequired == False and prop.fDefault != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000379 fileClass.write(' return ' + prop.fDefault.toCpp() + ';\n');
edisonn@google.com1277cf02013-06-17 23:36:45 +0000380 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000381 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
382 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n');
383 fileClass.write(' }\n')
384 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000385 else:
386 for type in prop.fTypes.split():
387 t = type.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000388 fileClass.write(' bool is' + prop.fCppName + 'A' + t.title() + '() const {\n')
389 fileClass.write(' SkPdfObject* ret = NULL;\n')
390 fileClass.write(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;\n')
391 fileClass.write(' return ' + knowTypes[t][3] + ';\n')
392 fileClass.write(' }\n')
393 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000394
edisonn@google.com59543d32013-06-18 22:00:40 +0000395 fileClass.write(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {\n')
396 fileClass.write(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';\n')
397 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
398 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
399 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n')
400 fileClass.write(' }\n')
401 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000402
edisonn@google.com45327112013-06-13 20:02:29 +0000403
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000404 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000405 fileClass.write('*/\n') # comment code of the atributes that can have any name
edisonn@google.com45327112013-06-13 20:02:29 +0000406
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000407
edisonn@google.com59543d32013-06-18 22:00:40 +0000408 fileClass.write('};\n')
409 fileClass.write('\n')
410
411 fileClass.write('#endif // __DEFINED__SkPdf' + cls.fName + '\n')
412 fileClass.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000413
414
415
416 # generate constructor when knowing the type
417 # later, p2, generate constructor when not knowing the type - very similar with parsing?
418
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000419 # generate parser
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000420 # TODO(edisonn): fast recognition based on must attributes.
edisonn@google.com59543d32013-06-18 22:00:40 +0000421 fileMapper = open('SkPdfPodofoMapper_autogen.h', 'w')
422 fileMapper.write('#ifndef __DEFINED__SkPdfPodofoMapper\n')
423 fileMapper.write('#define __DEFINED__SkPdfPodofoMapper\n')
424 fileMapper.write('\n')
425
426 fileMapper.write('#include "SkPdfHeaders_autogen.h"\n')
427 fileMapper.write('class PodofoMapper {\n')
428 fileMapper.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000429 for name in self.fClassesNamesInOrder:
430 cls = self.fClasses[name]
431
edisonn@google.com68d15c82013-06-17 20:46:27 +0000432
edisonn@google.com59543d32013-06-18 22:00:40 +0000433 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) {\n')
434 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n')
435 fileMapper.write(' }\n')
436 fileMapper.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000437
edisonn@google.com59543d32013-06-18 22:00:40 +0000438 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {\n')
439 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return false;\n')
440 fileMapper.write('\n')
edisonn@google.comff278442013-06-21 21:03:15 +0000441
442 # stream must be last one
443 hasStream = False
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000444 for sub in cls.fEnumSubclasses:
edisonn@google.comff278442013-06-21 21:03:15 +0000445 if cls.fName == 'Object' and enumToCls[sub].fName == 'Stream':
446 hasStream = True
447 else:
448 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;\n')
449
450 if hasStream:
451 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdfStream**)out)) return true;\n')
452
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000453
edisonn@google.com59543d32013-06-18 22:00:40 +0000454 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000455
edisonn@google.com59543d32013-06-18 22:00:40 +0000456 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj);\n')
457 fileMapper.write(' return true;\n')
458 fileMapper.write(' }\n')
459 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000460
461 for name in self.fClassesNamesInOrder:
462 cls = self.fClasses[name]
463
edisonn@google.com59543d32013-06-18 22:00:40 +0000464 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000465
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000466 if cls.fCheck != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000467 fileMapper.write(' return ' + cls.fCheck + ';\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000468 else:
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000469 cntMust = 0
470 for field in cls.fFields:
471 prop = field.fProp
472 if prop.fHasMust:
473 cntMust = cntMust + 1
edisonn@google.com59543d32013-06-18 22:00:40 +0000474 fileMapper.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';\n')
475 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 +0000476
477 eval = '';
478 # TODO(edisonn): this could get out of hand, and could have poor performance if continued on this path
479 # but if we would write our parser, then best thing would be to create a map of (key, value) -> to bits
480 # 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
481 # and for non unique posinilities (if any) based on context, or the requester of dictionry we can determine fast the dictionary type
482 mustBe = self.determineAllMustBe(cls, field, enumToCls)
483 if len(mustBe) > 0:
484 for cnd in mustBe:
485 if eval == '':
486 eval = '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')'
487 else:
488 eval = eval + ' && ' + '(' + prop.fCppName + ' != ' + cnd.toCpp() + ')'
489 fileMapper.write(' if (' + eval + ') return false;\n')
490 fileMapper.write('\n')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000491
edisonn@google.com59543d32013-06-18 22:00:40 +0000492 fileMapper.write(' return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000493
edisonn@google.com59543d32013-06-18 22:00:40 +0000494 fileMapper.write(' }\n')
495 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000496
edisonn@google.com59543d32013-06-18 22:00:40 +0000497 fileMapper.write('};\n')
498 fileMapper.write('\n')
499
500 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n')
501 fileMapper.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000502
edisonn@google.com1a191c62013-06-11 21:44:08 +0000503 return
504
505def generateCode():
edisonn@google.com59543d32013-06-18 22:00:40 +0000506 global fileHeaders
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000507 global knowTypes
edisonn@google.com59543d32013-06-18 22:00:40 +0000508
509 fileHeaders = open('SkPdfHeaders_autogen.h', 'w')
510 fileHeaders.write('#ifndef __DEFINED__SkPdfHeaders\n')
511 fileHeaders.write('#define __DEFINED__SkPdfHeaders\n')
512 fileHeaders.write('\n')
513
514 fileHeaders.write('#include "SkPdfEnums_autogen.h"\n')
515
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000516 manager = PdfClassManager()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000517
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000518 manager.addClass('Object')
edisonn@google.com1a191c62013-06-11 21:44:08 +0000519
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000520 manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000521 manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Bool')\
522 .carbonCopyPublic('bool value() const {return fPodofoObj->GetBool();}')
523
edisonn@google.com59543d32013-06-18 22:00:40 +0000524 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000525 .carbonCopyPublic('long value() const {return fPodofoObj->GetNumber();}')
526
edisonn@google.com59543d32013-06-18 22:00:40 +0000527 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000528 .carbonCopyPublic('double value() const {return fPodofoObj->GetReal();}')
529
530 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')\
531 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}')
532
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000533 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_Reference')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000534
535 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array')\
536 .carbonCopyPublic('const int size() const {return fPodofoObj->GetArray().GetSize();}')\
edisonn@google.com1be794f2013-06-21 21:43:09 +0000537 .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 +0000538
edisonn@google.com59543d32013-06-18 22:00:40 +0000539 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_String || podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000540 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
541
edisonn@google.com59543d32013-06-18 22:00:40 +0000542 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000543 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000544
edisonn@google.com59543d32013-06-18 22:00:40 +0000545 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_Dictionary')\
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000546 .carbonCopyPublic('SkPdfObject* get(const char* dictionaryKeyName) const {return new SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
547 .carbonCopyPublic('SkPdfObject* get(const char* dictionaryKeyName) {return new SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
edisonn@google.comff278442013-06-21 21:03:15 +0000548
549 manager.addClass('Stream') # attached to a dictionary in podofo
edisonn@google.com59543d32013-06-18 22:00:40 +0000550
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000551
552 # these classes are not explicitely backed by a table in the pdf spec
553 manager.addClass('XObjectDictionary', 'Dictionary')
554
555 manager.addClass('FontDictionary', 'Dictionary')
556
edisonn@google.comff278442013-06-21 21:03:15 +0000557 manager.addClass('TrueTypeFontDictionary', 'Type1FontDictionary')\
558 .required('NULL')\
559 .field('Subtype')\
560 .name('Subtype')\
561 .type('name')\
562 .comment('')\
563 .must([datatypes.PdfName('TrueType')])\
564 .done().done()\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000565
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000566
567 addDictionaryTypesTo(knowTypes)
568 buildPdfSpec(manager)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000569
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000570 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\
571 .required('NULL')\
572 .field('Subtype')\
573 .name('Subtype')\
edisonn@google.com45327112013-06-13 20:02:29 +0000574 .type('name')\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000575 .comment('')\
edisonn@google.comff278442013-06-21 21:03:15 +0000576 .must([datatypes.PdfName('MMType1')])\
edisonn@google.com45327112013-06-13 20:02:29 +0000577 .done().done()\
edisonn@google.com45327112013-06-13 20:02:29 +0000578
579
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000580 manager.write()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000581
edisonn@google.com59543d32013-06-18 22:00:40 +0000582 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n')
583 fileHeaders.close()
584
edisonn@google.com1a191c62013-06-11 21:44:08 +0000585 return 1
586
587if '__main__' == __name__:
588 sys.exit(generateCode())
edisonn@google.com45327112013-06-13 20:02:29 +0000589