[futurebasic] FB Examples broken as of FB's current ( 5.7.99 ) release

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2017 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Thu, 30 Mar 2017 04:00:01 -0400

This fixes QD and CG to PDF — A tribute to 13-year-old R.P. code that stands the test of time in macOS Sierra 10.12.4.

/*
How to print to a pdf under program control
without using a Print Manager dialog.
Both QD and CG vector graphics are saved to the PDF..

The code could be simplified by requiring the user to
generate the pdf by clicking the appropriate button in
a print dialog. Offering the save file dialog directly,
as in this demo, is less prone to user error.

Original demo   Robert P.  July 17  2004
Adapted for FB5; added CG drawing  20080919
Revised 20170330 KS
*/

#define PMDestinationType as UInt16
_kPMDestinationFile = 2

toolbox fn PMSessionSetDestination( ¬
  PMPrintSession      printSession,¬
  PMPrintSettings     printSettings,¬
  PMDestinationType   destType,¬
  CFStringRef         destFormat,¬
  CFURLRef            destLocation ) = OSStatus

toolbox FBBeginSession() // in runtime

include "Tlbx CoreGraphics.incl"

/*
Initialise the FB printing globals, and set
the print session to create a pdf file
*/
local fn SetupPrintToPDF( destCFURL as CFURLRef ) as OSStatus
'~'1
dim as OSStatus     err

if ( destCFURL )
fn FBBeginSession // initialise gFBPrintSession, gFBPrintSettings
err = fn PMSessionSetDestination( gFBPrintSession, gFBPrintSettings, _kPMDestinationFile, @"application/pdf", destCFURL )
CFRelease( destCFURL )
else
err = 1
end if
end fn = err


local mode
local fn DoMyPrinting
'~'1
dim as CGContextRef      cgContext
dim as Str255            fontName, txt
dim as CGrafPtr          port

route _toPrinter
// let's print some QuickDraw text...
print "QD text, line, circle"
// ...a QuickDraw line...
MoveTo( 50, 50 ) : LineTo( 200, 200 )
// ...a QuickDraw circle...
circle 150, 50, 30

// ...some CoreGraphics text...
GetPort( @port )
fn QDBeginCGContext( port, @cgContext )
fontName = "Lucida Grande"
fn FBPStr2CStr( @fontName )
CGContextSelectFont( cgContext, @fontName, 12.0, _kCGEncodingMacRoman)
txt$= "Quartz text, line"
// coordinate system has 0,0 at bottom left
CGContextShowTextAtPoint( cgContext, 20, 220, (ptr)@txt[1], txt[0])

// ...and a CoreGraphics line
CGContextBeginPath( cgContext )
CGContextMoveToPoint( cgContext, 10, 240 )
CGContextAddLineToPoint( cgContext, 120, 240 )
CGContextStrokePath( cgContext )

CGContextSynchronize( cgContext )
fn QDEndCGContext( port, @cgContext )

route _toScreen
// close LPrint saves pdf (if SetupPrintToPDF was called)
close LPrint
end fn


// main program
dim as CFURLRef   url
dim as OSStatus   err

// get a CFURLRef for the pdf we want to print to
long if ( files$( _CFURLRefSave, "Save pdf as…", "Untitled.pdf", url ) != "" )

err = fn SetupPrintToPDF( url )
long if ( err )
stop "SetupPrintToPDF error " + str$( err )
xelse
fn DoMyPrinting
end if

end if
end