Cairo__Examples2.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 28 Feb 2016 14:53:56 +0000
changeset 51 5293f2b851ab
parent 50 239120c68187
child 53 57718b3ac316
permissions -rw-r--r--
CairGraphicsContext: added support for displaying images with alpha channel

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

"{ NameSpace: Cairo }"

TestCase subclass:#Examples2
	instanceVariableNames:'view surface'
	classVariableNames:''
	poolDictionaries:''
	category:'Cairo-Examples'
!

!Examples2 class methodsFor:'accessing'!

isTestSelector:aSelector
    | method |

    aSelector isNil ifTrue:[ ^ false ].
    method := self lookupMethodFor: aSelector.
    ^ method notNil 
        and:[(method hasAnnotation: #example:) or:[ method hasAnnotation: #example:category: ] ]

    "
    Cairo::Examples1 isTestSelector: #example01:
    "

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

!Examples2 class methodsFor:'private'!

testSelectors
        "The API method is allTestSelectors which now includes #shouldInheritSelectors and so handles all cases.  Unlike that method, this does not guarantee to return a sorted ordered collection."

        ^self sunitSelectors select: [:each | self isTestSelector: each ]

    "Created: / 26-02-2016 / 22:40:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 methodsFor:'examples'!

example01: gc <example: '01 - Rectangles'>
gc lineWidth: 1.
gc paint: Color blue.
gc fillRectangleX: 10 y: 10 width: gc width - 20 height: gc height - 20.
gc paint: Color red.
gc displayRectangleX: 10 y: 10 width: gc width - 20 height: gc height - 20.
gc paint: Color blue.
gc fillRectangleX: 10 y: 10 + ((gc height - 20) / 2) rounded width: gc width - 20 height: ((gc height - 20) / 2) rounded.
gc paint: Color black.
gc displayLineFromX: 1 y: (gc height / 2) rounded toX: gc width y: (gc height / 2) rounded.

    "Created: / 26-02-2016 / 22:57:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example02: gc <example: '02 - Rectangles (rounded)'>
gc lineWidth: 10.
gc paint: Color red.
gc displayRoundRectangleX: 10 y: 10 width: gc width - 20 height: gc height - 20
   wCorner: 30 hCorner: 30.
gc paint: Color blue lighter.
gc fillRoundRectangleX: 10 y: 10 rounded width: gc width - 20 height: (gc height - 20) rounded
   wCorner: 30 hCorner: 30.
gc paint: Color black.
gc lineWidth: 1.
gc displayLineFromX: 1 y: (gc height / 2) rounded toX: gc width y: (gc height / 2) rounded.

    "Created: / 27-02-2016 / 09:04:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 27-02-2016 / 16:14:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example03: gc <example: '03 - PNG with alpha'>
| image |

gc paint: Color white.
gc fillRectangleX:1 y:1 width: 32 height: 32.

gc paint: Color black.
gc fillRectangleX:33 y:33 width: 32 height: 32.

image := Image fromFile: 'square1.png' inPackage: 'stx:goodies/libcairo'.
image displayOn: gc x: 1 y: 1.

    "Created: / 27-02-2016 / 16:14:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-02-2016 / 14:50:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example04: gc <example: '03 - PNG with alpha II'>
| image |

gc paint: Color white.
gc fillRectangleX:1 y:1 width: 32 height: 32.

gc paint: Color black.
gc fillRectangleX:33 y:33 width: 32 height: 32.

image := Image fromFile: 'circle1.png' inPackage: 'stx:goodies/libcairo'.
image displayOn: gc x: 1 y: 1.

    "Created: / 28-02-2016 / 14:49:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 methodsFor:'private'!

performTest
    | dGC cGC |

    dGC := view instVarNamed: #gc.
    cGC := CairoGraphicsContext onDeviceGraphicsContext: dGC.
    [
        view instVarNamed: #gc put: dGC.    
        self perform: testSelector sunitAsSymbol with: view.
        view instVarNamed: #gc put: cGC.    
        self perform: testSelector sunitAsSymbol with: view. 
    ] ensure:[ 
        view instVarNamed: #gc put: dGC.    
        cGC destroyCR.
    ].

    "Created: / 26-02-2016 / 22:01:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 27-02-2016 / 09:01:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 methodsFor:'running'!

setUp
    | top |
    self assert: Display notNil.
    top := StandardSystemView new.
    top origin: 10@10 extent: 200@100.       
    view := SimpleView origin: 0.0 @ 0.0 corner: 1.0 @ 1.0 in: top.
    top openAndWait.

    "Created: / 26-02-2016 / 22:15:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

tearDown
    view notNil ifTrue:[ 
        view topView close.
    ].

    "Created: / 26-02-2016 / 22:18:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !