blob: e730a6fa706ad11a489d5bc529f8dfb9ef06355b [file] [log] [blame]
edisonn@google.coma2fab9d2013-06-14 19:22:19 +00001
2
edisonn@google.com1a191c62013-06-11 21:44:08 +00003import sys
4
edisonn@google.coma2fab9d2013-06-14 19:22:19 +00005import datatypes
6import pdfspec_autogen
edisonn@google.comaf3daa02013-06-12 19:07:45 +00007
edisonn@google.com1277cf02013-06-17 23:36:45 +00008knowTypes = {
9'(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
10'(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
11'(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
12'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
13'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->podofo()->GetDataType() == ePdfDataType_Bool'],
14'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
15'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'],
16'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
17'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo()->GetDataType() == ePdfDataType_Number'],
18'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.FileSpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
19'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_Name'],
20'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
edisonn@google.com59543d32013-06-18 22:00:40 +000021'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podofo()->GetDataType() == ePdfDataType_Real || ret->podofo()->GetDataType() == ePdfDataType_Number'],
edisonn@google.com1277cf02013-06-17 23:36:45 +000022'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
23'stream': ['SkPdfStream', 'StreamFromDictionary', datatypes.PdfEmptyStream(), 'ret->podofo()->HasStream()'],
24'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType() == ePdfDataType_HexString'],
25'text': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType() == ePdfDataType_HexString'],
26'text string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType() == ePdfDataType_HexString'],
27}
edisonn@google.com45327112013-06-13 20:02:29 +000028
29
edisonn@google.com1a191c62013-06-11 21:44:08 +000030class PdfField:
31 def __init__(self, parent, name, abr):
32 self.fParent = parent
33 self.fName = name
34 self.fAbr = abr
35
36 self.fDefault = ''
edisonn@google.com1277cf02013-06-17 23:36:45 +000037 self.fTypes = ''
edisonn@google.comaf3daa02013-06-12 19:07:45 +000038 self.fCppName = ''
edisonn@google.com1277cf02013-06-17 23:36:45 +000039 self.fEnumValues = []
40 self.fHasMust = False
edisonn@google.comaf3daa02013-06-12 19:07:45 +000041 self.fMustBe = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000042
43 def must(self, value):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000044 self.fHasMust = True
45 self.fMustBe = value
46 return self
edisonn@google.com1a191c62013-06-11 21:44:08 +000047
48 def default(self, value):
49 self.fDefault = value
50 return self
51
edisonn@google.com1277cf02013-06-17 23:36:45 +000052 def multiple(self, enumValues):
53 self.fEnumValues = enumValues
edisonn@google.com1a191c62013-06-11 21:44:08 +000054 return self
55
edisonn@google.comaf3daa02013-06-12 19:07:45 +000056 def name(self, name):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000057 self.fCppName = name
edisonn@google.com1a191c62013-06-11 21:44:08 +000058 return self
59
edisonn@google.com1277cf02013-06-17 23:36:45 +000060 def type(self, types):
edisonn@google.com45327112013-06-13 20:02:29 +000061 # 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 +000062 types = types.strip()
63 types = types.replace('or', ' ')
64 types = types.replace(',', ' ')
65 types = types.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'?
66 types = types.replace('file specification', 'file_specification')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000067
edisonn@google.coma2fab9d2013-06-14 19:22:19 +000068
edisonn@google.com1277cf02013-06-17 23:36:45 +000069 self.fTypes = types
edisonn@google.com45327112013-06-13 20:02:29 +000070 return self
71
72 def comment(self, comment):
73 return self
74
edisonn@google.com1a191c62013-06-11 21:44:08 +000075 def done(self):
76 return self.fParent
77
78
79class PdfClassField:
edisonn@google.com45327112013-06-13 20:02:29 +000080 def __init__(self, parent, required, version='', inheritable=False, comment=''):
edisonn@google.comaf3daa02013-06-12 19:07:45 +000081 #self.fProp = ''
edisonn@google.com1a191c62013-06-11 21:44:08 +000082 self.fParent = parent
83 self.fRequired = required
edisonn@google.com45327112013-06-13 20:02:29 +000084 self.fVersion = version
85 self.fInheritable = inheritable
86 self.fComment = comment
edisonn@google.com1a191c62013-06-11 21:44:08 +000087
edisonn@google.comaf3daa02013-06-12 19:07:45 +000088 def field(self, name, abr=''):
89 self.fProp = PdfField(self, name, abr)
90 return self.fProp
edisonn@google.com1a191c62013-06-11 21:44:08 +000091
92 def done(self):
93 return self.fParent
94
95class PdfClass:
edisonn@google.com45327112013-06-13 20:02:29 +000096 def __init__(self, name, base, comment):
edisonn@google.com1a191c62013-06-11 21:44:08 +000097 self.fFields = []
98 self.fIncludes = []
edisonn@google.comaf3daa02013-06-12 19:07:45 +000099 self.fCCPublic = []
100 self.fCCPrivate = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000101 self.fName = name
102 self.fBase = base
edisonn@google.com45327112013-06-13 20:02:29 +0000103 self.fComment = comment
edisonn@google.com1a191c62013-06-11 21:44:08 +0000104
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000105 self.fEnumSubclasses = []
106
107 self.fEnum = '!UNDEFINED'
108 self.fEnumEnd = '!UNDEFINED'
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000109 self.fCheck = ''
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000110
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000111 def check(self, ifCheck):
112 self.fCheck = ifCheck
113 return self
114
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000115 def required(self, badDefault):
edisonn@google.com1a191c62013-06-11 21:44:08 +0000116 field = PdfClassField(self, True)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000117 field.fBadDefault = badDefault
edisonn@google.com1a191c62013-06-11 21:44:08 +0000118 self.fFields.append(field)
119 return field
120
121 def optional(self):
122 field = PdfClassField(self, False)
123 self.fFields.append(field)
124 return field
edisonn@google.com45327112013-06-13 20:02:29 +0000125
126 #([Required] [;] [inheritable] [;] [version]; [comments])
127 # version: PDF [d].[d]
128 # ; separate props
129 #inheritable
130 #version
131 #required, if
132 #optional, if
edisonn@google.com1a191c62013-06-11 21:44:08 +0000133
134 def include(self, path):
135 self.fIncludes.append(path)
136 return self
137
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000138 def carbonCopyPublic(self, cc):
139 self.fCCPublic.append(cc)
140 return self
141
142 def carbonCopyPrivate(self, cc):
143 self.fCCPrivate.append(cc)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000144 return self
edisonn@google.com45327112013-06-13 20:02:29 +0000145
146 def done(self):
147 return
edisonn@google.com1a191c62013-06-11 21:44:08 +0000148
149class PdfClassManager:
150 def __init__(self):
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000151 self.fClasses = {}
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000152 self.fClassesNamesInOrder = []
edisonn@google.com1a191c62013-06-11 21:44:08 +0000153
edisonn@google.com45327112013-06-13 20:02:29 +0000154 def addClass(self, name, base='Object', comment=''):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000155 if name == 'Object':
edisonn@google.com45327112013-06-13 20:02:29 +0000156 cls = PdfClass(name, '', comment)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000157 else:
edisonn@google.com45327112013-06-13 20:02:29 +0000158 cls = PdfClass(name, base, comment)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000159 self.fClasses[name] = cls
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000160 self.fClassesNamesInOrder.append(name)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000161 return cls
162
edisonn@google.com59543d32013-06-18 22:00:40 +0000163 def writeEnum(self, fileEnums, enum, enumToCls):
164 fileEnums.write(' ' + enum + ',\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000165 cls = enumToCls[enum]
166 cls.fEnumSubclasses.sort()
167
168 cnt = 0
169 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000170 self.writeEnum(fileEnums, cls.fEnumSubclasses[cnt], enumToCls)
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000171 cnt = cnt + 1
172
173 if cnt != 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000174 fileEnums.write(' ' + cls.fEnumEnd + ',\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000175
176
edisonn@google.com59543d32013-06-18 22:00:40 +0000177 def writeAsNull(self, fileClass, cls, enumToCls):
178 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return NULL;}\n')
179 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return NULL;}\n')
180 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000181
182 cnt = 0
183 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000184 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000185 cnt = cnt + 1
186
187
edisonn@google.com59543d32013-06-18 22:00:40 +0000188 def writeAsFoo(self, fileClass, cls, enumToCls):
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000189 # TODO(edisonn): add a container, with sections, public, private, default, ...
190 # the end code will be grouped
191
192 # me
edisonn@google.com59543d32013-06-18 22:00:40 +0000193 fileClass.write('public:\n')
194 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return this;}\n')
195 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {return this;}\n')
196 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000197
198 if cls.fName == 'Object':
199 cnt = 0
200 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000201 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000202 cnt = cnt + 1
203
204 if cls.fName != 'Object':
edisonn@google.com59543d32013-06-18 22:00:40 +0000205 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000206 base = self.fClasses[cls.fBase]
207 cnt = 0
208 for sub in base.fEnumSubclasses:
209 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName:
edisonn@google.com59543d32013-06-18 22:00:40 +0000210 self.writeAsNull(fileClass, enumToCls[base.fEnumSubclasses[cnt]], enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000211 cnt = cnt + 1
212
213
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000214
edisonn@google.com1a191c62013-06-11 21:44:08 +0000215 def write(self):
edisonn@google.com59543d32013-06-18 22:00:40 +0000216 global fileHeaders
edisonn@google.com1277cf02013-06-17 23:36:45 +0000217 global knowTypes
218
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000219 # generate enum
220 enumsRoot = []
221
222 enumToCls = {}
223
224 for name in self.fClasses:
225 cls = self.fClasses[name]
edisonn@google.com59543d32013-06-18 22:00:40 +0000226 cls.fEnum = 'k' + name + '_SkPdfObjectType'
227 cls.fEnumEnd = 'k' + name + '__End_SkPdfObjectType'
228
229 fileHeaders.write('#include "SkPdf' + cls.fName + '_autogen.h"\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000230
231 if cls.fBase != '':
232 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
233
234 if cls.fBase == '':
235 enumsRoot.append(cls.fEnum)
236
237 enumToCls[cls.fEnum] = cls
238
239 enumsRoot.sort()
240
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000241
242 # TODO(edisonn): move each .h in it's own file
243 # write imports
244
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000245 # write enums
edisonn@google.com59543d32013-06-18 22:00:40 +0000246 fileEnums = open('SkPdfEnums_autogen.h', 'w')
247 fileEnums.write('#ifndef __DEFINED__SkPdfEnums\n')
248 fileEnums.write('#define __DEFINED__SkPdfEnums\n')
249 fileEnums.write('\n')
250
251 fileEnums.write('enum SkPdfObjectType {\n')
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000252 for enum in enumsRoot:
edisonn@google.com59543d32013-06-18 22:00:40 +0000253 self.writeEnum(fileEnums, enum, enumToCls)
254 fileEnums.write('};\n')
255 fileEnums.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000256
257 # write forward class declaration
258 for name in self.fClassesNamesInOrder:
edisonn@google.com59543d32013-06-18 22:00:40 +0000259 fileEnums.write('class SkPdf' + name + ';\n')
260 fileEnums.write('\n')
261
262 fileEnums.write('#endif // __DEFINED__SkPdfEnums\n')
263 fileEnums.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000264
265 for name in self.fClassesNamesInOrder:
266 cls = self.fClasses[name]
267 enum = cls.fEnum
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000268
edisonn@google.com59543d32013-06-18 22:00:40 +0000269 fileClass = open('SkPdf' + cls.fName + '_autogen.h', 'w')
270 fileClass.write('#ifndef __DEFINED__SkPdf' + cls.fName + '\n')
271 fileClass.write('#define __DEFINED__SkPdf' + cls.fName + '\n')
272 fileClass.write('\n')
273
274 fileClass.write('#include "SkPdfEnums_autogen.h"\n')
275 fileClass.write('#include "SkPdfArray_autogen.h"\n')
276
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000277 if cls.fBase == '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000278 fileClass.write('\nclass SkPdf' + cls.fName + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000279 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000280 fileClass.write('#include "SkPdf' + cls.fBase + '_autogen.h"\n')
281 fileClass.write('\nclass SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000282
edisonn@google.com59543d32013-06-18 22:00:40 +0000283 fileClass.write('public:\n')
284 fileClass.write(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000285 if len(cls.fEnumSubclasses) == 0:
edisonn@google.com59543d32013-06-18 22:00:40 +0000286 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjectType)(' + cls.fEnum + ' + 1);}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000287 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000288 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000289
edisonn@google.com59543d32013-06-18 22:00:40 +0000290 self.writeAsFoo(fileClass, cls, enumToCls)
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000291
edisonn@google.com59543d32013-06-18 22:00:40 +0000292 fileClass.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000293 for cc in cls.fCCPublic:
edisonn@google.com59543d32013-06-18 22:00:40 +0000294 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000295
edisonn@google.com59543d32013-06-18 22:00:40 +0000296 fileClass.write('private:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000297 for cc in cls.fCCPrivate:
edisonn@google.com59543d32013-06-18 22:00:40 +0000298 fileClass.write(' ' + cc + '\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000299
300 if cls.fBase == '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000301 fileClass.write('protected:\n')
302 fileClass.write(' const PdfMemDocument* fPodofoDoc;\n')
303 fileClass.write(' const PdfObject* fPodofoObj;\n')
304 fileClass.write('\n')
305 fileClass.write('public:\n')
306 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj) {}\n')
307 fileClass.write(' const PdfMemDocument* doc() const { return fPodofoDoc;}\n')
308 fileClass.write(' const PdfObject* podofo() const { return fPodofoObj;}\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000309 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000310 fileClass.write('public:\n')
311 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}\n')
312 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000313
314 #check required fieds, also, there should be an internal_valid() manually wrote for complex
315 # situations
316 # right now valid return true
edisonn@google.com59543d32013-06-18 22:00:40 +0000317 fileClass.write(' virtual bool valid() const {return true;}\n')
318 fileClass.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000319
edisonn@google.com59543d32013-06-18 22:00:40 +0000320 fileClass.write(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fName + '& from) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPodofoObj; return *this;}\n')
321 fileClass.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000322
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000323 for field in cls.fFields:
324 prop = field.fProp
325 if prop.fCppName != '':
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000326 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000327 fileClass.write('/*\n') # comment code of the atributes that can have any name
edisonn@google.com1277cf02013-06-17 23:36:45 +0000328
329 # TODO(edisonn): has_foo();
edisonn@google.com59543d32013-06-18 22:00:40 +0000330 fileClass.write(' bool has_' + prop.fCppName + '() const {\n')
331 fileClass.write(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));\n')
332 fileClass.write(' }\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000333
334 if len(prop.fTypes.split()) == 1:
335 t = prop.fTypes.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000336 fileClass.write(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {\n')
337 fileClass.write(' ' + knowTypes[t][0] + ' ret;\n')
338 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 +0000339 if field.fRequired == False and prop.fDefault != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000340 fileClass.write(' return ' + prop.fDefault.toCpp() + ';\n');
edisonn@google.com1277cf02013-06-17 23:36:45 +0000341 else:
edisonn@google.com59543d32013-06-18 22:00:40 +0000342 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
343 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n');
344 fileClass.write(' }\n')
345 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000346 else:
347 for type in prop.fTypes.split():
348 t = type.strip()
edisonn@google.com59543d32013-06-18 22:00:40 +0000349 fileClass.write(' bool is' + prop.fCppName + 'A' + t.title() + '() const {\n')
350 fileClass.write(' SkPdfObject* ret = NULL;\n')
351 fileClass.write(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;\n')
352 fileClass.write(' return ' + knowTypes[t][3] + ';\n')
353 fileClass.write(' }\n')
354 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000355
edisonn@google.com59543d32013-06-18 22:00:40 +0000356 fileClass.write(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {\n')
357 fileClass.write(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';\n')
358 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;\n')
359 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
360 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n')
361 fileClass.write(' }\n')
362 fileClass.write('\n')
edisonn@google.com1277cf02013-06-17 23:36:45 +0000363
edisonn@google.com45327112013-06-13 20:02:29 +0000364
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000365 if prop.fCppName[0] == '[':
edisonn@google.com59543d32013-06-18 22:00:40 +0000366 fileClass.write('*/\n') # comment code of the atributes that can have any name
edisonn@google.com45327112013-06-13 20:02:29 +0000367
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000368
edisonn@google.com59543d32013-06-18 22:00:40 +0000369 fileClass.write('};\n')
370 fileClass.write('\n')
371
372 fileClass.write('#endif // __DEFINED__SkPdf' + cls.fName + '\n')
373 fileClass.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000374
375
376
377 # generate constructor when knowing the type
378 # later, p2, generate constructor when not knowing the type - very similar with parsing?
379
edisonn@google.comf7dd4912013-06-11 23:06:16 +0000380 # generate parser
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000381 # TODO(edisonn): fast recognition based on must attributes.
edisonn@google.com59543d32013-06-18 22:00:40 +0000382 fileMapper = open('SkPdfPodofoMapper_autogen.h', 'w')
383 fileMapper.write('#ifndef __DEFINED__SkPdfPodofoMapper\n')
384 fileMapper.write('#define __DEFINED__SkPdfPodofoMapper\n')
385 fileMapper.write('\n')
386
387 fileMapper.write('#include "SkPdfHeaders_autogen.h"\n')
388 fileMapper.write('class PodofoMapper {\n')
389 fileMapper.write('public:\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000390 for name in self.fClassesNamesInOrder:
391 cls = self.fClasses[name]
392
edisonn@google.com68d15c82013-06-17 20:46:27 +0000393
edisonn@google.com59543d32013-06-18 22:00:40 +0000394 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) {\n')
395 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n')
396 fileMapper.write(' }\n')
397 fileMapper.write('\n')
edisonn@google.com68d15c82013-06-17 20:46:27 +0000398
edisonn@google.com59543d32013-06-18 22:00:40 +0000399 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {\n')
400 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return false;\n')
401 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000402
403 for sub in cls.fEnumSubclasses:
edisonn@google.com59543d32013-06-18 22:00:40 +0000404 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000405
edisonn@google.com59543d32013-06-18 22:00:40 +0000406 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000407
edisonn@google.com59543d32013-06-18 22:00:40 +0000408 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj);\n')
409 fileMapper.write(' return true;\n')
410 fileMapper.write(' }\n')
411 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000412
413 for name in self.fClassesNamesInOrder:
414 cls = self.fClasses[name]
415
edisonn@google.com59543d32013-06-18 22:00:40 +0000416 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000417
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000418 if cls.fCheck != '':
edisonn@google.com59543d32013-06-18 22:00:40 +0000419 fileMapper.write(' return ' + cls.fCheck + ';\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000420 else:
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000421 cntMust = 0
422 for field in cls.fFields:
423 prop = field.fProp
424 if prop.fHasMust:
425 cntMust = cntMust + 1
edisonn@google.com59543d32013-06-18 22:00:40 +0000426 fileMapper.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';\n')
427 fileMapper.write(' if (!' + knowTypes[prop.fTypes.strip()][1] + '(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;\n')
428 fileMapper.write(' if (' + prop.fCppName + ' != ' + prop.fMustBe.toCpp() + ') return false;\n')
429 fileMapper.write('\n')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000430
edisonn@google.com59543d32013-06-18 22:00:40 +0000431 fileMapper.write(' return true;\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000432
edisonn@google.com59543d32013-06-18 22:00:40 +0000433 fileMapper.write(' }\n')
434 fileMapper.write('\n')
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000435
edisonn@google.com59543d32013-06-18 22:00:40 +0000436 fileMapper.write('};\n')
437 fileMapper.write('\n')
438
439 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n')
440 fileMapper.close()
edisonn@google.comaf3daa02013-06-12 19:07:45 +0000441
edisonn@google.com1a191c62013-06-11 21:44:08 +0000442 return
443
444def generateCode():
edisonn@google.com59543d32013-06-18 22:00:40 +0000445 global fileHeaders
446
447 fileHeaders = open('SkPdfHeaders_autogen.h', 'w')
448 fileHeaders.write('#ifndef __DEFINED__SkPdfHeaders\n')
449 fileHeaders.write('#define __DEFINED__SkPdfHeaders\n')
450 fileHeaders.write('\n')
451
452 fileHeaders.write('#include "SkPdfEnums_autogen.h"\n')
453
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000454 manager = PdfClassManager()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000455
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000456 manager.addClass('Object')
edisonn@google.com1a191c62013-06-11 21:44:08 +0000457
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000458 manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000459 manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Bool')\
460 .carbonCopyPublic('bool value() const {return fPodofoObj->GetBool();}')
461
edisonn@google.com59543d32013-06-18 22:00:40 +0000462 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000463 .carbonCopyPublic('long value() const {return fPodofoObj->GetNumber();}')
464
edisonn@google.com59543d32013-06-18 22:00:40 +0000465 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDataType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000466 .carbonCopyPublic('double value() const {return fPodofoObj->GetReal();}')
467
468 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name')\
469 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}')
470
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000471 #manager.addClass('Stream') - attached to a dictionary
472 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_Reference')
edisonn@google.com60533dc2013-06-18 14:51:21 +0000473
474 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array')\
475 .carbonCopyPublic('const int size() const {return fPodofoObj->GetArray().GetSize();}')\
476 .carbonCopyPublic('const SkPdfObject operator[](int i) const {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\
477 .carbonCopyPublic('SkPdfObject operator[](int i) {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')
478
edisonn@google.com59543d32013-06-18 22:00:40 +0000479 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_String || podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000480 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
481
edisonn@google.com59543d32013-06-18 22:00:40 +0000482 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdfDataType_HexString')\
edisonn@google.com60533dc2013-06-18 14:51:21 +0000483 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000484
edisonn@google.com59543d32013-06-18 22:00:40 +0000485 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_Dictionary')\
486 .carbonCopyPublic('const SkPdfObject get(const char* dictionaryKeyName) const {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
487 .carbonCopyPublic('SkPdfObject get(const char* dictionaryKeyName) {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
488
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000489
490 # these classes are not explicitely backed by a table in the pdf spec
491 manager.addClass('XObjectDictionary', 'Dictionary')
492
493 manager.addClass('FontDictionary', 'Dictionary')
494
495 manager.addClass('TrueTypeFontDictionary', 'FontDictionary')
496
497 pdfspec_autogen.buildPdfSpec(manager)
edisonn@google.com1a191c62013-06-11 21:44:08 +0000498
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000499 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\
500 .required('NULL')\
501 .field('Subtype')\
502 .name('Subtype')\
edisonn@google.com45327112013-06-13 20:02:29 +0000503 .type('name')\
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000504 .comment('')\
505 .must(datatypes.PdfName('MMType1'))\
edisonn@google.com45327112013-06-13 20:02:29 +0000506 .done().done()\
edisonn@google.com45327112013-06-13 20:02:29 +0000507
508
edisonn@google.coma2fab9d2013-06-14 19:22:19 +0000509 manager.write()
edisonn@google.com1a191c62013-06-11 21:44:08 +0000510
edisonn@google.com59543d32013-06-18 22:00:40 +0000511 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n')
512 fileHeaders.close()
513
edisonn@google.com1a191c62013-06-11 21:44:08 +0000514 return 1
515
516if '__main__' == __name__:
517 sys.exit(generateCode())
edisonn@google.com45327112013-06-13 20:02:29 +0000518