Re: Helvetica in NSQuickDrawView
Re: Helvetica in NSQuickDrawView
- Subject: Re: Helvetica in NSQuickDrawView
- From: Andrew Thompson <email@hidden>
- Date: Fri, 25 Apr 2003 08:27:02 -0400
If all you want is the height, you should be able to make an NSFont.
This code may not be the best way of doing things, but it draws a box
around a character pretty well. You won't need an NSLayoutManager if
you're only interested in the height.
- (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *)
controlView {
NSColor *borderColor = [NSColor lightGrayColor];
NSFont *currFont = [super font];
float asc = [currFont ascender];
float desc = [currFont descender];
float xheight = [currFont xHeight];
NSString *title = [super title];
NSTextStorage *textStorage = nil;
NSLayoutManager * layoutManager = nil;
if (title != nil && [title length] > 0 && [currFont
UTF32CharIsRenderable: [title UTF32CharAtIndex:0]]) {
//clear previous drawing
[[self backgroundColor] set];
NSRectFill(NSInsetRect(cellFrame, 1.0, 1.0));
//text storage serves as an Attributed string for drawing, and
it lets us query glyph attributes
textStorage = [[NSTextStorage alloc] initWithString: title
attributes: [self getAttr: currFont]];
layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager: layoutManager];
float advance = 0;
if ([layoutManager numberOfGlyphs] > 0) {
advance = [currFont advancementForGlyph: [layoutManager
glyphAtIndex: 0]].width;
}
//what we're doing here is putting the baseline of the text in
the exact center
//of the cell vertically. The descender goes below the middle
float topOffset = NSMidY(cellFrame) - [textStorage size].height
+ abs(desc);
float baseline = [textStorage size].height - abs(desc) +
topOffset;
//Work out how to center the text horizontally in the cell, and
draw the advancement borders
float leftOffset = NSMidX(cellFrame) - (advance / 2);
//draw borders around the text
float linex = leftOffset;
float liney = baseline;
[borderColor set];
[NSBezierPath strokeLineFromPoint: NSMakePoint(linex, 0)
toPoint: NSMakePoint(linex, cellFrame.size.height)];
linex += advance;
[NSBezierPath strokeLineFromPoint: NSMakePoint(linex, 0)
toPoint: NSMakePoint(linex, cellFrame.size.height)];
liney = baseline;
//draw the baseline and descender lines etc
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, liney)
toPoint: NSMakePoint(cellFrame.size.width, liney)];
liney = baseline + abs(desc); //since desc is negative
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, liney)
toPoint: NSMakePoint(cellFrame.size.width, liney)];
liney = baseline - asc;
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, liney)
toPoint: NSMakePoint(cellFrame.size.width, liney)];
liney = baseline - xheight;
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, liney)
toPoint: NSMakePoint(cellFrame.size.width, liney)];
//draw the text
[textStorage drawAtPoint: NSMakePoint(leftOffset, topOffset)];
//just wanted advancement, so get rid of these
[layoutManager release];
[textStorage release];
} else {
//no char to display
[super setTitle: @""];
[super setTextColor: borderColor];
[super drawInteriorWithFrame: cellFrame inView: controlView];
}
}
These functions demonstrate how to convert between NSFont, ATSFontRef
and FMFont. Well, actually they do more than just that, but they should
point you in the direction you need.
+ (ATSFontRef) NSFontToATSFont: (NSFont *) a_font {
ATSFontRef result = ATSFontFindFromPostScriptName((CFStringRef)
[a_font fontName], kATSOptionFlagsDefault);
return (result != kATSFontFamilyRefUnspecified) ? result : nil;
}
- (void) setOtherFontInfo: (NSFont *) a_currFont {
FourCharCode format=0;
FSSpec fontFileSpec;
OSStatus err=noErr;
BOOL success = NO;
FSRef fontFileRef;
CFURLRef fontURL = nil;
CFStringRef fontPath = nil;
FMFont theFMFont = nil;
ATSFontRef theATSFont = [FontInfoController NSFontToATSFont:
a_currFont];
if (theATSFont == nil) goto cleanup;
err = ATSFontGetFileSpecification(theATSFont, &fontFileSpec);
if (err != noErr) goto cleanup;
err = FSpMakeFSRef(&fontFileSpec, &fontFileRef);
if (err != noErr) goto cleanup;
fontURL = CFURLCreateFromFSRef (kCFAllocatorDefault, &fontFileRef);
//save the url in an instance variable, for later use in the reveal
in finder method
m_fontURL = [((NSURL *) fontURL) retain];
fontPath = CFURLCopyFileSystemPath (fontURL, kCFURLHFSPathStyle);
//set the filename in the user interface
[[m_filename cell] setTitle: (NSString *) fontPath];
theFMFont = FMGetFontFromATSFontRef(theATSFont);
err = FMGetFontFormat(theFMFont, &format);
if (err != noErr) NSLog(@"Problem getting fonr format: %i", err);
[[m_format cell] setTitle: [FontInfoController formatCodeToString:
format]];
success = YES;
[self getPropertiesFromFont: theATSFont];
cleanup:
if (fontURL != nil) CFRelease(fontURL);
if (fontPath != nil) CFRelease(fontPath);
if (!success) {
[[m_format cell] setTitle: @"Not Known"];
[[m_filename cell] setTitle: @"Not Known"];
}
}
Don't get discouraged by the amount of code above. For what you'll want
to do it'll probably come down to about 5 lines :)
On Friday, Apr 25, 2003, at 06:04 America/New_York, Dix Lorenz wrote:
Hi,
I have ported some old (10 years?) Quickdraw-code into my brandnew
Cocoa-App, using NSQuickDrawView. I am more than amazed how well it
worked and how easy the transition was, but I have one problem: I am
using GetFontInfo and TEGetHeight to find the needed height for a text
without drawing it. This code has worked all these years and continues
to work... Unless I use Helvetica. Using Helvetica 12 pt, I get a
TextHeight of 12, which is obviously to small. It works for other
fonts I tried (Lucida Grande and Arial)...
The code in question:
FontInfo theFontInfo;
::GetFontInfo(&theFontInfo);
(*mTE)->lineHeight = (theFontInfo.descent + theFontInfo.ascent +
theFontInfo.leading);
int theHeight = ::TEGetHeight(32767,1,mTE);
I know the Apple Docs say I shouldn't use TextEdit anymore and use
MLTE, but if there is a way to simply find the height of a text, just
using a Grafport, without actually drawing it, I didn't see it...
Any suggestions or ideas?
Thanks,
Dix
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
AndyT (lordpixel - the cat who walks through walls)
A little bigger on the inside
(see you later space cowboy ...)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.