Anyway to add fonts to an iPhone program?

Discussion in 'Mac Programming' started by yayaba, Apr 6, 2008.

  1. macrumors 6502

    Joined:
    Apr 24, 2007
    Messages:
    297
    Location:
    San Francisco Bay Area
    #1
    The iPhone doesn't have very many built in fonts. Is there a way to embed fonts inside your program to display?
     
  2. macrumors newbie

    Joined:
    Apr 7, 2008
    Messages:
    14
    #2
    Non-Functional code snippet

    removed
     
  3. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Messages:
    1,251
    Location:
    /dev/null
    #3
  4. macrumors newbie

    Joined:
    Apr 7, 2008
    Messages:
    14
    #4
    I greatly appreciate the help! Unfortunately the solutions provided through the threads require linking to a framework that isn't supported by the Iphone SDK. Hopefully they will release a little more support for getting custom fonts working in a later version. = )
     
  5. macrumors newbie

    Joined:
    Apr 7, 2008
    Messages:
    14
    #5
    removed
     
  6. macrumors newbie

    Joined:
    Apr 7, 2008
    Messages:
    14
    #6
    removed
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Messages:
    3,172
    Location:
    Dallas, TX
    #7
    Interesting work, I hope this is helpful to others working on iPhone development. i was curious about the 29, it seemed nonsensical. After some research I wanted to warn that that may be specific to the font you are working with:
    http://developer.apple.com/document.../reference.html#//apple_ref/doc/c_ref/CGGlyph

    Per that page a GCGlyph is:
    I don't know that every font will have things at the some position in their internal glyph table. I'm not a truetype guru, so maybe they will be, but it doesn't seem like that's a guarantee.

    -Lee
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Messages:
    3,172
    Location:
    Dallas, TX
    #8
    I suddenly got very interested in this and started digging more. There is a cmap, or "Character to Glyph Map" in the font (perhaps a few, for different platforms). This states which glyph should be used for a character in an outside encoding such as unicode. From:
    http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&item_id=IWS-Chapter08#ba57949e

    I am not sure if the cmap(s) can be accessed using the CGFontRef, but I am going to keep digging. If there is, it should be easy to write a routine that takes an NSString or utf8 string and turns it into an array of Glyphs.

    -Lee
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Messages:
    3,172
    Location:
    Dallas, TX
    #9
    I'm not there yet, but this seems to be the way you might be able to get glyphs for a generic font:

    Code:
    NSTextStorage
      setFont
        You have to pass an NSFont to this. This might complicate things because you have a CGFontRef, but hopefully it's easy to get the NSFont pretty similarly.
      setCharacters
        Pass an NSArray of characters. I would just pass in 0-127 or 0-256
    
    NSLayoutManager
      glyphIndexForCharacterAtIndex
        You should be able to pass in a character index (which would just be the ascii values or unicode code points) and get the glyph back. it will be a NSUInteger, but you should be able to cast that to a CGGlyph. The CGGlyph is an unsigned short, not an integer, but I think the cast should do it.
    I'm afraid other than parsing the font yourself, this is the only way to go. I have C code that parses a ttf, but I didn't write it and it belongs to my employer, so I probably can't share it. It's painful anyway.

    You may not need to do this, but it's probably the best way to go unless you want to make the glyph list for each font you will use.

    That is also a valid method. For each font you will use, just figure out a map of the characters (at least the ones you want to use) to the glyphs and keep it around to use when you use that font.

    Good luck.

    -Lee
     
  10. macrumors 6502

    Joined:
    Apr 24, 2007
    Messages:
    297
    Location:
    San Francisco Bay Area
    #10
    Wow, great work guys. I've been a bit busy lately so I haven't had time to code (haven't even tried Beta 3 yet!) but I'll start digging around what you guys suggested.

    Thanks!
     
  11. macrumors newbie

    Joined:
    Apr 4, 2003
    Messages:
    2
    #11
    Hey guys, I was in need of a unicode-to-glyph converter, so here's what I came up with using the lead provided by lee1210 (thanks!).

    Note the comments/warnings in the body and that since this proc has high overhead, you'll want to cache the results, etc. The class 'UtilStr' is our in-house string C++ wrapper and should be self-explainitory.

    Finally, can someone confirm that I'm not leaking anything? I'm not a Cocoa guy, so that stuff doesn't jump out at me. Thanks in advance!

    Code:
    void GetGlyphsForUniChars( const UtilStr& inTypefaceName, const UniChar inChars[], int inNumChars, CGGlyph outGlyphs[] ) {
    
    	// A negative length denotes that inChars[] is NUL terminated.
    	if ( inNumChars < 0 ) {
    		inNumChars = 0;
    		while ( inChars && inChars[ inNumChars ] )
    			inNumChars++;
    	}
    
    	if ( inNumChars <= 0 )
    		return;
    
    	NSString* srcChars = [ NSString stringWithCharacters: inChars length: inNumChars ];
    	NSString* fontName = [ NSString stringWithUTF8String: inTypefaceName.CStr() ];
    	NSFont* font = [ NSFont fontWithName:fontName size:0 ];
    
    	NSTextStorage* textStorage = [[ NSTextStorage alloc ] initWithString: srcChars ];
    	
    	if ( font ) {
    		[ textStorage setFont: font ];
    	} else {
    		ss_break();
    	}
    
    	NSLayoutManager* layoutMgr = [[ NSLayoutManager alloc ] init ];
    	[ textStorage addLayoutManager: layoutMgr ];
    
    
    	// Unicode strings can be arbitraily complex and all kinds of reordering and glyph combination can occur (including consolidation to fewer glyphs).
    	// We assume below that one unicode char maps to one glyph and that they occur in order.  Since this assuption doesn't hold for asian languages, 
    	//    we must be careful.  Fortunately, for typical western fonts and the text we intend to convert, this is good enough.
    	SInt32 numGlyphs = [ layoutMgr numberOfGlyphs ];
    	ss_assert( (int) numGlyphs == inNumChars );
    
    	NSGlyph* nsglyphs = (NSGlyph*) ss_malloc( sizeof( NSGlyph ) * numGlyphs );
    
    	NSRange range = { 0, numGlyphs };
    	[ layoutMgr getGlyphsInRange : range glyphs: nsglyphs characterIndexes:NULL glyphInscriptions:NULL elasticBits:NULL ];
    
     	{
    		int i;
    
    		for ( i = 0; i < numGlyphs; i++ ) {
    			if ( nsglyphs[ i ] < 0xFFFF ) {
    				outGlyphs[ i ] = (CGGlyph) nsglyphs[ i ];
    			} else {
    				outGlyphs[ i ] = 0;
    			}
    		}
    
    		for ( ; i < inNumChars; i++ ) {
    			outGlyphs[ i ] = 0;
    		}
    	}
    
    	ss_free( nsglyphs );
    	nsglyphs = NULL;
    
    	[ layoutMgr release ];
    	[ textStorage release ];
    }
    
     

Share This Page