Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos CTRunDraw()实现了正确的行为_Macos_Core Text_Cgcontextref - Fatal编程技术网

Macos CTRunDraw()实现了正确的行为

Macos CTRunDraw()实现了正确的行为,macos,core-text,cgcontextref,Macos,Core Text,Cgcontextref,我正在使用CoreText在MacOSX上实现一个自定义文本布局算法。我必须在自定义NSView子类对象中的不同位置部分渲染CTRun的一部分 下面是我对drawRect:method的实现 - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; CGConte

我正在使用CoreText在MacOSX上实现一个自定义文本布局算法。我必须在自定义NSView子类对象中的不同位置部分渲染CTRun的一部分

下面是我对drawRect:method的实现

- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
CGContextRef context =
(CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context); {
    [[NSColor whiteColor] set];
    NSRectFill(dirtyRect);


    CTFontRef font = CTFontCreateWithName(CFSTR("Menlo"), 20, &CGAffineTransformIdentity);

    CFTypeRef values[] = {font};
    CFStringRef keys[] = {kCTFontAttributeName};

    CFDictionaryRef dictionary =
    CFDictionaryCreate(NULL,
                       (const void **)&keys,
                       (const void **)&values,
                       sizeof(keys) / sizeof(keys[0]),
                       &kCFTypeDictionaryKeyCallBacks,
                       &kCFTypeDictionaryValueCallBacks);

    CFAttributedStringRef longString =
    CFAttributedStringCreate(kCFAllocatorDefault, CFSTR("this_is_a_very_long_string_that_compromises_many_glyphs,we_wil_see_it:)"), dictionary);
    CTLineRef lineRef = CTLineCreateWithAttributedString(longString);

    CFArrayRef runsArray = CTLineGetGlyphRuns(lineRef);
    CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runsArray, 0);

    CGAffineTransform textTransform = CGAffineTransformIdentity;
    textTransform = CGAffineTransformScale(textTransform, 1.0, -1.0);
    CGContextSetTextMatrix(context, textTransform);

    CGAffineTransform sequenceTransform =
    CGAffineTransformIdentity;
    sequenceTransform = CGAffineTransformTranslate(sequenceTransform, 0, 23.2818);


    CGPoint firstPoint = CGPointApplyAffineTransform(CGPointMake(0, 0), sequenceTransform);
    CFRange firstRange = CFRangeMake(0, 24);
    CGContextSetTextPosition(context, firstPoint.x, firstPoint.y);
    CTRunDraw(run, context, firstRange);

    CGPoint secondPoint = CGPointApplyAffineTransform(CGPointMake(0, 26.2812), sequenceTransform);
    CFRange secondRange = CFRangeMake(24, 24);
    CGContextSetTextPosition(context, secondPoint.x, secondPoint.y);
    CTRunDraw(run, context, secondRange);

    CGPoint thirdPoint = CGPointApplyAffineTransform(CGPointMake(0, 52.5625), sequenceTransform);
    CFRange thirdRange = CFRangeMake(48, 23);
    CGContextSetTextPosition(context, thirdPoint.x, thirdPoint.y);
    CTRunDraw(run, context, thirdRange);

}
CGContextRestoreGState(context);
}

下面是这段代码的输出

问题是CTRunDraw()方法在指定范围以外的位置插入空格

我想要的是它应该在正确的位置渲染运行的部分。 这是我想要的正确输出(正确的输出是原始输出的photoshop)。

注意:我在自定义NSView子类中使用翻转坐标系

- (BOOL)isFlipped {
return YES;

}您在这里误用了
CTRun
CTRun
是一个包含布局图示符的水平框。试图将一部分内容画在另一部分下面是没有意义的(当然,这种排版在任何稍微复杂的情况下都是错误的)。为什么?嗯,因为在某些情况下,如果在给定位置有换行符,所选择的图示符可能会有所不同(例如,连字可能会发生这种情况)。另外,请注意,字符和glyph之间不一定存在1:1的映射

我的猜测是,你可能不需要自己的全定制排版机(相信我,写一个很复杂,所以如果你不需要,就不要写一个)。相反,只需使用
CTTypesetter
和/或
CTFramesetter
即可获得所需的输出