'From Cuis 1.0 of 27 August 2009 [latest update: #282] on 3 September 2009 at 10:21:19 am'!

!Text methodsFor: 'emphasis' stamp: 'jmv 9/3/2009 09:40'!
withAttributeValues: attributes do: aBlock
	"Evaluate aBlock with the values of various attributes applied in the correct order
	The order is (each overwriting the previous one)
	1) initialStyle
	2) TextStyleReferene
	3) CharacterStyleReference
	4) TextFontReference
	5) TextEmphasis"

	| textStyle characterStyle font emphasis alignment color kern |
	textStyle _ initialStyle.	"First candidate as the textStyle"
	emphasis _ 0.
	kern _ 0.
	color _ nil.
	
	"TextStyle is the first to set several values"
	attributes do: [ :attribute |
		attribute forTextStyleReferenceDo: [ :s | 
			textStyle _ s]].
	font _ textStyle font.
	alignment _ textStyle alignment.
	textStyle color ifNotNil: [ :c | color _ c ].
	
	"CharacterStyle, if present, can override font and color"
	attributes do: [ :attribute |
		attribute forCharacterStyleReferenceDo: [ :s | 
			characterStyle _ s.
			font _ characterStyle font.
			characterStyle color ifNotNil: [ :c | color _ c ]]].
	
	"Hardcoded color for TextAction comes before TextColor"
	attributes do: [ :attribute |
		attribute forTextActionInfoDo: [ :info | color _ TextAction purple ]].

	"These will not interfere with each other, and all of them take precedence over previous values"
	attributes do: [ :attribute |
		attribute forFontReferenceDo: [ :f | font _ f ].
		attribute forTextEmphasisDo: [ :e | emphasis _ emphasis bitOr: e ].
		attribute forTextColorDo: [ :c | color _ c ].
		attribute forTextAlignmentDo: [ :a | alignment _ a ].
		attribute forTextKernDo: [ :k | kern _ kern + k ].
	].

	"Finally, the font must include any specified emphasis"
	font _ font emphasized: emphasis.
	
	"Done. Now evaluate the block."
	aBlock valueWithArguments: {font. color. alignment. textStyle. characterStyle. kern }! !

