_h_d_m_x.py :  » GUI » TTX-FontTools » fonttools-2.3 » Lib » fontTools » ttLib » tables » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » TTX FontTools 
TTX FontTools » fonttools 2.3 » Lib » fontTools » ttLib » tables » _h_d_m_x.py
import DefaultTable
import sstruct
import string

hdmxHeaderFormat = """
  >   # big endian!
  version:  H
  numRecords:  H
  recordSize:  l
"""

class table__h_d_m_x(DefaultTable.DefaultTable):
  
  def decompile(self, data, ttFont):
    numGlyphs = ttFont['maxp'].numGlyphs
    glyphOrder = ttFont.getGlyphOrder()
    dummy, data = sstruct.unpack2(hdmxHeaderFormat, data, self)
    self.hdmx = {}
    for i in range(self.numRecords):
      ppem = ord(data[0])
      maxSize = ord(data[1])
      widths = {}
      for glyphID in range(numGlyphs):
        widths[glyphOrder[glyphID]] = ord(data[glyphID+2])
      self.hdmx[ppem] = widths
      data = data[self.recordSize:]
    assert len(data) == 0, "too much hdmx data"
  
  def compile(self, ttFont):
    self.version = 0
    numGlyphs = ttFont['maxp'].numGlyphs
    glyphOrder = ttFont.getGlyphOrder()
    self.recordSize = 4 * ((2 + numGlyphs + 3) / 4)
    pad = (self.recordSize - 2 - numGlyphs) * "\0"
    self.numRecords = len(self.hdmx)
    data = sstruct.pack(hdmxHeaderFormat, self)
    items = self.hdmx.items()
    items.sort()
    for ppem, widths in items:
      data = data + chr(ppem) + chr(max(widths.values()))
      for glyphID in range(len(glyphOrder)):
        width = widths[glyphOrder[glyphID]]
        data = data + chr(width)
      data = data + pad
    return data
  
  def toXML(self, writer, ttFont):
    writer.begintag("hdmxData")
    writer.newline()
    ppems = self.hdmx.keys()
    ppems.sort()
    records = []
    format = ""
    for ppem in ppems:
      widths = self.hdmx[ppem]
      records.append(widths)
      format = format + "%4d"
    glyphNames = ttFont.getGlyphOrder()[:]
    glyphNames.sort()
    maxNameLen = max(map(len, glyphNames))
    format = "%" + `maxNameLen` + 's:' + format + ' ;'
    writer.write(format % (("ppem",) + tuple(ppems)))
    writer.newline()
    writer.newline()
    for glyphName in glyphNames:
      row = []
      for ppem in ppems:
        widths = self.hdmx[ppem]
        row.append(widths[glyphName])
      if ";" in glyphName:
        glyphName = "\\x3b".join(glyphName.split(";"))
      writer.write(format % ((glyphName,) + tuple(row)))
      writer.newline()
    writer.endtag("hdmxData")
    writer.newline()
  
  def fromXML(self, (name, attrs, content), ttFont):
    if name <> "hdmxData":
      return
    content = string.join(content, "")
    lines = string.split(content, ";")
    topRow = string.split(lines[0])
    assert topRow[0] == "ppem:", "illegal hdmx format"
    ppems = map(int, topRow[1:])
    self.hdmx = hdmx = {}
    for ppem in ppems:
      hdmx[ppem] = {}
    lines = map(string.split, lines[1:])
    for line in lines:
      if not line:
        continue
      assert line[0][-1] == ":", "illegal hdmx format"
      glyphName = line[0][:-1]
      if "\\" in glyphName:
        from fontTools.misc.textTools import safeEval
        glyphName = safeEval('"""' + glyphName + '"""')
      line = map(int, line[1:])
      assert len(line) == len(ppems), "illegal hdmx format"
      for i in range(len(ppems)):
        hdmx[ppems[i]][glyphName] = line[i]

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.