CTRun CTLine Glyphs 详解

iOS上的字体的解释

FONT Glyphs.png

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

![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 = "<CGColor 0x6080000b7c40> [<CGColorSpace 0x6080000377e0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0 0 1 )";
    NSFont = "<UICTFont: 0x7fd35cd21910> font-family: \"PingFangSC-Regular\"; font-weight: normal; font-style: normal; font-size: 20.00pt";
    NSParagraphStyle = "<CTParagraphStyle: 0x6080000cf500>{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    \"<CTTextTab: 0x60800008df70>{location = 28, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008dfc0>{location = 56, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e010>{location = 84, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e060>{location = 112, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e0b0>{location = 140, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e100>{location = 168, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e150>{location = 196, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e1a0>{location = 224, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e1f0>{location = 252, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e240>{location = 280, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e290>{location = 308, alignment = 0, options = (null)}\",\n    \"<CTTextTab: 0x60800008e2e0>{location = 336, alignment = 0, options = (null)}\"\n)}";
}

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

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

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

{
    CTForegroundColor = "<CGColor 0x6000000bff80> [<CGColorSpace 0x6080000380e0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0 0 1 )";
    CTRunDelegate = "<CTRunDelegate 0x6000002a00c0 [0x109c2ddf0]>";
    NSFont = "<UICTFont: 0x7fdd8ac1e050> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

推荐阅读更多精彩内容

  • 1.iOS中的round、ceil、floor函数略解 round如果参数是小数,则求本身的四舍五入.ceil如果...
    K_Gopher阅读 858评论 1 0
  • 去年十月份改论文时,用matplotlib做图,生成pdf,插入到word文档中。但是需要把整个字体都嵌入到pdf...
    figs阅读 3,744评论 0 5
  • 可以看我的博客 lmwen.top 或者订阅我的公众号 简介有稍微接触python的人就会知道,python中...
    ayuLiao阅读 2,300评论 1 5
  • 急盼着开学,但是又怕大学新的感动,新的朋友,新的心动,会把我现在的伙伴都抹去。时间真是把双刃剑,无声息的我们迎来了...
    因为一个人阅读 57评论 0 1
  • 准备充分再去推荐,需要正确推荐。推荐人至少用到25种以上再去推荐。 社会上大致有两种人: 一种是每天都生计奔波,每...
    洁Regina阅读 190评论 0 0