Re: Make characters same width in textview
Re: Make characters same width in textview
- Subject: Re: Make characters same width in textview
- From: Douglas Davidson <email@hidden>
- Date: Mon, 1 Apr 2002 13:31:31 -0800
On Monday, April 1, 2002, at 11:25 AM, Michael Gersten wrote:
Ok, here's a different question on same-width fonts:
Most fixed-width fonts aren't as pretty as variable width fonts.
I'd like to know if there's an easy way (ok, I know there's no easy
way, but how about any way) to say "I want to use font x, and I want
the characters centered on the middle of the n-sized cell".
If you've used some terminal programs that let you select arbitrary
fonts, you've probably noticed that they usually use w-sized, or
m-sized (whichever is larger) cells, and they left-edge the letters,
instead of centering the letters (ruins thin letters like 'i' and 'l')
In other words, I'd like to have a window that is 80x25 (or whatever)
times the size of the space needed for the 'n', plus a little more on
the edge; I'd like to consider that I can put any character at the
middle of one of those cells; if I have adjacent 'w' or 'm', then I'll
get a little overlap.
Can this be done? Is this too much of a wish that only I would possibly
want?
Yes, and probably yes.
This can be done, but not terribly easily. The straightforward way to
do this would be to subclass NSTypesetter--probably subclassing the
abstract NSTypesetter rather than the concrete
NSSimpleHorizontalTypesetter. NSTypesetter has a very simple interface,
but you will have to do all of the layout yourself. With a simple
enough layout model--which you seem to have--that might not be
prohibitive, but it still wouldn't be especially easy.
The main NSTypesetter method you have to implement is
- (void)layoutGlyphsInLayoutManager:(NSLayoutManager *)layoutManager
startingAtGlyphIndex:(unsigned)startGlyphIndex
maxNumberOfLineFragments:(unsigned)maxNumLines nextGlyphIndex:(unsigned
*)nextGlyph;
This will be called on you by the layout manager as needed. As a
typesetter, you are required to do the layout and inform the layout
manager of the results by calling
-[NSLayoutManager setTextContainer:forGlyphRange:]
-[NSLayoutManager setLineFragmentRect:forGlyphRange:usedRect:]
-[NSLayoutManager setLocation:forStartOfGlyphRange:]
for appropriate ranges of glyphs. There are a few other methods you can
call as needed (e.g. setNotShownAttribute:forGlyphAtIndex:,
setAttachmentSize:forGlyphRange:,
setDrawsOutsideLineFragment:forGlyphAtIndex:, etc.) but you probably
won't need them.
You are supposed to get the geometry in which to do the layout from the
layout manager's text containers, but if you are making a subclass
purely for your own private use you could skip that.
Note that you may be called to lay out any line of the text at any time,
although previous lines should already be laid out when you are called.
You can find out where to position the current line by looking at where
the previous line was placed--call -[NSLayoutManager
lineFragmentRectForGlyphAtIndex:effectiveRange:] with the index of the
previous glyph, if there was one.
Hope this helps,
Douglas Davidson
_______________________________________________
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.