1

漢字の輪郭を知りたいです。

次のコードは、ラテン文字に対して機能しています。

[letter drawInRect:brect withAttributes:attributes];
[...]
CGGlyph glyph;
glyph = [font glyphWithName: letter];
CGPathRef glyphPath = CTFontCreatePathForGlyph((__bridge CTFontRef) font, glyph, NULL);
CGPathAddPath(path0, &transform, glyphPath);

が漢字の場合letter、たとえば男の場合、文字は正しく描画されますが、CGPathRef は正方形です。漢字の輪郭を抽出するには何が必要ですか?

4

1 に答える 1

3

メソッド glyphWithName: は、文字ではなく、glyphName を想定しています。単純なラテン文字の場合、glyphName は文字 (@"A") と同じです。私の知る限り、ひらがなやカタカナには名前がありますが、漢字には名前がありません。漢字は単純に数が多すぎて、グリフの多くは同じ漢字の変形です。

したがって、漢字では別の方法を使用する必要があります。ここに私のために働く例があります。

// Convert a single character to a bezier path
- (UIBezierPath *)bezierPathFromChar:(NSString *)aChar inFont:(CTFontRef)aFont {
// Buffers
unichar chars[1];
CGGlyph glyphs[1];

// Copy the character into a buffer    
chars[0] = [aChar characterAtIndex:0];

// Encode the glyph for the single character into another buffer
CTFontGetGlyphsForCharacters(aFont, chars, glyphs, 1);

// Get the single glyph
CGGlyph aGlyph = glyphs[0];

// Find a reference to the Core Graphics path for the glyph
CGPathRef glyphPath = CTFontCreatePathForGlyph(aFont, aGlyph, NULL);

// Create a bezier path from the CG path
UIBezierPath *glyphBezierPath = [UIBezierPath bezierPath];
[glyphBezierPath moveToPoint:CGPointZero];
[glyphBezierPath appendPath:[UIBezierPath bezierPathWithCGPath:glyphPath]];

CGPathRelease(glyphPath);

return glyphBezierPath;
}

次のように使用します。

NSString *theChar = @"男";

CTFontRef font = CTFontCreateWithName(CFSTR("HiraKakuProN-W6"), 114.0, NULL);

UIBezierPath *glyphBezierPath = [self bezierPathFromChar:theChar inFont:font];

編集 - ローカライズ可能なフォントを定義する別の方法:

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica-Bold", 114.0, NULL);
于 2012-10-25T20:17:12.227 に答える