CTRun CTLine Glyphs 详解

iOS上的字体的解释

CTRun CTLine Glyphs 详解_第1张图片
FONT Glyphs.png

阿毛的蛋疼地
字体(Font):和我们平时说的字体不同,计算机意义上的字体表示的是同一大小,同一样式(Style)字形的集合。从这个意义上来说,当我们为文字设置粗体,斜体时其实是使用了另外一种字体(下划线不算)。而平时我们所说的字体只是具有相同设计属性的字体集合,即Font Family或typeface。
字符(Character)和字形(Glyphs):排版过程中一个重要的步骤就是从字符到字形的转换,字符表示信息本身,而字形是它的图形表现形式。字符一般指某种编码,如Unicode编码,而字形则是这些编码对应的图片。但是他们之间不是一一对应关系,同个字符的不同字体族,不同字体大小,不同字体样式都对应了不同的字形。而由于连写(Ligatures)的存在,也会出现多个字符对应一个字形的情况。

CTRun CTLine Glyphs 详解_第2张图片
![fontDesc.gif](http://upload-images.jianshu.io/upload_images/935738-9f3fbd13f2268c31.gif?imageMogr2/auto-orient/strip)

我的理解

coretext上的一个glyph( 字形)对应一个CTRun ,CTRun记录了CFAttributedStringRef的信息例如:

CFAttributedStringRef携带的信息:
    CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
    CTParagraphStyleSetting setting[] = {
        {kCTParagraphStyleSpecifierAlignment,sizeof(alignment),&alignment},
        {kCTParagraphStyleSpecifierLineBreakMode,sizeof(breakMode),&breakMode},
        {kCTParagraphStyleSpecifierLineSpacingAdjustment,sizeof(lineSpace),&lineSpace},
        {kCTParagraphStyleSpecifierBaseWritingDirection,sizeof(direction),&direction}
    };
    CTParagraphStyleRef paragraph = CTParagraphStyleCreate(setting, sizeof(setting)/sizeof(setting[0]));
    
    
    NSDictionary *attsParam = [NSDictionary dictionaryWithObjectsAndKeys:
                               (__bridge id)fontRef,(__bridge NSString *)kCTFontAttributeName,
                               (__bridge id)color,(__bridge NSString*)kCTForegroundColorAttributeName,
                               (__bridge id)paragraph,(__bridge NSString*)kCTParagraphStyleAttributeName,
                               nil];
    
    NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:printInfo attributes:attsParam];
打印一个CTRun上携带的属性
    CFArrayRef runarrayRef = CTLineGetGlyphRuns(line);
    CTRunRef run = CFArrayGetValueAtIndex(runarrayRef, 0);
    
    NSLog(@"%@",CTRunGetAttributes(run));

结果显示
{
    CTForegroundColor = " [ (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0 0 1 )";
    NSFont = " font-family: \"PingFangSC-Regular\"; font-weight: normal; font-style: normal; font-size: 20.00pt";
    NSParagraphStyle = "{base writing direction = -1, alignment = 0, line break mode = 0, default tab interval = 0\nfirst line head indent = 0, head indent = 0, tail indent = 0\nline height multiple = 0, maximum line height = 0, minimum line height = 0\nline spacing adjustment = 5, paragraph spacing = 0, paragraph spacing before = 0\ntabs = (\n    \"{location = 28, alignment = 0, options = (null)}\",\n    \"{location = 56, alignment = 0, options = (null)}\",\n    \"{location = 84, alignment = 0, options = (null)}\",\n    \"{location = 112, alignment = 0, options = (null)}\",\n    \"{location = 140, alignment = 0, options = (null)}\",\n    \"{location = 168, alignment = 0, options = (null)}\",\n    \"{location = 196, alignment = 0, options = (null)}\",\n    \"{location = 224, alignment = 0, options = (null)}\",\n    \"{location = 252, alignment = 0, options = (null)}\",\n    \"{location = 280, alignment = 0, options = (null)}\",\n    \"{location = 308, alignment = 0, options = (null)}\",\n    \"{location = 336, alignment = 0, options = (null)}\"\n)}";
}

基本上一个CTLine上包含多个CTRun 相同属性的为一个CTRun 不同属性的文字形成不同的CTRun

CTFrameGetLines得到所有的line
CTLineGetGlyphCount 得到一行所有的字形的个数
CTLineGetGlyphRuns 得到一行上所有的run的个数

例如:打印一行上的我添加的代理形成的run的属性打印如下

{
    CTForegroundColor = " [ (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0 0 1 )";
    CTRunDelegate = "";
    NSFont = " font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

你可能感兴趣的:(CTRun CTLine Glyphs 详解)