0

I have to show Chinese characters with the function:

CG_EXTERN void CGContextShowGlyphsAtPoint(CGContextRef context, CGFloat x,
CGFloat y, const CGGlyph glyphs[], size_t count)

But it doesn't show accurately. The code I use is as follows:

CGFontRef cgfont = CGFontCreateWithFontName((CFStringRef)label.font.fontName);
CGContextSetFont(theContext, cgfont);
CGContextSetFontSize(theContext, label.font.pointSize);
CGContextSetTextDrawingMode (theContext, kCGTextClip);

CGGlyph *glyphs = malloc(sizeof(CGGlyph) * [label.text length]);
char *Chars = malloc(sizeof(char) * ([label.text length] + 1));
[label.text getCString:Chars maxLength:([label.text length] + 1) encoding:NSISOLatin2StringEncoding];

for(int currentChar = 0; currentChar < [label.text length]; ++currentChar)
{
    glyphs[currentChar] = Chars[currentChar];
}
CGContextShowGlyphsAtPoint(theContext, 0, (size_t)label.font.ascender, glyphs, [label.text length]);

Edit

The device is iPhone. For example, I want show the Chinese characters like "中文", but use CGContextShowGlyphsAtPoint to draw the string will show like this "@#Radcx67".

How to solve this problem? Thanks!

STW
  • 44,917
  • 17
  • 105
  • 161
Bunsman
  • 1
  • 2
  • Could you be more specific? E.g. on what you mean by "doesn't show accurately"? Please, give an example of what you expected and what you got instead. Besides, can you specify what devices you are developing for? Mac, iPad, iPhone? None of them?!? – Paolo Stefan May 21 '12 at 08:49
  • Thank you for your reply.The device is iPhone. For example, I want show the Chinese characters like "中文", but use CGContextShowGlyphsAtPoint to draw the string will show like this "@#Radcx67". – Bunsman May 23 '12 at 05:19
  • I edited your question with the info you provided, I hope it gets more attention now. – Paolo Stefan May 23 '12 at 09:50
  • Thank you.Through I found some information that may be use glyph can not draw the Chinese characters. So, does have another way to work it out? – Bunsman May 24 '12 at 06:02
  • Maybe you should edit your question, answer yourself or post another question mentioning the source of the information you've found... good luck! – Paolo Stefan May 28 '12 at 13:07
  • For drawing text, I guess `CoreText` is what you want. With either `CTTypesetter` or `CTFrameRef`, you can do pretty much everything related to drawing text. – neevek Jun 06 '12 at 01:49

1 Answers1

1

First, include this

 #import "CoreText/CTFont.h"

Then Please use below function.

 void drawStringWithglyphs(CTFontRef iFont, CFStringRef iString, CGContextRef ctx, int x, int y)

{
UniChar *characters;
CGGlyph *glyphs;
//    CGPoint *points;
CFIndex count;

assert(iFont != NULL && iString != NULL);

// Get our string length.
count = CFStringGetLength(iString);

// Allocate our buffers for characters and glyphs.
characters = (UniChar *)malloc(sizeof(UniChar) * count);
assert(characters != NULL);

glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * count);
assert(glyphs != NULL);

// Get the characters from the string.
CFStringGetCharacters(iString, CFRangeMake(0, count), characters);

CTFontGetGlyphsForCharacters(iFont, characters, glyphs, count);


// Do something with the glyphs here, if a character is unmapped
    CGContextShowGlyphsAtPoint(ctx, x, y, glyphs, count);

// Free our buffers
free(characters);
free(glyphs);
}
Jiejing Zhang
  • 1,030
  • 8
  • 16
  • Thank you for your reply.I have tried, but still don't work.I replace the method: CGContextShowGlyphsAtPoint with this: CTFontRef ctfont = CTFontCreateWithName((CFStringRef)label.font.fontName, label.font.pointSize, &xform); drawStringWithglyphs(ctfont, (CFStringRef)label.text, theContext, 0, (size_t)label.font.ascender); does it is correct way? – Bunsman Jul 03 '12 at 08:27
  • at last, I success with use NSString draw method: CGContextSaveGState(ctx); CGAffineTransform save = CGContextGetTextMatrix(ctx); CGContextTranslateCTM(ctx, 0.0f, self.bounds.size.height); CGContextScaleCTM(ctx, 1.0f, -1.0f); [str drawAtPoint:point withFont:font]; CGContextSetTextMatrix(ctx, save); CGContextRestoreGState(ctx); – Jiejing Zhang Jul 04 '12 at 17:56
  • see this link:http://stackoverflow.com/questions/1252343/flipped-nsstring-drawing-in-cgcontext/11281065#11281065 – Jiejing Zhang Jul 04 '12 at 17:57