Skip to content

Commit 5151911

Browse files
committed
Fix glyph positioning for multibyte characters.
1 parent 800da6f commit 5151911

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/MacVim/MMCoreTextView.m

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -706,26 +706,34 @@ - (void)drawRect:(NSRect)rect
706706

707707
__block NSMutableString *lineString = nil;
708708
__block CGFloat lineStringStart = 0;
709+
__block CFRange lineStringRange;
709710
__block GridCell lastStringCell;
710711
void (^flushLineString)() = ^{
711712
if (!lineString.length)
712713
return;
713-
CTLineRef line = [self lineForCharacterString:lineString textFlags:lastStringCell.textFlags];
714-
CGFloat width = cellSize.width * (lastStringCell.textFlags & DRAW_WIDE ? 2 : 1);
714+
CGPoint positionsByIndex[lineString.length];
715+
for (size_t i = 0, stringIndex = 0; i < lineStringRange.length; i++) {
716+
GridCell cell = *grid_cell(&grid, r, lineStringRange.location + i);
717+
size_t cell_length = cell.string.length;
718+
for (size_t j = 0; j < cell_length; j++)
719+
positionsByIndex[stringIndex++] = CGPointMake(i * cellSize.width, 0);
720+
if (cell.textFlags & DRAW_WIDE)
721+
i++;
722+
}
715723
CGContextSetFillColor(ctx, COMPONENTS(lastStringCell.fg));
716724
CGContextSetTextPosition(ctx, lineStringStart, rowRect.origin.y + fontDescent);
717725
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
726+
CTLineRef line = [self lineForCharacterString:lineString textFlags:lastStringCell.textFlags];
718727
for (id obj in (NSArray*)CTLineGetGlyphRuns(line)) {
719728
CTRunRef run = (CTRunRef)obj;
720729
CFIndex glyphCount = CTRunGetGlyphCount(run);
721730
CFIndex indices[glyphCount];
722731
CGPoint positions[glyphCount];
723732
CGGlyph glyphs[glyphCount];
724733
CTRunGetStringIndices(run, CFRangeMake(0, 0), indices);
725-
CTRunGetPositions(run, CFRangeMake(0, 0), positions);
726734
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs);
727735
for (CFIndex i = 0; i < glyphCount; i++)
728-
positions[i].x = indices[i] * width;
736+
positions[i] = positionsByIndex[indices[i]];
729737
CTFontRef font = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
730738
CTFontDrawGlyphs(font, glyphs, positions, glyphCount, ctx);
731739
}
@@ -735,10 +743,8 @@ - (void)drawRect:(NSRect)rect
735743
for (size_t c = 0; c < grid.cols; c++) {
736744
GridCell cell = *grid_cell(&grid, r, c);
737745
CGRect cellRect = {{rowRect.origin.x + cellSize.width * c, rowRect.origin.y}, cellSize};
738-
if (cell.textFlags & DRAW_WIDE) {
746+
if (cell.textFlags & DRAW_WIDE)
739747
cellRect.size.width *= 2;
740-
c++;
741-
}
742748
if (cell.inverted) {
743749
cell.bg ^= 0xFFFFFF;
744750
cell.fg ^= 0xFFFFFF;
@@ -793,13 +799,18 @@ - (void)drawRect:(NSRect)rect
793799
flushLineString();
794800
if (!lineString)
795801
lineString = [[NSMutableString alloc] init];
796-
if (!lineString.length)
802+
if (!lineString.length) {
797803
lineStringStart = cellRect.origin.x;
804+
lineStringRange = CFRangeMake(c, 1);
805+
}
798806
[lineString appendString:cell.string];
807+
lineStringRange.length = c - lineStringRange.location + 1;
799808
lastStringCell = cell;
800809
} else {
801810
flushLineString();
802811
}
812+
if (cell.textFlags & DRAW_WIDE)
813+
c++;
803814
}
804815
flushLineString();
805816
[lineString release];

0 commit comments

Comments
 (0)