V_O_R_G_.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 » V_O_R_G_.py
import operator
import DefaultTable
import struct
from fontTools.ttLib import sfnt
from fontTools.misc.textTools import safeEval,readHex
from types import IntType,StringType


class table_V_O_R_G_(DefaultTable.DefaultTable):

  """ This table is structured so that you can treat it like a dictionary keyed by glyph name.
  ttFont['VORG'][<glyphName>] will return the vertical origin for any glyph
  ttFont['VORG'][<glyphName>] = <value> will set the vertical origin for any glyph.
  """

  def decompile(self, data, ttFont):
    self.getGlyphName = ttFont.getGlyphName # for use in get/set item functions, for access by GID
    self.majorVersion, self.minorVersion, self.defaultVertOriginY, self.numVertOriginYMetrics = struct.unpack(">HHhH", data[:8])
    assert (self.majorVersion <= 1), "Major version of VORG table is higher than I know how to handle"
    data = data[8:]
    vids = []
    gids = []
    pos = 0
    for i in range(self.numVertOriginYMetrics):
      gid, vOrigin = struct.unpack(">Hh", data[pos:pos+4])
      pos += 4
      gids.append(gid)
      vids.append(vOrigin)

    self.VOriginRecords = vOrig = {}
    glyphOrder = ttFont.getGlyphOrder()
    try:
      names = map(operator.getitem, [glyphOrder]*self.numVertOriginYMetrics, gids )
    except IndexError:
      getGlyphName = self.getGlyphName
      names = map(getGlyphName, gids )

    map(operator.setitem, [vOrig]*self.numVertOriginYMetrics, names, vids)


  def compile(self, ttFont):
    vorgs = self.VOriginRecords.values()
    names = self.VOriginRecords.keys()
    nameMap = ttFont.getReverseGlyphMap()
    lenRecords = len(vorgs) 
    try:
      gids = map(operator.getitem, [nameMap]*lenRecords, names)
    except KeyError:
      nameMap = ttFont.getReverseGlyphMap(rebuild=1)
      gids = map(operator.getitem, [nameMap]*lenRecords, names)
    vOriginTable = map(None, gids, vorgs)
    self.numVertOriginYMetrics = lenRecords
    vOriginTable.sort() # must be in ascending GID order
    dataList = [ struct.pack(">Hh", rec[0], rec[1]) for rec in vOriginTable]
    header = struct.pack(">HHhH", self.majorVersion, self.minorVersion, self.defaultVertOriginY, self.numVertOriginYMetrics)
    dataList.insert(0, header)
    data = "".join(dataList)
    return data

  def toXML(self, writer, ttFont):
    writer.simpletag("majorVersion", value=self.majorVersion)
    writer.newline()
    writer.simpletag("minorVersion", value=self.minorVersion)
    writer.newline()
    writer.simpletag("defaultVertOriginY", value=self.defaultVertOriginY)
    writer.newline()
    writer.simpletag("numVertOriginYMetrics", value=self.numVertOriginYMetrics)
    writer.newline()
    vOriginTable = []
    glyphNames = self.VOriginRecords.keys()
    for glyphName in glyphNames:
      try:
        gid = ttFont.getGlyphID(glyphName)
      except:
        assert 0, "VORG table contains a glyph name not in ttFont.getGlyphNames(): " + str(glyphName)
      vOriginTable.append([gid, glyphName, self.VOriginRecords[glyphName]])
    vOriginTable.sort()
    for entry in vOriginTable:
      vOriginRec = VOriginRecord(entry[1], entry[2])
      vOriginRec.toXML(writer, ttFont)

  def fromXML(self, (name, attrs, content), ttFont):
    if not hasattr(self, "VOriginRecords"):
      self.VOriginRecords = {}
    self.getGlyphName = ttFont.getGlyphName # for use in get/set item functions, for access by GID
    if name == "VOriginRecord":
      for element in content:
        if isinstance(element, StringType):
          continue
      vOriginRec = VOriginRecord()
      for element in content:
        if isinstance(element, StringType):
          continue
        vOriginRec.fromXML(element, ttFont)
      self.VOriginRecords[vOriginRec.glyphName] = vOriginRec.vOrigin
    elif attrs.has_key("value"):
      value =  safeEval(attrs["value"])
      setattr(self, name, value)


  def __getitem__(self, glyphSelector):
    if type(glyphSelector) == IntType:
      # its a gid, convert to glyph name
      glyphSelector = self.getGlyphName(glyphSelector)

    if not self.VOriginRecords.has_key(glyphSelector):
      return self.defaultVertOriginY
      
    return self.VOriginRecords[glyphSelector]

  def __setitem__(self, glyphSelector, value):
    if type(glyphSelector) == IntType:
      # its a gid, convert to glyph name
      glyphSelector = self.getGlyphName(glyphSelector)

    if  value != self.defaultVertOriginY:
      self.VOriginRecords[glyphSelector] = value
    elif self.VOriginRecords.has_key(glyphSelector):
      del self.VOriginRecords[glyphSelector]

class VOriginRecord:

  def __init__(self, name = None, vOrigin = None):
    self.glyphName = name
    self.vOrigin = vOrigin

  def toXML(self, writer, ttFont):
    writer.begintag("VOriginRecord")
    writer.newline()
    writer.simpletag("glyphName", value=self.glyphName)
    writer.newline()
    writer.simpletag("vOrigin", value=self.vOrigin)
    writer.newline()
    writer.endtag("VOriginRecord")
    writer.newline()

  def fromXML(self, (name, attrs, content), ttFont):
    value = attrs["value"]
    if name == "glyphName":
      setattr(self, name, value)
    else:
      try:
        value = safeEval(value)
      except OverflowError:
        value = long(value)
      setattr(self, name, value)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.