More work for using CairoGrahicsContext for rendering views
authorJan Vrany <jan.vrany@fit.cvut.cz>
Tue, 16 Feb 2016 07:46:52 +0000
changeset 39 8af34937e1ec
parent 38 9c94e463018a
child 40 28dfc583beb5
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)
CairoGraphicsContext.st
Cairo__Operator.st
Cairo__SurfacePDF.st
Cairo__SurfaceXlib.st
Make.proto
bc.mak
extensions.st
stx_goodies_libcairo.st
--- a/CairoGraphicsContext.st	Sun Feb 14 00:15:15 2016 +0000
+++ b/CairoGraphicsContext.st	Tue Feb 16 07:46:52 2016 +0000
@@ -22,6 +22,26 @@
     "Modified: / 09-01-2015 / 15:08:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!CairoGraphicsContext class methodsFor:'instance creation'!
+
+onDeviceGraphicsContext: dGC
+    | cGC |
+
+    cGC := self onDevice: dGC device.  
+    1 to: DeviceGraphicsContext instSize do:[:i | 
+        cGC instVarAt: i put: (dGC instVarAt: i).
+    ].
+    dGC gcId notNil ifTrue:[ 
+        cGC createCR.
+    ].
+    cGC lineWidth: dGC lineWidth.
+    cGC font: dGC font.
+    ^ cGC
+
+    "Created: / 15-02-2016 / 21:20:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-02-2016 / 10:44:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !CairoGraphicsContext class methodsFor:'accessing'!
 
 dllPath
@@ -102,27 +122,32 @@
 
 !CairoGraphicsContext methodsFor:'accessing'!
 
-font:aFont
-    | dfont family slant psize weight |
+clippingBounds:aRectangleOrNil
+    "set the clipping rectangle for drawing (in logical coordinates);
+     a nil argument turn off clipping (i.e. whole view is drawable)"    
+
+    ^ super clippingBounds:aRectangleOrNil
+
+    "Created: / 15-02-2016 / 21:38:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
 
-    dfont := aFont onDevice: device.
+font:aFont
+    | family slant psize weight |
+
+    super font: aFont.
 
-    family := dfont family.
-    slant := SymbolicFontSlantToCairoFontSlantMap at: (dfont style ? 'roman'). 
-    weight := SymbolicFontFaceToCairoFontWeightMap at: (dfont face ? 'regular').
+    family := font family.
+    slant := SymbolicFontSlantToCairoFontSlantMap at: (font style ? 'roman'). 
+    weight := SymbolicFontFaceToCairoFontWeightMap at: (font face ? 'regular').
+    psize := font pixelSize.
+    psize isNil ifTrue:[ 
+        psize := (self device verticalPixelPerInch / 72) * font size.
+    ].
 
     cr font: family slant: slant weight: weight.
-
-    psize := dfont pixelSize.
-    psize isNil ifTrue:[ 
-        psize := (self device verticalPixelPerInch / 72) * dfont size.
-    ].
     cr fontSize: psize .
 
-    font := ScaledFont family: dfont family face: dfont face style: dfont style size: dfont size.
-    font handle: (CPrimitives cairo_get_scaled_font: crId).
-
-    "Modified: / 13-02-2016 / 21:17:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-02-2016 / 10:55:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 function:aFunctionSymbol
@@ -133,10 +158,10 @@
 
 lineWidth: w
     super lineWidth: w. 
-    cr lineWidth: w.
+    cr lineWidth: (w == 0 ifTrue:[ 1 ] ifFalse:[ w ]).
 
     "Created: / 17-06-2012 / 21:55:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 13-02-2016 / 17:38:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-02-2016 / 10:13:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 mask:aForm
@@ -160,13 +185,16 @@
     "set the transformation"
 
     super transformation: aTransformation.
-    cr 
-        matrixReset;
-        scale: transformation scale;
-        transform: transformation translation.
+
+    cr matrixReset.
+    transformation notNil ifTrue:[    
+        cr
+            scale: transformation scale;
+            transform: transformation translation.
+    ]
 
     "Created: / 01-01-2015 / 12:07:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 13-02-2016 / 19:55:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-02-2016 / 10:59:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContext methodsFor:'basic drawing'!
@@ -211,12 +239,6 @@
     "Modified: / 13-02-2016 / 20:05:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-displayOpaqueString:aString from:index1 to:index2 x:x y:y maxWitdh:maxWidth
-    "draw part of a string with both fg and bg at x/y in current font"
-
-    ^ self shouldImplement
-!
-
 displayPolygon:points
     "draw a polygon
      - this could be recoded to draw using displayLine"
@@ -241,22 +263,27 @@
     "Modified: / 13-02-2016 / 20:04:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-displayString:aString from:index1 to:index2 x:x y:y
-    "draw part of a string with fg at x/y in current font"
+displayString:aStringArg from:index1Arg to:index2Arg x:x y:y opaque:opaqueArg maxWidth:maxWidth
+    "draw a substring at the coordinate x/y - draw foreground pixels in
+     paint-color and (if opaque is true), background pixels in bgPaint-color.
+     If the transformation involves scaling, the font's point-size is scaled as appropriate.
+     Assuming that device can only draw in device colors, we have to handle
+     the case where paint and/or bgPaint are dithered colors.
+     maxWidth is the maximum width of the string in pixels or nil if unknown."    
 
     cr save.
     [ 
         cr moveToX: x y: y.
-        (index1 == 1 and:[ index2 == aString size ]) ifTrue:[ 
-            cr showText: aString
+        (index1Arg == 1 and:[ index2Arg == aStringArg size ]) ifTrue:[ 
+            cr showText: aStringArg
         ] ifFalse:[ 
-            cr showText: (aString copyFrom: index1 to: index2).
+            cr showText: (aStringArg copyFrom: index1Arg to: index2Arg).
         ].
     ] ensure:[ 
         cr restore.
     ]
 
-    "Modified: / 13-02-2016 / 20:04:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 16-02-2016 / 10:51:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContext methodsFor:'basic filling'!
@@ -318,15 +345,14 @@
     cr strokeAndPreserve.
     cr fill.
 
-    "Modified: / 13-02-2016 / 20:01:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!CairoGraphicsContext methodsFor:'bit blitting'!
+"/    cr save.
+"/    cr rectangleX: x y: y width: w height: h. 
+"/    cr sourceR: 1 G: 0 B: 0.
+"/    cr lineWidth: 3.  
+"/    cr stroke.
+"/    cr restore.
 
-copyFrom:aGC x:srcX y:srcY toX:dstX y:dstY width:w height:h
-    "copy from a drawable - maybe self"
-
-    ^ self shouldImplement
+    "Modified: / 16-02-2016 / 07:58:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContext methodsFor:'drawing'!
@@ -339,23 +365,25 @@
 
     | image width height stride data image_surface |
 
+    ^ super displayForm:aFormOrImage x:x y:y.
+
     image := aFormOrImage asImage.
     width := image width.
     height := image height.
-    stride := CPrimitives cairo_format_stride_for_width: CAIRO_FORMAT_ARGB32 _: width.
+    stride := Cairo::CPrimitives cairo_format_stride_for_width: CAIRO_FORMAT_ARGB32 _: width.
     data := ExternalBytes basicNew allocateBytes: stride * height clear: false.
     [
         image bitsARGB32Into: data stride: stride fg: self paint bg:  self backgroundPaint. 
-        image_surface := CPrimitives cairo_image_surface_create_for_data: data _: CAIRO_FORMAT_ARGB32 _: width _: height _: stride.
-        CPrimitives cairo_set_source_surface: crId _: image_surface _: x asFloat _: y asFloat.
-        CPrimitives cairo_paint: crId.
+        image_surface := Cairo::CPrimitives cairo_image_surface_create_for_data: data _: CAIRO_FORMAT_ARGB32 _: width _: height _: stride.
+        Cairo::CPrimitives cairo_set_source_surface: crId _: image_surface _: x asFloat _: y asFloat.
+        Cairo::CPrimitives cairo_paint: crId.
     ] ensure:[ 
         data finalize.
         image_surface release.
     ].
 
     "Created: / 31-12-2014 / 12:08:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 01-01-2015 / 02:48:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 15-02-2016 / 21:32:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 displayRoundRectangleX:x y:y width:w height:h wCorner:wCorn hCorner:hCorn
--- a/Cairo__Operator.st	Sun Feb 14 00:15:15 2016 +0000
+++ b/Cairo__Operator.st	Tue Feb 16 07:46:52 2016 +0000
@@ -21,6 +21,7 @@
 	category:'Cairo-Constants'
 !
 
+
 !Operator class methodsFor:'initialization'!
 
 initialize
@@ -203,5 +204,12 @@
     ^CAIRO_OPERATOR_XOR
 ! !
 
+!Operator class methodsFor:'documentation'!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+
 
 Operator initialize!
--- a/Cairo__SurfacePDF.st	Sun Feb 14 00:15:15 2016 +0000
+++ b/Cairo__SurfacePDF.st	Tue Feb 16 07:46:52 2016 +0000
@@ -9,3 +9,11 @@
 	category:'Cairo-Objects'
 !
 
+
+!SurfacePDF class methodsFor:'documentation'!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+
--- a/Cairo__SurfaceXlib.st	Sun Feb 14 00:15:15 2016 +0000
+++ b/Cairo__SurfaceXlib.st	Tue Feb 16 07:46:52 2016 +0000
@@ -5,7 +5,7 @@
 Surface subclass:#SurfaceXlib
 	instanceVariableNames:''
 	classVariableNames:''
-	poolDictionaries:''
+	poolDictionaries:'Cairo::SurfaceType Cairo::Format'
 	category:'Cairo-Objects'
 !
 
@@ -60,14 +60,10 @@
 !
 
 width: w height: h
-    self type == CAIRO_SURFACE_TYPE_XLIB ifTrue:[
-        ^ CPrimitives cairo_xlib_surface_set_size: self _: w _: h 
-    ].
-
-    self error:'Operation not supported'
+    CPrimitives cairo_xlib_surface_set_size: self _: w _: h
 
     "Created: / 10-07-2008 / 19:36:39 / Jan Vrany <vranyj1@fel.cvut.cz>"
-    "Modified: / 28-12-2014 / 21:52:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-02-2016 / 07:45:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !SurfaceXlib methodsFor:'change & update'!
--- a/Make.proto	Sun Feb 14 00:15:15 2016 +0000
+++ b/Make.proto	Tue Feb 16 07:46:52 2016 +0000
@@ -195,7 +195,7 @@
 $(OUTDIR)CairoGraphicsContext.$(O) CairoGraphicsContext.$(H): CairoGraphicsContext.st $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__FontSlant.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__FontWeight.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__Format.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(INCLUDE_TOP)/stx/libview/DeviceGraphicsContext.$(H) $(INCLUDE_TOP)/stx/libview/GraphicsContext.$(H) $(STCHDR)
 $(OUTDIR)Cairo__SurfaceImage.$(O) Cairo__SurfaceImage.$(H): Cairo__SurfaceImage.st $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__CObject.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__Surface.$(H) $(INCLUDE_TOP)/stx/libbasic/ExternalAddress.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
 $(OUTDIR)Cairo__SurfacePDF.$(O) Cairo__SurfacePDF.$(H): Cairo__SurfacePDF.st $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__CObject.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__Surface.$(H) $(INCLUDE_TOP)/stx/libbasic/ExternalAddress.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
-$(OUTDIR)Cairo__SurfaceXlib.$(O) Cairo__SurfaceXlib.$(H): Cairo__SurfaceXlib.st $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__CObject.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__Surface.$(H) $(INCLUDE_TOP)/stx/libbasic/ExternalAddress.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
+$(OUTDIR)Cairo__SurfaceXlib.$(O) Cairo__SurfaceXlib.$(H): Cairo__SurfaceXlib.st $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__CObject.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__Format.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__Surface.$(H) $(INCLUDE_TOP)/stx/goodies/libcairo/Cairo__SurfaceType.$(H) $(INCLUDE_TOP)/stx/libbasic/ExternalAddress.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
 $(OUTDIR)extensions.$(O): extensions.st $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(INCLUDE_TOP)/stx/libview/DeviceGraphicsContext.$(H) $(INCLUDE_TOP)/stx/libview/DisplaySurface.$(H) $(INCLUDE_TOP)/stx/libview/GraphicsContext.$(H) $(INCLUDE_TOP)/stx/libview/GraphicsDevice.$(H) $(INCLUDE_TOP)/stx/libview/GraphicsMedium.$(H) $(INCLUDE_TOP)/stx/libview/Image.$(H) $(INCLUDE_TOP)/stx/libview/SimpleView.$(H) $(STCHDR)
 
 # ENDMAKEDEPEND --- do not remove this line
--- a/bc.mak	Sun Feb 14 00:15:15 2016 +0000
+++ b/bc.mak	Tue Feb 16 07:46:52 2016 +0000
@@ -120,7 +120,7 @@
 $(OUTDIR)CairoGraphicsContext.$(O) CairoGraphicsContext.$(H): CairoGraphicsContext.st $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__FontSlant.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__FontWeight.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__Format.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(INCLUDE_TOP)\stx\libview\DeviceGraphicsContext.$(H) $(INCLUDE_TOP)\stx\libview\GraphicsContext.$(H) $(STCHDR)
 $(OUTDIR)Cairo__SurfaceImage.$(O) Cairo__SurfaceImage.$(H): Cairo__SurfaceImage.st $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__CObject.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__Surface.$(H) $(INCLUDE_TOP)\stx\libbasic\ExternalAddress.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
 $(OUTDIR)Cairo__SurfacePDF.$(O) Cairo__SurfacePDF.$(H): Cairo__SurfacePDF.st $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__CObject.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__Surface.$(H) $(INCLUDE_TOP)\stx\libbasic\ExternalAddress.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
-$(OUTDIR)Cairo__SurfaceXlib.$(O) Cairo__SurfaceXlib.$(H): Cairo__SurfaceXlib.st $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__CObject.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__Surface.$(H) $(INCLUDE_TOP)\stx\libbasic\ExternalAddress.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
+$(OUTDIR)Cairo__SurfaceXlib.$(O) Cairo__SurfaceXlib.$(H): Cairo__SurfaceXlib.st $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__CObject.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__Format.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__Surface.$(H) $(INCLUDE_TOP)\stx\goodies\libcairo\Cairo__SurfaceType.$(H) $(INCLUDE_TOP)\stx\libbasic\ExternalAddress.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
 $(OUTDIR)extensions.$(O): extensions.st $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(INCLUDE_TOP)\stx\libview\DeviceGraphicsContext.$(H) $(INCLUDE_TOP)\stx\libview\DisplaySurface.$(H) $(INCLUDE_TOP)\stx\libview\GraphicsContext.$(H) $(INCLUDE_TOP)\stx\libview\GraphicsDevice.$(H) $(INCLUDE_TOP)\stx\libview\GraphicsMedium.$(H) $(INCLUDE_TOP)\stx\libview\Image.$(H) $(INCLUDE_TOP)\stx\libview\SimpleView.$(H) $(STCHDR)
 
 # ENDMAKEDEPEND --- do not remove this line
--- a/extensions.st	Sun Feb 14 00:15:15 2016 +0000
+++ b/extensions.st	Tue Feb 16 07:46:52 2016 +0000
@@ -100,6 +100,16 @@
     "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
--- a/stx_goodies_libcairo.st	Sun Feb 14 00:15:15 2016 +0000
+++ b/stx_goodies_libcairo.st	Tue Feb 16 07:46:52 2016 +0000
@@ -195,6 +195,7 @@
         GraphicsContext displayDeviceRectangleX:y:width:height:
         GraphicsContext fillDeviceRectangleX:y:width:height:
         DeviceGraphicsContext cairoSurface
+        GraphicsMedium cairoify
     )
 ! !