Objective c 自定义CTLine结构

Objective c 自定义CTLine结构,objective-c,macos,performance,cocoa,core-text,Objective C,Macos,Performance,Cocoa,Core Text,我正在为我的项目创建自定义CTLine结构。这是一个非常简单的实现,因为它不使用CFAttributeString来创建行。整条线有一种颜色,所有标志符号大小相同。但是CFString越长,创建它所需的时间就越长,并且比CTLineCreateWithAttributedString()慢 您可以看到,第一对调用比CTLine创建更快,但是我的实现开始需要更多的时间来完成这项工作。 可能是多个CTFontGetGlyphsForCharacters()调用的错误?你能给我一些建议来加速这段代码吗

我正在为我的项目创建自定义CTLine结构。这是一个非常简单的实现,因为它不使用CFAttributeString来创建行。整条线有一种颜色,所有标志符号大小相同。但是CFString越长,创建它所需的时间就越长,并且比CTLineCreateWithAttributedString()慢

您可以看到,第一对调用比CTLine创建更快,但是我的实现开始需要更多的时间来完成这项工作。
可能是多个CTFontGetGlyphsForCharacters()调用的错误?你能给我一些建议来加速这段代码吗;dr在必要时使用
CTLineCreateWithAttributedString()
,然后继续;在您的管道中的其他地方存在更大的性能问题
CGContextDrawPath()
是你真正的敌人

作为证明,我编写了一个iOS应用程序,可以在iPhone上显示,一直到人行道和建筑物。看起来是这样的(请注意,人行道的所有小脚都是字体中的字形):

我的管道看起来有点像这样:

  • 检查每个字符串的边界框,并尽可能放弃
  • 对其余字符串调用
    CTLineCreateWithAttributedString()
  • 迭代每个
    CTRun
    并使用
    CTFontCreatePathForGlyph()
    获取每个glyph路径
  • 检查每个路径的边界框,如果可能,将其丢弃。
  • 调用
    CGContextDrawPath()
    其余路径
  • 步骤1和4是优化的地方。绘制扭曲的线条(字形)是计算密集型的,无法回避。唯一的赢家是不要玩


    (为了获得更多的优化乐趣,请特别关注
    MRPathMetrics
    函数,这些函数可用于避免昂贵的基于
    CGPath
    的函数)

    因此,我应该放弃创建自己的Ctlines的想法;是的,但我想做一个语法高亮系统,我不想布局所有的字形,只改变它们的颜色,我恐怕我只能对你最初提出的问题进行评论。但是,对于语法突出显示,NSLayoutManager(和)提供了一些方法,这些方法可能对您有很大帮助。
    #import <Cocoa/Cocoa.h>
    
    #define sc (CFStringRef)
    
        struct Line {
            CGGlyph*line_glyphs;
            CGPoint*point;
            CTFontRef font;
            int length;
        };
    
    
        typedef  struct Line* LineRef;
    
        LineRef create(CFStringRef str, CTFontRef font);
    
        @interface View : NSView
        {
            LineRef line;
            CTLineRef l;
            CTFontRef font;
            CGFontRef font2;
            CFMutableStringRef string;
    
        }
     @end
    
    
    
     #import "View.h"
        #include <time.h>
    
    
        LineRef create(CFStringRef str, CTFontRef font){
    
            LineRef line = malloc(sizeof(struct Line));
            long length = CFStringGetLength(str);
    
            CGGlyph* gl = malloc(sizeof(CGGlyph)*length);
            CGPoint* points = malloc(sizeof(CGPoint)*length);
            CGRect rects[length];
            UniChar buffer[length];
            CFStringGetCharacters(str, CFRangeMake(0, length), buffer);
            CTFontGetGlyphsForCharacters(font, buffer, gl, length);
            CTFontGetBoundingRectsForGlyphs(font, kCTFontOrientationHorizontal, gl, rects, length);
            int x_offset  = 0;
            int y = 200;
            CGPoint temp;
            for(int i = 0;i<length;i++)
            {
                temp = CGPointMake(x_offset , y);
                x_offset += rects[i].size.width + (rects[i].size.width == 0)*10;
    
                points[i] = temp;
            }
    
            line->line_glyphs = gl;
            line->point = points;
            line->font = font;
            line->length = length;
            return line;
        }
    
    
    
        @implementation View
    
    
        -(void)awakeFromNib{
            font = CTFontCreateWithName(CFSTR("Comic Sans MS"), 30, 0);
            font2= CGFontCreateWithFontName(CFSTR("Comic Sans MS"));
            line = create(CFSTR(""), font);
            string = CFStringCreateMutable(kCFAllocatorDefault, 0);
            [[self window] makeFirstResponder:self];
    
    
        }
    
    
    
        -(void)keyDown:(NSEvent *)event{
            static int index = 0;
    
            NSString* i = [event characters];
    
            CFStringAppend(string,sc  i);
            CFMutableAttributedStringRef s = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
            CFAttributedStringReplaceString(s, CFRangeMake(0, 0), string);
            CFAttributedStringSetAttribute(s, CFRangeMake(0, CFStringGetLength(string)), kCTFontAttributeName, font);
            CFAbsoluteTime t1 = CFAbsoluteTimeGetCurrent();
            l = CTLineCreateWithAttributedString(s);
            CFAbsoluteTime t2 = CFAbsoluteTimeGetCurrent();
            double d1 = t2-t1;
            CFAbsoluteTime T1 = CFAbsoluteTimeGetCurrent();
            line = create(string, font);
            CFAbsoluteTime T2 = CFAbsoluteTimeGetCurrent();
            double d2 = T2 - T1;
            printf("test:%i core text: %f       my implem : %f \n",index, d1,d2);
    
    
    
            index++;
        }
    @end
    
      test:0 core text: 0.000761       my implem : 0.000016 
        test:1 core text: 0.000047       my implem : 0.000029 
        test:2 core text: 0.000041       my implem : 0.000027 
        test:3 core text: 0.000045       my implem : 0.000032 
        test:4 core text: 0.000045       my implem : 0.000032 
        test:5 core text: 0.000046       my implem : 0.000034
        ...
        test:176 core text: 0.000068     my implem : 0.000151 
        test:177 core text: 0.000084     my implem : 0.000171 
        test:178 core text: 0.000099     my implem : 0.000230 
        test:179 core text: 0.000061     my implem : 0.000145 
        test:180 core text: 0.000071     my implem : 0.000224 
        test:181 core text: 0.000057     my implem : 0.000149