Pdfviewer: generate isAFoo() and getAsFoo() api for fields that can have multiple types.
Review URL: https://codereview.chromium.org/16968007 git-svn-id: http://skia.googlecode.com/svn/trunk@9646 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
bb2c7534de
commit
1277cf05cc
@ -45,4 +45,30 @@ class PdfBoolean:
|
|||||||
class CppNull:
|
class CppNull:
|
||||||
def toCpp(self):
|
def toCpp(self):
|
||||||
return 'NULL'
|
return 'NULL'
|
||||||
|
|
||||||
|
class PdfDateNever:
|
||||||
|
def toCpp(self):
|
||||||
|
return 'SkPdfDate()'
|
||||||
|
|
||||||
|
class FileSpecNone:
|
||||||
|
def toCpp(self):
|
||||||
|
return 'SkPdfFileSpec()'
|
||||||
|
|
||||||
|
class PdfEmptyRect:
|
||||||
|
def toCpp(self):
|
||||||
|
return 'SkRect()'
|
||||||
|
|
||||||
|
class PdfEmptyStream:
|
||||||
|
def toCpp(self):
|
||||||
|
return 'SkPdfStream()'
|
||||||
|
|
||||||
|
class PdfArrayNone:
|
||||||
|
def toCpp(self):
|
||||||
|
return 'SkPdfArray()'
|
||||||
|
|
||||||
|
class PdfFunctionNone:
|
||||||
|
def toCpp(self):
|
||||||
|
return 'SkPdfFunction()'
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5,7 +5,26 @@ import sys
|
|||||||
import datatypes
|
import datatypes
|
||||||
import pdfspec_autogen
|
import pdfspec_autogen
|
||||||
|
|
||||||
|
knowTypes = {
|
||||||
|
'(any)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
|
||||||
|
'(undefined)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
|
||||||
|
'(various)': ['SkPdfObject*', 'ObjectFromDictionary', datatypes.CppNull(), 'true'],
|
||||||
|
'array': ['SkPdfArray', 'ArrayFromDictionary', datatypes.PdfArrayNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
|
||||||
|
'boolean': ['bool', 'BoolFromDictionary', datatypes.PdfBoolean('false'), 'ret->podofo()->GetDataType() == ePdfDataType_Bool'],
|
||||||
|
'date': ['SkPdfDate', 'DateFromDictionary', datatypes.PdfDateNever(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
|
||||||
|
'dictionary': ['SkPdfDictionary*', 'DictionaryFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Dictionary'],
|
||||||
|
'function': ['SkPdfFunction', 'FunctionFromDictionary', datatypes.PdfFunctionNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
|
||||||
|
'integer': ['long', 'LongFromDictionary', datatypes.PdfInteger(0), 'ret->podofo()->GetDataType() == ePdfDataType_Number'],
|
||||||
|
'file_specification': ['SkPdfFileSpec', 'FileSpecFromDictionary', datatypes.FileSpecNone(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
|
||||||
|
'name': ['std::string', 'NameFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_Name'],
|
||||||
|
'tree': ['SkPdfTree*', 'TreeFromDictionary', datatypes.CppNull(), 'ret->podofo()->GetDataType() == ePdfDataType_Reference'],
|
||||||
|
'number': ['double', 'DoubleFromDictionary', datatypes.PdfNumber(0), 'ret->podofo()->GetDataType() == ePdfDataType_Real'],
|
||||||
|
'rectangle': ['SkRect', 'SkRectFromDictionary', datatypes.PdfEmptyRect(), 'ret->podofo()->GetDataType() == ePdfDataType_Array'],
|
||||||
|
'stream': ['SkPdfStream', 'StreamFromDictionary', datatypes.PdfEmptyStream(), 'ret->podofo()->HasStream()'],
|
||||||
|
'string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType() == ePdfDataType_HexString'],
|
||||||
|
'text': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType() == ePdfDataType_HexString'],
|
||||||
|
'text string': ['std::string', 'StringFromDictionary', datatypes.PdfString('""'), 'ret->podofo()->GetDataType() == ePdfDataType_String || ret->podofo()->GetDataType() == ePdfDataType_HexString'],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class PdfField:
|
class PdfField:
|
||||||
@ -15,12 +34,10 @@ class PdfField:
|
|||||||
self.fAbr = abr
|
self.fAbr = abr
|
||||||
|
|
||||||
self.fDefault = ''
|
self.fDefault = ''
|
||||||
self.fType = ''
|
self.fTypes = ''
|
||||||
self.fCppName = ''
|
self.fCppName = ''
|
||||||
self.fCppType = ''
|
self.fEnumValues = []
|
||||||
self.fCppReader = ''
|
self.fHasMust = False
|
||||||
self.fValidOptions = []
|
|
||||||
self.fHasMust = False
|
|
||||||
self.fMustBe = ''
|
self.fMustBe = ''
|
||||||
|
|
||||||
def must(self, value):
|
def must(self, value):
|
||||||
@ -32,87 +49,24 @@ class PdfField:
|
|||||||
self.fDefault = value
|
self.fDefault = value
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def number(self, name):
|
def multiple(self, enumValues):
|
||||||
self.fType = 'number'
|
self.fEnumValues = enumValues
|
||||||
self.fCppName = name
|
|
||||||
self.fCppType = 'double'
|
|
||||||
self.fCppReader = 'DoubleFromDictionary'
|
|
||||||
return self
|
|
||||||
|
|
||||||
def integer(self, name):
|
|
||||||
self.fType = 'integer'
|
|
||||||
self.fCppName = name
|
|
||||||
self.fCppType = 'long'
|
|
||||||
self.fCppReader = 'LongFromDictionary'
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def name(self, name):
|
def name(self, name):
|
||||||
self.fType = 'name'
|
|
||||||
self.fCppName = name
|
self.fCppName = name
|
||||||
self.fCppType = 'std::string'
|
|
||||||
self.fCppReader = 'NameFromDictionary'
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def string(self, name):
|
def type(self, types):
|
||||||
self.fType = 'string'
|
|
||||||
self.fCppName = name
|
|
||||||
self.fCppType = 'std::string'
|
|
||||||
self.fCppReader = 'StringFromDictionary'
|
|
||||||
return self
|
|
||||||
|
|
||||||
def multiple(self, validOptions):
|
|
||||||
self.fValidOptions = validOptions
|
|
||||||
return self
|
|
||||||
|
|
||||||
def dictionary(self, name):
|
|
||||||
self.fType = 'dictionary'
|
|
||||||
self.fCppName = name
|
|
||||||
self.fDictionaryType = 'Dictionary'
|
|
||||||
self.fCppType = 'SkPdfDictionary*'
|
|
||||||
self.fCppReader = 'DictionaryFromDictionary'
|
|
||||||
self.fDefault = datatypes.CppNull()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def type(self, type):
|
|
||||||
# TODO (edisonn): if simple type, use it, otherwise set it to Dictionary, and set a mask for valid types, like array or name
|
# TODO (edisonn): if simple type, use it, otherwise set it to Dictionary, and set a mask for valid types, like array or name
|
||||||
type = type.replace('or', ' ')
|
types = types.strip()
|
||||||
type = type.replace(',', ' ')
|
types = types.replace('or', ' ')
|
||||||
type = type.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'?
|
types = types.replace(',', ' ')
|
||||||
|
types = types.replace('text', ' ') # TODO(edisonn): what is the difference between 'text string' and 'string'?
|
||||||
|
types = types.replace('file specification', 'file_specification')
|
||||||
|
|
||||||
type = type.strip()
|
|
||||||
types = type.split()
|
|
||||||
|
|
||||||
if len(types) == 1:
|
self.fTypes = types
|
||||||
if type == 'integer':
|
|
||||||
self.integer(self.fCppName)
|
|
||||||
self.default(datatypes.PdfInteger(0))
|
|
||||||
return self
|
|
||||||
|
|
||||||
if type == 'number':
|
|
||||||
self.number(self.fCppName)
|
|
||||||
self.default(datatypes.PdfNumber(0))
|
|
||||||
return self
|
|
||||||
|
|
||||||
if type == 'string':
|
|
||||||
self.string(self.fCppName)
|
|
||||||
self.default(datatypes.PdfString('""'))
|
|
||||||
return self
|
|
||||||
|
|
||||||
if type == 'name':
|
|
||||||
self.name(self.fCppName)
|
|
||||||
self.default(datatypes.PdfName('""'))
|
|
||||||
return self
|
|
||||||
|
|
||||||
if type == 'dictionary':
|
|
||||||
self.dictionary(self.fCppName)
|
|
||||||
self.default(datatypes.CppNull())
|
|
||||||
return self
|
|
||||||
|
|
||||||
self.fType = 'object'
|
|
||||||
self.fDictionaryType = 'Object'
|
|
||||||
self.fCppType = 'SkPdfObject*'
|
|
||||||
self.fCppReader = 'ObjectFromDictionary'
|
|
||||||
self.fDefault = datatypes.CppNull()
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def comment(self, comment):
|
def comment(self, comment):
|
||||||
@ -271,6 +225,9 @@ class PdfClassManager:
|
|||||||
|
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
|
|
||||||
|
global knowTypes
|
||||||
|
|
||||||
# generate enum
|
# generate enum
|
||||||
enumsRoot = []
|
enumsRoot = []
|
||||||
|
|
||||||
@ -363,17 +320,42 @@ class PdfClassManager:
|
|||||||
if prop.fCppName != '':
|
if prop.fCppName != '':
|
||||||
if prop.fCppName[0] == '[':
|
if prop.fCppName[0] == '[':
|
||||||
print('/*') # comment code of the atributes that can have any name
|
print('/*') # comment code of the atributes that can have any name
|
||||||
|
|
||||||
print(' ' + prop.fCppType + ' ' + prop.fCppName + '() const {')
|
# TODO(edisonn): has_foo();
|
||||||
print(' ' + prop.fCppType + ' ret;')
|
print(' bool has_' + prop.fCppName + '() const {')
|
||||||
print(' if (' + prop.fCppReader + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;')
|
print(' return (ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", NULL));')
|
||||||
if field.fRequired == False:
|
print(' }')
|
||||||
print(' return ' + prop.fDefault.toCpp() + ';');
|
|
||||||
if field.fRequired == True:
|
if len(prop.fTypes.split()) == 1:
|
||||||
print(' // TODO(edisonn): warn about missing required field, assert for known good pdfs')
|
t = prop.fTypes.strip()
|
||||||
print(' return ' + field.fBadDefault + ';');
|
print(' ' + knowTypes[t][0] + ' ' + prop.fCppName + '() const {')
|
||||||
print(' }')
|
print(' ' + knowTypes[t][0] + ' ret;')
|
||||||
print
|
print(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;')
|
||||||
|
if field.fRequired == False and prop.fDefault != '':
|
||||||
|
print(' return ' + prop.fDefault.toCpp() + ';');
|
||||||
|
else:
|
||||||
|
print(' // TODO(edisonn): warn about missing required field, assert for known good pdfs')
|
||||||
|
print(' return ' + knowTypes[t][2].toCpp() + ';');
|
||||||
|
print(' }')
|
||||||
|
print
|
||||||
|
else:
|
||||||
|
for type in prop.fTypes.split():
|
||||||
|
t = type.strip()
|
||||||
|
print(' bool is' + prop.fCppName + 'A' + t.title() + '() const {')
|
||||||
|
print(' SkPdfObject* ret = NULL;')
|
||||||
|
print(' if (!ObjectFromDictionary(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return false;')
|
||||||
|
print(' return ' + knowTypes[t][3] + ';')
|
||||||
|
print(' }')
|
||||||
|
print
|
||||||
|
|
||||||
|
print(' ' + knowTypes[t][0] + ' get' + prop.fCppName + 'As' + t.title() + '() const {')
|
||||||
|
print(' ' + knowTypes[t][0] + ' ret = ' + knowTypes[t][2].toCpp() + ';')
|
||||||
|
print(' if (' + knowTypes[t][1] + '(fPodofoDoc, fPodofoObj->GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &ret)) return ret;')
|
||||||
|
print(' // TODO(edisonn): warn about missing required field, assert for known good pdfs')
|
||||||
|
print(' return ' + knowTypes[t][2].toCpp() + ';')
|
||||||
|
print(' }')
|
||||||
|
print
|
||||||
|
|
||||||
|
|
||||||
if prop.fCppName[0] == '[':
|
if prop.fCppName[0] == '[':
|
||||||
print('*/') # comment code of the atributes that can have any name
|
print('*/') # comment code of the atributes that can have any name
|
||||||
@ -403,7 +385,7 @@ class PdfClassManager:
|
|||||||
print
|
print
|
||||||
|
|
||||||
print(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {')
|
print(' static bool map(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj, SkPdf' + name + '** out) {')
|
||||||
print(' if (!isA' + name + '(podofoDoc, podofoObj)) return false;')
|
print(' if (!is' + name + '(podofoDoc, podofoObj)) return false;')
|
||||||
print
|
print
|
||||||
|
|
||||||
for sub in cls.fEnumSubclasses:
|
for sub in cls.fEnumSubclasses:
|
||||||
@ -419,7 +401,7 @@ class PdfClassManager:
|
|||||||
for name in self.fClassesNamesInOrder:
|
for name in self.fClassesNamesInOrder:
|
||||||
cls = self.fClasses[name]
|
cls = self.fClasses[name]
|
||||||
|
|
||||||
print(' static bool isA' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {')
|
print(' static bool is' + name + '(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {')
|
||||||
|
|
||||||
if cls.fCheck != '':
|
if cls.fCheck != '':
|
||||||
print(' return ' + cls.fCheck + ';')
|
print(' return ' + cls.fCheck + ';')
|
||||||
@ -429,8 +411,8 @@ class PdfClassManager:
|
|||||||
prop = field.fProp
|
prop = field.fProp
|
||||||
if prop.fHasMust:
|
if prop.fHasMust:
|
||||||
cntMust = cntMust + 1
|
cntMust = cntMust + 1
|
||||||
print(' ' + prop.fCppType + ' ' + prop.fCppName + ';')
|
print(' ' + knowTypes[prop.fTypes.strip()][0] + ' ' + prop.fCppName + ';')
|
||||||
print(' if (!' + prop.fCppReader + '(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;')
|
print(' if (!' + knowTypes[prop.fTypes.strip()][1] + '(&podofoDoc, podofoObj.GetDictionary(), \"' + prop.fName + '\", \"' + prop.fAbr + '\", &' + prop.fCppName + ')) return false;')
|
||||||
print(' if (' + prop.fCppName + ' != ' + prop.fMustBe.toCpp() + ') return false;')
|
print(' if (' + prop.fCppName + ' != ' + prop.fMustBe.toCpp() + ') return false;')
|
||||||
print
|
print
|
||||||
|
|
||||||
|
@ -76,8 +76,104 @@ bool ObjectFromDictionary(const PdfMemDocument* pdfDoc,
|
|||||||
SkPdfObject** data);
|
SkPdfObject** data);
|
||||||
|
|
||||||
|
|
||||||
|
struct SkPdfFileSpec {};
|
||||||
|
class SkPdfArray;
|
||||||
|
struct SkPdfStream {};
|
||||||
|
struct SkPdfDate {};
|
||||||
|
struct SkPdfTree {};
|
||||||
|
struct SkPdfFunction {};
|
||||||
|
|
||||||
|
bool ArrayFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfArray* data);
|
||||||
|
|
||||||
|
|
||||||
|
bool FileSpecFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfFileSpec* data);
|
||||||
|
|
||||||
|
|
||||||
|
bool StreamFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfStream* data);
|
||||||
|
|
||||||
|
bool TreeFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfTree** data);
|
||||||
|
|
||||||
|
bool DateFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfDate* data);
|
||||||
|
|
||||||
|
bool SkRectFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkRect* data);
|
||||||
|
|
||||||
|
bool FunctionFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfFunction* data);
|
||||||
|
|
||||||
|
|
||||||
#include "pdf_auto_gen.h"
|
#include "pdf_auto_gen.h"
|
||||||
|
|
||||||
|
bool ArrayFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfArray* data) {return false;}
|
||||||
|
|
||||||
|
bool FileSpecFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfFileSpec* data) {return false;}
|
||||||
|
|
||||||
|
bool StreamFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfStream* data) {return false;}
|
||||||
|
|
||||||
|
bool TreeFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfTree** data) {return false;}
|
||||||
|
|
||||||
|
bool DateFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfDate* data) {return false;}
|
||||||
|
|
||||||
|
bool SkRectFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkRect* data) {return false;}
|
||||||
|
|
||||||
|
bool FunctionFromDictionary(const PdfMemDocument* pdfDoc,
|
||||||
|
const PdfDictionary& dict,
|
||||||
|
const char* key,
|
||||||
|
const char* abr,
|
||||||
|
SkPdfFunction* data) {return false;}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO(edisonn):
|
* TODO(edisonn):
|
||||||
* - encapsulate podofo in the pdf api so the skpdf does not know anything about podofo
|
* - encapsulate podofo in the pdf api so the skpdf does not know anything about podofo
|
||||||
@ -1134,12 +1230,11 @@ SkBitmap getImageFromObject(PdfContext* pdfContext, const SkPdfImageDictionary*
|
|||||||
long bpc = image->BitsPerComponent();
|
long bpc = image->BitsPerComponent();
|
||||||
long width = image->Width();
|
long width = image->Width();
|
||||||
long height = image->Height();
|
long height = image->Height();
|
||||||
SkPdfObject* colorSpaceDict = image->ColorSpace();
|
|
||||||
std::string colorSpace = "DeviceRGB";
|
std::string colorSpace = "DeviceRGB";
|
||||||
// TODO(edisonn): for multiple type fileds, generate code, like, isName(), isArray(), ...and fields like colorSpace_name(), colorSpace_array()
|
|
||||||
// so we do nto go to podofo anywhere in our cpp file
|
// TODO(edisonn): color space can be an array too!
|
||||||
if (colorSpaceDict && colorSpaceDict->podofo() && colorSpaceDict->podofo()->IsName()) {
|
if (image->isColorSpaceAName()) {
|
||||||
colorSpace = colorSpaceDict->podofo()->GetName().GetName();
|
colorSpace = image->getColorSpaceAsName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user