extensions.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 16 Feb 2016 07:46:52 +0000
changeset 39 8af34937e1ec
parent 38 9c94e463018a
child 41 17bc740cbc2a
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' }"!

!DeviceGraphicsContext methodsFor:'accessing'!

cairo
    "Return a Cairo context for drawing onto this GC" 

    | cr |

    cr := Cairo::GraphicsContext onSurface: self cairoSurface.
    transformation notNil ifTrue:[  
        cr transformation: transformation
    ].
    ^ cr

    "Created: / 26-12-2014 / 23:28:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-02-2016 / 15:59:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!DeviceGraphicsContext methodsFor:'accessing'!

cairoSurface
    | view surface |

    view := device viewFromId:drawableId.
    surface := Cairo::Surface onView: view.
    ^ surface

    "Modified: / 14-02-2016 / 00:10:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!DeviceGraphicsContext methodsFor:'cairo support'!

drawableId

    ^drawableId

    "Created: / 10-07-2008 / 10:20:04 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!GraphicsContext methodsFor:'drawing in device coordinates'!

displayDeviceLineFromX:x1 y:y1 toX:x2 y:y2
    "draw a line in device coordinates"

    |sav|

    sav := transformation.
    self transformation: nil.
    self displayLineFromX:x1 y:y1 toX:x2 y:y2.
    self transformation: sav

    "Created: / 01-01-2015 / 22:50:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GraphicsContext methodsFor:'drawing in device coordinates'!

displayDeviceRectangleX:x y:y width:w height:h
    "draw a rectangle in device coordinates"

    |sav|

    sav := transformation.
    self transformation: nil.
    self displayRectangleX:x y:y width:w height:h.
    self transformation: sav

    "Created: / 01-01-2015 / 22:51:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GraphicsContext methodsFor:'drawing in device coordinates'!

fillDeviceRectangleX:x y:y width:w height:h
    "fill a rectangle with current paint color (device coordinates)"

    |sav|

    sav := transformation.
    self transformation: nil.
    self fillRectangleX:x y:y width:w height:h.
    self transformation: sav

    "Created: / 01-01-2015 / 22:51:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GraphicsDevice methodsFor:'cairo support'!

cairoSurfaceFor: view

    self error:'Graphics device not supported'

    "Created: / 10-07-2008 / 10:16:21 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!GraphicsDevice methodsFor:'accessing'!

displayId
    ^ displayId

    "Created: / 04-07-2008 / 12:58:56 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!GraphicsMedium methodsFor:'misc'!

cairoify
    "Change to use Cairo for rendering"
    
    gc := CairoGraphicsContext onDeviceGraphicsContext:gc.

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

!Image methodsFor:'accessing'!

bitsARGB32Into: buffer stride: stride fg: fg bg: bg
    | aOffset rOffset gOffset bOffset |

    UninterpretedBytes isBigEndian ifTrue:[
        aOffset := 1.
        rOffset := 2.
        gOffset := 3.
        bOffset := 4.
    ] ifFalse:[ 
        aOffset := 4.
        rOffset := 3.
        gOffset := 2.
        bOffset := 1.
    ].
    0 to: height - 1 do:[:y |  
        | base |
        base := y * stride.
        0 to: width - 1 do:[:x |  
            | pixel color offset |    

            offset := base + (x * 4).
            (mask isNil or:[ (mask pixelAtX: x y:y) == 1 ]) ifTrue:[ 
                pixel := self pixelAtX: x y: y.
                color := self colorFromValue: pixel.
                buffer at: (offset + rOffset) put: color redByte.  
                buffer at: (offset + gOffset) put: color greenByte.  
                buffer at: (offset + bOffset) put: color blueByte.  
                buffer at: (offset + aOffset) put: 16rFF.
            ] ifFalse:[ 
                buffer at: (offset + rOffset) put: 0.  
                buffer at: (offset + gOffset) put: 0.  
                buffer at: (offset + bOffset) put: 0.  
                buffer at: (offset + aOffset) put: 0.

            ].
        ]
    ]

    "Created: / 31-12-2014 / 13:08:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 01-01-2015 / 11:41:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Image methodsFor:'inspecting'!

inspector2TabImageCairo
    <inspector2Tab>

    | view |

    view := PluggableView new.
    view redrawAction:[
        | cr |

        cr := view cairo.
        [
            self displayOn: cr
        ] ensure:[ 
            cr release.
        ].
    ].
    ^self newInspector2Tab
        label: 'Image (Cairo)';
        priority: 49;
        view: (HVScrollableView forView: view);
        yourself

    "Created: / 31-12-2014 / 12:01:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'accessing - cairo'!

cairo
    "Return a Cairo context for drawing onto this view"           
    ^ gc cairo

    "Created: / 10-09-2008 / 18:23:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified (comment): / 26-12-2014 / 23:29:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairo
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairo: cr
    ] ensure:[
        cr ~~ gc ifTrue:[ cr release ].
    ].

    "Created: / 27-12-2014 / 00:30:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 12-02-2016 / 16:38:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairo: cr x: x y: y width: w height: h
    cr rectangleX: x  y: y width: w height: h. 
    cr clip.
    self redrawWithCairo: cr

    "Created: / 27-12-2014 / 00:29:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBuffered
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairoBuffered: cr
    ] ensure:[ 
        cr ~~ gc ifTrue:[ cr release ].
    ].

    "Created: / 27-12-2014 / 00:30:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 12-02-2016 / 16:38:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBuffered: view_cr

    | image_surface image_cr |

    [     
        image_surface := Cairo::Surface newImageWithFormatARGB32width:self width
                               height:self height.
        image_cr := Cairo::GraphicsContext onSurface: image_surface. 
        self redrawWithCairo: image_cr.  
        view_cr setSourceSurface: image_surface. 
        view_cr draw.
    ] ensure:[ 
        image_surface notNil ifTrue:[ image_surface release ].
        image_cr notNil ifTrue:[ image_cr release ]
    ].

    "Created: / 27-12-2014 / 00:13:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 14-02-2016 / 00:13:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBuffered: view_cr x: x y: y width: w height: h

    | image_surface image_cr |

    [     
        image_surface := Cairo::Surface newImageWithFormatARGB32width:self width
                               height:self height.
        image_cr := Cairo::GraphicsContext onSurface: image_surface. 
        image_cr rectangleX: x  y: y width: w height: h. 
        image_cr clip.
        self redrawWithCairo: image_cr x: x y: y width: w height: h.
        view_cr rectangleX: x  y: y width: w height: h. 
        view_cr clip.
        view_cr setSourceSurface: image_surface. 
        view_cr paint.
    ] ensure:[ 
        image_surface notNil ifTrue:[ image_surface release ].
        image_cr notNil ifTrue:[ image_cr release ]
    ].

    "Created: / 27-12-2014 / 00:28:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 14-02-2016 / 00:12:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBufferedX: x y: y width: w height: h     
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairoBuffered: cr x: x y: y width: w height: h     
    ] ensure:[ 
        cr ~~ gc ifTrue:[ cr release ].
    ].

    "Created: / 27-12-2014 / 00:31:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 12-02-2016 / 16:38:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoX: x y: y width: w height: h     
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairo: cr x: x y: y width: w height: h     
    ] ensure:[ 
        cr ~~ gc ifTrue:[ cr release ].
    ].

    "Created: / 27-12-2014 / 00:31:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 12-02-2016 / 16:38:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!stx_goodies_libcairo class methodsFor:'documentation'!

extensionsVersion_HG

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