在CoreText iOS上从右到左

时间:2012-08-01 21:07:38

标签: ios core-text

我想用CGContextShowGlyphsAtPositions绘制字形,但是当我使用CTRunGetPositions时,字形的位置从左边开始。 CTRunGetStatus会返回kCTRunStatusRightToLeft但结果是从左到右。如何从右到左绘制文字?

对于从左到右的英文字符是可以的,因为这种语言是从左到右但我使用阿拉伯语字符而阿拉伯语是从右到左的语言。

2 个答案:

答案 0 :(得分:2)

位置是从左到右,因为字形通常是从左到右绘制的,因为这是“x”增加的方向。当你绘制CGGlyph时,语言无关紧要更多。在绘制كتب时,系统首先绘制ب,因此它的位置首先出现在数组中。

CTRunGetGlyphs的结果与CTRunGetPositions的顺序相同。所有kCTRunStatusRightToLeft都告诉您字形顺序与字符串索引顺序完全相反。

我用CGContextShowGlyphsAtPositions从右到左绘制阿拉伯语没有任何问题。你看到了什么问题?

CFIndex glyphCount = CTRunGetGlyphCount(run);
CGPoint *positions = calloc(glyphCount, sizeof(CGPoint));
CTRunGetPositions(run, CFRangeMake(0, 0), positions);
CGGlyph *glyphs = calloc(glyphCount, sizeof(CGGlyph));
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs);
CGContextShowGlyphsAtPositions(context, glyphs, positions, glyphCount);
free(positions);
free(glyphs);

答案 1 :(得分:0)

tanx Rob 这是关于最后一行,当我创建多行第1行或第2行是好的但最后一行淹没在左边,但在阿拉伯语最后一行必须淹没在右边。

我的代码是:

while (isLine) {

    CFIndex count = CTTypesetterSuggestLineBreak(typesetter, start, self.frame.size.width);
    if (count <= 0) {
        isLine = NO;
        break;
    }
    currentline ++;
    CTLineRef justifyline = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count));
    if (start + count >= lettercount) {

        justifyline = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count)); 
    }
    else {
        CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count));        
        justifyline = CTLineCreateJustifiedLine(line, 0, self.frame.size.width);
        CFRelease(line);

    }

    CTLineGetTypographicBounds(justifyline, &ascent, &descent, &leading);
    float lineheight = ascent + descent + leading;
    CFArrayRef runArray = CTLineGetGlyphRuns(justifyline);
    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
    {
        // Get FONT for this run
        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
        //NSLog(@"GlyphCount= %d",(int)CTRunGetGlyphCount(run));
        // for each GLYPH in run
        for (CFIndex runGlyphIndex = 0 ; runGlyphIndex < CTRunGetGlyphCount(run) ; runGlyphIndex++) 
        {
            // get Glyph & Glyph-data


            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
            CGGlyph glyph;
            CGPoint position;
            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
            CTRunStatus state = CTRunGetStatus(run);
            if (state == kCTRunStatusRightToLeft) {
                NSLog(@"rtl");
            }
            CTRunGetPositions(run, thisGlyphRange, &position);



            position = CGPointMake(position.x, self.frame.size.height + position.y - (100 * currentline));
            NSLog(@"x %f y %f",position.x,position.y);
            // Render it
            {
                CGFontRef cgFont = CTFontCopyGraphicsFont(runFont, NULL);

                CGAffineTransform textMatrix = CTRunGetTextMatrix(run);
                CGContextSetTextMatrix(context, textMatrix);
                CGContextSetFont(context, cgFont);
                CGContextSetFontSize(context, CTFontGetSize(runFont));
                switch (runGlyphIndex) {
                    case 4:
                        CGContextSetRGBFillColor(context, 1, 0.0, 0.0, 1);
                        break;

                    default:
                        CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1);
                        break;
                }               


                CGContextShowGlyphsAtPositions(context, &glyph, &position, 1);

                CFRelease(cgFont);
            }
        }
        CFRelease(runFont);
    }




    start+=count;


}

我想按字形处理字形,当字形显示在最后一行时,它显示在页面的左侧。