parent previous next question (Smalltalk Textbook 14)

ComposedText

'ComposedText' is the class we'll study in this section. 'ComposedText' is a set of 'Text's, 'Text' is a set of 'String's, and 'String' is a set of 'Character's. Let me describe briefly how to create instances of them and how to transform each of them.

Ways to create a 'String'
----------------------------------------------------
	1)	aString := 'Smalltalk'
	2)	aString := String fromString: 'Smalltalk'
	3)	aString := (String new: 9)
				at: 1 put: $S;
				at: 2 put: $m;
				at: 3 put: $a;
				at: 4 put: $l;
				at: 5 put: $l;
				at: 6 put: $t;
				at: 7 put: $a;
				at: 8 put: $l;
				at: 9 put: $k;
				yourself
----------------------------------------------------

There are several other ways to create an instance of 'String'. And 'String' has subclasses like 'ByteString' or 'TwoByteString'. But you must use only the 'String' class if you want to preserve cross-platform portability.

Ways to create a 'Text'
-------------------------------------------------------------
	1)	aText := Text fromString: 'Smalltalk'
	2)	aText := Text string: 'Smalltalk' emphasis: #bold
-------------------------------------------------------------

Arguments for 'emphasis:' are #italic, #serif, #sansSerif, #small, #large, #underline, #strikeout, (#color->aColorValue) and #bold. Program 14-1 displays a red 'Smalltalk' for example.


Program-14-1: (Text, ColorValue; red, string:emphasis:)
--------------------------------------------------------------
| aText activeWindow |
aText := Text
		string: 'Smalltalk'
		emphasis: (#color -> ColorValue red).
activeWindow := ScheduledControllers activeController view.
aText displayOn: activeWindow graphicsContext at: 100 @ 100
--------------------------------------------------------------
Ways to create a 'ComposedText'.
------------------------------------------------------------
	1)	aComposedText := ComposedText withText: aText
	2)	aComposedText := ComposedText
					withText: aText
					style: aTextAttributes
	3)	aComposedText := ComposedText
					withText: aText
					style: aTextAttributes
					compositionWidth: anInteger
------------------------------------------------------------
Ways to transform
----------------------------------------------------------
	1)	aText := aString asText
	2)	aComposedText := aString asComposedText
	3)	aComposedText := aString asParagraph
	4)	aString := aText asString
	5)	aComposedText := aText asComposedText
	6)	aComposedText := aText asParagraph
	7)	aString := aComposedText asString
	8)	aText := aComposedText asText
----------------------------------------------------------

Let's examine these in detail. The difference between 'Text' and 'ComposedText' is that 'ComposedText' has 'TextAttributes' and can be justified. You can specify indentation, line spacing, tab and so on as 'TextAttributes'.

Program 14-2 illustrates how to indent a 'ComposedText'. 'firstIndent' indents just the first line. 'restIndent' indents all lines except the first. 'rightIndent', as expected, indents the right margin.


Program-14-2: (Text, String, TextAttributes; text attributes, 
firstIndent:, restIndent:, rightIndent:, displayRectangularBorder:)
-------------------------------------------------------------
| activeWindow activeSensor graphicsContext aString
  aTextAttributes aComposedText |
activeWindow := ScheduledControllers activeController view.
activeSensor := activeWindow sensor.
graphicsContext := activeWindow graphicsContext.
aString := 'Smalltalk Family
VisualWorks
ObjectWorks
Smalltalk/V
SmalltalkAgents
GNU Smalltalk'.
#(1 2 3 4 5 )
	do: 
		[:each | 
		activeWindow clear.
		aTextAttributes := TextAttributes default.
		aTextAttributes firstIndent: each * 8.
		aTextAttributes restIndent: each * 4.
		aTextAttributes rightIndent: each * 6.
		aComposedText := ComposedText
					withText: aString asText
					style: aTextAttributes.
		aComposedText displayOn: graphicsContext.
		graphicsContext
			displayRectangularBorder: aComposedText bounds.
		activeSensor waitClickButton].
activeWindow display
-------------------------------------------------------------

Program 14-3 shows how to specify line spacing, which increases with each mouse click.


Program-14-3: (Text, String, TextAttributes; text attributes, 
gridWithLead:, line spacing)
-------------------------------------------------------------
| activeWindow activeSensor graphicsContext aString aComposedText |
activeWindow := ScheduledControllers activeController view.
activeSensor := activeWindow sensor.
graphicsContext := activeWindow graphicsContext.
aString := 'Smalltalk Family
VisualWorks
ObjectWorks
Smalltalk/V
SmalltalkAgents
GNU Smalltalk'.
#(1 2 3 4 5 )
	do: 
		[:each | 
		activeWindow clear.
		aComposedText := ComposedText withText: aString asText.
		aComposedText gridWithLead: each.
		aComposedText displayOn: graphicsContext.
		graphicsContext
			displayRectangularBorder: aComposedText bounds.
		activeSensor waitClickButton].
activeWindow display
-------------------------------------------------------------

Program 14-4 uses tabs which increase from 8 pixels to 16, 24, 32, and 40. You specify a set of integers which indicate tab positions for 'useTabs:'. Using an 'Interval' is the typical way to specify a range of integers.


Program-14-4: (Text, String, TextAttributes, ComposedText; text attributes, 
useTabs:, withText:style:)
-------------------------------------------------------------
| activeWindow activeSensor graphicsContext aString
  aTextAttributes aComposedText |
activeWindow := ScheduledControllers activeController view.
activeSensor := activeWindow sensor.
graphicsContext := activeWindow graphicsContext.
aString := 'Smalltalk Family
	VisualWorks
		ObjectWorks
			Smalltalk/V
				SmalltalkAgents
					GNU Smalltalk'.
#(8 to: 40 by: 8)
	do: 
		[:each | 
		activeWindow clear.
		aTextAttributes := TextAttributes default.
		aTextAttributes useTabs: (each to: 1000 by: each).
		aComposedText := ComposedText
					withText: aString asText
					style: aTextAttributes.
		aComposedText displayOn: graphicsContext.
		graphicsContext
			displayRectangularBorder: aComposedText bounds.
		activeSensor waitClickButton].
activeWindow display
-------------------------------------------------------------

Program 14-5 aligns a 'Text' as 'leftFlush', 'centered', 'rightFlush', and 'justified'.


Program-14-5: (Text, String, TextAttributes, ComposedText; text 
attributes, withText:style:compositionWidth:, perform:, leftFlush, 
centered, rightFlush, justified)
-------------------------------------------------------------
| activeWindow activeSensor graphicsContext aString aComposedText |
activeWindow := ScheduledControllers activeController view.
activeSensor := activeWindow sensor.
graphicsContext := activeWindow graphicsContext.
aString := 'Smalltalk Family --- '.
aString := aString , 'VisualWorks (PPS), '.
aString := aString , 'ObjectWorks (PPS), '.
aString := aString , 'Smalltalk/V (Digitalk), '.
aString := aString , 'SmalltalkAgents (QKS), '.
aString := aString , 'GNU Smalltalk (GNU)'.
#(#leftFlush #centered #rightFlush #justified )
	do: 
		[:alignmentSelector | 
		activeWindow clear.
		aComposedText := ComposedText
					withText: aString asText
					style: TextAttributes default
					compositionWidth: 200.
		aComposedText perform: alignmentSelector.
		aComposedText displayOn: graphicsContext.
		graphicsContext
			displayRectangularBorder: aComposedText bounds.
		activeSensor waitClickButton].
activeWindow display
-------------------------------------------------------------

If you do not see the difference of parameters clearly, change the value of 'compositionWidth:' and try it again. In the next section we'll continue with ComposedText and describe type face (Font, Size, Style etc.).


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