On May 24, 2012, at 9:36 AM, Manfred Schwind wrote:

>> CGContextShowTextAtPoint(contextRef, 0, 40,
>>      [textToDraw UTF8String], [textToDraw length]);
> 
> One bug I see here: you're passing a wrong length parameter.
> CGContextShowTextAtPoint expects the length of the char array (number of 
> bytes of the UTF-8 encoded string), but you'e passing the number of Unicode 
> characters in the string. That's not the same; one character may be 
> represented by multiple chars in UTF-8 encoding.
> 
> E.g. use it this way:
> 
> const char *myUTF8String = [textToDraw UTF8String];
> CGContextShowTextAtPoint(contextRef, 0, 40,
>       myUTF8String, strlen(myUTF8String));
> 

Also, CGContextShowTextAtPoint is designed only to display text in MacRoman 
encoding (and a corresponding font that has MacRoman character data tables).

If the string has anything other than ASCII text, the UTF8String won't be in 
MacRoman (and in general, MacRoman is a very small subset of what Unicode 
supports, so even if you got the string in MacRoman encoding, there is a very 
good chance it still won't draw correctly because somebody used a character 
outside of MacRoman).

>From the docs:
> If setting the font to a MacRoman text encoding is sufficient for your 
> application, use the CGContextSelectFont function. Then, when you are ready 
> to draw the text, you call the function CGContextShowTextAtPoint. The 
> function CGContextSelectFont takes as parameters a graphics context, the 
> PostScript name of the font to set, the size of the font (in user space 
> units), and a text encoding.
> 
> To set the font to a text encoding other than MacRoman, you can use the 
> functions CGContextSetFont and CGContextSetFontSize. You must supply a CGFont 
> object to the function CGContextSetFont. You call the function 
> CGFontCreateWithPlatformFont to obtain a CGFont object from an ATS font. When 
> you are ready to draw the text, you use the function 
> CGContextShowGlyphsAtPoint rather thanCGContextShowTextAtPoint.
> 

So for general case drawing, you'll need to generate glyphs from the text, 
which is an extremely complicated procedure to do without some sort of 
type-setter support (such as found in CoreText or (not available on iOS) 
NSTypesetter).


Basically, CGContextShowTextAtPoint is designed to show simple debugging 
information and very limited/controlled strings, not a general purpose drawing 
routine.

Just use NSString's drawAtPoint:withFont: and the like...



Glenn Andreas                      gandr...@gandreas.com 
The most merciful thing in the world ... is the inability of the human mind to 
correlate all its contents - HPL

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to