[Gambas-user] How to insert a .png file into a CairoPdfSurface (gb.cairo)

Tobias Boege taboege at ...626...
Sun Aug 25 14:28:29 CEST 2013


On Sun, 25 Aug 2013, Willy Raets wrote:
> On Sat, 2013-08-24 at 15:34 +0200, Willy Raets wrote:
> > On Sat, 2013-08-24 at 10:54 +0200, Tobias Boege wrote:
> > > On Sat, 24 Aug 2013, Willy Raets wrote:
> > > > On Sat, 2013-08-24 at 03:19 +0200, Willy Raets wrote:
> > > > > Hi All,
> > > > > 
> > > > > Been struggling with inserting an existing .png file onto a
> > > > > CairoPdfSurface (gb.cairo).
> > > > > 
> > > > > Done some extensive reading in the documentation but do not seem to find
> > > > > a method get the job done. Is this not possible or am I misunderstanding
> > > > > the documentation?
> > > > > 
> > > > > Piece of the code of the project:
> > > > > 
> > > > > Public Sub MakePdf()
> > > > >   
> > > > >   Dim hPdfProjectFile As CairoPdfSurface
> > > > >   Dim sPdfPath, sPicPath As String
> > > > >   Dim iX, iY As Float
> > > > >   sPicPath = User.Home & "/MyFolder/mypicture.png"
> > > > >   sPdfPath = User.Home & "/MyFolder/project.pdf"
> > > > >   hPdfProjectFile = New CairoPdfSurface(sPdfPath, 210, 297) 
> > > > >   Cairo.Begin(hPdfProjectFile)
> > > > >   '--Draw title --
> > > > >   iY = 250
> > > > >   '-- Some code to determine X co??rdinate iX --
> > > > >   ...
> > > > >   ...
> > > > >   Cairo.Font.Bold = True
> > > > >   Cairo.Font.Size = 18
> > > > >   Cairo.MoveTo(iX, iY)
> > > > >   Cairo.DrawText(Start.ProjectLoadedTitle)
> > > > >   Cairo.Font.Bold = False
> > > > >   '
> > > > >   '--Draw project logo --
> > > > >   iY = 285
> > > > >   ...                   '<== stuck right here!! 
> > > > >   '
> > > > >   '--Draw Document Version--
> > > > >   iY = 500
> > > > >   ...
> > > > >   ...
> > > > >   Cairo.End
> > > > > 
> > > > > End
> > > > > 
> > > > > Anyone any ideas, suggestions, links?
> > > > > 
> > > > 
> > > > Just to clarify, I need the .png in sPicPath = User.Home &
> > > > "/MyFolder/mypicture.png" do be drawn underneath the Title at 
> > > > section '--Draw project logo --
> > > > 
> > > 
> > > Maybe you could (ab)use Cairo.ImagePattern()? Load the image, call that
> > > method and assign the result to Cairo.Source. Cairo.Paint() then does the
> > > job.
> > 
> > I already tried the Cairo.ImagePattern, but didn't know about having to
> > assign the result to Cairo.Source.
> > 
> > so I have tried this now, but still no result. I am new to cairo and not
> > completely understand all that it does (my lack of insight).
> > 
> > This is the code I tried now but it doesn't give me a result. What am I
> > doing wrong?
> > 
> > Public Sub MakePdf()
> > 
> >   Dim hPdfProjectFile As CairoPdfSurface
> >   Dim sPdfPath, sPicPath As String
> >   Dim picImage As Image
> >   Dim iX, iY As Float
> >   sPicPath = User.Home & "/MyFolder/mypicture.png"
> >   sPdfPath = User.Home & "/MyFolder/project.pdf"
> >   hPdfProjectFile = New CairoPdfSurface(sPdfPath, 210, 297) 
> >   Cairo.Begin(hPdfProjectFile)
> >   '--Draw title --
> >   ..
> >   ..
> >   '--Draw project logo --
> >   iX = 230
> >   iY = 235
> >   If Exist(sPicturePath) Then
> >     picImage.Load(sPicturePath)
> >     Cairo.MoveTo(iX, iY)
> >     Cairo.Source = Cairo.ImagePattern(picImage)
> >     Cairo.Paint              '<-- nothing gets painted on PdfSurface
> >   Endif
> >   ..
> > 
> > 
> > End
> 
> With help of Tobias, now a image appears on the PdfSurface
> My code now looks like this:
> 
> 
> Public Sub MakePdf()
>  
>    Dim hPdfProjectFile As CairoPdfSurface
>    Dim sPdfPath, sPicPath As String
>    Dim picImage As Image
>    Dim iX, iY As Float
>    sPicPath = User.Home & "/MyFolder/mypicture.png"
>    sPdfPath = User.Home & "/MyFolder/project.pdf"
>    hPdfProjectFile = New CairoPdfSurface(sPdfPath, 210, 297) 
>    Cairo.Begin(hPdfProjectFile)
>    '----- Draw title -----
>    ..
>    ..
> 
>   '----- Draw project logo -----
>   iX = 230
>   iY = 235
>   If Exist(sPicturePath) Then
>      Cairo.MoveTo(iX, iY)         '<-- Does NOT place image
>      Cairo.Source = Cairo.ImagePattern(Image.Load(sPicturePath)
>      Cairo.Paint()
>   Endif
>   '----- Draw version -----
>    ..
>    ..
>   
> 
> End
> 
> Some new problems arise:
> 
> 1. The Cairo.Move(iX,iY) (before Cairo.Source) however seems of no
> influence on the Cairo.Paint() as the image appears top-left on the
> PdfSurface. I don't want it there but at ccordinates X = 230, Y = 235
> 
> Question 1: How to position the image on the CairoPdfSurface?
> 

Sorry, I was so busy that I forgot to answer (the project was already
prepared) :-)

The ImagePattern method has X/Y translation parameters. I think that this
image pattern has a separate translation matrix which can be set either when
creating it (Cairo.ImagePattern(hImage, 40, 40) to move to 40,40) or by
manipulating the Cairo.Source.Matrix property after assigning the image
pattern to Cairo.Source.

> 2. What is even worse is that all the Cairo.DrawText() methods following
> Cairo.Source and Cairo.Paint() are no longer drawn onto the
> CairoPdfSurface as they where before!
> 
> Question 2: How to free up the source and go back to drawing text?
> 
> Tobias made a small example to show me how to get an icon on a
> PdfSurface. I modified this example to show you the 2 above problems.
> 
> First just run the example to see only text drawn on the surface (code
> with Cairo.source and Cairo.paint are your commented out).
> Next uncomment the lines with Cairo.source and Cairo.paint and run the
> example again.
> 
> You will notice that text drawn after Cairo.source and Cairo.paint no
> longer draws on the surface and that the icon is ALWAYS top-left
> 

I think that the reason for the lost text is that the image is actually only
an image pattern so you would draw text not with a solid black line but as
if this image was your drawing device. Resetting the source to Null works
for me.

Patch attached ;-)

Regards,
Tobi

PS: I have to use the Shell "kpdf " & sFile line because gb.desktop does not
    recognise my desktop by default and cannot open the PDF therefore.
-------------- next part --------------
diff -urNaX /tmp/gambas-patch-ignore a/.project b/.project
--- a/.project	2013-08-24 17:02:03.000000000 +0200
+++ b/.project	2013-08-25 14:27:00.000000000 +0200
@@ -1,8 +1,8 @@
 # Gambas Project File 3.0
-# Compiled with Gambas 3.4.2
+# Compiled with Gambas 3.4.90
 Title=cairotest
 Startup=FMain
-Version=0.0.2
+Version=0.0.3
 Component=gb.image
 Component=gb.cairo
 Component=gb.qt4
diff -urNaX /tmp/gambas-patch-ignore a/.src/FMain.class b/.src/FMain.class
--- a/.src/FMain.class	2013-08-24 16:53:20.000000000 +0200
+++ b/.src/FMain.class	2013-08-25 14:26:40.000000000 +0200
@@ -5,14 +5,16 @@
   Dim hPdf As New CairoPdfSurface(sFile, 100, 100)
 
   Cairo.Begin(hPdf)
+
   Cairo.MoveTo(100, 40)
   Cairo.DrawText("Text before paint method")
-  Cairo.MoveTo(40, 40)
-  'Cairo.Source = Cairo.ImagePattern(Image.Load(".icon.png"))
-  'Cairo.Paint()
+
+  Cairo.Source = Cairo.ImagePattern(Image.Load(".icon.png"), 40, 40)
+  Cairo.Paint()
+
+  Cairo.Source = Null
   Cairo.MoveTo(100, 80)
   Cairo.DrawText("Tekst after paint method")
   Cairo.End()
-  Desktop.Open(sFile)
-  
+  Shell "kpdf " & sFile
 End


More information about the User mailing list