import Tkinter
import string

def initialise(root, size=None, fontScheme = None):
  global _fontSize
  if size is not None:
    _fontSize = size

  if fontScheme == 'pmw1':
      defaultFont = logicalfont('Helvetica')
      menuFont =    logicalfont('Helvetica', weight='bold', slant='italic')
      scaleFont =   logicalfont('Helvetica', slant='italic')
      root.option_add('*Font',            defaultFont,  'userDefault')
      root.option_add('*Menu*Font',       menuFont,     'userDefault')
      root.option_add('*Menubutton*Font', menuFont,     'userDefault')
      root.option_add('*Scale.*Font',     scaleFont,    'userDefault')

  balloonFont = logicalfont('Helvetica', -6, pixel = '12')
  if not fontexists(root, balloonFont):
      balloonFont = logicalfont('Helvetica', -2)
  root.option_add('*Balloon.*Font', balloonFont, 'userDefault')

def fontexists(widget, fontName):
    test = Tkinter.Label(widget)
    exists = 1
    try:
	test.configure(font = fontName)
    except Tkinter.TclError:
	exists = 0
    test.destroy()
    return exists

maxfontwidthcache = {}
def maxfontwidth(widget, fontName):
    if not maxfontwidthcache.has_key(fontName):
	test = Tkinter.Text(widget)
	test.tag_configure('FONTTAG', font=fontName)
	test.insert('end', 'W', ('FONTTAG'))
	maxfontwidthcache[fontName] = test.dlineinfo('1.0')[2]
	test.destroy()

    return maxfontwidthcache[fontName]

def logicalfont(name='Helvetica', sizeIncr = 0, **kw):
  if not fontInfo.has_key(name):
    raise ValueError, 'font %s does not exist' % name

  rtn = []
  for field in _fontFields:
    if kw.has_key(field):
      logicalValue = kw[field]
    elif fontInfo[name].has_key(field):
      logicalValue = fontInfo[name][field]
    else:
      logicalValue = '*'

    if propertyAliases[name].has_key((field, logicalValue)):
      realValue = propertyAliases[name][(field, logicalValue)]
    elif propertyAliases[name].has_key((field, None)):
      realValue = propertyAliases[name][(field, None)]
    elif propertyAliases[None].has_key((field, logicalValue)):
      realValue = propertyAliases[None][(field, logicalValue)]
    elif propertyAliases[None].has_key((field, None)):
      realValue = propertyAliases[None][(field, None)]
    else:
      realValue = logicalValue

    if field == 'size':
      if realValue == '*':
	  realValue = _fontSize
      realValue = str((realValue + sizeIncr) * 10)

    rtn.append(realValue)

  return string.join(rtn, '-')

def logicalfontnames():
  return fontInfo.keys()

_fontSize = 14

_fontFields = (
  'registry', 'foundry', 'family', 'weight', 'slant', 'width', 'style',
  'pixel', 'size', 'xres', 'yres', 'spacing', 'avgwidth', 'charset', 'encoding')

# <propertyAliases> defines other names for which property values may
# be known by.  This is required because italics in adobe-helvetica
# are specified by 'o', while other fonts use 'i'.

propertyAliases = {}

propertyAliases[None] = {
  ('slant', 'italic') : 'i',
  ('slant', 'normal') : 'r',
  ('weight', 'light') : 'normal',
  ('width', 'wide') : 'normal',
  ('width', 'condensed') : 'normal',
}

#  describes a 'logical' font, giving the default values of
# some of its properties.

fontInfo = {}

fontInfo['Helvetica'] = {
  'foundry' : 'adobe',
  'family' : 'helvetica',
  'registry' : '',
  'charset' : 'iso8859',
  'encoding' : '1',
  'spacing' : 'p',
  'slant' : 'normal',
  'width' : 'normal',
  'weight' : 'normal',
}

propertyAliases['Helvetica'] = {
  ('slant', 'italic') : 'o',
  ('weight', 'normal') : 'medium',
  ('weight', 'light') : 'medium',
}

fontInfo['Times'] = {
  'foundry' : 'adobe',
  'family' : 'times',
  'registry' : '',
  'charset' : 'iso8859',
  'encoding' : '1',
  'spacing' : 'p',
  'slant' : 'normal',
  'width' : 'normal',
  'weight' : 'normal',
}

propertyAliases['Times'] = {
  ('weight', 'normal') : 'medium',
  ('weight', 'light') : 'medium',
}

fontInfo['Fixed'] = {
  'foundry' : 'misc',
  'family' : 'fixed',
  'registry' : '',
  'charset' : 'iso8859',
  'encoding' : '1',
  'spacing' : 'c',
  'slant' : 'normal',
  'width' : 'normal',
  'weight' : 'normal',
}

propertyAliases['Fixed'] = {
  ('weight', 'normal') : 'medium',
  ('weight', 'light') : 'medium',
  ('style', None) : '',
  ('width', 'condensed') : 'semicondensed',
}

fontInfo['Courier'] = {
  'foundry' : 'adobe',
  'family' : 'courier',
  'registry' : '',
  'charset' : 'iso8859',
  'encoding' : '1',
  'spacing' : 'm',
  'slant' : 'normal',
  'width' : 'normal',
  'weight' : 'normal',
}

propertyAliases['Courier'] = {
  ('weight', 'normal') : 'medium',
  ('weight', 'light') : 'medium',
  ('style', None) : '',
}

fontInfo['Typewriter'] = {
  'foundry' : 'b&h',
  'family' : 'lucidatypewriter',
  'registry' : '',
  'charset' : 'iso8859',
  'encoding' : '1',
  'spacing' : 'm',
  'slant' : 'normal',
  'width' : 'normal',
  'weight' : 'normal',
}

propertyAliases['Typewriter'] = {
  ('weight', 'normal') : 'medium',
  ('weight', 'light') : 'medium',
}