parent previous next question (Smalltalk Textbook 17)

EngiFontModel

Let's make a tool which handles font attributes such as bold, italic, underline, outline, shadow, character spacing, and color in this section. The actual program is in Textbook 15.

--------------------------------------------------
	font name (family: aString)
	size (pixelSize: anInteger)
	bold (boldness: 0.3 / 0.5 / 0.7)
	italic (italic: aBoolean)
	underline (underline: aBoolean)
	outline (outline: aBoolean)
	shadow (shadow: aBoolean)
	character spacing (setWidth: 0.3 / 0.5 / 0.7)
	color (color: aColorValue)
	etc...
--------------------------------------------------

Program 17-1 handles these attributes.


Program-17-1: (FontDescription, CharacterAttributes, Screen, FontPolicy;
boldness, italic:, underline:, outline:, shadow:, setWidth:, color:)
---------------------------------------------------------
| fontDescription characterAttributes textAttributes
  deviceFont composedText activeWindow displayBlock |
fontDescription := FontDescription new.
fontDescription family: 'times'.
fontDescription pixelSize: 18.
characterAttributes := CharacterAttributes newWithDefaultAttributes.
characterAttributes setDefaultQuery: fontDescription.
textAttributes := TextAttributes default.
textAttributes setCharacterAttributes: characterAttributes.
deviceFont := Screen default defaultFontPolicy
			findFont: textAttributes defaultFont.
textAttributes lineGrid: deviceFont height.
textAttributes baseline: deviceFont ascent.
activeWindow := ScheduledControllers activeController view.
displayBlock := 
	[activeWindow clear.
	Screen default defaultFontPolicy searchForFont: fontDescription allowance: 9.
	composedText := ComposedText
				withText: 'Smalltalk' asText
				style: textAttributes.
	composedText displayOn: activeWindow graphicsContext.
	activeWindow sensor waitClickButton].
displayBlock value.
fontDescription boldness: 0.7.
displayBlock value.
fontDescription italic: true.
displayBlock value.
fontDescription underline: true.
displayBlock value.
fontDescription outline: true.
displayBlock value.
fontDescription shadow: true.
displayBlock value.
fontDescription setWidth: 0.7.
displayBlock value.
fontDescription color: ColorValue red.
displayBlock value.
activeWindow display
-----------------------------------------------------------------

Fonts are platform dependent, so you may need to adjust the font name (family) and size (pixelSize) in order to find a match on your system. Note that you can specify wild cards like '*' or '#'. EngiFontModel in Appendix 07 lets you see these attributes. 'file in' this program and execute example 1. You will see two windows containing the following views:

-----------------------------------------
	a font displaying view
	a list view for font names
	a list view for font sizes
	3 toggle buttons for bold
	a switch button for italic
	a switch button for underline
	a switch button for outline
	a switch button for shadow
	3 toggle buttons for character spacing
-----------------------------------------

Radio buttons are used to select one of many, and switch buttons are used for boolean options. You can select a font name in the list view or click buttons to change the contents of the font display view. The red button of the font display view leads to a pop up menu to choose one of a predefined set of colors. The yellow button in the font display view leads to the color picker which you made in the 'EngiColorModel' section.

The two windows are linked to the same model so that when you press any button or select any item in the list view, both windows are updated. 13 objects in the window all depend on an instance of 'EngiFontModel'. If you open two windows, 26 objects depend on an instance of 'EngiFontModel'. This is an example of viewing a model through more than one view and controller. 'EngiFontModel' is a kind of 'EngiVariable' which is a kind of 'Model' which is a kind of 'Object'. It has a 'value' because it inherits from 'EngiVariable'.

Messages for parent previous next question
Copyright (C) 1994-1996 by Atsushi Aoki
Translated by Kaoru Rin Hayashi & Brent N. Reeves