Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
On TextAlignement, TextAttribute, TextColor
  • Loading branch information
hilaire committed Sep 27, 2019
1 parent beb2724 commit d7e0a61
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
30 changes: 21 additions & 9 deletions 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,
Expand Down
17 changes: 15 additions & 2 deletions 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<Smalltalk beep>
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.
Expand Down
34 changes: 34 additions & 0 deletions src/Text-Tests/TextAlignmentTest.class.st
Expand Up @@ -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
]
6 changes: 5 additions & 1 deletion src/Text-Tests/TextColorTest.class.st
Expand Up @@ -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 }
Expand Down

0 comments on commit d7e0a61

Please sign in to comment.