python/stdwin fonts/text_edit

Jim Densmore (densmore@opus.dsaco.dsai.com)
Wed, 21 Sep 94 18:20:16 -0600

> > 1) There seems to be a "get" in the stdwin interface provided by Python
> > for everything except current font; that is, I find no analog for
> > stdwin.setfont(). I also have found no such routine in the stdwin
> > source code, though wsetfont is present in .../Ports/x11/font.c .
> > Is this an oversight? Hard to do in X for some reason?
>
>From Guido:
> An oversight, I guess. Not hard to do at all. You could of course
> keep track of the setfont calls you make...

Surely. In fact I'd already done that. I was just wondering why . . . and
also noting that if done, it would gain me access to the X resource menuFont
if the font had never been set. In case anyone needs to fiddle with fonts
in windows, hopefully the following offering will avoid their having to
reinvent that wheel. Thanks for the response. --Jim
(watch out, signature file is at the end)

#!/usr/local/bin/python
################################################################################
# Copyright 1994 by Decision-Science Applications, Inc. Arlington Virginia USA #
# Permission to use this software in any (reasonable) way is hereby granted. #
# It is provided free of charge but without any warranty. All liabilities are #
# disclaimed. This notice shall be retained in all copies of this software. #
################################################################################

# font.py: simple font manager. Jim Densmore, DSA Colorado Springs, 17-Sep-1994
# Functions:
# current() - return current font
# set(font) - change current font to arg, return whether success
# push(font) - store current font on stack, then return set(font)
# pop() - return to the previously stored font, return success
# default() - same as set(<default font>)
# default(Fn,Args)- return function value with font temporarily defaulted
# reset() - clears stack and does default()
# Warnings:
# 1. If you do "import font" it is easy to accidently later use a local
# variable named "font" and hose the connection to the module.
# 2. I don't know how to make sure the default font is set to same value as
# the X resource "menuFont", so I just assume it is font "fixed". If
# this is a problem and you don't know how to fix it, just push your own
# font onto the stack early. If you do know how to fix it, please send
# me mail at "densmore@dsai.com".

# Globals used:
# _FontManager_default - default font, currently hardwired to "fixed"
# _FontManager_current - current font
# _FontManager_stack - list used as stack of fonts

import stdwin

def initialize(debug=None): ##################################################
global _FontManager_default, _FontManager_current, _FontManager_stack

# default is only 'fixed' if the X resource "menuFont" has not been changed
_FontManager_default = 'fixed'

_FontManager_current = _FontManager_default
_FontManager_stack = []
if debug: print "font manager initialized"

def current(): ###############################################################
global _FontManager_current
return _FontManager_current

def set(Font): ###############################################################
global _FontManager_default, _FontManager_current, _FontManager_stack
try:
stdwin.setfont(Font)
_FontManager_current = Font
result = 1
except:
stdwin.setfont(_FontManager_default)
_FontManager_current = _FontManager_default
result = 0
return result

def push(newFont): ###########################################################
global _FontManager_default, _FontManager_current, _FontManager_stack
_FontManager_stack.append(_FontManager_current)
return set(newFont)

def pop(): ###################################################################
global _FontManager_default, _FontManager_current, _FontManager_stack
last = len(_FontManager_stack) - 1
if last >= 0:
newFont = _FontManager_stack[last]
del _FontManager_stack[last]
result = 1
else:
newFont = _FontManager_default
result = 0
set(newFont)
return result

def reset(): #################################################################
global _FontManager_stack
del _FontManager_stack
initialize()

def default(Function = None, arg1 = None, arg2 = None): ######################
# Designed for use with textwidth, textbreak, lineheight, baseline.
# With no arguments, the default font is selected (permanently).
# With arguments, the default font is selected temporarily and the
# indicated function is then applied with the appropriate arguments.
#
global _FontManager_default, _FontManager_current, _FontManager_stack
if Function == None:
result = set(_FontManager_default)
else:
stdwin.setfont(_FontManager_default)
if arg1 == None:
result = (Function)()
elif arg2 == None:
result = (Function)(arg1)
else:
result = (Function)(arg1, arg2)
stdwin.setfont(_FontManager_current)
return result

initialize()

________________________________________________________________
Jim Densmore, DECISION-SCIENCE APPLICATIONS Colorado Operations
1755 Telstar Drive, Suite 201, Colorado Springs, CO 80920-1018
Voice: 719/593-5974, Fax: -5978; Internet: densmore@dsai.com
Newsletter Editor, High Flights Soaring Club, Meadowlake Airport