views:

885

answers:

3

Hi,

I am working on an app where I am using CGContextShowTextAtPoint to display text to the screen. I want to also display Japanese characters, but CGContextShowTextAtPoint takes as its input a C string. So either A) How do I change Japanese characters into a C string? If this is not possible, B) How can I manually print Japanese characters to the screen (within the drawRect method).

Thanks in advance :)

+1  A: 

Hi, you need to use UIKit for this.

Check out [NSString drawAtPoint:...] to get started.

This SO question is useful, too.

I don't know what they were thinking with the CoreGraphic text stuff, it's useless.

Rhythmic Fistman
Thanks :) ! After reading a little into the Docs, it seems I've coded something along the lines of the NSAttributedString class so I guess now I should switch all my code to that :(
kiyoshi
You're welcome. p.s. There's no NSAttributeString on the iPhone, so check out NSLabel.
Rhythmic Fistman
The Core Graphics text drawing routines have been around on the Mac for a long while, I'm guessing before Unicode support became common, so it's not too surprising that they only support the character set they do. You may also be able to print custom characters with CGContextShowGlyphsAtPoint(), but the challenge is generating the glyphs on the iPhone. This post has an example: http://www.gogo-robot.com/devblog/?p=5
Brad Larson
Thanks Brad, after looking into it a bit I think I actually will go with the CGContextShowGlyphsAtPoint thing since it fits my code better. As long as I can figure out the glyphs thing...
kiyoshi
Good luck converting your unicode char to glyph indices. The CG interface for that appears to be private. However maybe kiyoshi has his own Japanese font (wow!) and can hard code the conversion.
Rhythmic Fistman
Yeah gave up on converting to glyphs. Ended up using the [NSString drawAtPoint:withFont: ] and it works great :)
kiyoshi
All's well that ends well.
Rhythmic Fistman
+1  A: 

For what it's worth, I spent a long time trying to get Japanese characters to work well in CoreGraphics, and didn't like where it left me.

In the end I switched to using UILabels to handle the text. All the CoreGraphics-like stuff I needed could be replicated using the transform & animation support, and in the end the resulting code was much simpler.

It may not be appropriate for your situation, but it's worth considering.

Kris Jenkins
A: 

I was able to get this working by using a reimplementation of CGFontGetGlyphsForUnichars by Jens Egeblad: GlyphDrawing.mm

First load in a Japanese font as an otf file from the app bundle:

// Load font file from otf file
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"HStdNW8" ofType:@"otf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef _cgFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);

Then you can convert your unichar text to glyphs and draw them:

NSString *text = @"日本語"
CGContextSetFont(context, _cgFont);
CGContextSetFontSize(context, 12);
CGGlyph textGlyphs[[text length]];

unichar textChars[[text length]];
for(int i = 0; i < [text length]; i++) {
    textChars[i] = [text characterAtIndex:i];
}
CMFontGetGlyphsForUnichars(_cgFont, textChars, textGlyphs, [text length]);
CGContextShowGlyphsAtPoint(context, xCoord, yCoord, textGlyphs, [text length]);
Smendrick