Cairo__GraphicsContext.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 28 Feb 2016 14:53:56 +0000
changeset 51 5293f2b851ab
parent 46 e624554ca9a3
child 54 209a2b0b721a
permissions -rw-r--r--
CairGraphicsContext: added support for displaying images with alpha channel

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

"{ NameSpace: Cairo }"

CObject subclass:#GraphicsContext
	instanceVariableNames:'surface'
	classVariableNames:''
	poolDictionaries:''
	category:'Cairo-Objects'
!

!GraphicsContext class methodsFor:'instance creation'!

onSurface: surface
    | instance |

    self
        assert: (surface isKindOf: Cairo::Surface)
        message: 'surface is not valid Cairo surface'.

    instance := CPrimitives cairo_create: surface.
    ^ instance initializeWithSurface: surface

    "Created: / 28-12-2014 / 23:45:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-02-2016 / 16:07:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GraphicsContext methodsFor:'accessing'!

referenceCount
    "Return value or reference counter"

    ^ CPrimitives cairo_get_reference_count: self.

    "Modified: / 13-02-2016 / 16:13:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

status
    ^ CPrimitives cairo_status: self

    "Created: / 18-02-2016 / 20:01:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

surface
    ^ surface
! !

!GraphicsContext methodsFor:'cairo api - paths'!

antialias
    "Gets the current shape antialiasing mode, as set Cairo::GraphicsContext>#antialias"

    ^ CPrimitives cairo_get_antialias: self

    "Created: / 24-02-2016 / 00:03:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

antialias: antialias
    "Set the antialiasing mode of the rasterizer used for drawing shapes. This 
     value is a hint, and a particular backend may or may not support a particular 
     value. At the current time, no backend supports CAIRO_ANTIALIAS_SUBPIXEL when 
     drawing shapes.

     Note that this option does not affect text rendering, instead 
     see Cairo::FontOptions>>#antialias."


    CPrimitives cairo_set_antialias: self _:antialias.
    self statusCheck.

    "Created: / 24-02-2016 / 00:02:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

arcNegativeX:x y:y radius:r from:startAngle to:stopAngle 
    CPrimitives 
        cairo_arc_negative:self
        _:x asDouble
        _:y asDouble
        _:r asDouble
        _:startAngle asDouble
        _:stopAngle asDouble.
    self statusCheck.

    "Created: / 07-01-2015 / 02:35:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:17:14 / jv"
!

arcX:x y:y radius:r from:startAngle to:stopAngle 
    CPrimitives 
        cairo_arc:self
        _:x asDouble
        _:y asDouble
        _:r asDouble
        _:startAngle asDouble
        _:stopAngle asDouble.
    self statusCheck.

    "Created: / 17-06-2012 / 21:50:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-12-2014 / 22:00:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:17:24 / jv"
!

closePath
    CPrimitives cairo_close_path:self.
    self statusCheck.

    "Created: / 01-01-2015 / 22:42:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:17:34 / jv"
!

lineCap: lc

    CPrimitives cairo_set_line_cap: self _: lc.
    self statusCheck.

    "Created: / 17-06-2012 / 22:09:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-02-2016 / 16:42:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:17:42 / jv"
!

lineToX:x y:y 
    CPrimitives 
        cairo_line_to:self
        _:x asDouble
        _:y asDouble.
    self statusCheck.

    "Created: / 17-06-2012 / 22:15:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-12-2014 / 22:00:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:17:53 / jv"
!

lineWidth: aNumber
    CPrimitives cairo_set_line_width: self _:aNumber asFloat.
    self statusCheck.

    "Created: / 13-02-2016 / 16:45:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:18:04 / jv"
!

moveToX:x y:y 
    CPrimitives 
        cairo_move_to:self
        _:x asDouble
        _:y asDouble.
    self statusCheck.

    "Created: / 23-04-2009 / 17:21:00 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 28-12-2014 / 22:00:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:18:12 / jv"
!

rectangleX:x y:y width:w height:h 
    | rx  ry  rw  rh |

    rx := x.
    ry := y.
    rw := w.
    rh := h.
    rw < 0 ifTrue:[
        rx := rx + rw.
        rw := rw abs.
    ].
    rh < 0 ifTrue:[
        ry := ry + rh.
        rh := rh abs.
    ].
    CPrimitives 
        cairo_rectangle:self
        _:rx asDouble
        _:ry asDouble
        _:rw asDouble
        _:rh asDouble.
    self statusCheck.

    "Created: / 10-07-2008 / 09:41:50 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 02-01-2015 / 01:21:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:18:25 / jv"
! !

!GraphicsContext methodsFor:'cairo api - save & restore'!

restore
    CPrimitives cairo_restore:self.
    self statusCheck.

    "Created: / 17-06-2012 / 21:51:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-02-2016 / 16:14:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:16:13 / jv"
!

save
    CPrimitives cairo_save:self.
    self statusCheck.

    "Created: / 17-06-2012 / 21:51:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-02-2016 / 16:15:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:16:19 / jv"
! !

!GraphicsContext methodsFor:'cairo api - source'!

source: aCairoPatternOrColor
    aCairoPatternOrColor isColor ifTrue:[ 
        self  sourceR: (aCairoPatternOrColor red / 100)  
                    G: (aCairoPatternOrColor green / 100)  
                    B: (aCairoPatternOrColor blue / 100)  
                    A: aCairoPatternOrColor alpha.
        ^ self.
    ].
    self notYetImplemented

    "Created: / 13-02-2016 / 16:52:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

sourceR:r G:g B:b
    CPrimitives cairo_set_source_rgb: self 
                                   _: r asDouble
                                   _: g asDouble
                                   _: b asDouble.
    self statusCheck.

    "Created: / 13-02-2016 / 16:55:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:16:01 / jv"
!

sourceR:r G:g B:b A:a
    CPrimitives cairo_set_source_rgba: self 
                                    _: r asDouble
                                    _: g asDouble
                                    _: b asDouble
                                    _: a asDouble.
    self statusCheck.

    "Created: / 13-02-2016 / 16:54:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:16:05 / jv"
!

sourceSurface: source x: x y: y
    "This is a convenience method for creating a pattern from surface
     and setting it as the source in receiver with Cairo::GraphicsContext>>#source:.

     The x and y parameters give the user-space coordinate at which the surface 
     origin should appear. (The surface origin is its upper-left corner before 
     any transformation has been applied.) The x and y parameters are negated and 
     then set as translation values in the pattern matrix.

     Other than the initial translation pattern matrix, as described above, all other 
     pattern attributes, (such as its extend mode), are set to the default values as 
     in cairo_pattern_create_for_surface(). The resulting pattern can be queried with 
     cairo_get_source() so that these attributes can be modified if desired, (eg. to 
     create a repeating pattern with cairo_pattern_set_extend())."

    #todo. "/ fix documentation ro refer to Smalltalk methids"

    CPrimitives cairo_set_source_surface: self _: source _: x asFloat _: y asFloat.
    self statusCheck

    "Created: / 27-02-2016 / 17:01:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GraphicsContext methodsFor:'cairo api - stroke & fill'!

fill
    CPrimitives cairo_fill:self.
    self statusCheck.

    "Created: / 10-07-2008 / 09:42:50 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 28-12-2014 / 22:01:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:26 / jv"
!

fillAndPreserve
    CPrimitives cairo_fill_preserve:self.
    self statusCheck.

    "Created: / 17-06-2012 / 21:52:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-12-2014 / 22:01:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:30 / jv"
!

paint
    "A drawing operator that paints the current source everywhere within 
     the current clip region."

    CPrimitives cairo_paint:self.
    self statusCheck.

    "Created: / 13-02-2016 / 16:59:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:34 / jv"
!

showPage
    "Makes sense only for PDF surfaces"
    
    CPrimitives cairo_show_page:self.
    self statusCheck.

    "Created: / 17-06-2012 / 08:44:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-12-2014 / 22:02:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:39 / jv"
!

stroke
    CPrimitives cairo_stroke:self.
    self statusCheck.

    "Created: / 10-07-2008 / 09:42:43 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 28-12-2014 / 22:02:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:44 / jv"
!

strokeAndPreserve
    CPrimitives cairo_stroke_preserve:self.
    self statusCheck.

    "Created: / 17-06-2012 / 21:52:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-12-2014 / 22:15:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:48 / jv"
! !

!GraphicsContext methodsFor:'cairo api - text'!

font
    ^ CPrimitives cairo_get_scaled_font: self

    "Created: / 17-02-2016 / 21:02:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

font: scaledFont
    CPrimitives cairo_set_scaled_font: self _: scaledFont.
    self statusCheck.

    "Created: / 17-02-2016 / 21:01:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:08:25 / jv"
!

font:family slant:slant weight:weight 
    CPrimitives cairo_select_font_face:self _:family asString _:slant asInteger _:weight asInteger.
    self statusCheck.

    "Created: / 29-12-2014 / 01:08:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:13:15 / jv"
!

fontMatrix
    | matrix |

    matrix := Matrix new.
    CPrimitives cairo_get_font_matrix: self _: matrix.
    ^ matrix

    "Created: / 18-02-2016 / 10:09:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

fontMatrix: matrix
    "Sets the current font matrix to matrix . The font matrix gives a transformation from 
     the design space of the font (in this space, the em-square is 1 unit by 1 unit) 
     to user space. Normally, a simple scale is used (see #fontSize:), but a more complex 
     font matrix can be used to shear the font or stretch it unequally along the two axes"

    CPrimitives cairo_set_font_matrix: self _: matrix.
    self statusCheck.

    "Created: / 18-02-2016 / 10:11:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:14:09 / jv"
!

fontSize:sz 
    CPrimitives cairo_set_font_size:self _:sz asFloat.
    self statusCheck

    "Created: / 23-04-2009 / 17:24:33 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 02-01-2015 / 01:39:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:13:30 / jv"
!

showText:aString 
    CPrimitives cairo_show_text:self _:aString utf8Encoded.
    self statusCheck.

    "Created: / 23-04-2009 / 17:25:20 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 28-12-2014 / 22:02:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:13:46 / jv"
!

textExtents:aString
    | extents |

    extents := TextExtents new.
    CPrimitives cairo_text_extents:self _:aString utf8Encoded _: extents.
    self statusCheck.
    ^ extents.

    "Created: / 18-02-2016 / 08:55:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:13:58 / jv"
! !

!GraphicsContext methodsFor:'cairo api - transformations & clipping'!

clip
    CPrimitives cairo_clip:self.
    self statusCheck.

    "Created: / 17-06-2012 / 21:56:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-12-2014 / 22:02:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:18 / jv"
!

matrix: aCairoMatrix
    self notYetImplemented

    "Created: / 13-02-2016 / 19:51:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

matrixReset
    "Resets the current transformation matrix (CTM) by setting it equal to the 
     identity matrix. That is, the user-space and device-space axes will be 
     aligned and one user-space unit will transform to one device-space unit."
    CPrimitives cairo_identity_matrix: self.
    self statusCheck.

    "Created: / 13-02-2016 / 19:54:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:09 / jv"
!

scale: aNumberOrPoint
    | sx sy |
    aNumberOrPoint isPoint ifTrue:[ 
        sx := aNumberOrPoint x asFloat.
        sy := aNumberOrPoint y asFloat.
    ] ifFalse:[ 
        sx := sy := aNumberOrPoint asFloat.
    ].
    CPrimitives cairo_scale: self _: sx _: sy.
    self statusCheck.

    "Created: / 13-02-2016 / 16:40:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:05 / jv"
!

translate: aNumberOrPoint
    | tx ty |
    aNumberOrPoint isPoint ifTrue:[ 
        tx := aNumberOrPoint x asFloat.
        ty := aNumberOrPoint y asFloat.
    ] ifFalse:[ 
        tx := ty := aNumberOrPoint asFloat.
    ].
    CPrimitives cairo_translate: self _: tx _: ty.
    self statusCheck.

    "Created: / 13-02-2016 / 16:40:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:15:00 / jv"
! !

!GraphicsContext methodsFor:'initialization & release'!

initializeWithSurface: aSurface
    surface := aSurface.

    "Created: / 13-02-2016 / 16:08:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-02-2016 / 15:30:05 / jv"
    "Modified: / 23-02-2016 / 10:54:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GraphicsContext methodsFor:'private'!

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."

    surface := nil.
    CPrimitives cairo_destroy: self.
    self setAddress: nil.

    "Modified: / 13-02-2016 / 16:13:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !