Re: A Tab after 4 SPACEs
Re: A Tab after 4 SPACEs
- Subject: Re: A Tab after 4 SPACEs
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Fri, 28 Mar 2008 10:16:04 +0100
On 28 Mar 2008, at 00:53, Alastair Houghton wrote:
On 27 Mar 2008, at 16:12, Gerriet M. Denkmann wrote:
Current I am using:
NSString *dummyString = [ NSString stringWithUTF8String: " " ];
NSTextView *dummyTextView = [ [ NSTextView alloc ] initWithFrame:
NSMakeRect(0,0,1e4,1e4) ];
[ dummyTextView setString: dummyString ];
[ dummyTextView setFont: usedFont ];
NSLayoutManager *dummyLayoutManager = [ dummyTextView
layoutManager ];
[ dummyLayoutManager setUsesScreenFonts: useScreenFont ];
unsigned int length = [ dummyString length ];
NSRange glyphRange = [ dummyLayoutManager
glyphRangeForCharacterRange: NSMakeRange(0,length)
actualCharacterRange: NULL ];
unsigned int glyphIndex = glyphRange.location;
NSTextContainer *aTextContainer = [ dummyLayoutManager
textContainerForGlyphAtIndex: glyphIndex effectiveRange: NULL ];
NSRect boundingRect = [ dummyLayoutManager
boundingRectForGlyphRange: glyphRange inTextContainer:
aTextContainer];
[dummyTextView release];
float widtOfDummyString = boundingRect.size.width;
This looks incredible clumsy. Do you know of any better way?
How about the following (largely untested; I did check that the
glyph returned seemed sane):
NSGlyph glyph = [usedFont glyphWithName:@"space"];
The documentation says: "Returns the named encoded glyph, or –1 if
the receiver contains no such glyph."
Returning a negative number seems to be kind of difficult as NSGlyph
is defined as unsigned int.
Tests with "sp8 f8 s83 3ace" (assumed to be name of no glyph in this
universe) returns either 0x0 (NSNullGlyph) or 0xffff, depending on font.
NSSize size
[usedFont getAdvancements:&size forGlyphs:&glyph count:1];
This returns {font-size-in-pnts,0} for NSNullGlyph, {0,0} for
absurdly high numbers (I tried NSNotFound + 14) and, for a glyph
representing a space, almost the same as my clumsy method above (e.g.
{10.8018, 0} versus {10.801, 25} for Monaco 18pt).
("space" is the standard PostScript name for the space glyph; see,
e.g. <http://www.adobe.com/devnet/opentype/archives/glyph.html>)
There are quite a few fonts which do not understand "space" and
return NSNullGlyph. E.g. AppleGothic, HiraMinPro, STSong, etc.
All of these seem to be intended for Asian languages (Korean,
Japanese, Chinese). They do seem to have a space though, which
typically is a glyph with a small number (1 or 3).
Alternatively you could use the Core Text function
CTFontGetGlyphsForCharacters() (but you'd need a CTFontRef rather
than an NSFont), or you could implement a lightweight
NSGlyphStorage and use either the shared NSGlyphGenerator or the
one from your layout manager.
I did not try this yet. The ATSUI Programming Guide seems to contain
also a lot of information, which I might look into later today.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden