buildmetrics.py :  » GUI » piddle » piddle-1.0.15 » docs » notesPIL » 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 » piddle 
piddle » piddle 1.0.15 » docs » notesPIL » buildmetrics.py
"""
buildmetrics.py

This little program builds the metrics.dat file needed to properly
measure PIL fonts.
"""

from piddlePIL import *
from piddlePIL import _fontprefix,_pilFont

# fonts we're going to support
fonts = ['courier', 'helvetica', 'symbol', 'times']

# sizes we're going to support
sizes = [8, 10, 12, 14, 18, 24]

# global scratch canvas
canvas = PILCanvas( size=(200,100) )
canvas.defaultLineColor = white

# global things we're measuring
ascents = {}
descents = {}
widths = {}

def reset():
  canvas.clear()
  canvas.drawRect( 0,0,200,100, edgeColor=black, fillColor=black )
  
def fontKey():
  f = canvas.defaultFont
  if f.bold: return "%s-bold-%d.pil" % (f.face, f.size)
  else: return "%s-%d.pil" % (f.face, f.size)

def displayForDebugging():
  return       # for faster output, don't display for debugging at all!
  global qdcanvas, canvas
  import piddleQD
  try:
    qdcanvas.clear()
  except:
    qdcanvas = piddleQD.QDCanvas()
  qdcanvas.drawImage( canvas.getImage(), 0, 0 );
  # PATCH...
  bbox = canvas.getImage().getbbox()
  if bbox:
    qdcanvas.drawRect( bbox[0], bbox[1], bbox[2], bbox[3], 
        edgeColor=blue, fillColor=transparent )
  qdcanvas.flush()

def measureAscent():
  reset()
  canvas.drawString("PYTHON", 50, 50)
  displayForDebugging()
  left,top,right,bottom = canvas.getImage().getbbox()
  ascents[fontKey()] = bottom - top

def measureDescent():
  reset()
  canvas.drawString("PTHMygjp_", 50, 50)
  displayForDebugging()
  left,top,right,bottom = canvas.getImage().getbbox()
  key = fontKey()
  descents[key] = bottom - top - ascents[key]

#def measureWidths():
#  w = {}
#  # first get inter-letter spacing
#  reset()
#  canvas.drawString("M", 50, 50)
#  left,top,right,bottom = canvas.getImage().getbbox()
#  M = right - left    # width of one "M"
#  for asciicode in range(33,245):
#    reset()
#    c = chr(asciicode)
#    canvas.drawString(c+'M', 50, 50)
#    displayForDebugging()
#    bbox = canvas.getImage().getbbox()
#    if bbox:
#      left,top,right,bottom = bbox
#      w[c] = right - left - M
#    else:
#      w[c] = 0
#  # special case for handling spaces
#  reset()
#  canvas.drawString("M MM", 50, 50)
#  displayForDebugging()
#  left,top,right,bottom = canvas.getImage().getbbox()
#  w[' '] = right - left - M - 2*w['M']
#  
#  widths[fontKey()] = w

def measureWidths():
  w = {}

  # get widths directly from PIL internals
  import ImageFont
  font = _pilFont(canvas.defaultFont)
  for asciicode in range(32,245):
    c = chr(asciicode)
    m = font.getmask(c)
    w[c] = m.size[0]
  widths[fontKey()] = w

def saveData():
  f = open(os.path.join(_fontprefix,'metrics.dat'), 'wb')
  import cPickle
  cPickle.dump(widths, f, 1)
  cPickle.dump(ascents, f, 1)
  cPickle.dump(descents, f, 1)
  f.close()

for fontname in fonts:
  for size in sizes:
    # measure the plain font
    print "Measuring %s %d plain..." % (fontname, size)
    canvas.defaultFont = Font(face=fontname, size=size)
    measureAscent()
    measureDescent()
    measureWidths()

    # measure the bold version
    print "Measuring %s %d bold..." % (fontname, size)
    canvas.defaultFont = Font(face=fontname, size=size, bold=1)
    measureAscent()
    measureDescent()
    measureWidths()


print "Saving data..."
saveData()
print "All done!"
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.