本文整理汇总了Python中sstruct.pack函数的典型用法代码示例。如果您正苦于以下问题:Python pack函数的具体用法?Python pack怎么用?Python pack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compile
def compile(self, ttFont):
dataList = []
dataList.append(sstruct.pack(bigGlyphMetricsFormat, self.metrics))
dataList.append(struct.pack(">H", len(self.componentArray)))
for curComponent in self.componentArray:
curComponent.glyphCode = ttFont.getGlyphID(curComponent.name)
dataList.append(sstruct.pack(ebdtComponentFormat, curComponent))
return string.join(dataList, "")
示例2: compile
def compile(self, ttFont):
if 'glyf' in ttFont:
if ttFont.isLoaded('glyf') and ttFont.recalcBBoxes:
self.recalc(ttFont)
else:
pass # CFF
self.numGlyphs = len(ttFont.getGlyphOrder())
if self.tableVersion != 0x00005000:
self.tableVersion = 0x00010000
data = sstruct.pack(maxpFormat_0_5, self)
if self.tableVersion == 0x00010000:
data = data + sstruct.pack(maxpFormat_1_0_add, self)
return data
示例3: compile
def compile(self, ttFont):
panose = self.panose
self.panose = sstruct.pack(panoseFormat, self.panose)
if self.version == 0:
data = sstruct.pack(OS2_format_0, self)
elif self.version == 1:
data = sstruct.pack(OS2_format_1, self)
elif self.version in (2, 3, 4):
data = sstruct.pack(OS2_format_2, self)
else:
from fontTools import ttLib
raise ttLib.TTLibError("unknown format for OS/2 table: version %s" % self.version)
self.panose = panose
return data
示例4: test_incorrect_compressed_size
def test_incorrect_compressed_size(self):
data = self.file.read(woff2DirectorySize)
header = sstruct.unpack(woff2DirectoryFormat, data)
header['totalCompressedSize'] = 0
data = sstruct.pack(woff2DirectoryFormat, header)
with self.assertRaises(brotli.error):
WOFF2Reader(BytesIO(data + self.file.read()))
示例5: _calcMasterChecksum
def _calcMasterChecksum(self, directory):
# calculate checkSumAdjustment
tags = self.tables.keys()
checksums = []
for i in range(len(tags)):
checksums.append(self.tables[tags[i]].checkSum)
# TODO(behdad) I'm fairly sure the checksum for woff is not working correctly.
# Haven't debugged.
if self.DirectoryEntry != SFNTDirectoryEntry:
# Create a SFNT directory for checksum calculation purposes
self.searchRange, self.entrySelector, self.rangeShift = getSearchRange(self.numTables)
directory = sstruct.pack(sfntDirectoryFormat, self)
tables = self.tables.items()
tables.sort()
for tag, entry in tables:
sfntEntry = SFNTDirectoryEntry()
for item in ['tag', 'checkSum', 'offset', 'length']:
setattr(sfntEntry, item, getattr(entry, item))
directory = directory + sfntEntry.toString()
directory_end = sfntDirectorySize + len(self.tables) * sfntDirectoryEntrySize
assert directory_end == len(directory)
checksums.append(calcChecksum(directory))
checksum = sum(checksums) & 0xffffffff
# BiboAfba!
checksumadjustment = (0xB1B0AFBA - checksum) & 0xffffffff
return checksumadjustment
示例6: _writeTableDirectory
def _writeTableDirectory(self):
if self.verbose:
debugmsg("writing table directory")
self.file.seek(woffHeaderSize)
for tag, (index, entry, data) in sorted(self.tables.items()):
entry = sstruct.pack(woffDirectoryEntryFormat, entry)
self.file.write(entry)
示例7: compile
def compile(self, ttFont):
self.numGMAPs = len(self.GMAPs)
self.numGlyplets = len(self.glyphlets)
GMAPoffsets = [0]*(self.numGMAPs + 1)
glyphletOffsets = [0]*(self.numGlyplets + 1)
dataList =[ sstruct.pack(GPKGFormat, self)]
pos = len(dataList[0]) + (self.numGMAPs + 1)*4 + (self.numGlyplets + 1)*4
GMAPoffsets[0] = pos
for i in range(1, self.numGMAPs +1):
pos += len(self.GMAPs[i-1])
GMAPoffsets[i] = pos
gmapArray = numpy.array(GMAPoffsets, numpy.uint32)
if sys.byteorder <> "big":
gmapArray = gmapArray.byteswap()
dataList.append(gmapArray.tostring())
glyphletOffsets[0] = pos
for i in range(1, self.numGlyplets +1):
pos += len(self.glyphlets[i-1])
glyphletOffsets[i] = pos
glyphletArray = numpy.array(glyphletOffsets, numpy.uint32)
if sys.byteorder <> "big":
glyphletArray = glyphletArray.byteswap()
dataList.append(glyphletArray.tostring())
dataList += self.GMAPs
dataList += self.glyphlets
data = "".join(dataList)
return data
示例8: calcHeadCheckSumAdjustment
def calcHeadCheckSumAdjustment(flavor, tables):
numTables = len(tables)
# build the sfnt header
searchRange, entrySelector, rangeShift = getSearchRange(numTables)
sfntDirectoryData = dict(
sfntVersion=flavor,
numTables=numTables,
searchRange=searchRange,
entrySelector=entrySelector,
rangeShift=rangeShift
)
# build the sfnt directory
directory = sstruct.pack(sfntDirectoryFormat, sfntDirectoryData)
for tag, entry in sorted(tables.items()):
entry = tables[tag]
sfntEntry = SFNTDirectoryEntry()
sfntEntry.tag = tag
sfntEntry.checkSum = entry["checkSum"]
sfntEntry.offset = entry["offset"]
sfntEntry.length = entry["length"]
directory += sfntEntry.toString()
# calculate the checkSumAdjustment
checkSums = [entry["checkSum"] for entry in tables.values()]
checkSums.append(calcChecksum(directory))
checkSumAdjustment = sum(checkSums)
checkSumAdjustment = (0xB1B0AFBA - checkSumAdjustment) & 0xffffffff
# done
return checkSumAdjustment
示例9: close
def close(self):
if self.numTables != len(self.tables):
raise WOFFLibError("wrong number of tables; expected %d, found %d" % (self.numTables, len(self.tables)))
# first, handle the checkSumAdjustment
if self.recalculateHeadChecksum and "head" in self.tables:
self._handleHeadChecksum()
# check the table directory conformance
for tag, (index, entry, data) in sorted(self.tables.items()):
self._checkTableConformance(entry, data)
# write the header
header = sstruct.pack(woffHeaderFormat, self)
self.file.seek(0)
self.file.write(header)
# update the directory offsets
offset = woffHeaderSize + (woffDirectoryEntrySize * self.numTables)
order = self._tableOrder()
for tag in order:
index, entry, data = self.tables[tag]
entry.offset = offset
offset += calc4BytePaddedLength(entry.compLength) # ensure byte alignment
# write the directory
self._writeTableDirectory()
# write the table data
self._writeTableData()
# write the metadata
self._writeMetadata()
# write the private data
self._writePrivateData()
# write the header
self._writeHeader()
# go to the beginning of the file
self.file.seek(0)
示例10: compile
def compile(self, ttFont):
packed = sstruct.pack(DSIG_HeaderFormat, self)
headers = [packed]
offset = len(packed) + self.usNumSigs * sstruct.calcsize(DSIG_SignatureFormat)
data = []
for sigrec in self.signatureRecords:
# first pack signature block
sigrec.cbSignature = len(sigrec.pkcs7)
packed = sstruct.pack(DSIG_SignatureBlockFormat, sigrec) + sigrec.pkcs7
data.append(packed)
# update redundant length field
sigrec.ulLength = len(packed)
# update running table offset
sigrec.ulOffset = offset
headers.append(sstruct.pack(DSIG_SignatureFormat, sigrec))
offset += sigrec.ulLength
return "".join(headers + data)
示例11: test_incorrect_file_size
def test_incorrect_file_size(self):
data = self.file.read(woff2DirectorySize)
header = sstruct.unpack(woff2DirectoryFormat, data)
header['length'] -= 1
data = sstruct.pack(woff2DirectoryFormat, header)
with self.assertRaisesRegex(
ttLib.TTLibError, "doesn't match the actual file size"):
WOFF2Reader(BytesIO(data + self.file.read()))
示例12: compile
def compile(self, parentTable):
data = sstruct.pack(METAStringRecordFormat, self)
if parentTable.metaFlags == 0:
datum = struct.pack(">H", self.offset)
elif parentTable.metaFlags == 1:
datum = struct.pack(">L", self.offset)
data = data + datum
return data
示例13: compile
def compile(self, ttFont):
if self.UV == None:
self.UV = 0
nameLen = len(self.name)
if nameLen < 32:
self.name = self.name + "\0"*(32 - nameLen)
data = sstruct.pack(GMAPRecordFormat1, self)
return data
示例14: compile
def compile(self, ttFont):
sbixData = ""
self.numSets = len(self.bitmapSets)
sbixHeader = sstruct.pack(sbixHeaderFormat, self)
# calculate offset to start of first bitmap set
setOffset = sbixHeaderFormatSize + sbixBitmapSetOffsetFormatSize * self.numSets
for si in sorted(self.bitmapSets.keys()):
myBitmapSet = self.bitmapSets[si]
myBitmapSet.compile(ttFont)
# append offset to this bitmap set to table header
myBitmapSet.offset = setOffset
sbixHeader += sstruct.pack(sbixBitmapSetOffsetFormat, myBitmapSet)
setOffset += len(myBitmapSet.data)
sbixData += myBitmapSet.data
return sbixHeader + sbixData
示例15: compile
def compile(self, ttFont):
glyphIds = map(ttFont.getGlyphID, self.names)
# Make sure all the ids are consecutive. This is required by Format 2.
assert glyphIds == range(self.firstGlyphIndex, self.lastGlyphIndex+1), "Format 2 ids must be consecutive."
self.imageDataOffset = min(zip(*self.locations)[0])
dataList = [EblcIndexSubTable.compile(self, ttFont)]
dataList.append(struct.pack(">L", self.imageSize))
dataList.append(sstruct.pack(bigGlyphMetricsFormat, self.metrics))
return string.join(dataList, "")