Printing with Carbon's Print Manager

Mac OSX specific forum
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Printing with Carbon's Print Manager

Post by coder14 »

Can anyone please help me. This code works perfectly in FutureBASIC, and prints out a yellow circle with a red border in the middle of a page. However, in PB, nothing. The print dialog pops up, but the printer "prints out" a blank page. Besides CGRectMake, the code compiles without any errors, and OSStat returns no errors for any of the Print Manager calls. What did I do wrong?

Code: Select all

EnableExplicit

Structure CGPoint
  x.f
  y.f
EndStructure

Structure CGSize
  width.f
  height.f
EndStructure

Structure CGRect
  origin.CGPoint
  size.CGSize
EndStructure

ImportC ""
  PMSessionBeginCGDocument(printSession.l, printSettings.l, pageFormat.l)
  PMSessionGetCGGraphicsContext (printSession.l, context.l)
  PMCreateSession(printSession.l)
  PMCreatePageFormat(pageFormat.l)
  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.RECT)
  PMSessionEndDocument(printSession.l)
  PMSessionEndPage(printSession.l)
  PMRelease(object.l)
  
  CGContextSetAlpha(context.l, alpha.f)
  CGContextSetRGBFillColor(context.l, r.f, g.f, b.f, a.f)
  CGRectMake(x.f, y.f, width.f, height.f)
  CGContextFillRect(context.l, *pageFrame.RECT)
  CGContextStrokeRect(context.l, *pageFrame.RECT)
  CGContextSetRGBStrokeColor(context.l, r.f, g.f, b.f, a.f)
  CGContextFillEllipseInRect(context.l, *pageFrame.RECT)
  CGContextStrokeEllipseInRect(context.l, *pageFrame.RECT)
  CGContextFlush(context.l)
EndImport

Define session.l, pageFormat.l, printSettings.l, OSStat.l
Define cg.l, PageRect.CGRect, accepted.l, pbVoid.l

OSStat = PMCreateSession(@session)
OSStat = PMCreatePageFormat(@pageFormat)
OSStat = PMSessionDefaultPageFormat(session, pageFormat)
OSStat = PMCreatePrintSettings(@printSettings)
OSStat = PMSessionDefaultPrintSettings(session, printSettings)
OSStat = PMSessionPrintDialog(session, printSettings, pageFormat, @accepted)

If accepted = 1
  OSStat = PMSessionBeginCGDocument(session, printSettings, pageFormat)
  OSStat = PMSessionBeginPage(session, pageFormat, pbVoid)
  OSStat = PMSessionGetCGGraphicsContext(session, @cg)
  
  CGContextSetAlpha(cg, 1.0)
  CGContextSetRGBFillColor(cg, 1.0, 1.0, 1.0, 1.0)

  With PageRect
    \origin\x = 16
    \origin\y = 15
    \size\height = 765
    \size\width = 584
  EndWith

; PageRect = CGRectMake(16, 15, 584, 765)
  
  CGContextFillRect(cg, PageRect)
  CGContextSetRGBFillColor(cg, 0.0, 0.0, 0.0, 1.0)
  CGContextStrokeRect(cg, PageRect)
  CGContextSetRGBFillColor(cg, 1.0, 1.0, 0.0, 1.0)
  CGContextSetRGBStrokeColor(cg, 1.0, 0.0, 0.0, 1.0)
  
  With PageRect
    \origin\x = 200
    \origin\y = 200
    \size\height = 300
    \size\width = 300    
  EndWith
  
;  CGContextFillEllipseInRect(cg, CGRectMake(200,200,300,300))
  CGContextFillEllipseInRect(cg, PageRect)
;  CGContextStrokeEllipseInRect(cg, CGRectMake(200,200,300,300))
  CGContextStrokeEllipseInRect(cg, PageRect)
  CGContextFlush(cg)
  
  OSStat = PMSessionEndPage(session)
  OSStat = PMSessionEndDocument(session)
  OSStat = PMRelease(printSettings)
  OSStat = PMRelease(pageFormat)
  OSStat = PMRelease(session)

EndIf
  
End
Would truly appreciate some direction. Thanks in advance!
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Printing with Carbon's Print Manager

Post by WilliamL »

I get a 'Structure not found: RECT' error. Will have to get past that first...

Code: Select all

PMSessionBeginPage(printSession.l, pageFormat.l, *pageFrame.RECT)
I hope you get some answers. (looks like Greek to me)
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Printing with Carbon's Print Manager

Post by jesperbrannmark »

Futurebasic. Intresting.
Is there any guide on how to import functions like you do?
I have wanted to do a webcam connection from purebasic for very long, but will need to do importfunctions similar to what you do..
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing with Carbon's Print Manager

Post by wilbert »

Code: Select all

EnableExplicit

ImportC ""

  PMSessionBeginCGDocument(printSession.l, printSettings.l, pageFormat.l)
  PMSessionGetCGGraphicsContext (printSession.l, context.l)
  PMCreateSession(printSession.l)
  PMCreatePageFormat(pageFormat.l)
  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)
  
  CGContextSetAlpha(context.l, alpha.f)
  CGContextSetRGBFillColor(context.l, r.f, g.f, b.f, a.f)
  CGContextFillRect(context.l, x.f, y.f, w.f, h.f)
  CGContextStrokeRect(context.l, x.f, y.f, w.f, h.f)
  CGContextSetRGBStrokeColor(context.l, r.f, g.f, b.f, a.f)
  CGContextFillEllipseInRect(context.l, x.f, y.f, w.f, h.f)
  CGContextStrokeEllipseInRect(context.l, x.f, y.f, w.f, h.f)
  CGContextFlush(context.l)
  
EndImport

Define session.l, pageFormat.l, printSettings.l
Define cg.l, accepted.l

PMCreateSession(@session)
PMCreatePageFormat(@pageFormat)
PMSessionDefaultPageFormat(session, pageFormat)
PMCreatePrintSettings(@printSettings)
PMSessionDefaultPrintSettings(session, printSettings)
PMSessionPrintDialog(session, printSettings, pageFormat, @accepted)

If accepted = 1

  PMSessionBeginCGDocument(session, printSettings, pageFormat)
  
  PMSessionBeginPage(session, pageFormat, #Null)
  PMSessionGetCGGraphicsContext(session, @cg)
    
  CGContextSetAlpha(cg, 1.0)
  CGContextSetRGBFillColor(cg, 1.0, 1.0, 1.0, 1.0)

  CGContextFillRect(cg, 16, 15, 584, 765)
  CGContextSetRGBFillColor(cg, 0.0, 0.0, 0.0, 1.0)
  CGContextStrokeRect(cg, 16, 15, 584, 765)
  CGContextSetRGBFillColor(cg, 1.0, 1.0, 0.0, 1.0)
  CGContextSetRGBStrokeColor(cg, 1.0, 0.0, 0.0, 1.0)
  
  CGContextFillEllipseInRect(cg, 200, 200, 300, 300)
  CGContextStrokeEllipseInRect(cg, 200, 200, 300, 300)
  CGContextFlush(cg)
  
  PMSessionEndPage(session)
  PMSessionEndDocument(session)
  PMRelease(printSettings)
  PMRelease(pageFormat)
  PMRelease(session)

EndIf
  
End
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Printing with Carbon's Print Manager

Post by WilliamL »

wilbert,

Works! Now, how do I pass text that is in a string? Then, of course, to change the font and size...

...then a library with a 'Print' (for the printer - Print@(x,y) or Print%(x,y) :wink: ).

Good job, I didn't think it would work.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing with Carbon's Print Manager

Post by wilbert »

The trick is the CGRect structures in the import functions.
They are passed as a structure (4 floats) and not as a pointer to a structure. Since PureBasic can't pass a structure that way (as far as I know), the solution was to replace the structure in the import with 4 floats (writing it out as x, y, w, h).

You can look up the functions you can use in the CGContextRef docs ( http://developer.apple.com/library/mac/ ... rence.html ).

Simple text adding can be done like this

Code: Select all

EnableExplicit

ImportC ""

  PMSessionBeginCGDocument(printSession.l, printSettings.l, pageFormat.l)
  PMSessionGetCGGraphicsContext (printSession.l, context.l)
  PMCreateSession(printSession.l)
  PMCreatePageFormat(pageFormat.l)
  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)
  
  CGContextSetAlpha(context.l, alpha.f)
  CGContextSetRGBFillColor(context.l, r.f, g.f, b.f, a.f)
  CGContextFillRect(context.l, x.f, y.f, w.f, h.f)
  CGContextStrokeRect(context.l, x.f, y.f, w.f, h.f)
  CGContextSetRGBStrokeColor(context.l, r.f, g.f, b.f, a.f)
  CGContextFillEllipseInRect(context.l, x.f, y.f, w.f, h.f)
  CGContextStrokeEllipseInRect(context.l, x.f, y.f, w.f, h.f)
  CGContextFlush(context.l)
  
  CGContextSelectFont(context.l, name.p-ascii, size.f, textEncoding.l)
  CGContextShowTextAtPoint(context.l, x.f, y.f, string.p-ascii, length.l)
  
EndImport

Define session.l, pageFormat.l, printSettings.l
Define cg.l, accepted.l
Define Text.s

PMCreateSession(@session)
PMCreatePageFormat(@pageFormat)
PMSessionDefaultPageFormat(session, pageFormat)
PMCreatePrintSettings(@printSettings)
PMSessionDefaultPrintSettings(session, printSettings)
PMSessionPrintDialog(session, printSettings, pageFormat, @accepted)

If accepted = 1

  PMSessionBeginCGDocument(session, printSettings, pageFormat)
  
  PMSessionBeginPage(session, pageFormat, #Null)
  PMSessionGetCGGraphicsContext(session, @cg)
    
  CGContextSetAlpha(cg, 1.0)
  CGContextSetRGBFillColor(cg, 1.0, 1.0, 1.0, 1.0)

  CGContextFillRect(cg, 16, 15, 584, 765)
  CGContextSetRGBFillColor(cg, 0.0, 0.0, 0.0, 1.0)
  CGContextStrokeRect(cg, 16, 15, 584, 765)
  CGContextSetRGBFillColor(cg, 1.0, 1.0, 0.0, 1.0)
  CGContextSetRGBStrokeColor(cg, 1.0, 0.0, 0.0, 1.0)
  
  CGContextFillEllipseInRect(cg, 200, 200, 300, 300)
  CGContextStrokeEllipseInRect(cg, 200, 200, 300, 300)
  
  Text.s = "Text string"
  CGContextSetRGBFillColor(cg, 0.0, 0.0, 1.0, 1.0)
  CGContextSelectFont(cg, "Helvetica", 12, 1); 1 = Mac Roman encoding
  CGContextShowTextAtPoint(cg, 200, 200, Text, Len(Text))
  
  CGContextFlush(cg)
  
  PMSessionEndPage(session)
  PMSessionEndDocument(session)
  PMRelease(printSettings)
  PMRelease(pageFormat)
  PMRelease(session)

EndIf
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Printing with Carbon's Print Manager

Post by WilliamL »

yes, I see.. I'm going to play with this and see how it goes.

This is a really good start for a print routine for the Mac and an important contribution. :)

I will add your code to the 'API list for Mac' thread and I hope it will be built upon.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Printing with Carbon's Print Manager

Post by WilliamL »

[edit] see below
Last edited by WilliamL on Tue Jul 12, 2011 6:06 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Printing with Carbon's Print Manager

Post by coder14 »

wilbert wrote:The trick is the CGRect structures in the import functions.
They are passed as a structure (4 floats) and not as a pointer to a structure. Since PureBasic can't pass a structure that way (as far as I know), the solution was to replace the structure in the import with 4 floats (writing it out as x, y, w, h).
The plain and simple truth is that you're a GURU! I've poured over the CGContext and Printing references at length, but didn't figure it out, and probably wouldn't either. WilliamL's right; this is a major contribution to the Mac platform.

I thank you, and I'm sure all the OSX developers here thank you as well. Great job!
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Printing with Carbon's Print Manager

Post by coder14 »

I've been tinkering with more of the Carbon CGContext functions, and discovered something very interesting. CGContextDrawImage can use images loaded by good old PB, which means that we can do away with the numerous CGImage function calls to load and create images.

Code: Select all

; add this declaration to the ImportC section:
ImportC ""
  ...
  CGContextDrawImage(context.l, x.f, y.f, width.f, height.f, image.l)
  ...
EndImport

; load your image (make sure to call the relevant image encoders for non-BMP images):
LoadImage(Image#, filename)

; and just reference it with ImageID like this:
CGContextDrawImage(cg, x, y, width, height, ImageID(Image#))
Voila! The image gets drawn at x/y and stretched to the specified width/height.

Simply marvelous.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Printing with Carbon's Print Manager

Post by WilliamL »

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 library 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)
PrintDialog() ; start printing job (must do first!)
PrintTextLine() ; prints text at line number
PrintTextAt() ;prints text at x,y position on page
PrintImage() ; prints image at x,y position on page
PrintBox() ; print drawing box at x,y position
PrintLine() ; print drawing line at x,y position
PrintNewPage() ; cause a new page to be started
PrintClose() ; end of printing job (must do!)

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
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply