我使用下面的代码提取了 TrueType 字体的路径元素。
问题是由 描述的曲线element->type
只有kCGPathElementAddQuadCurveToPoint
2 个点(低质量)的类型。为什么没有任何元素使用更高质量的kCGPathElementAddCurveToPoint
类型(3分)?
有人有想法吗?
int count = 0;
void applierFunction(void *info, const CGPathElement *element)
{
int point_count;
switch (element->type)
{
case kCGPathElementMoveToPoint:
point_count = 1;
break;
case kCGPathElementAddLineToPoint:
point_count = 1;
break;
case kCGPathElementAddQuadCurveToPoint:
point_count = 2;
break;
case kCGPathElementAddCurveToPoint:
point_count = 3;
break;
case kCGPathElementCloseSubpath:
point_count = 0;
break;
}
count+=point_count;
printf("%u.:\n", count);
for (int i = 0; i<point_count; i++) {
switch (element->type) {
case kCGPathElementMoveToPoint:
printf("kCGPathElementMoveToPoint\n");
break;
case kCGPathElementAddLineToPoint:
printf("kCGPathElementAddLineToPoint\n");
break;
case kCGPathElementAddQuadCurveToPoint:
printf("kCGPathElementAddQuadCurveToPoint\n");
break;
case kCGPathElementAddCurveToPoint:
printf("kCGPathElementAddCurveToPoint\n");
break;
case kCGPathElementCloseSubpath:
printf("kCGPathElementCloseSubpath\n");
break;
default:
printf("unknown path type\n");
}
}
}
void extractStringPathElements()
{
CFStringRef str = CFSTR("Q");
CFStringRef strFont = CFSTR("Arial");
CTFontRef font = CTFontCreateWithName(strFont, 500, NULL);
CFStringRef keys[] = {kCTFontAttributeName};
CFTypeRef values[] = {font};
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys,
(const void **)&values, sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString =
CFAttributedStringCreate(NULL, str, attr);
CFRelease(attr);
CTLineRef line = CTLineCreateWithAttributedString(attrString);
CFArrayRef runs = CTLineGetGlyphRuns(line);
long runCount = CFArrayGetCount(runs);
for (int ri = 0; ri < runCount; ri++) {
::CTRunRef run = (::CTRunRef)CFArrayGetValueAtIndex(runs, ri);
long glyphCount = CTRunGetGlyphCount(run);
const unsigned short *glyphs = CTRunGetGlyphsPtr(run);
for (int gi = 0; gi < glyphCount; gi++) {
::CGPathRef path = CTFontCreatePathForGlyph(font, glyphs[gi], NULL);
CGPathApply(path, 0, applierFunction);
printf("glyph %u has %u elements.\n", glyphs[gi], count);
CGPathRelease(path);
}
}
CFRelease(line);
CFRelease(attrString);
CFRelease(font);
}
int main (int argc, const char * argv[])
{
extractStringPathElements();
return 0;
}