CGFontGetGlyphBBoxes错误的结果 [英] CGFontGetGlyphBBoxes wrong result
本文介绍了CGFontGetGlyphBBoxes错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试精确测量字形范围,但是此代码可以打印出916 !!!实际宽度为69。
I have been trying to measure glyph bounds precisely but this code prints out 916!!! The real width of this is 69.
- (void)drawRect:(NSRect)dirtyRect {
CGContextRef main = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetTextMatrix(main, CGAffineTransformIdentity);
CGGlyph g;
CGPoint p = CGPointMake(100, 100);
CGRect rect = CGRectMake(0, 0, 0, 0);
CGFontRef font = CGFontCreateWithFontName((CFStringRef)@"Arial");
g = CGFontGetGlyphWithGlyphName(font, CFSTR("L"));
CGContextSetFont(main, font);
CGContextSetTextPosition(main, 0, 0);
CGContextSetFontSize(main, 200);
CGContextSetRGBFillColor(main, 0, 0, 1, 1);
CGContextShowGlyphsAtPositions(main, &g, &p, 1);
CGFontGetGlyphBBoxes(font, &g, 1, &rect);
printf("%f", rect.size.width);
}
推荐答案
您正在使用 CGFontGetGlyphBBoxes
,它以字形空间单位返回大小。要使用此功能,您需要使用每个em的单位和字体大小对其进行缩放。
You are using CGFontGetGlyphBBoxes
, which returns the size in glyph space units. To use this, you need to scale it with the units per em and the font size.
CGRect rect;
CGFontRef font = CGFontCreateWithFontName((CFStringRef)@"Arial");
CGFloat fontSize = 200.0;
CGGlyph g = CGFontGetGlyphWithGlyphName(font, CFSTR("L"));
CGFontGetGlyphBBoxes(font, &g, 1, &rect);
CGFloat width = rect.size.Width / CGFontGetUnitsPerEm(font) * fontSize;
使用 [NSFont boundingRectForCGGlyph:]的另一种方法code>。
An alternate way to do it to use [NSFont boundingRectForCGGlyph:]
.
NSFont *font = [NSFont fontWithName:@"Arial" size:200];
NSRect rect = [font boundingRectForCGGlyph:g];
boundingRectForCGGlyph
返回指定字形的边界矩形,缩放到接收者的大小。
boundingRectForCGGlyph
Returns the bounding rectangle for the specified glyph, scaled to the receiver’s size.
这篇关于CGFontGetGlyphBBoxes错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文