[futurebasic] Re: [FB] Core Text Sample Help

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2009 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Fri, 2 Jan 2009 09:35:00 +1300
Lake Group Ltd. wrote:

> Can someone help translate the following short Core Text example?  
> (Apple source code.)  I'm tripping over correctly defining some of  
> the new types and FNs.

FB5 demo below.
Robert P.

'---------------------
include "Tlbx CoreGraphics.incl"

// CGPath.h
#if ndef _DEFINEDINCARBON
#define CGPathRef as ptr
#define CGMutablePathRef as ptr
toolbox fn CGPathCreateMutable = CGMutablePathRef
toolbox CGPathAddRect( CGMutablePathRef path, const CGAffineTransform  
*m, CGRect rect )
#endif

// CFAttributedString.h AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER
#if ndef _DEFINEDINCARBON
#define CFAttributedStringRef as ptr
#define CFMutableAttributedStringRef as ptr
toolbox fn CFAttributedStringCreateMutable( CFAllocatorRef alloc,  
CFIndex maxLength ) = CFMutableAttributedStringRef
toolbox CFAttributedStringReplaceString( CFMutableAttributedStringRef  
aStr, CFRange range, CFStringRef replacement )
toolbox CFAttributedStringSetAttribute( CFMutableAttributedStringRef  
aStr, CFRange range, CFStringRef attrName, CFTypeRef value )
#endif

// CoreText AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
#if ndef _DEFINEDINCARBON
#define CTFrameRef as ptr
#define CTFramesetterRef as ptr
toolbox CTFrameDraw( CTFrameRef frame, CGContextRef context )
toolbox fn  
CTFramesetterCreateWithAttributedString( CFAttributedStringRef  
string ) = CTFramesetterRef
toolbox fn CTFramesetterCreateFrame( CTFramesetterRef framesetter,  
CFRange stringRange, CGPathRef path, CFDictionaryRef frameAttributes )  
= CTFrameRef
system CFStringRef kCTForegroundColorAttributeName
#endif


local fn MyCoreText( ctx as CGContextRef )
'~'1
dim as CGAffineTransform m
dim as CGMutablePathRef  path
dim as CGRect            bounds
dim as CFStringRef       string
dim as CFMutableAttributedStringRef attrString
dim as CGColorSpaceRef   rgbColorSpace
dim as float             components(3)
dim as CGColorRef        red
dim as CTFramesetterRef  framesetter
dim as CTFrameRef        frame

fn CGAffineTransformIdentity( @m )
fn CGContextSetTextMatrix( ctx, m )

// Initialize a rectangular path.
path = fn CGPathCreateMutable()
fn CGRectMake( @bounds, 10.0, 10.0, 200.0, 200.0 )
CGPathAddRect( path, 0, bounds )

// Initialize an attributed string.
string = fn CFSTR( "We hold this truth to be self-evident, that  
everyone is created equal." )
attrString = fn CFAttributedStringCreateMutable( _kCFAllocatorDefault,  
0 )
CFAttributedStringReplaceString( attrString, fn CFRangeMake( 0, 0 ),  
string )

// Create a color and add it as an attribute to the string.
rgbColorSpace = fn CGColorSpaceCreateDeviceRGB()
components(0) = 1.0
components(1) = 0.0
components(2) = 0.0
components(3) = 0.8
red = fn CGColorCreate( rgbColorSpace, @components(0) )
CGColorSpaceRelease( rgbColorSpace )
CFAttributedStringSetAttribute( attrString, fn CFRangeMake( 0, 50 ),  
kCTForegroundColorAttributeName, red )

// Create the framesetter with the attributed string.
framesetter = fn CTFramesetterCreateWithAttributedString( attrString )
CFRelease( attrString )

// Create the frame and draw it into the graphics context
frame = fn CTFramesetterCreateFrame( framesetter, fn CFRangeMake( 0,  
0 ), path, 0 )
CFRelease( framesetter )
CTFrameDraw( frame, ctx )
CFRelease( frame )
end fn


// main program
dim as CGContextRef ctx
window 1
fn CreateCGContextForPort( window( _wndPort ), @ctx )
fn MyCoreText( ctx )
CGContextSynchronize( ctx ) // show what we drew
do
HandleEvents
until gFBQuit
'-------------------