Cairo__ScaledFont.st
changeset 34 97705b5a9411
parent 30 c8fe298c8cc7
child 40 28dfc583beb5
--- a/Cairo__ScaledFont.st	Wed Jan 07 21:49:27 2015 +0100
+++ b/Cairo__ScaledFont.st	Fri Jan 09 16:15:59 2015 +0000
@@ -2,14 +2,26 @@
 
 "{ NameSpace: Cairo }"
 
-CObject subclass:#ScaledFont
-	instanceVariableNames:''
-	classVariableNames:''
+FontDescription subclass:#ScaledFont
+	instanceVariableNames:'handle device extents'
+	classVariableNames:'Lobby RecentlyUsedFonts'
 	poolDictionaries:''
 	category:'Cairo-Objects'
 !
 
 
+!ScaledFont class methodsFor:'initialization'!
+
+initialize
+    "Invoked at system start or when the class is dynamically loaded."
+
+    "/ please change as required (and remove this comment)
+
+    Lobby := Registry new
+
+    "Modified: / 09-01-2015 / 11:22:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !ScaledFont class methodsFor:'accessing'!
 
 dllPath
@@ -65,6 +77,139 @@
     ^0
 ! !
 
+!ScaledFont methodsFor:'finalization'!
+
+finalization
+    self destroy
+
+    "Created: / 09-01-2015 / 10:20:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+finalizationLobby
+    "answer a Registry used for finalization.
+     Use a generic Registry for any object.
+     Subclasses using their own Registry should redefine this"
+
+    ^ Lobby
+
+    "Created: / 09-01-2015 / 10:20:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!ScaledFont methodsFor:'initialize'!
+
+destroy
+    "Tell Cairo library to destroy the corresponding C object.
+     Remember that object is physically destroyed only if internal
+     refcounter goes to zero. However, after calling destroy,
+     this instance should be treated as invalid."
+
+    handle notNil ifTrue:[
+        CPrimitives cairo_scaled_font_destroy: handle
+    ].
+
+    "Created: / 28-12-2014 / 22:10:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 09-01-2015 / 11:57:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+handle: anExternalAddress
+    handle := anExternalAddress.
+    CPrimitives cairo_scaled_font_reference: handle.
+    self registerForFinalization.
+    extents := FontExtents new.
+    CPrimitives cairo_scaled_font_extents: handle _: extents
+
+    "Created: / 09-01-2015 / 15:15:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+release
+    self unregisterForFinalization.
+    ^self destroy
+
+    "Created: / 28-12-2014 / 23:49:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 09-01-2015 / 10:23:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!ScaledFont methodsFor:'queries-dimensions'!
+
+ascent
+    "return the ascent - the number of pixels above the baseLine."
+
+    ^ extents ascent
+
+    "Created: / 09-01-2015 / 15:33:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+descent
+    "return the descent - the number of pixels below the baseLine."
+
+    ^ extents descent
+
+    "Created: / 09-01-2015 / 15:33:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+height
+    "return the height - the number of pixels above plus below the baseLine."
+
+    ^ extents height
+
+    "Created: / 09-01-2015 / 15:33:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+isFixedWidth
+    "return true, if this is a fixed pitch font (i.e. all characters
+     are of the same width)"
+
+    ^ false
+
+    "Created: / 09-01-2015 / 15:35:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+maxAscent
+    "return the fonts maximum-ascent (i.e. the maximum of all characters);
+     That is the number of units (usually pixels) above the baseline."
+
+    ^ self ascent
+
+    "Created: / 09-01-2015 / 15:34:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+maxDescent
+    "return the fonts maximum-descent (i.e. the maximum of all characters);
+     That is the number of units (usually pixels) below the baseline."
+
+    ^ self descent
+
+    "Created: / 09-01-2015 / 15:34:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+maxWidth
+    "return the fonts maximum-width character (i.e. the maximum of all characters);
+     That is a number of units (usually pixels)."
+
+    ^ extents maxXAdvance
+
+    "Created: / 09-01-2015 / 15:35:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+widthOf:aString from:start to:stop
+    "return the width of a sub string"
+
+    | str extents width |
+
+    (start == 1 and: [ stop == aString size ]) ifTrue:[ 
+        str := aString
+    ] ifFalse:[ 
+        str := aString copyFrom: start to: stop.
+    ].
+    extents := TextExtents new.
+    Cairo::CPrimitives cairo_scaled_font_text_extents: handle _: str utf8Encoded _: extents.
+    width := extents width.
+    extents free.
+    ^ width
+
+    "Created: / 09-01-2015 / 15:28:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !ScaledFont class methodsFor:'documentation'!
 
 version
@@ -75,3 +220,5 @@
     ^ '$Changeset: <not expanded> $'
 ! !
 
+
+ScaledFont initialize!