Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
The WWDC videos inspired me to play around with the text system.
So I am trying to dynamically replace strings in a NSTextView. A bit like syntax colouring, I guess. For example I want to replace H2O with a subscript 2 or "delta" with the greek "∆" (delta) symbol.

In NSLayoutManager I can replace unwanted glyphs with NSNullGlyph. But how do I insert the correct glyphs like subscript 2 or Delta? Gplyh mapping is a complex many - to - many system and nslayoutmanager does it normally for you.

Code:
#if 1
-(void) insertGlyphs:(const NSGlyph *)glyphs
			  length:(NSUInteger)length
forStartingGlyphAtIndex:(NSUInteger)glyphIndex
	  characterIndex:(NSUInteger)charIndex
{
	NSString *subString = [[[self attributedString] string] substringWithRange:NSMakeRange(charIndex, length)];
	
	NSGlyph *buffer = malloc(sizeof(NSGlyph) * length);

	for (NSUInteger index = 0; index < length; index++)
	{
		if ([subString characterAtIndex:index] == '_')
		{
			buffer[index] = NSNullGlyph;
		
			//next character should be subscript
			//todo
		}
		else
		{
			buffer[index] = glyphs[index];
		}
	}
	
	
    [super insertGlyphs:buffer
				 length:length
forStartingGlyphAtIndex:glyphIndex
		 characterIndex:charIndex];
	
    if (buffer)
	free(buffer);
}
#endif

Although hiding the characters works, the text caret still "sees" them in textview. I have to enter an arrow key for every hidden character to move the caret. They don't mention anything about this in the WWDC video on code folding.

The text system is horribly complex, I know, but all and any insights, suggestions or hints you might have are appreciated.
Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.