_h_e_a_d.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_e_a_d.py
import DefaultTable
import sstruct
import time
import string
from fontTools.misc.textTools import safeEval,num2binary,binary2num


headFormat = """
    >  # big endian
    tableVersion:       16.16F
    fontRevision:       16.16F
    checkSumAdjustment: I
    magicNumber:        I
    flags:              H
    unitsPerEm:         H
    created:            8s
    modified:           8s
    xMin:               h
    yMin:               h
    xMax:               h
    yMax:               h
    macStyle:           H
    lowestRecPPEM:      H
    fontDirectionHint:  h
    indexToLocFormat:   h
    glyphDataFormat:    h
"""

class table__h_e_a_d(DefaultTable.DefaultTable):
  
  dependencies = ['maxp', 'loca']
  
  def decompile(self, data, ttFont):
    dummy, rest = sstruct.unpack2(headFormat, data, self)
    if rest:
      # this is quite illegal, but there seem to be fonts out there that do this
      assert rest == "\0\0"
    self.unitsPerEm = int(self.unitsPerEm)
    self.flags = int(self.flags)
    self.strings2dates()
  
  def compile(self, ttFont):
    self.modified = long(time.time() - mac_epoch_diff)
    self.dates2strings()
    data = sstruct.pack(headFormat, self)
    self.strings2dates()
    return data
  
  def strings2dates(self):
    self.created = bin2long(self.created)
    self.modified = bin2long(self.modified)
  
  def dates2strings(self):
    self.created = long2bin(self.created)
    self.modified = long2bin(self.modified)
  
  def toXML(self, writer, ttFont):
    writer.comment("Most of this table will be recalculated by the compiler")
    writer.newline()
    formatstring, names, fixes = sstruct.getformat(headFormat)
    for name in names:
      value = getattr(self, name)
      if name in ("created", "modified"):
        try:
          value = time.asctime(time.gmtime(max(0, value + mac_epoch_diff)))
        except ValueError:
          value = time.asctime(time.gmtime(0))
      if name in ("magicNumber", "checkSumAdjustment"):
        if value < 0:
          value = value + 0x100000000L
        value = hex(value)
        if value[-1:] == "L":
          value = value[:-1]
      elif name in ("macStyle", "flags"):
        value = num2binary(value, 16)
      writer.simpletag(name, value=value)
      writer.newline()
  
  def fromXML(self, (name, attrs, content), ttFont):
    value = attrs["value"]
    if name in ("created", "modified"):
      value = parse_date(value) - mac_epoch_diff
    elif name in ("macStyle", "flags"):
      value = binary2num(value)
    else:
      try:
        value = safeEval(value)
      except OverflowError:
        value = long(value)
    setattr(self, name, value)
  
  def __cmp__(self, other):
    selfdict = self.__dict__.copy()
    otherdict = other.__dict__.copy()
    # for testing purposes, compare without the modified and checkSumAdjustment
    # fields, since they are allowed to be different.
    for key in ["modified", "checkSumAdjustment"]:
      del selfdict[key]
      del otherdict[key]
    return cmp(selfdict, otherdict)


def calc_mac_epoch_diff():
  """calculate the difference between the original Mac epoch (1904)
  to the epoch on this machine.
  """
  safe_epoch_t = (1972, 1, 1, 0, 0, 0, 0, 0, 0)
  safe_epoch = time.mktime(safe_epoch_t) - time.timezone
  # This assert fails in certain time zones, with certain daylight settings
  #assert time.gmtime(safe_epoch)[:6] == safe_epoch_t[:6]
  seconds1904to1972 = 60 * 60 * 24 * (365 * (1972-1904) + 17) # thanks, Laurence!
  return long(safe_epoch - seconds1904to1972)

mac_epoch_diff = calc_mac_epoch_diff()


_months = ['   ', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
    'sep', 'oct', 'nov', 'dec']
_weekdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']

def parse_date(datestring):
  datestring = string.lower(datestring)
  weekday, month, day, tim, year = string.split(datestring)
  weekday = _weekdays.index(weekday)
  month = _months.index(month)
  year = int(year)
  day = int(day)
  hour, minute, second = map(int, string.split(tim, ":"))
  t = (year, month, day, hour, minute, second, weekday, 0, 0)
  try:
    return long(time.mktime(t) - time.timezone)
  except OverflowError:
    return 0L


def bin2long(data):
  # thanks </F>!
  v = 0L
  for i in map(ord, data):
      v = v<<8 | i
  return v

def long2bin(v, bytes=8):
  mask = long("FF" * bytes, 16)
  data = ""
  while v:
    data = chr(v & 0xff) + data
    v = (v >> 8) & mask
  data = (bytes - len(data)) * "\0" + data
  assert len(data) == 8, "long too long"
  return data

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