CGGlyphs 错误的位置
CGGlyphs wrong position
我一直在尝试用核心文本绘制单个字形,但字母的 x 位置有点不同。红色矩形显示正确的位置。
CGContextRef main = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetAllowsAntialiasing(main, false);
CGContextSetFont(main, font);
CGContextSetFontSize(main, 200);
CGContextSetTextPosition(main, 0, 0);
glyphs[0] = CGFontGetGlyphWithGlyphName(font, CFSTR("E"));
points[0] = CGPointMake(100, 100);
CGContextSetRGBFillColor(main, 0, 1, 0, 1);
CGContextShowGlyphsAtPositions(main, glyphs, points, 1);
CGRect *r = malloc(sizeof(CGRect)*1);
CGFontGetGlyphBBoxes(font, glyphs, 1, r);
float t = roundf(r[0].size.width/CGFontGetUnitsPerEm(font)*200);
float t2 = roundf(r[0].size.height/CGFontGetUnitsPerEm(font)*200);
CGRect r2 = CGRectMake(points[0].x, points[0].y-1, t, t2+2);
CGContextSetRGBStrokeColor(main, 1, 0, 0, 1);
CGContextStrokeRect(main, r2);
截图如下:
您假设边界框的原点为零。它不是。您需要添加其偏移量。像(按照你的模式):
float cornerX = roundf(r[0].origin.x/CGFontGetUnitsPerEm(font)*200);
float cornerY = roundf(r[0].origin.y/CGFontGetUnitsPerEm(font)*200);
CGRect r2 = CGRectMake(points[0].x+cornerX, points[0].y-1+cornerY, t, t2+2);
我一直在尝试用核心文本绘制单个字形,但字母的 x 位置有点不同。红色矩形显示正确的位置。
CGContextRef main = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetAllowsAntialiasing(main, false);
CGContextSetFont(main, font);
CGContextSetFontSize(main, 200);
CGContextSetTextPosition(main, 0, 0);
glyphs[0] = CGFontGetGlyphWithGlyphName(font, CFSTR("E"));
points[0] = CGPointMake(100, 100);
CGContextSetRGBFillColor(main, 0, 1, 0, 1);
CGContextShowGlyphsAtPositions(main, glyphs, points, 1);
CGRect *r = malloc(sizeof(CGRect)*1);
CGFontGetGlyphBBoxes(font, glyphs, 1, r);
float t = roundf(r[0].size.width/CGFontGetUnitsPerEm(font)*200);
float t2 = roundf(r[0].size.height/CGFontGetUnitsPerEm(font)*200);
CGRect r2 = CGRectMake(points[0].x, points[0].y-1, t, t2+2);
CGContextSetRGBStrokeColor(main, 1, 0, 0, 1);
CGContextStrokeRect(main, r2);
截图如下:
您假设边界框的原点为零。它不是。您需要添加其偏移量。像(按照你的模式):
float cornerX = roundf(r[0].origin.x/CGFontGetUnitsPerEm(font)*200);
float cornerY = roundf(r[0].origin.y/CGFontGetUnitsPerEm(font)*200);
CGRect r2 = CGRectMake(points[0].x+cornerX, points[0].y-1+cornerY, t, t2+2);