Cairo__ScaledFont.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 16 Feb 2016 07:46:52 +0000
changeset 39 8af34937e1ec
parent 34 97705b5a9411
child 40 28dfc583beb5
permissions -rw-r--r--
More work for using CairoGrahicsContext for rendering views * Added GraphicsMedium>>cairoify to change from device rendering to Cairo rendering. * Handle lineWidth: 0 specially as it actually means lineWidth = 1. * Small cleanup / fixes in text displaying (this would need more work, though)

"{ Package: 'stx:goodies/libcairo' }"

"{ NameSpace: Cairo }"

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

    OperatingSystem isMSWINDOWSlike ifTrue:[
        ^ #( 'C:\Windows' 'C:\Windows\System32' "Wild guess, should not harm" )
    ].

    OperatingSystem isUNIXlike ifTrue:[
        OperatingSystem getSystemType == #linux ifTrue:[
            | path |

            path := #( '/lib' '/usr/lib' '/usr/local/lib' ).
            (OperatingSystem getSystemInfo at:#machine) = 'x86_64' ifTrue:[
                "If the machine is 64bit, prepend standard path for 32bit libs.
                 Leave standard paths at the end, as the system might be completely 
                 32bit but running on 64bit-capable CPU.

                CAVEAT: This is bit dangerous, as on 64bit OS, if ia32 libs are
                not installed byt 64bit sqlite libs are, then 64bit libs are found
                and when a function is called, segfault will occur!!

                Q: Is there a way how to figure out if the OS itself is 32bit,
                regardles on CPU?"
                path := #( '/lib32' '/usr/lib32' '/usr/local/lib32' ) , path.
            ].
            ^path

        ].
    ].

    self error:'Unsupported operating system'

    "
        SqliteLibrary dllPath
    "

    "Created: / 31-08-2011 / 18:02:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

libraryName

    OperatingSystem isUNIXlike ifTrue:[^'libcairo.so.2'].

    OperatingSystem isMSWINDOWSlike ifTrue:[^'cairo.dll'].

    self error:'Library name for host OS is not known'
!

sizeof
    "Returns size of undelaying structure in bytes"

    ^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
    ^'$Id$'
!

version_HG
    ^ '$Changeset: <not expanded> $'
! !


ScaledFont initialize!