Font.st
changeset 5167 39ac1126dc6b
parent 5146 286f1473a06b
child 5170 4646aa850bf2
--- a/Font.st	Tue Feb 17 18:37:01 2009 +0100
+++ b/Font.st	Tue Feb 17 18:37:09 2009 +0100
@@ -230,6 +230,20 @@
     "returns a font for given family, face, style, size and encoding. 
      The returned font is not associated to a specific device"
 
+    ^ self
+        family:familyString 
+        face:faceString 
+        style:styleString 
+        size:sizeNum
+        pixelSize:nil
+        encoding:encodingSym
+!
+
+family:familyString face:faceString style:styleString size:sizeArgOrNil pixelSize:pixelSizeArgOrNil encoding:encodingSym
+    "returns a font for given family, face, style, size and encoding. 
+     The returned font is not associated to a specific device.
+     Either size, or pixelSize must be non-nil (points vs. pixels)"
+
     |family newFont|
 
     (familyString at:1) isUppercase ifTrue:[
@@ -238,34 +252,25 @@
         family := familyString
     ].
 
-    "look if this font is already known on the default device
-     (the most common case)"
-
-    Display notNil ifTrue:[
-        Display deviceFonts do:[:aFont |
-            (family = aFont family) ifTrue:[
-                (faceString = aFont face) ifTrue:[
-                    (styleString = aFont style
-                    or:[styleString = 'italic' and:[aFont style = 'oblique']]) ifTrue:[
-                         (aFont size = sizeNum) ifTrue:[
-                            (encodingSym isNil or:[aFont encoding = encodingSym]) ifTrue:[
-                                ^ aFont
-                            ]
-                        ]
-                    ]
-                ]
-            ]
-        ]
-    ].
-
     newFont := self basicNew 
                     setFamily:familyString
                     face:faceString
                     style:styleString
-                    size:sizeNum
+                    size:sizeArgOrNil
+                    pixelSize:pixelSizeArgOrNil
                     encoding:encodingSym
                     device:nil.
 
+    "look if this font is already known on the default device (the most common case)"
+
+    Display notNil ifTrue:[
+        Display deviceFonts do:[:aFont |
+            (newFont sameDeviceFontAs:aFont) ifTrue:[
+                ^ aFont
+            ]
+        ]
+    ].
+
     ^ newFont
 
     "Modified: / 26.9.1999 / 13:49:45 / cg"
@@ -336,7 +341,7 @@
     "create a new Font representing the same font as
      myself on aDevice; if one already exists, return the one."
 
-    |newFont rep nearestFont|
+    |newFont rep|
 
     "if I am already assigned to that device ..."
     (device == aDevice) ifTrue:[^ self].
@@ -344,26 +349,9 @@
     aDevice isNil ifTrue:[^ self].
 
     aDevice deviceFonts do:[:aFont |
-        (family = aFont family) ifTrue:[
-            (face = aFont face) ifTrue:[
-                (style = aFont style) ifTrue:[
-                    (encoding isNil or:[encoding = aFont encoding]) ifTrue:[
-                        (size = aFont size) ifTrue:[
-                            ^ aFont
-                        ].
-"/                        nearestFont isNil ifTrue:[
-"/                            "font exists, but has different size,
-"/                             remember the font with the nearest size"
-"/                            nearestFont = aFont.
-"/                        ] ifFalse:[
-"/                            ((nearestFont size - size) abs > (aFont size - size) abs) ifTrue:[
-"/                                nearestFont := aFont.
-"/                            ].
-"/                        ].
-                    ]
-                ]
-            ]
-        ]
+        (self sameDeviceFontAs:aFont) ifTrue:[
+            ^ aFont
+        ].
     ].
 
     newFont := self onDevice:aDevice ifAbsent:nil.
@@ -387,7 +375,13 @@
         ].
 
         newFont := (self class basicNew)
-                     setFamily:family face:face style:style size:size encoding:encoding device:aDevice.
+                        setFamily:family 
+                        face:face 
+                        style:style 
+                        size:size 
+                        pixelSize:pixelSize 
+                        encoding:encoding 
+                        device:aDevice.
         newFont setReplacementFont:rep.
         aDevice registerFont:newFont.
         ^ newFont
@@ -412,7 +406,13 @@
     "receiver was not associated - do it now"
     device isNil ifTrue:[
         "ask that device for the font"
-        id := aDevice getFontWithFamily:family face:face style:style size:size encoding:encoding.
+        id := aDevice 
+                getFontWithFamily:family 
+                face:face 
+                style:style 
+                size:size 
+                pixelSize:pixelSize 
+                encoding:encoding.
         id isNil ifTrue:[
             "oops did not work - (device has no such font)"
 
@@ -442,11 +442,13 @@
 
     trySize := size - 1.
     [
-        id := aDevice getFontWithFamily:family
-                                   face:face
-                                  style:style 
-                                   size:trySize
-                               encoding:encoding.
+        id := aDevice 
+                getFontWithFamily:family
+                face:face
+                style:style 
+                size:trySize
+                pixelSize:pixelSize
+                encoding:encoding.
     ] doWhile:[id isNil and:[trySize := trySize - 1. trySize > 4]].
 
     id notNil ifTrue:[
@@ -456,11 +458,13 @@
         alternative notNil ifTrue:[
             trySize := size - 1.
             [
-                id := aDevice getFontWithFamily:alternative
-                                           face:face
-                                          style:style 
-                                           size:trySize
-                                       encoding:encoding.
+                id := aDevice 
+                        getFontWithFamily:alternative
+                        face:face
+                        style:style 
+                        size:trySize
+                        pixelSize:pixelSize
+                        encoding:encoding.
             ] doWhile:[id isNil and:[trySize := trySize - 1. trySize > 4]].
         ].
         id notNil ifTrue:[
@@ -479,22 +483,14 @@
     "/ if there already is a device font for that replacement, return that one
     "/ to avoid allocating multiple replacements for the same font
     aDevice deviceFonts do:[:aFont |
-        (family = aFont family) ifTrue:[
-            (face = aFont face) ifTrue:[
-                (style = aFont style) ifTrue:[
-                    (encoding isNil or:[encoding = aFont encoding]) ifTrue:[
-                        (size = aFont size) ifTrue:[
-                            ^ aFont
-                        ].
-                    ]
-                ]
-            ]
+        (self sameDeviceFontAs:aFont) ifTrue:[
+            ^ aFont
         ]
     ].
 
     f := self class basicNew.
 
-    f setFamily:family face:face style:style size:size encoding:encoding device:aDevice.
+    f setFamily:family face:face style:style size:size pixelSize:pixelSize encoding:encoding device:aDevice.
     f setDevice:aDevice fontId:id.
     f getFontInfos.
     aDevice registerFont:f.
@@ -685,6 +681,20 @@
     "Modified: 20.4.1996 / 23:26:56 / cg"
 !
 
+setFamily:familyString face:faceString style:styleString size:sizeArgOrNil pixelSize:pixelSizeArgOrNil encoding:encodingSym device:aDevice
+    "set my instance attributes"
+
+    family := familyString.
+    face := faceString.
+    style := styleString.
+    size := sizeArgOrNil.
+    pixelSize := pixelSizeArgOrNil.
+    encoding := encodingSym.
+    device := aDevice
+
+    "Modified: 20.4.1996 / 23:26:56 / cg"
+!
+
 setFontId:aFontId
     "set my font handle"
 
@@ -1185,7 +1195,7 @@
 !Font class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Font.st,v 1.107 2009-01-23 09:36:45 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Font.st,v 1.108 2009-02-17 17:37:09 cg Exp $'
 ! !
 
 Font initialize!