olaf
olaf

Reputation: 73

Trouble with CGFontGetGlyphWithGlyphName()

I have been trying to create CGGlyph with CGFontGetGlyphWithGlyphName() method. It is working for all letters, but no for numbers.

-(void)drawInContext:(CGContextRef)ctx{
    CGContextSetAllowsAntialiasing(ctx, true);
    CGContextSetFont(ctx, font);
    CGContextSetFontSize(ctx, size);
    CGGlyph glyph = CGFontGetGlyphWithGlyphName(font, CFSTR("L"));
    CGContextShowGlyphsAtPositions(ctx, &glyph, &(textbounds.origin), 1);
}

Windows shows L letter

And when I am trying to draw number it does not work:

-(void)drawInContext:(CGContextRef)ctx{
    CGContextSetAllowsAntialiasing(ctx, true);
    CGContextSetFont(ctx, font);
    CGContextSetFontSize(ctx, size);
    CGGlyph glyph = CGFontGetGlyphWithGlyphName(font, CFSTR("3"));
    CGContextShowGlyphsAtPositions(ctx, &glyph, &(textbounds.origin), 1);
}

Window shows empty rect

Upvotes: 0

Views: 112

Answers (1)

rob mayoff
rob mayoff

Reputation: 385510

The glyph for “3” is probably named three (spelled out). Here's my test:

import Foundation
import CoreGraphics

let font = CGFont("Helvetica" as CFString)!
for g in CGGlyph.min ... CGGlyph.max {
    if let name = font.name(for: g) {
        print("\(g) \(name)")
    }
}

Output:

... many lines omitted ....
17 period
18 slash
19 zero
20 one
21 two
22 three
23 four
24 five
25 six
26 seven
... many more lines omitted ...

You might be better off using the Core Text CTFontGetGlyphsForCharacters function. Note that it uses a CTFontRef, not a CGFontRef. You can convert a CGFontRef to a CTFontRef using CTFontCreateWithGraphicsFont.

Upvotes: 1

Related Questions