Text.st
changeset 3689 b927cd41a141
parent 3662 e11af67c06f9
child 3784 25519ee4239d
--- a/Text.st	Wed Jan 13 16:07:35 2016 +0100
+++ b/Text.st	Sat Jan 16 13:50:11 2016 +0100
@@ -21,7 +21,7 @@
 		BoldOverlineEmphasis BoldUnderwaveEmphasis
 		ItalicUnderlineEmphasis ItalicUnderwaveEmphasis
 		UnderlineColorEmphasis StrikeoutColorEmphasis EtchColorEmphasis
-		FontEmphasis'
+		FontEmphasis Superscript Subscript'
 	poolDictionaries:''
 	category:'Collections-Text'
 !
@@ -46,10 +46,11 @@
 documentation
 "
     Texts add emphasis information to a string.
+    
     Texts and strings should behave interchanchably to the outside
     world, except that texts keep per-character emphasis information.
-    (strings return nil, when asked for an elements emphasis).
-    Use #string, to get a texts underlying string without any emphasis
+    (strings return nil, when asked for an element's emphasis).
+    Use #string, to get a text's underlying plain string without any emphasis
     information.
 
     Currently, the following attributes are supported:
@@ -66,7 +67,7 @@
     Attributes may be combined (pass an array of above) as emphasis.
     See examples.
 
-    This class is not yet fully implemented - being constructed.
+    This class is a hack and may need some massage.
 
     [author:]
         Claus Gittinger
@@ -78,51 +79,84 @@
 
 examples
 "
+  In a textView:
+                                                                        [exBegin]
+    |t v|
+
+    t := 'The quick brown fox jumps over the lazy dog' asText.
+    t emphasizeFrom:(t findString:'quick') 
+      count:5 with:#bold.
+      
+    t emphasizeFrom:(t findString:'brown') 
+      count:9 with:(Array with:#color->(Color name:'brown')
+                                                     with:#bold).
+    t emphasizeFrom:(t findString:'lazy') 
+      count:4 with:(Array with:#color->(Color red)
+                                                      with:#italic).
+    t emphasizeFrom:(t findString:'dog') 
+      count:3 with:#underline.
+
+    v := HVScrollableView for:EditTextView.
+    v contents:t.
+
+    v width:450.
+    v open.
+                                                                        [exEnd]
+
+                                                                        [exBegin]
+    |t v|
+    
+    t := 'a',('1' emphasizeAllWith:#subscript),'x',('3' emphasizeAllWith:#superscript)
+         ,'a',('2' emphasizeAllWith:#subscript),'x',('2' emphasizeAllWith:#superscript)
+         ,'a',('3' emphasizeAllWith:#subscript),'x',('n' emphasizeAllWith:#superscript).
+
+    Dialog information:t.
+                                                                        [exEnd]
+
   plain string (for comparison):
                                                                         [exBegin]
-    Dialog
-        warn:'hello'
+    Dialog information:'hello'
                                                                         [exEnd]
 
 
   emphasized strings as dialog titles:
                                                                         [exBegin]
-    Dialog
-        warn:((Text string:'hello') allBold)
+    Dialog 
+        information:((Text string:'hello') allBold)
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' emphasis:#italic)
+        information:(Text string:'hello' emphasis:#italic)
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' emphasis:#(underline))
+        information:(Text string:'hello' emphasis:#(underline))
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' emphasis:#(underwave))
+        information:(Text string:'hello' emphasis:#(underwave))
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' emphasis:#(bold underline))
+        information:(Text string:'hello' emphasis:#(bold underline))
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' 
+        information:(Text string:'hello' 
                  emphasis:(Array with:#bold
                                  with:#strikeout
                                  with:(#color->Color red)))
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' 
+        information:(Text string:'hello' 
                  emphasis:(Array with:(#color->Color black)
                                  with:#underwave
                                  with:(#underlineColor->Color red)))
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' 
+        information:(Text string:'hello' 
                  emphasis:(Array with:#bold
                                  with:#strikeout
                                  with:(#color->Color red)
@@ -130,30 +164,10 @@
                                                                         [exEnd]
                                                                         [exBegin]
     Dialog
-        warn:(Text string:'hello' color:(Color red))
+        information:(Text string:'hello' color:(Color red))
                                                                         [exEnd]
 
 
-  in an editTextView:
-                                                                        [exBegin]
-    |t v|
-
-    t := 'The quick brown fox jumps over the lazy dog' asText.
-    t emphasizeFrom:(t findString:'quick') count:5 with:#bold.
-    t emphasizeFrom:(t findString:'brown') count:9 
-                                         with:(Array with:#color->(Color name:'brown')
-                                                     with:#bold).
-    t emphasizeFrom:(t findString:'lazy') count:4 
-                                          with:(Array with:#color->(Color red)
-                                                      with:#italic).
-    t emphasizeFrom:(t findString:'dog') count:3 with:#underline.
-
-    v := HVScrollableView for:EditTextView.
-    v contents:t.
-
-    v width:450.
-    v open.
-                                                                        [exEnd]
 "
 ! !
 
@@ -188,6 +202,8 @@
     StrikeoutColorEmphasis := #strikeoutColor. 
     EtchColorEmphasis := #etchColor.
     FontEmphasis := #font.
+    Superscript := #superscript.
+    Subscript := #subscript.
 
     "
      Text initialize
@@ -746,16 +762,17 @@
 
 !Text methodsFor:'displaying'!
 
-displayOn:aGC x:x0 y:y opaque:opaqueWanted
-    "display the receiver on a GC"
+displayOn:aGC x:x0 y:yBase opaque:opaqueWanted
+    "display the receiver on a GC.
+     This is one of the ugliest pieces of code..."
 
     |savedFont savedPaint savedFgPaint savedBgPaint font color boldFont italicFont boldItalicFont 
      bgPaint etchColor ulPaint strikePaint
      wasItalic overline altFont
-     bold italic underline underwave strikeout reverse
+     bold italic underline underwave strikeout reverse subOrSuperscript
      pos    "{ Class: SmallInteger }"
      endPos "{ Class: SmallInteger }"
-     x      
+     x y    
      l      "{ Class: SmallInteger }"
      yL k value device opaque|
 
@@ -779,9 +796,10 @@
         wasItalic := italic.
         color := savedPaint.
         bold := italic := underline := underwave := strikeout := reverse := false.
-        altFont := nil.
+        altFont := subOrSuperscript := nil.
         bgPaint := savedBgPaint.
-
+        y := yBase.
+        
         emphasis isSymbol ifTrue:[
             emphasis == BoldEmphasis ifTrue:[bold := true]
             ifFalse:[emphasis == ItalicEmphasis ifTrue:[italic := true]
@@ -795,7 +813,9 @@
             ifFalse:[emphasis == BoldUnderwaveEmphasis ifTrue:[bold := underwave := true]
             ifFalse:[emphasis == ItalicUnderlineEmphasis ifTrue:[italic := underline := true]
             ifFalse:[emphasis == ItalicUnderwaveEmphasis ifTrue:[italic := underwave := true]
-            ]]]]]]]]]]]
+            ifFalse:[emphasis == Subscript ifTrue:[subOrSuperscript := Subscript]
+            ifFalse:[emphasis == Superscript ifTrue:[subOrSuperscript := Superscript]
+            ]]]]]]]]]]]]]
         ] ifFalse:[
             (emphasis isMemberOf:Association) ifTrue:[
                 value := emphasis value.
@@ -836,6 +856,8 @@
                         ifFalse:[emphasis == BoldUnderwaveEmphasis ifTrue:[bold := underwave := true]
                         ifFalse:[entry == ItalicUnderlineEmphasis ifTrue:[italic := underline := true]
                         ifFalse:[emphasis == ItalicUnderwaveEmphasis ifTrue:[italic := underwave := true]
+                        ifFalse:[emphasis == Subscript ifTrue:[subOrSuperscript := Subscript]
+                        ifFalse:[emphasis == Superscript ifTrue:[subOrSuperscript := Superscript]
                         ifFalse:[
                             (entry isMemberOf:Association) ifTrue:[
                                 value := entry value.
@@ -863,7 +885,7 @@
                                     ]]]]]]
                                 ]
                             ]
-                        ]]]]]]]]]]]
+                        ]]]]]]]]]]]]]
                     ]
                 ]
             ]
@@ -894,6 +916,14 @@
                 ]
             ].
         ].
+        subOrSuperscript notNil ifTrue:[
+            font := font asSize:(font size * 3 // 4).
+            subOrSuperscript == Superscript ifTrue:[
+                y := y - (font height // 3).
+            ] ifFalse:[                
+                y := y + (font height // 3).
+            ].    
+        ].    
         aGC basicFont:font.
         reverse ifTrue:[
             aGC paint:bgPaint on:color.