From d7e0a619c4ada730d490563d4090638cc7201150 Mon Sep 17 00:00:00 2001 From: hilaire Date: Fri, 27 Sep 2019 10:19:58 +0200 Subject: [PATCH] #4712 On TextAlignement, TextAttribute, TextColor --- src/Text-Core/TextAlignment.class.st | 30 ++++++++++++++------ src/Text-Core/TextAttribute.class.st | 17 ++++++++++-- src/Text-Tests/TextAlignmentTest.class.st | 34 +++++++++++++++++++++++ src/Text-Tests/TextColorTest.class.st | 6 +++- 4 files changed, 75 insertions(+), 12 deletions(-) diff --git a/src/Text-Core/TextAlignment.class.st b/src/Text-Core/TextAlignment.class.st index 5bdeaf22421..53ea52da48f 100644 --- a/src/Text-Core/TextAlignment.class.st +++ b/src/Text-Core/TextAlignment.class.st @@ -1,16 +1,28 @@ " I'm a Text attribute that tells how content should be aligned. +----- exemple 1 --------------- TextMorph new - newContents: (Text streamContents: [:aStream| - aStream - nextPutAll: 'Left flush' asText; - cr; - nextPutAll: ('Centered' asText addAttribute: TextAlignment centered); - cr; - nextPutAll: ('Right flush' asText addAttribute: TextAlignment rightFlush); - cr ]); - openInWindowLabeled: 'TextAlignment demo' + newContents: (Text streamContents: [:aStream| + aStream + nextPutAll: 'Left flush' asText; + cr; + nextPutAll: ('Centered' asText addAttribute: TextAlignment centered); + cr; + nextPutAll: ('Right flush' asText addAttribute: TextAlignment rightFlush); + cr ]); + openInWindowLabeled: 'TextAlignment demo' +----- exemple 2 --------------- +| stream | +stream := TextStream on: (Text new: 100). +stream + nextPutAll: 'Pharo'; cr; + withAttribute: TextAlignment centered do: [stream nextPutAll: 'is' ]; cr; + withAttribute: TextAlignment rightFlush do: [ stream nextPutAll: 'cool ']; cr; + withAttribute: TextAlignment justified do: [ stream nextPutAll: 'because it is both an interactive programming enviornment and a reflexive programming language.' ]. +TextMorph new + newContents: stream contents; + openInWindow . " Class { #name : #TextAlignment, diff --git a/src/Text-Core/TextAttribute.class.st b/src/Text-Core/TextAttribute.class.st index cd4424cc9f1..16009bac0ca 100644 --- a/src/Text-Core/TextAttribute.class.st +++ b/src/Text-Core/TextAttribute.class.st @@ -1,6 +1,19 @@ " -Tells a piece of text to be a certain way. - +I am an abstract class to represent a text attribute, the way a portion of text is graphically represented. +My sub-classes specify these representations: +- colouring (TextColor), +- bold, italic, underlined, +- narrow, strike out (TextEmphasis), +- alignment (TextAlignment), +- indentation (TextIndent), +- font change (TextFontChange), +- kerning (TextKern), +- url and related action (TextAction hierarchy). +- morph (TextAnchor) + +My instances are stored in the Text instance runs variable (a RunArray). + +------------8<----------------------------------------------------------------------------------------- Select text, press Command-6, choose a attribute. If selected text is of the form Hi There the part in angle brackets is saved for action, and the Hi There appears in the paragraph. If selection has no angle brackets, use the whole thing as both the text and the action. diff --git a/src/Text-Tests/TextAlignmentTest.class.st b/src/Text-Tests/TextAlignmentTest.class.st index 1605e71ebe1..c99e7d453ed 100644 --- a/src/Text-Tests/TextAlignmentTest.class.st +++ b/src/Text-Tests/TextAlignmentTest.class.st @@ -15,3 +15,37 @@ TextAlignmentTest >> setUp [ add: TextAlignment leftFlush; add: TextAlignment rightFlush ] + +{ #category : #tests } +TextAlignmentTest >> testAlignment [ + | attribute | + self assert: TextAlignment leftFlush alignment equals: TextConstants LeftFlush. + self assert: TextAlignment rightFlush alignment equals: TextConstants RightFlush. + self assert: TextAlignment centered alignment equals: TextConstants Centered . + self assert: TextAlignment justified alignment equals: TextConstants Justified . + + attribute := TextAlignment leftFlush. + attribute alignment: TextConstants RightFlush. + self assert: attribute alignment equals: TextConstants RightFlush +] + +{ #category : #tests } +TextAlignmentTest >> testDominates [ + | text | + self assert: (TextAlignment centered dominates: TextAlignment rightFlush). + self assert: (TextAlignment centered dominates: TextAlignment centered). + self deny: (TextAlignment centered dominates: TextEmphasis bold). + self deny: (TextAlignment centered dominates: TextFontChange font2). + "In action" + text := 'Pharo is cool' asText. + text addAttribute: TextAlignment centered. + "Only one alignement time, the last added alignement win" + text addAttribute: TextAlignment justified . + self assert: (text attributesAt: 1) size equals: 1. + self assert: (text attributesAt: 1) first equals: TextAlignment justified. + "Can be both aligned and bold" + text addAttribute: TextEmphasis bold. + self assert: (text attributesAt: 1) size equals: 2. + self assert: (text attributesAt: 1) first equals: TextAlignment justified. + self assert: (text attributesAt: 2) second emphasisCode equals: 1 +] diff --git a/src/Text-Tests/TextColorTest.class.st b/src/Text-Tests/TextColorTest.class.st index 1fd06137fb1..d16fa791f54 100644 --- a/src/Text-Tests/TextColorTest.class.st +++ b/src/Text-Tests/TextColorTest.class.st @@ -7,10 +7,14 @@ Class { { #category : #tests } TextColorTest >> testColor [ | textColor | - textColor := TextColor new color: Color red. + textColor := TextColor color: Color red. self assert: textColor color equals: Color red. self assert: textColor asColor equals: Color red. + textColor color: Color yellow. + self assert: textColor color equals: Color yellow. + self assert: textColor asColor equals: Color yellow. + ] { #category : #tests }