This is an attempt to create a set of printing commands that would be used as an include file and added to your program to be able to print text. See also the
by wilbert that is more extensive and does the same thing (and more).
Commands include: (* note that you have to set up the printer sizes in the variables below)
Code: Select all
; PrintAPILibrary.pbi vers.091
; credit must go to wilbert for the original code I've just re-packaged it for my purpose.
; must set default values to your printer size *
; can use the PureBasic command r=Red(ColorValue):g=Green(ColorValue):b=Blue(ColorValue)
ImportC ""
PMSessionBeginCGDocument(printSession.l, printSettings.l, pageFormat.l)
PMSessionGetCGGraphicsContext (printSession.l, context.l)
PMCreateSession(printSession.l)
PMCreatePageFormat(pageFormat.l)
PMSetOrientation(pageFormat.l,pageOrientaton.l,kPMUnlocked.l) ; ; kPMLandscape 1=portrait 2=landscape
PMCreatePrintSettings(printSettings.l)
PMSessionDefaultPageFormat(printSession.l, pageFormat.l)
PMSessionDefaultPrintSettings(printSession.l, printSettings.l)
PMSessionPrintDialog(printSession.l, printSettings.l, constPageFormat.l, accepted.l)
PMSessionBeginPage(printSession.l, pageFormat.l, *pageFrame)
PMSessionEndDocument(printSession.l)
PMSessionEndPage(printSession.l)
PMRelease(object.l)
CGContextSetRGBFillColor(context.l, r.f, g.f, b.f, a.f) ; text font color (r,g,b,a) ; rgb values are 0 to 1 (eg 0.5)
CGContextDrawImage(context.l, x.f, y.f, width.f, height.f, image.l) ; for image
CGContextFlush(context.l) ; use at end of page
CGContextSelectFont(context.l, name.p-ascii, size.f, textEncoding.l) ; textEncoding=1 = Mac Roman encoding
CGContextShowTextAtPoint(context.l, x.f, y.f, string.p-ascii, length.l) ; for printing text
CGContextFillRect(context.l, x.f, y.f, w.f, h.f) ; box
CGContextStrokeRect(context.l, x.f, y.f, w.f, h.f) ; box outline
CGContextSetRGBStrokeColor(context.l, r.f, g.f, b.f, a.f) ; stroke color
EndImport
Structure pdata
cg.l
session.l
printsettings.l
pageformat.l
pageOrientation.l
pagewidth.l
pageheight.l
pageborder.l
fontname.s
fontheight.l
EndStructure
Global Printing.pdata
;* defaults
Printing\pageOrientation=1 ; 1=portrait 2=landscape mode
Printing\pageborder=12 ; unprintable area ; adjust to your printer
Printing\pagewidth=612 ; not needed unless you check boundaries
Printing\pageheight=792 ; not needed unless you check boundaries
;may be set in main program
;Printing\fontname="Arial" ; default must be set!
;Printing\fontheight=12 ; default must be set!
If Print\fontname="" ; forgot to set fontname? Try to load a default. If no fontname/height the page will be blank!
If LoadFont(1,"Arial",12) ; default font load ; if font already loaded then just set fontname/fontheight below
Printing\fontname="Arial" ; default must be set!
Printing\fontheight=12 ; default must be set!
EndIf
EndIf
Procedure PrintDialog()
Protected accepted.l
PMCreateSession(@Printing\session)
PMCreatePageFormat(@Printing\pageFormat)
PMSessionDefaultPageFormat(Printing\session,Printing\pageFormat)
PMCreatePrintSettings(@Printing\printSettings)
PMSessionDefaultPrintSettings(Printing\session,Printing\printSettings)
PMSetOrientation(Printing\pageFormat,Printing\pageOrientation,0) ; kPMLandscape 1=portrait 2=landscape
If Printing\pageorientation=2
Swap Printing\pagewidth,Printing\pageheight
EndIf
PMSessionPrintDialog(Printing\session,Printing\printSettings,Printing\pageFormat,@accepted)
If accepted = 1
PMSessionBeginCGDocument(Printing\session,Printing\printSettings,Printing\pageFormat)
PMSessionBeginPage(Printing\session,Printing\pageFormat,#Null)
PMSessionGetCGGraphicsContext(Printing\session,@Printing\cg)
EndIf
ProcedureReturn accepted ; 1= ok to print (not canceled)
EndProcedure
Procedure PrintTextLine(lne,text.s,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
Protected y=Printing\pageheight-(Printing\fontheight*lne)-Printing\fontheight
CGContextSetRGBFillColor(Printing\cg,r/255,g/255,b/255,a)
CGContextSelectFont(Printing\cg,Printing\fontname,Printing\fontheight, 1)
CGContextShowTextAtPoint(Printing\cg,Printing\pageborder,y-Printing\pageborder, Text, Len(Text))
EndProcedure
Procedure PrintTextAt(x,y,text.s,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
y=Printing\pageheight-y-Printing\pageborder
CGContextSetRGBFillColor(Printing\cg,r/255,g/255,b/255,a)
CGContextSelectFont(Printing\cg,Printing\fontname,Printing\fontheight, 1)
CGContextShowTextAtPoint(Printing\cg,Printing\pageborder+x,y-Printing\pageborder, Text, Len(Text))
EndProcedure
Procedure PrintImage(x,y,imgid)
y=Printing\pageheight-y-ImageHeight(imgid)
CGContextDrawImage(Printing\cg,Printing\pageborder+x,y-Printing\pageborder,ImageWidth(imgid),ImageHeight(imgid), ImageID(imgid))
EndProcedure
Procedure PrintBox(x,y,w,h,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
;default color is black
If w=0 : w=1 : EndIf
If h=0 : h=1 : EndIf
y=Printing\pageheight-y-Printing\pageborder-h
CGContextSetRGBStrokeColor(Printing\cg,r/255,g/255,b/255,a) ; color not working (only black)
CGContextStrokeRect(Printing\cg,Printing\pageborder+x,y,w,h)
EndProcedure
Procedure PrintLine(x,y,w,h,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
;default color is black
If w=0 : w=1 : EndIf
If h=0 : h=1 : EndIf
y=Printing\pageheight-y-Printing\pageborder-h
CGContextSetRGBFillColor(Printing\cg,r/255,g/255,b/255,a)
CGContextFillRect(Printing\cg,Printing\pageborder+x,y,w,h)
EndProcedure
Procedure PrintNewPage()
CGContextFlush(Printing\cg)
PMSessionEndPage(Printing\session)
PMSessionBeginPage(Printing\session,Printing\pageFormat,#Null)
PMSessionGetCGGraphicsContext(Printing\session,@Printing\cg)
EndProcedure
Procedure PrintClose()
CGContextFlush(Printing\cg)
PMSessionEndPage(Printing\session)
PMSessionEndDocument(Printing\session)
PMRelease(Printing\printSettings)
PMRelease(Printing\pageFormat)
PMRelease(Printing\pageOrientation)
PMRelease(Printing\session)
EndProcedure; PrintAPILibrary.pbi vers.091
; must set default values to your printer size *
; can use the PureBasic command r=Red(ColorValue):g=Green(ColorValue):b=Blue(ColorValue)
ImportC ""
PMSessionBeginCGDocument(printSession.l, printSettings.l, pageFormat.l)
PMSessionGetCGGraphicsContext (printSession.l, context.l)
PMCreateSession(printSession.l)
PMCreatePageFormat(pageFormat.l)
PMSetOrientation(pageFormat.l,pageOrientaton.l,kPMUnlocked.l) ; ; kPMLandscape 1=portrait 2=landscape
PMCreatePrintSettings(printSettings.l)
PMSessionDefaultPageFormat(printSession.l, pageFormat.l)
PMSessionDefaultPrintSettings(printSession.l, printSettings.l)
PMSessionPrintDialog(printSession.l, printSettings.l, constPageFormat.l, accepted.l)
PMSessionBeginPage(printSession.l, pageFormat.l, *pageFrame)
PMSessionEndDocument(printSession.l)
PMSessionEndPage(printSession.l)
PMRelease(object.l)
CGContextSetRGBFillColor(context.l, r.f, g.f, b.f, a.f) ; text font color (r,g,b,a) ; rgb values are 0 to 1 (eg 0.5)
CGContextDrawImage(context.l, x.f, y.f, width.f, height.f, image.l) ; for image
CGContextFlush(context.l) ; use at end of page
CGContextSelectFont(context.l, name.p-ascii, size.f, textEncoding.l) ; textEncoding=1 = Mac Roman encoding
CGContextShowTextAtPoint(context.l, x.f, y.f, string.p-ascii, length.l) ; for printing text
CGContextFillRect(context.l, x.f, y.f, w.f, h.f) ; box
CGContextStrokeRect(context.l, x.f, y.f, w.f, h.f) ; box outline
CGContextSetRGBStrokeColor(context.l, r.f, g.f, b.f, a.f) ; stroke color
EndImport
Structure pdata
cg.l
session.l
printsettings.l
pageformat.l
pageOrientation.l
pagewidth.l
pageheight.l
pageborder.l
fontname.s
fontheight.l
EndStructure
Global Printing.pdata
;* defaults
Printing\pageOrientation=1 ; 1=portrait 2=landscape mode
Printing\pageborder=12 ; unprintable area ; adjust to your printer
Printing\pagewidth=612 ; not needed unless you check boundaries
Printing\pageheight=792 ; not needed unless you check boundaries
;may be set in main program
;Printing\fontname="Arial" ; default must be set!
;Printing\fontheight=12 ; default must be set!
If Print\fontname="" ; forgot to set fontname? Try to load a default. If no fontname/height the page will be blank!
If LoadFont(1,"Arial",12) ; default font load ; if font already loaded then just set fontname/fontheight below
Printing\fontname="Arial" ; default must be set!
Printing\fontheight=12 ; default must be set!
EndIf
EndIf
Procedure PrintDialog()
Protected accepted.l
PMCreateSession(@Printing\session)
PMCreatePageFormat(@Printing\pageFormat)
PMSessionDefaultPageFormat(Printing\session,Printing\pageFormat)
PMCreatePrintSettings(@Printing\printSettings)
PMSessionDefaultPrintSettings(Printing\session,Printing\printSettings)
PMSetOrientation(Printing\pageFormat,Printing\pageOrientation,0) ; kPMLandscape 1=portrait 2=landscape
If Printing\pageorientation=2
Swap Printing\pagewidth,Printing\pageheight
EndIf
PMSessionPrintDialog(Printing\session,Printing\printSettings,Printing\pageFormat,@accepted)
If accepted = 1
PMSessionBeginCGDocument(Printing\session,Printing\printSettings,Printing\pageFormat)
PMSessionBeginPage(Printing\session,Printing\pageFormat,#Null)
PMSessionGetCGGraphicsContext(Printing\session,@Printing\cg)
EndIf
ProcedureReturn accepted ; 1= ok to print (not canceled)
EndProcedure
Procedure PrintTextLine(lne,text.s,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
Protected y=Printing\pageheight-(Printing\fontheight*lne)-Printing\fontheight
CGContextSetRGBFillColor(Printing\cg,r/255,g/255,b/255,a)
CGContextSelectFont(Printing\cg,Printing\fontname,Printing\fontheight, 1)
CGContextShowTextAtPoint(Printing\cg,Printing\pageborder,y-Printing\pageborder, Text, Len(Text))
EndProcedure
Procedure PrintTextAt(x,y,text.s,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
y=Printing\pageheight-y-Printing\pageborder
CGContextSetRGBFillColor(Printing\cg,r/255,g/255,b/255,a)
CGContextSelectFont(Printing\cg,Printing\fontname,Printing\fontheight, 1)
CGContextShowTextAtPoint(Printing\cg,Printing\pageborder+x,y-Printing\pageborder, Text, Len(Text))
EndProcedure
Procedure PrintImage(x,y,imgid)
y=Printing\pageheight-y-ImageHeight(imgid)
CGContextDrawImage(Printing\cg,Printing\pageborder+x,y-Printing\pageborder,ImageWidth(imgid),ImageHeight(imgid), ImageID(imgid))
EndProcedure
Procedure PrintBox(x,y,w,h,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
;default color is black
If w=0 : w=1 : EndIf
If h=0 : h=1 : EndIf
y=Printing\pageheight-y-Printing\pageborder-h
CGContextSetRGBStrokeColor(Printing\cg,r/255,g/255,b/255,a) ; color not working (only black)
CGContextStrokeRect(Printing\cg,Printing\pageborder+x,y,w,h)
EndProcedure
Procedure PrintLine(x,y,w,h,r.f=0,g.f=0,b.f=0,a.f=1)
; looking for color input of 0-255
;default color is black
If w=0 : w=1 : EndIf
If h=0 : h=1 : EndIf
y=Printing\pageheight-y-Printing\pageborder-h
CGContextSetRGBFillColor(Printing\cg,r/255,g/255,b/255,a)
CGContextFillRect(Printing\cg,Printing\pageborder+x,y,w,h)
EndProcedure
Procedure PrintNewPage()
CGContextFlush(Printing\cg)
PMSessionEndPage(Printing\session)
PMSessionBeginPage(Printing\session,Printing\pageFormat,#Null)
PMSessionGetCGGraphicsContext(Printing\session,@Printing\cg)
EndProcedure
Procedure PrintClose()
CGContextFlush(Printing\cg)
PMSessionEndPage(Printing\session)
PMSessionEndDocument(Printing\session)
PMRelease(Printing\printSettings)
PMRelease(Printing\pageFormat)
PMRelease(Printing\pageOrientation)
PMRelease(Printing\session)
EndProcedure