[futurebasic] Re: [FB] CGContext & CGFonts

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2011 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Wed, 20 Jul 2011 17:28:26 +1200
Sofia wrote:

> Your font panel suggestion is a good one, but assuming that it could also pass the underline attribute to the calling thread, how would we apply that attribute? CGContextSelectFont accepts only the font name and size as parameters.

You can't, but perhaps you can live without underlining for the reasons given here:
<stewartdesign.com/2009/04/basic-tips-for-more-fashionable-typography/>

The minimal demo below shows the principle of drawing CG text with font/size determined by the Font Panel.

This works out OK for most fonts. But some fonts with 'oriental' names give outline rectangles instead of letters; I don't understand this bug.

Robert P.


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

#define ATSUFontID   as FMFont
#define FontNameCode as UInt32
#define FontPlatformCode as UInt32
#define FontScriptCode   as UInt32
#define FontLanguageCode as UInt32
system UInt32 kFontFullName
system UInt32 kFontNoPlatformCode
system UInt32 kFontNoScriptCode
system UInt32 kFontNoLanguageCode

toolbox fn ATSUFindFontName(¬
   ATSUFontID iFontID,¬
   FontNameCode iFontNameCode,¬
   FontPlatformCode iFontNamePlatform,¬
   FontScriptCode iFontNameScript,¬
   FontLanguageCode iFontNameLanguage,¬
   ByteCount iMaximumNameLength,¬
   Ptr oName,¬
   ByteCount *oActualNameLength,¬
   ItemCount *oFontNameIndex ) = OSStatus


// private to the 4 getters/setters below
begin globals
dim as ATSUFontID   sATSUFontID
dim as CGFloat      sUserFontSize : sUserFontSize = 18.0 // default
end globals

local fn SetUserATSUFontID( font as ATSUFontID )
'~'1
sATSUFontID = font
end fn

local fn GetUserFontPostscriptName as Str255
'~'1
dim as ByteCount   actLen
dim as ItemCount   idx
dim as Str255      name

long if ( sATSUFontID == 0 )
name = "Lucida Grande" // default
xelse
fn ATSUFindFontName( sATSUFontID, kFontFullName, kFontNoPlatformCode, kFontNoScriptCode, kFontNoLanguageCode, 255, @name[1], @actLen, @idx )
name[0] = actLen
end if
end fn = name

local fn SetUserFontSize( atsuiSize as Fixed )
'~'1
dim as long  x

x = atsuiSize >> 16
sUserFontSize = x
end fn

local fn GetUserFontSize as CGFloat
'~'1
end fn = sUserFontSize



local mode
local fn DemoCGTextInCurrentPort
'~'1
dim as CGContextRef   ctx 
dim as Str255         fontName, txt

cls
fn QDBeginCGContext( window( _wndPort ), @ctx )

fontName = fn GetUserFontPostscriptName
fn FBPStr2CStr( @fontName )
CGContextSelectFont( ctx, @fontName, fn GetUserFontSize, _kCGEncodingMacRoman )
txt = "abcdefg"
CGContextShowTextAtPoint( ctx, 20, 300, @txt[1], txt[0] )

fn QDEndCGContext( window( _wndPort ), @ctx )
end fn



local mode
local fn FontSelectionHandler( nextHandler as EventHandlerCallRef, theEvent as EventRef, userData as ptr )
'~'1
dim as OSStatus    err
dim as ATSUFontID  fontID
dim as Fixed       size
dim as Boolean     changed : changed = _false

err = fn GetEventParameter( theEvent, _kEventParamATSUFontID, _typeATSUFontID, 0, sizeof( ATSUFontID ), 0, @fontID )
long if ( err ==  _noErr )
fn SetUserATSUFontID( fontID )
changed = _true
end if
err = fn GetEventParameter( theEvent, _kEventParamATSUFontSize, _typeATSUSize, 0, sizeof( ATSUFontID ), 0, @size )
long if ( err ==  _noErr )
fn SetUserFontSize( size )
changed = _true
end if

// refresh the UI
if ( changed ) then fn DemoCGTextInCurrentPort

end fn = _noErr


local mode
local fn InstallFontSelectionHandler
'~'1
dim as EventTypeSpec  ev

ev.eventClass = _kEventClassFont
ev.eventKind  = _kEventFontSelection
end fn = fn InstallEventHandler( fn GetApplicationEventTarget(), @fn FontSelectionHandler, 1, @ev, 0, 0 )


local mode
local fn InitApp
'~'1
dim as FontSelectionQDStyle  initFont

window 1, "CG Font Tester"
// set up the font panel
initFont.version  = _kFontSelectionQDStyleVersionZero
initFont.hasColor = _false
initFont.instance.fontFamily = _sysFont // Lucida Grande
initFont.instance.fontStyle  = 0
initFont.size = fn GetUserFontSize
fn SetFontInfoForSelection( _kFontSelectionQDType, 1, @initFont, 0 )
fn InstallFontSelectionHandler
fn FPShowHideFontPanel() // show
end fn


fn InitApp
RunApplicationEventLoop()
'---------------