'
' test for PDF canvas
'
#include once "inc/cdpdf.bi"

sub _init_ constructor
  IupOpen(0,0)
end sub

sub _exit_ destructor
  IupClose()
end sub


const FILENAME = "canvas.pdf"
const BOX_SIZE = 300
dim shared as zstring ptr HatchNames(...) = _
{ _
  @"CD_HORIZONTAL", _
  @"CD_VERTICAL", _
  @"CD_FDIAGONAL", _
  @"CD_BDIAGONAL", _
  @"CD_CROSS", _
  @"CD_DIAGCROSS" _
}

' draw a filled box with a hatch type
' swap y pos. from bottom->up coords to up->bottom coords
sub DrawBox(c as cdCanvas ptr, _
            x as integer, y as integer, _
            w as integer, h as integer, _
            hatchtype as integer)
  dim as integer ch ' canvas height
  cdCanvasGetSize(c,NULL,@ch,NULL,NULL)
  cdCanvasHatch  (c, hatchtype)
  cdCanvasBox    (c,x,x+w,ch-y,ch-(y+w))
  cdCanvasText   (c,x+w+40,ch-(y+h/2),HatchNames(hatchtype))
end sub

'
' main
'

dim as integer w,h
dim as double  wmm,hmm

chdir exepath()
  
dim as cdCanvas ptr pdfCanvas = cdCreateCanvas(CD_PDF, @FILENAME)

cdCanvasGetSize(pdfCanvas,@w,@h,@wmm,@hmm)

' use any cd drawing primitieves
for Hatch as integer = CD_HORIZONTAL to CD_DIAGCROSS
  dim as integer y = 20 + hatch*(BOX_SIZE+20)
  DrawBox(pdfCanvas, 20,y, BOX_SIZE,BOX_SIZE,Hatch)
next

' optional add a new page cdCanvasFlush(pdfCanvas) 


' !!! close the pdf file !!!
cdKillCanvas(pdfCanvas)

IupMessage(command(0), _
           !"file: \"" & FILENAME & !"\" created\n" & _
           "page dimension: " & w   & " x " & h   & !" in pixels\n" & _
           "page dimension: " & int(wmm) & " x " & int(hmm) & " in millimeter")


