Skip to content

Commit

Permalink
Merge pull request #61 from Rinzwind/canvas-background-formset
Browse files Browse the repository at this point in the history
Extend AeCanvas to support setting a FormSet as background
  • Loading branch information
tinchodias authored Sep 3, 2024
2 parents df9fc9a + 9b5ff74 commit 676a97b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Alexandrie-Cairo-Tests/AeFilesystemResources.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ AeFilesystemResources class >> imagesDirectory [
^ self testsDirectory / 'images'
]

{ #category : #'*Alexandrie-Cairo-Tests' }
AeFilesystemResources class >> removeIcon2xPNG [

^ self imagesDirectory / 'removeIcon-2x.png'
]

{ #category : #'*Alexandrie-Cairo-Tests' }
AeFilesystemResources class >> removeIconPNG [

Expand Down
27 changes: 27 additions & 0 deletions src/Alexandrie-Canvas-Tests/AeCanvasTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,33 @@ AeCanvasTest >> renderForm [
^ aeCanvas
]

{ #category : #tests }
AeCanvasTest >> renderFormSet [

| aeCanvas aForm aFormSet |

SystemVersion current major >= 12 ifFalse: [ self skip ]. "Earlier versions do not have FormSet"

aeCanvas := AeCanvas newFor: ((AeCairoImageSurface extent: (60@20) * 2) deviceScale: 2 asPoint; yourself).
aeCanvas clear: Color paleGreen.

aForm := Form fromFileNamed: AeFilesystemResources removeIcon2xPNG.
aFormSet := FormSet extent: aForm extent // 2 depth: aForm depth forms: { aForm }.

aeCanvas pathScale: ((aeCanvas extent / aeCanvas deviceScale) / aFormSet extent) asFloatPoint.
aeCanvas pathFactory: [ :cairoContext |
"Tip: Use another path to clip the form"
cairoContext rectangleTo: aFormSet extent ].

aeCanvas
setBackgroundWithFormSet: aFormSet
alpha: 1.0.
aeCanvas setBorderOff.
aeCanvas drawFigure.

^ aeCanvas
]

{ #category : #private }
AeCanvasTest >> renderGlyphs: aCairoGlyphsArray scaledFont: aCairoScaledFont on: aeCanvas [

Expand Down
21 changes: 21 additions & 0 deletions src/Alexandrie-Canvas/AeCanvas.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Class {
'hasBackground',
'borderWidth',
'formToCairoSurfaceCache',
'formSetToCairoSurfaceCache',
'backgroundAlpha',
'cairoSurface',
'backgroundSourceBlock',
Expand Down Expand Up @@ -414,6 +415,15 @@ AeCanvas >> initializeCaches [
factory: [ :form | AeCairoImageSurface fromForm: form ];
yourself.

formSetToCairoSurfaceCache := LRUCache new
maximumWeight: 400;
factory: [ :formSet |
| scale |
scale := self deviceScale max rounded.
(AeCairoImageSurface fromForm: (formSet asFormAtScale: scale))
deviceScale: scale asPoint;
yourself ];
yourself.
]

{ #category : #initialization }
Expand Down Expand Up @@ -693,6 +703,17 @@ AeCanvas >> setBackgroundWithForm: aForm alpha: aDouble [

]

{ #category : #'API - background' }
AeCanvas >> setBackgroundWithFormSet: aFormSet alpha: aDouble [

self
setBackgroundWith: [
| aCairoSurface |
aCairoSurface := formSetToCairoSurfaceCache at: aFormSet.
cairoContext sourceSurface: aCairoSurface x: 0.0 y: 0.0 ]
alpha: aDouble
]

{ #category : #'API - border' }
AeCanvas >> setBorderBlock: aBlockClosure [

Expand Down
Binary file added tests/canvas/renderFormSet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/removeIcon-2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 676a97b

Please sign in to comment.