我想用 绘制字形CGContextShowGlyphsAtPositions
,但是当我使用CTRunGetPositions
字形的位置时,它是从左侧开始的。CTRunGetStatus
返回但结果kCTRunStatusRightToLeft
是从左到右。如何从右到左绘制文本?
对于英语字符,从左到右是可以的,因为这种语言是从左到右的,但我使用阿拉伯字符,而阿拉伯语是从右到左的语言。
这些位置是从左到右的,因为字形通常是从左到右绘制的,因为这是“x”增加的方向。当你绘制 aCGGlyph
时,语言不再重要。绘制 كتب 时,系统首先绘制 ب,因此它的位置在数组中排在第一位。
的结果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);
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;
}
我想按字形处理字形,当字形显示在最后一行时,它显示在页面的左侧。