Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(823)

Side by Side Diff: experimental/PdfViewer/generate_code.py

Issue 17395005: put each generated pdf api class in a file (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 2
3 import sys 3 import sys
4 4
5 import datatypes 5 import datatypes
6 import pdfspec_autogen 6 import pdfspec_autogen
7 7
8 knowTypes = { 8 knowTypes = {
9 '(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'], 9 '(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
10 '(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'tr ue'], 10 '(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'tr ue'],
11 '(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true '], 11 '(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true '],
12 'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->p odofo()->GetDataType() == ePdfDataType_Array'], 12 'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->p odofo()->GetDataType() == ePdfDataType_Array'],
13 'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->p odofo()->GetDataType() == ePdfDataType_Bool'], 13 'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->p odofo()->GetDataType() == ePdfDataType_Bool'],
14 'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podo fo()->GetDataType() == ePdfDataType_Array'], 14 'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podo fo()->GetDataType() == ePdfDataType_Array'],
15 'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull (), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'], 15 'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull (), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'],
16 'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNon e(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], 16 'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNon e(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
17 'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo( )->GetDataType() == ePdfDataType_Number'], 17 'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo( )->GetDataType() == ePdfDataType_Number'],
18 'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.File SpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'], 18 'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.File SpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
19 'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->p odofo()->GetDataType() == ePdfDataType_Name'], 19 'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->p odofo()->GetDataType() == ePdfDataType_Name'],
20 'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo() ->GetDataType() == ePdfDataType_Reference'], 20 'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo() ->GetDataType() == ePdfDataType_Reference'],
21 'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podof o()->GetDataType() == ePdfDataType_Real'], 21 'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podof o()->GetDataType() == ePdfDataType_Real || ret->podofo()->GetDataType() == ePdfD ataType_Number'],
22 'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret-> podofo()->GetDataType() == ePdfDataType_Array'], 22 'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret-> podofo()->GetDataType() == ePdfDataType_Array'],
23 'stream': ['SkPdfStream', 'StreamFromDictionary', datatypes.PdfEmptyStream(), ' ret->podofo()->HasStream()'], 23 'stream': ['SkPdfStream', 'StreamFromDictionary', datatypes.PdfEmptyStream(), ' ret->podofo()->HasStream()'],
24 'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 're t->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType( ) == ePdfDataType_HexString'], 24 'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 're t->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'], 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()->GetData Type() == ePdfDataType_HexString'], 26 'text string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""') , 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetData Type() == ePdfDataType_HexString'],
27 } 27 }
28 28
29 29
30 class PdfField: 30 class PdfField:
31 def __init__(self, parent, name, abr): 31 def __init__(self, parent, name, abr):
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 153
154 def addClass(self, name, base='Object', comment=''): 154 def addClass(self, name, base='Object', comment=''):
155 if name == 'Object': 155 if name == 'Object':
156 cls = PdfClass(name, '', comment) 156 cls = PdfClass(name, '', comment)
157 else: 157 else:
158 cls = PdfClass(name, base, comment) 158 cls = PdfClass(name, base, comment)
159 self.fClasses[name] = cls 159 self.fClasses[name] = cls
160 self.fClassesNamesInOrder.append(name) 160 self.fClassesNamesInOrder.append(name)
161 return cls 161 return cls
162 162
163 def longName(self, name): 163 def writeEnum(self, fileEnums, enum, enumToCls):
164 #return name 164 fileEnums.write(' ' + enum + ',\n')
165 # TODO(edisonn): we need the long name to nenerate and sort enums, but we ca n generate them recursively
166 ret = ''
167 while name != '':
168 cls = self.fClasses[name]
169 ret = name + ret
170 name = cls.fBase
171
172 return ret
173
174
175 def writeEnum(self, enum, enumToCls):
176 print(' ' + enum + ',')
177 cls = enumToCls[enum] 165 cls = enumToCls[enum]
178 cls.fEnumSubclasses.sort() 166 cls.fEnumSubclasses.sort()
179 167
180 cnt = 0 168 cnt = 0
181 for sub in cls.fEnumSubclasses: 169 for sub in cls.fEnumSubclasses:
182 self.writeEnum(cls.fEnumSubclasses[cnt], enumToCls) 170 self.writeEnum(fileEnums, cls.fEnumSubclasses[cnt], enumToCls)
183 cnt = cnt + 1 171 cnt = cnt + 1
184 172
185 if cnt != 0: 173 if cnt != 0:
186 print(' ' + cls.fEnumEnd + ',') 174 fileEnums.write(' ' + cls.fEnumEnd + ',\n')
187 175
188 176
189 def writeAsNull(self, cls, enumToCls): 177 def writeAsNull(self, fileClass, cls, enumToCls):
190 print(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return NULL;} ') 178 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {ret urn NULL;}\n')
191 print(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {r eturn NULL;}') 179 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '( ) const {return NULL;}\n')
192 print 180 fileClass.write('\n')
193 181
194 cnt = 0 182 cnt = 0
195 for sub in cls.fEnumSubclasses: 183 for sub in cls.fEnumSubclasses:
196 self.writeAsNull(enumToCls[cls.fEnumSubclasses[cnt]], enumToCls) 184 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToCls )
197 cnt = cnt + 1 185 cnt = cnt + 1
198 186
199 187
200 def writeAsFoo(self, cls, enumToCls): 188 def writeAsFoo(self, fileClass, cls, enumToCls):
201 # TODO(edisonn): add a container, with sections, public, private, default, . .. 189 # TODO(edisonn): add a container, with sections, public, private, default, . ..
202 # the end code will be grouped 190 # the end code will be grouped
203 191
204 # me 192 # me
205 print('public:') 193 fileClass.write('public:\n')
206 print(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {return this;} ') 194 fileClass.write(' virtual SkPdf' + cls.fName +'* as' + cls.fName + '() {ret urn this;}\n')
207 print(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '() const {r eturn this;}') 195 fileClass.write(' virtual const SkPdf' + cls.fName +'* as' + cls.fName + '( ) const {return this;}\n')
208 print 196 fileClass.write('\n')
209 197
210 if cls.fName == 'Object': 198 if cls.fName == 'Object':
211 cnt = 0 199 cnt = 0
212 for sub in cls.fEnumSubclasses: 200 for sub in cls.fEnumSubclasses:
213 self.writeAsNull(enumToCls[cls.fEnumSubclasses[cnt]], enumToCls) 201 self.writeAsNull(fileClass, enumToCls[cls.fEnumSubclasses[cnt]], enumToC ls)
214 cnt = cnt + 1 202 cnt = cnt + 1
215 203
216 if cls.fName != 'Object': 204 if cls.fName != 'Object':
217 print('private:') 205 fileClass.write('private:\n')
218 base = self.fClasses[cls.fBase] 206 base = self.fClasses[cls.fBase]
219 cnt = 0 207 cnt = 0
220 for sub in base.fEnumSubclasses: 208 for sub in base.fEnumSubclasses:
221 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName: 209 if enumToCls[base.fEnumSubclasses[cnt]].fName != cls.fName:
222 self.writeAsNull(enumToCls[base.fEnumSubclasses[cnt]], enumToCls) 210 self.writeAsNull(fileClass, enumToCls[base.fEnumSubclasses[cnt]], enum ToCls)
223 cnt = cnt + 1 211 cnt = cnt + 1
224 212
225 213
226 214
227 def write(self): 215 def write(self):
228 216 global fileHeaders
229 global knowTypes 217 global knowTypes
230 218
231 # generate enum 219 # generate enum
232 enumsRoot = [] 220 enumsRoot = []
233 221
234 enumToCls = {} 222 enumToCls = {}
235 223
236 for name in self.fClasses: 224 for name in self.fClasses:
237 cls = self.fClasses[name] 225 cls = self.fClasses[name]
238 enum = self.longName(name) 226 cls.fEnum = 'k' + name + '_SkPdfObjectType'
239 cls.fEnum = 'k' + enum + '_SkPdfObjectType' 227 cls.fEnumEnd = 'k' + name + '__End_SkPdfObjectType'
240 cls.fEnumEnd = 'k' + enum + '__End_SkPdfObjectType' 228
229 fileHeaders.write('#include "SkPdf' + cls.fName + '_autogen.h"\n')
241 230
242 if cls.fBase != '': 231 if cls.fBase != '':
243 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum) 232 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
244 233
245 if cls.fBase == '': 234 if cls.fBase == '':
246 enumsRoot.append(cls.fEnum) 235 enumsRoot.append(cls.fEnum)
247 236
248 enumToCls[cls.fEnum] = cls 237 enumToCls[cls.fEnum] = cls
249 238
250 enumsRoot.sort() 239 enumsRoot.sort()
251 240
252 241
253 # TODO(edisonn): move each .h in it's own file 242 # TODO(edisonn): move each .h in it's own file
254 # write imports 243 # write imports
255 244
256 # write enums 245 # write enums
257 print('enum SkPdfObjectType {') 246 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')
258 for enum in enumsRoot: 252 for enum in enumsRoot:
259 self.writeEnum(enum, enumToCls) 253 self.writeEnum(fileEnums, enum, enumToCls)
260 print('};') 254 fileEnums.write('};\n')
261 print 255 fileEnums.write('\n')
262 256
263 # write forward class declaration 257 # write forward class declaration
264 for name in self.fClassesNamesInOrder: 258 for name in self.fClassesNamesInOrder:
265 print('class SkPdf' + name + ';') 259 fileEnums.write('class SkPdf' + name + ';\n')
266 print 260 fileEnums.write('\n')
261
262 fileEnums.write('#endif // __DEFINED__SkPdfEnums\n')
263 fileEnums.close()
267 264
268 for name in self.fClassesNamesInOrder: 265 for name in self.fClassesNamesInOrder:
269 cls = self.fClasses[name] 266 cls = self.fClasses[name]
270 enum = cls.fEnum 267 enum = cls.fEnum
271 268
269 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
272 if cls.fBase == '': 277 if cls.fBase == '':
273 print('class SkPdf' + cls.fName + ' {') 278 fileClass.write('\nclass SkPdf' + cls.fName + ' {\n')
274 else: 279 else:
275 print('class SkPdf' + cls.fName + ' : public SkPdf' + cls.fBase + ' {') 280 fileClass.write('#include "SkPdf' + cls.fBase + '_autogen.h"\n')
281 fileClass.write('\nclass SkPdf' + cls.fName + ' : public SkPdf' + cls.fB ase + ' {\n')
276 282
277 print('public:') 283 fileClass.write('public:\n')
278 print(' virtual SkPdfObjectType getType() const { return ' + cls.fEnum + ';}') 284 fileClass.write(' virtual SkPdfObjectType getType() const { return ' + cl s.fEnum + ';}\n')
279 if len(cls.fEnumSubclasses) == 0: 285 if len(cls.fEnumSubclasses) == 0:
280 print(' virtual SkPdfObjectType getTypeEnd() const { return (SkPdfObjec tType)(' + cls.fEnum + ' + 1);}') 286 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ( SkPdfObjectType)(' + cls.fEnum + ' + 1);}\n')
281 else: 287 else:
282 print(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEn umEnd + ';}') 288 fileClass.write(' virtual SkPdfObjectType getTypeEnd() const { return ' + cls.fEnumEnd + ';}\n')
283 289
290 self.writeAsFoo(fileClass, cls, enumToCls)
284 291
285 self.writeAsFoo(cls, enumToCls) 292 fileClass.write('public:\n')
286
287 print('public:')
288 for cc in cls.fCCPublic: 293 for cc in cls.fCCPublic:
289 print(' ' + cc) 294 fileClass.write(' ' + cc + '\n')
290 295
291 print('private:') 296 fileClass.write('private:\n')
292 for cc in cls.fCCPrivate: 297 for cc in cls.fCCPrivate:
293 print(' ' + cc) 298 fileClass.write(' ' + cc + '\n')
294 299
295 if cls.fBase == '': 300 if cls.fBase == '':
296 print('protected:') 301 fileClass.write('protected:\n')
297 print(' const PdfMemDocument* fPodofoDoc;') 302 fileClass.write(' const PdfMemDocument* fPodofoDoc;\n')
298 print(' const PdfObject* fPodofoObj;') 303 fileClass.write(' const PdfObject* fPodofoObj;\n')
299 print 304 fileClass.write('\n')
300 print('public:') 305 fileClass.write('public:\n')
301 print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj(podofoObj ) {}') 306 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDo c = NULL, const PdfObject* podofoObj = NULL) : fPodofoDoc(podofoDoc), fPodofoObj (podofoObj) {}\n')
302 print(' const PdfMemDocument* doc() const { return fPodofoDoc;}') 307 fileClass.write(' const PdfMemDocument* doc() const { return fPodofoDoc ;}\n')
303 print(' const PdfObject* podofo() const { return fPodofoObj;}') 308 fileClass.write(' const PdfObject* podofo() const { return fPodofoObj;} \n')
304 else: 309 else:
305 print('public:') 310 fileClass.write('public:\n')
306 print(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDoc = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj ) {}') 311 fileClass.write(' SkPdf' + cls.fName + '(const PdfMemDocument* podofoDo c = NULL, const PdfObject* podofoObj = NULL) : SkPdf' + cls.fBase + '(podofoDoc, podofoObj) {}\n')
307 print 312 fileClass.write('\n')
308 313
309 #check required fieds, also, there should be an internal_valid() manually wrote for complex 314 #check required fieds, also, there should be an internal_valid() manually wrote for complex
310 # situations 315 # situations
311 # right now valid return true 316 # right now valid return true
312 print(' virtual bool valid() const {return true;}') 317 fileClass.write(' virtual bool valid() const {return true;}\n')
313 print 318 fileClass.write('\n')
314 319
315 print(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fName + '& f rom) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPodofoObj; re turn *this;}') 320 fileClass.write(' SkPdf' + cls.fName + '& operator=(const SkPdf' + cls.fN ame + '& from) {this->fPodofoDoc = from.fPodofoDoc; this->fPodofoObj = from.fPod ofoObj; return *this;}\n')
316 print 321 fileClass.write('\n')
317 322
318 for field in cls.fFields: 323 for field in cls.fFields:
319 prop = field.fProp 324 prop = field.fProp
320 if prop.fCppName != '': 325 if prop.fCppName != '':
321 if prop.fCppName[0] == '[': 326 if prop.fCppName[0] == '[':
322 print('/*') # comment code of the atributes that can have any name 327 fileClass.write('/*\n') # comment code of the atributes that can ha ve any name
323 328
324 # TODO(edisonn): has_foo(); 329 # TODO(edisonn): has_foo();
325 print(' bool has_' + prop.fCppName + '() const {') 330 fileClass.write(' bool has_' + prop.fCppName + '() const {\n')
326 print(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetD ictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));') 331 fileClass.write(' return (ObjectFromDictionary(fPodofoDoc, fPodof oObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));\n' )
327 print(' }') 332 fileClass.write(' }\n')
328 333
329 if len(prop.fTypes.split()) == 1: 334 if len(prop.fTypes.split()) == 1:
330 t = prop.fTypes.strip() 335 t = prop.fTypes.strip()
331 print(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {') 336 fileClass.write(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() c onst {\n')
332 print(' ' + knowTypes[t][0] + ' ret;') 337 fileClass.write(' ' + knowTypes[t][0] + ' ret;\n')
333 print(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDi ctionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;') 338 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofo Obj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) retu rn ret;\n')
334 if field.fRequired == False and prop.fDefault != '': 339 if field.fRequired == False and prop.fDefault != '':
335 print(' return ' + prop.fDefault.toCpp() + ';'); 340 fileClass.write(' return ' + prop.fDefault.toCpp() + ';\n');
336 else: 341 else:
337 print(' // TODO(edisonn): warn about missing required field, as sert for known good pdfs') 342 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
338 print(' return ' + knowTypes[t][2].toCpp() + ';'); 343 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n');
339 print(' }') 344 fileClass.write(' }\n')
340 print 345 fileClass.write('\n')
341 else: 346 else:
342 for type in prop.fTypes.split(): 347 for type in prop.fTypes.split():
343 t = type.strip() 348 t = type.strip()
344 print(' bool is' + prop.fCppName + 'A' + t.title() + '() const {' ) 349 fileClass.write(' bool is' + prop.fCppName + 'A' + t.title() + '( ) const {\n')
345 print(' SkPdfObject* ret = NULL;') 350 fileClass.write(' SkPdfObject* ret = NULL;\n')
346 print(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDi ctionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;' ) 351 fileClass.write(' if (!ObjectFromDictionary(fPodofoDoc, fPodofo Obj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) retu rn false;\n')
347 print(' return ' + knowTypes[t][3] + ';') 352 fileClass.write(' return ' + knowTypes[t][3] + ';\n')
348 print(' }') 353 fileClass.write(' }\n')
349 print 354 fileClass.write('\n')
350 355
351 print(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.t itle() + '() const {') 356 fileClass.write(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {\n')
352 print(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp () + ';') 357 fileClass.write(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t ][2].toCpp() + ';\n')
353 print(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->Get Dictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;' ) 358 fileClass.write(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodo foObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) re turn ret;\n')
354 print(' // TODO(edisonn): warn about missing required field, as sert for known good pdfs') 359 fileClass.write(' // TODO(edisonn): warn about missing required field, assert for known good pdfs\n')
355 print(' return ' + knowTypes[t][2].toCpp() + ';') 360 fileClass.write(' return ' + knowTypes[t][2].toCpp() + ';\n')
356 print(' }') 361 fileClass.write(' }\n')
357 print 362 fileClass.write('\n')
358 363
359 364
360 if prop.fCppName[0] == '[': 365 if prop.fCppName[0] == '[':
361 print('*/') # comment code of the atributes that can have any name 366 fileClass.write('*/\n') # comment code of the atributes that can ha ve any name
362 367
363 368
364 print('};') 369 fileClass.write('};\n')
365 print 370 fileClass.write('\n')
366 print 371
372 fileClass.write('#endif // __DEFINED__SkPdf' + cls.fName + '\n')
373 fileClass.close()
367 374
368 375
369 376
370 # generate constructor when knowing the type 377 # generate constructor when knowing the type
371 # later, p2, generate constructor when not knowing the type - very similar with parsing? 378 # later, p2, generate constructor when not knowing the type - very similar with parsing?
372 379
373 # generate parser 380 # generate parser
374
375 # TODO(edisonn): fast recognition based on must attributes. 381 # TODO(edisonn): fast recognition based on must attributes.
376 print('class PodofoMapper {') 382 fileMapper = open('SkPdfPodofoMapper_autogen.h', 'w')
377 print('public:') 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')
378 for name in self.fClassesNamesInOrder: 390 for name in self.fClassesNamesInOrder:
379 cls = self.fClasses[name] 391 cls = self.fClasses[name]
380 392
381 393
382 print(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) { ') 394 fileMapper.write(' static bool map(const SkPdfObject& in, SkPdf' + name + '** out) {\n')
383 print(' return map(*in.doc(), *in.podofo(), out);') 395 fileMapper.write(' return map(*in.doc(), *in.podofo(), out);\n')
384 print(' }') 396 fileMapper.write(' }\n')
385 print 397 fileMapper.write('\n')
386 398
387 print(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {') 399 fileMapper.write(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {\n')
388 print(' if (!is' + name + '(podofoDoc, podofoObj)) return false;') 400 fileMapper.write(' if (!is' + name + '(podofoDoc, podofoObj)) return fa lse;\n')
389 print 401 fileMapper.write('\n')
390 402
391 for sub in cls.fEnumSubclasses: 403 for sub in cls.fEnumSubclasses:
392 print(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[sub].fName + '**)out)) return true;') 404 fileMapper.write(' if (map(podofoDoc, podofoObj, (SkPdf' + enumToCls[ sub].fName + '**)out)) return true;\n')
393 405
394 print 406 fileMapper.write('\n')
395 407
396 print(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj);') 408 fileMapper.write(' *out = new SkPdf' + name + '(&podofoDoc, &podofoObj) ;\n')
397 print(' return true;') 409 fileMapper.write(' return true;\n')
398 print(' }') 410 fileMapper.write(' }\n')
399 print 411 fileMapper.write('\n')
400 412
401 for name in self.fClassesNamesInOrder: 413 for name in self.fClassesNamesInOrder:
402 cls = self.fClasses[name] 414 cls = self.fClasses[name]
403 415
404 print(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {') 416 fileMapper.write(' static bool is' + name + '(const PdfMemDocument& podof oDoc, const PdfObject& podofoObj) {\n')
405 417
406 if cls.fCheck != '': 418 if cls.fCheck != '':
407 print(' return ' + cls.fCheck + ';') 419 fileMapper.write(' return ' + cls.fCheck + ';\n')
408 else: 420 else:
409 cntMust = 0 421 cntMust = 0
410 for field in cls.fFields: 422 for field in cls.fFields:
411 prop = field.fProp 423 prop = field.fProp
412 if prop.fHasMust: 424 if prop.fHasMust:
413 cntMust = cntMust + 1 425 cntMust = cntMust + 1
414 print(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppNa me + ';') 426 fileMapper.write(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';\n')
415 print(' if (!' + knowTypes[prop.fTypes.strip()][1] + '(&podofoDoc , podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;') 427 fileMapper.write(' if (!' + knowTypes[prop.fTypes.strip()][1] + ' (&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;\n')
416 print(' if (' + prop.fCppName + ' != ' + prop.fMustBe.toCpp() + ' ) return false;') 428 fileMapper.write(' if (' + prop.fCppName + ' != ' + prop.fMustBe. toCpp() + ') return false;\n')
417 print 429 fileMapper.write('\n')
418 430
419 print(' return true;') 431 fileMapper.write(' return true;\n')
420 432
421 print(' }') 433 fileMapper.write(' }\n')
422 print 434 fileMapper.write('\n')
423 435
424 print('};') 436 fileMapper.write('};\n')
425 print 437 fileMapper.write('\n')
438
439 fileMapper.write('#endif // __DEFINED__SkPdfPodofoMapper\n')
440 fileMapper.close()
426 441
427 return 442 return
428 443
429 def generateCode(): 444 def generateCode():
445 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
430 manager = PdfClassManager() 454 manager = PdfClassManager()
431 455
432 manager.addClass('Object') 456 manager.addClass('Object')
433 457
434 manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null') 458 manager.addClass('Null').check('podofoObj.GetDataType() == ePdfDataType_Null')
435 manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Boo l')\ 459 manager.addClass('Boolean').check('podofoObj.GetDataType() == ePdfDataType_Boo l')\
436 .carbonCopyPublic('bool value() const {return fPodo foObj->GetBool();}') 460 .carbonCopyPublic('bool value() const {return fPodo foObj->GetBool();}')
437 461
438 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Num ber')\ 462 manager.addClass('Integer').check('podofoObj.GetDataType() == ePdfDataType_Num ber || podofoObj.GetDataType() == ePdfDataType_Real')\
439 .carbonCopyPublic('long value() const {return fPodo foObj->GetNumber();}') 463 .carbonCopyPublic('long value() const {return fPodo foObj->GetNumber();}')
440 464
441 manager.addClass('Number').check('podofoObj.GetDataType() == ePdfDataType_Real ')\ 465 manager.addClass('Number', 'Integer').check('podofoObj.GetDataType() == ePdfDa taType_Number || podofoObj.GetDataType() == ePdfDataType_Real')\
442 .carbonCopyPublic('double value() const {return fPo dofoObj->GetReal();}') 466 .carbonCopyPublic('double value() const {return fPo dofoObj->GetReal();}')
443 467
444 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name') \ 468 manager.addClass('Name').check('podofoObj.GetDataType() == ePdfDataType_Name') \
445 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}') 469 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetName().GetName();}')
446 470
447 #manager.addClass('Stream') - attached to a dictionary 471 #manager.addClass('Stream') - attached to a dictionary
448 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_R eference') 472 manager.addClass('Reference').check('podofoObj.GetDataType() == ePdfDataType_R eference')
449 473
450 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array ')\ 474 manager.addClass('Array').check('podofoObj.GetDataType() == ePdfDataType_Array ')\
451 .carbonCopyPublic('const int size() const {return f PodofoObj->GetArray().GetSize();}')\ 475 .carbonCopyPublic('const int size() const {return f PodofoObj->GetArray().GetSize();}')\
452 .carbonCopyPublic('const SkPdfObject operator[](int i) const {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\ 476 .carbonCopyPublic('const SkPdfObject operator[](int i) const {return SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')\
453 .carbonCopyPublic('SkPdfObject operator[](int i) {r eturn SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}') 477 .carbonCopyPublic('SkPdfObject operator[](int i) {r eturn SkPdfObject(fPodofoDoc, &fPodofoObj->GetArray()[i]);}')
454 478
455 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_Stri ng')\ 479 manager.addClass('String').check('podofoObj.GetDataType() == ePdfDataType_Stri ng || podofoObj.GetDataType() == ePdfDataType_HexString')\
456 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}') 480 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
457 481
458 manager.addClass('HexString').check('podofoObj.GetDataType() == ePdfDataType_H exString')\ 482 manager.addClass('HexString', 'String').check('podofoObj.GetDataType() == ePdf DataType_HexString')\
459 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}') 483 .carbonCopyPublic('const std::string& value() const {return fPodofoObj->GetString().GetStringUtf8();}')
460 484
461 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_ Dictionary') 485 manager.addClass('Dictionary').check('podofoObj.GetDataType() == ePdfDataType_ Dictionary')\
486 .carbonCopyPublic('const SkPdfObject get(const c har* dictionaryKeyName) const {return SkPdfObject(fPodofoDoc, resolveReferenceOb ject(fPodofoDoc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))) );}')\
487 .carbonCopyPublic('SkPdfObject get(const char* d ictionaryKeyName) {return SkPdfObject(fPodofoDoc, resolveReferenceObject(fPodofo Doc, fPodofoObj->GetDictionary().GetKey(PdfName(dictionaryKeyName))));}')\
488
462 489
463 # these classes are not explicitely backed by a table in the pdf spec 490 # these classes are not explicitely backed by a table in the pdf spec
464 manager.addClass('XObjectDictionary', 'Dictionary') 491 manager.addClass('XObjectDictionary', 'Dictionary')
465 492
466 manager.addClass('FontDictionary', 'Dictionary') 493 manager.addClass('FontDictionary', 'Dictionary')
467 494
468 manager.addClass('TrueTypeFontDictionary', 'FontDictionary') 495 manager.addClass('TrueTypeFontDictionary', 'FontDictionary')
469 496
470 pdfspec_autogen.buildPdfSpec(manager) 497 pdfspec_autogen.buildPdfSpec(manager)
471 498
472 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\ 499 manager.addClass('MultiMasterFontDictionary', 'Type1FontDictionary')\
473 .required('NULL')\ 500 .required('NULL')\
474 .field('Subtype')\ 501 .field('Subtype')\
475 .name('Subtype')\ 502 .name('Subtype')\
476 .type('name')\ 503 .type('name')\
477 .comment('')\ 504 .comment('')\
478 .must(datatypes.PdfName('MMType1'))\ 505 .must(datatypes.PdfName('MMType1'))\
479 .done().done()\ 506 .done().done()\
480 507
481 508
482 manager.write() 509 manager.write()
483 510
511 fileHeaders.write('#endif // __DEFINED__SkPdfHeaders\n')
512 fileHeaders.close()
513
484 return 1 514 return 1
485 515
486 if '__main__' == __name__: 516 if '__main__' == __name__:
487 sys.exit(generateCode()) 517 sys.exit(generateCode())
488 518
489
OLDNEW
« no previous file with comments | « experimental/PdfViewer/SkPdfXObjectDictionary_autogen.h ('k') | experimental/PdfViewer/pdf_viewer_main.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698