'From Cuis 1.0 of 21 August 2009 [latest update: #268] on 27 August 2009 at 1:11:32 pm'!

!CharacterScanner methodsFor: 'private' stamp: 'jmv 8/26/2009 15:28'!
setFont
	| priorFont c |
	"Set the font and other emphasis.
	In fact set actual TextStyle, alignment, font and emphasis"
	priorFont _ font.
	text ifNotNil: [
		font _ nil.
		emphasisCode _ 0.
		kern _ 0.
		actualTextStyle _ text initialStyle.
		(c _ actualTextStyle color) ifNotNil: [
			self textColor: c ].
		alignment _ actualTextStyle alignment.
		(text attributesAt: lastIndex)
			do: [:att | att emphasizeScanner: self]].
	font ifNil: [
		self setActualFont: actualTextStyle font].
	font _ font emphasized: emphasisCode.
	priorFont ifNotNil: [ destX _ destX + priorFont descentKern ].
	destX _ destX - font descentKern.
	kern _ kern - font baseKern.

	"Install various parameters from the font."
	spaceWidth _ font widthOf: Character space.
	xTable _ font xTable.
	map _ font characterToGlyphMap.
	stopConditions _ DefaultStopConditions.! !


!CompositionScanner methodsFor: 'scanning' stamp: 'jmv 8/26/2009 15:21'!
composeFrom: startIndex inRectangle: lineRectangle firstLine: firstLine leftSide: leftSide rightSide: rightSide

	"Answer an instance of TextLineInterval that represents the next line in the paragraph."
	| runLength done stopCondition xtraSpaceBefore spaceAfterParagraph |
	
	lastIndex _ startIndex.	"scanning sets last index"
	destY _ lineRectangle top.
	lineHeight _ baseline _ 0.  "Will be increased by setFont"
	self setStopConditions.	"also sets font, style, etc"

	"Set up margins"
	leftMargin _ lineRectangle left.
	leftSide ifTrue: [
		leftMargin _ leftMargin +
			(firstLine ifTrue: [actualTextStyle firstIndent]
					ifFalse: [actualTextStyle restIndent])].
	destX _ spaceX _ leftMargin.
	rightMargin _ lineRectangle right.
	rightSide ifTrue: [
		rightMargin _ rightMargin - actualTextStyle rightIndent].

	runLength _ text runLengthFor: startIndex.
	runStopIndex _ (lastIndex _ startIndex) + (runLength - 1).
	line _ (TextLine start: lastIndex stop: 0 internalSpaces: 0 paddingWidth: 0)
				rectangle: lineRectangle.
	spaceCount _ 0.
	leftMargin _ destX.
	line leftMargin: leftMargin.

	done _ false.
	xtraSpaceBefore _ firstLine
		ifTrue: [ actualTextStyle paragraphSpacingBefore ]
		ifFalse: [ 0 ].
	spaceAfterParagraph _ actualTextStyle paragraphSpacingAfter.
	[ done ]
		whileFalse: [
			stopCondition _ self scanCharactersFrom: lastIndex to: runStopIndex
				in: text string rightX: rightMargin stopConditions: stopConditions
				kern: kern.
			"See setStopConditions for stopping conditions for composing."
			(self perform: stopCondition) ifTrue: [
				^ line 
					lineHeight: lineHeight + xtraSpaceBefore + 
						(stopCondition = #cr ifTrue: [spaceAfterParagraph] ifFalse: [0]) 
					baseline: baseline + xtraSpaceBefore ]]! !

