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

Willy Raets willy at ...2734...
Sun Aug 25 14:13:05 CEST 2013


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?

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

-- 
Kind regards,

Willy (aka gbWilly)

http://gambasshowcase.org/
http://howtogambas.org
http://gambos.org



-------------- next part --------------
A non-text attachment was scrubbed...
Name: cairotest-0.0.2.tar.gz
Type: application/x-compressed-tar
Size: 5038 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20130825/860da742/attachment.bin>


More information about the User mailing list