这里的代码似乎大部分都有效——我可以打印出每次运行,看起来运行是有效的,但是当我打印出字形时,它们是垃圾。而且位置都在(0,0),我的界限对我来说看起来不正确。有人可以帮助我,也许我没有正确地做这些事情,顺便说一下,这只是一个示例程序,所以我可以查看运行中的实际数据。
-(void) printRuns:(CTFrameRef)ctframe {
CFArrayRef lines;
lines = CTFrameGetLines(ctframe);
CGPoint origins[CFArrayGetCount(lines)];//the origins of each line at the baseline
CTFrameGetLineOrigins(ctframe, CFRangeMake(0, 0), origins);
NSUInteger lineIndex = 0;
for(int i = 0; i < CFArrayGetCount(lines); i++)
//for(id lineObj in lines)
{
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
NSLog(@"LineOrigin:%@", NSStringFromCGPoint(origins[i]));
CFArrayRef runs = CTLineGetGlyphRuns(line);
for(int j = 0; j < CFArrayGetCount(runs); j++)
//for(id runObj in (__bridge NSArray*)CTLineGetGlyphRuns(line))
{
CTRunRef run = CFArrayGetValueAtIndex(runs, j);
CFRange runRange = CTRunGetStringRange(run);
CGRect runBounds;
CGFloat ascent;//height above the baseline
CGFloat descent;//height below the baseline
runBounds.size.width = CTRunGetTypographicBounds(run, runRange, &ascent, &descent, NULL);
runBounds.size.height = ascent + descent;
CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL);
runBounds.origin.x = origins[lineIndex].x + 0 + xOffset;
runBounds.origin.y = origins[lineIndex].y + 0;
runBounds.origin.y -= descent;
CGGlyph glyphBuffer[runRange.length];
CTRunGetGlyphs(run, runRange, glyphBuffer);
//const CGGlyph *glyphs = CTRunGetGlyphsPtr(run);
//NSLog(@"Glyph:%c", glyph);
for(int i = 0; i < runRange.length; i++)
{
NSLog(@"Glyph:%c", glyphBuffer[i]);
if(i % 10 == 0)
{
NSString *test = @"";
}
}
CFIndex glyphCount = CTRunGetGlyphCount(run);
CGPoint *positions = calloc(glyphCount, sizeof(CGPoint));
CTRunGetPositions(run, runRange, positions);
for(int i = 0; i < glyphCount; i++)
{
CGPoint *position = &positions[i];
NSLog(@"Position:%@", NSStringFromCGPoint(*position));
}
NSLog(@"%@", NSStringFromCGRect(runBounds));
}
lineIndex++;
}
}