Skip to content

Support optional ligatures in CoreText #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,040 changes: 321 additions & 1,719 deletions src/MacVim/English.lproj/Preferences.nib/designable.nib

Large diffs are not rendered by default.

Binary file modified src/MacVim/English.lproj/Preferences.nib/keyedobjects.nib
Binary file not shown.
22 changes: 22 additions & 0 deletions src/MacVim/MMAppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ + (void)initialize
[NSNumber numberWithBool:NO], MMNoFontSubstitutionKey,
[NSNumber numberWithBool:YES], MMLoginShellKey,
[NSNumber numberWithInt:2], MMRendererKey,
[NSNumber numberWithBool:NO], MMRendererLigaturesSupportKey,
[NSNumber numberWithInt:MMUntitledWindowAlways],
MMUntitledWindowKey,
[NSNumber numberWithBool:NO], MMTexturedWindowKey,
Expand Down Expand Up @@ -1225,6 +1226,27 @@ - (IBAction)atsuiButtonClicked:(id)sender
[self rebuildPreloadCache];
}

- (IBAction)ligaturesButtonClicked:(id)sender
{
ASLogDebug(@"Toggle CoreText ligatures");
BOOL enable = ([sender state] == NSOnState);

// Update the user default MMRendererLigaturesSupport and synchronize the
// change so that any new Vim process will pick up on the changed setting.
CFPreferencesSetAppValue(
(CFStringRef)MMRendererLigaturesSupportKey,
(CFPropertyListRef)[NSNumber numberWithBool:enable],
kCFPreferencesCurrentApplication);
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);

ASLogInfo(@"Use ligatures=%ld", enable);

// This action is called when the user clicks the "enable support for ligatures"
// button in the advanced preferences pane.
[self rebuildPreloadCache];
}


- (IBAction)loginShellButtonClicked:(id)sender
{
ASLogDebug(@"Toggle login shell option");
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/MMCoreTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

float fontDescent;
BOOL antialias;
BOOL useLigatures;
NSMutableArray *drawData;

MMTextViewHelper *helper;
Expand Down
80 changes: 76 additions & 4 deletions src/MacVim/MMCoreTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ - (id)initWithFrame:(NSRect)frame
[self registerForDraggedTypes:[NSArray arrayWithObjects:
NSFilenamesPboardType, NSStringPboardType, nil]];

// Check if ligatures should be used or not
{
Boolean val;
Boolean keyValid;
val = CFPreferencesGetAppBooleanValue((CFStringRef)MMRendererLigaturesSupportKey,
kCFPreferencesCurrentApplication,
&keyValid);

useLigatures = NO;
if(keyValid) { useLigatures = val; }
}
return self;
}

Expand Down Expand Up @@ -1017,14 +1028,75 @@ - (void)batchDrawData:(NSData *)data
return newFontRef;
}

static void
ligatureGlyphsForChars(const unichar *chars, CGGlyph *glyphs, CGPoint *positions, UniCharCount *length, CTFontRef font )
{
/* CoreText has no simple wait of retrieving a ligature for a set of UniChars.
* The way proposed on the CoreText ML is to convert the text to an attributed
* string, create a CTLine from it and retrieve the Glyphs from the CTRuns in it.
*/
NSString *text = [NSString stringWithCharacters:chars
length:*length];

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
(id)font, kCTFontAttributeName,
// 2 - full ligatures including rare
[NSNumber numberWithInteger: 2], kCTLigatureAttributeName,
nil
];

NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:text
attributes:attrs];

CGPoint refPos = positions[0];

CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrText);

UniCharCount offset = 0;
NSArray *glyphRuns = (NSArray*)CTLineGetGlyphRuns(line);

for (id item in glyphRuns) {
CTRunRef run = (CTRunRef)item;
CFIndex count = CTRunGetGlyphCount(run);

if(count > 0) {
if(count - offset > *length) {
count = (*length) - offset;
}
}

CFRange range = CFRangeMake(0, count);
CTRunGetGlyphs(run, range, &glyphs[offset]);
CTRunGetPositions(run, range, &positions[offset]);

offset += count;
if(offset >= *length) {
// don't copy more glyphs then there is space for
break;
}
}
// fixup relative positioning
for( CFIndex i = 0; i < offset; ++i ) {
positions[i].x += refPos.x;
positions[i].y += refPos.y;
}
// as ligatures combine characters it is required to adjust the
// original length value
*length = offset;
}

static void
recurseDraw(const unichar *chars, CGGlyph *glyphs, CGPoint *positions,
UniCharCount length, CGContextRef context, CTFontRef fontRef,
NSMutableArray *fontCache)
NSMutableArray *fontCache, BOOL useLigatures)
{

if (CTFontGetGlyphsForCharacters(fontRef, chars, glyphs, length)) {
// All chars were mapped to glyphs, so draw all at once and return.
if (useLigatures) {
memset(glyphs, 0, sizeof(CGGlyph) * length);
ligatureGlyphsForChars(chars, glyphs, positions, &length, fontRef);
}

CGFontRef cgFontRef = CTFontCopyGraphicsFont(fontRef, NULL);
CGContextSetFont(context, cgFontRef);
CGContextShowGlyphsAtPositions(context, glyphs, positions, length);
Expand Down Expand Up @@ -1077,7 +1149,7 @@ - (void)batchDrawData:(NSData *)data
}

recurseDraw(chars, glyphs, positions, attemptedCount, context,
fallback, fontCache);
fallback, fontCache, useLigatures);

// If only a portion of the invalid range was rendered above,
// the remaining range needs to be attempted by subsequent
Expand Down Expand Up @@ -1201,7 +1273,7 @@ - (void)drawString:(const UniChar *)chars length:(UniCharCount)length
}

CGContextSetTextPosition(context, x, y+fontDescent);
recurseDraw(chars, glyphs, positions, length, context, fontRef, fontCache);
recurseDraw(chars, glyphs, positions, length, context, fontRef, fontCache, useLigatures);

CFRelease(fontRef);
CGContextRestoreGState(context);
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/MacVim.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ extern NSString *MMNoWindowKey;
extern NSString *MMAutosaveRowsKey;
extern NSString *MMAutosaveColumnsKey;
extern NSString *MMRendererKey;
extern NSString *MMRendererLigaturesSupportKey;

enum {
MMRendererDefault = 0,
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/MacVim.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
NSString *MMAutosaveRowsKey = @"MMAutosaveRows";
NSString *MMAutosaveColumnsKey = @"MMAutosaveColumns";
NSString *MMRendererKey = @"MMRenderer";
NSString *MMRendererLigaturesSupportKey = @"MMRendererLigaturesSupport";

// Vim find pasteboard type (string contains Vim regex patterns)
NSString *VimFindPboardType = @"VimFindPboardType";
Expand Down