Skip to content

Commit

Permalink
feat(font): add support for toggling ligatures (onivim#2133)
Browse files Browse the repository at this point in the history
* Service_Font: add ligatures and scaffolding for other features

* Bench: add features to default font

Co-authored-by: Bryan Phelps <[email protected]>
  • Loading branch information
zbaylin and bryphe authored Jul 20, 2020
1 parent 1b1b5da commit 982b28f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions bench/lib/Helpers.re
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let defaultFont: Service_Font.font = {
measuredHeight: 10.,
descenderHeight: 1.,
smoothing: Revery.Font.Smoothing.default,
features: [],
};

let simpleState =
Expand Down
4 changes: 4 additions & 0 deletions src/Core/ConfigurationParser.re
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ let configurationParsers: list(configurationTuple) = [
editorFontFile: parseString(~default=Constants.defaultFontFile, json),
},
),
(
"editor.fontLigatures",
(config, json) => {...config, editorFontLigatures: parseBool(json)},
),
(
"editor.fontSize",
(config, json) => {
Expand Down
2 changes: 2 additions & 0 deletions src/Core/ConfigurationValues.re
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type t = {
editorDetectIndentation: bool,
editorFontFile: string,
editorFontSize: float,
editorFontLigatures: bool,
editorFontSmoothing: fontSmoothing,
editorHoverDelay: int,
editorHoverEnabled: bool,
Expand Down Expand Up @@ -90,6 +91,7 @@ let default = {
editorFontFile: Constants.defaultFontFile,
editorFontSmoothing: Default,
editorFontSize: Constants.defaultFontSize,
editorFontLigatures: true,
editorHoverDelay: 1000,
editorHoverEnabled: true,
editorLargeFileOptimizations: true,
Expand Down
8 changes: 7 additions & 1 deletion src/Feature/Editor/Draw.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type context = {
charWidth: float,
charHeight: float,
smoothing: Revery.Font.Smoothing.t,
features: list(Revery.Font.Feature.t),
};

let createContext =
Expand All @@ -34,6 +35,7 @@ let createContext =
charWidth: editorFont.measuredWidth,
charHeight: editorFont.measuredHeight,
smoothing: editorFont.smoothing,
features: editorFont.features,
};
};

Expand Down Expand Up @@ -84,7 +86,11 @@ let drawShapedText = {
bold ? Revery.Font.Weight.Bold : Revery.Font.Weight.Normal,
context.fontFamily,
);
let text = Revery.Font.(shape(font, text) |> ShapeResult.getGlyphStrings);
let text =
Revery.Font.(
shape(~features=context.features, font, text)
|> ShapeResult.getGlyphStrings
);

Revery.Font.Smoothing.setPaint(~smoothing=context.smoothing, paint);
Skia.Paint.setTextSize(paint, context.fontSize);
Expand Down
2 changes: 2 additions & 0 deletions src/Feature/Editor/EditorConfiguration.re
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ let detectIndentation =
setting("editor.detectIndentation", bool, ~default=true);
let fontFamily =
setting("editor.fontFamily", string, ~default="JetBrainsMono-Regular.ttf");
let fontLigatures = setting("editor.fontLigatures", bool, ~default=true);
let fontSize = setting("editor.fontSize", int, ~default=14);
let largeFileOptimization =
setting("editor.largeFileOptimizations", bool, ~default=true);
Expand Down Expand Up @@ -113,6 +114,7 @@ module Experimental = {
let contributions = [
detectIndentation.spec,
fontFamily.spec,
fontLigatures.spec,
fontSize.spec,
largeFileOptimization.spec,
highlightActiveIndentGuide.spec,
Expand Down
44 changes: 41 additions & 3 deletions src/Service/Font/Service_Font.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type font = {
measuredHeight: float,
descenderHeight: float,
smoothing: [@opaque] Revery.Font.Smoothing.t,
features: [@opaque] list(Revery.Font.Feature.t),
};

let toString = show_font;
Expand All @@ -22,6 +23,7 @@ let default = {
measuredHeight: 1.,
descenderHeight: 0.,
smoothing: Revery.Font.Smoothing.default,
features: [],
};

let measure = (~text, v: font) => {
Expand Down Expand Up @@ -51,6 +53,7 @@ let setFont =
~requestId,
~fontFamily as familyString,
~fontSize,
~fontLigatures,
~smoothing,
~dispatch,
) => {
Expand All @@ -76,6 +79,26 @@ let setFont =
Revery_Font.Family.system(familyString);
};

// This is formatted this way to accomodate other future features
let features =
[]
|> (
features =>
fontLigatures
? features
: [
Revery.Font.Feature.make(
~tag=Revery.Font.Features.contextualAlternates,
~value=0,
),
Revery.Font.Feature.make(
~tag=Revery.Font.Features.standardLigatures,
~value=0,
),
...features,
]
);

let res =
FontLoader.loadAndValidateEditorFont(
~requestId=req,
Expand Down Expand Up @@ -114,6 +137,7 @@ let setFont =
measuredHeight,
descenderHeight,
smoothing,
features,
}),
);
}
Expand All @@ -128,6 +152,7 @@ module Sub = {
type params = {
fontFamily: string,
fontSize: float,
fontLigatures: bool,
fontSmoothing: ConfigurationValues.fontSmoothing,
uniqueId: string,
};
Expand All @@ -137,6 +162,7 @@ module Sub = {
type state = {
fontFamily: string,
fontSize: float,
fontLigatures: bool,
fontSmoothing: ConfigurationValues.fontSmoothing,
requestId: ref(int),
};
Expand Down Expand Up @@ -165,6 +191,7 @@ module Sub = {
~requestId,
~fontFamily=params.fontFamily,
~fontSize=params.fontSize,
~fontLigatures=params.fontLigatures,
~smoothing=reveryFontSmoothing,
~dispatch,
);
Expand All @@ -173,28 +200,32 @@ module Sub = {
fontFamily: params.fontFamily,
fontSize: params.fontSize,
fontSmoothing: params.fontSmoothing,
fontLigatures: params.fontLigatures,
requestId,
};
};

let update = (~params: params, ~state: state, ~dispatch: msg => unit) =>
if (params.fontFamily != state.fontFamily
|| !Float.equal(params.fontSize, state.fontSize)
|| params.fontSmoothing != state.fontSmoothing) {
|| params.fontSmoothing != state.fontSmoothing
|| params.fontLigatures != state.fontLigatures) {
let reveryFontSmoothing =
getReveryFontSmoothing(params.fontSmoothing);
setFont(
~requestId=state.requestId,
~fontFamily=params.fontFamily,
~fontSize=params.fontSize,
~smoothing=reveryFontSmoothing,
~fontLigatures=params.fontLigatures,
~dispatch,
);
{
...state,
fontFamily: params.fontFamily,
fontSize: params.fontSize,
fontSmoothing: params.fontSmoothing,
fontLigatures: params.fontLigatures,
};
} else {
state;
Expand All @@ -206,7 +237,14 @@ module Sub = {
};
});

let font = (~uniqueId, ~fontFamily, ~fontSize, ~fontSmoothing) => {
FontSubscription.create({uniqueId, fontFamily, fontSize, fontSmoothing});
let font =
(~uniqueId, ~fontFamily, ~fontSize, ~fontLigatures, ~fontSmoothing) => {
FontSubscription.create({
uniqueId,
fontFamily,
fontSize,
fontSmoothing,
fontLigatures,
});
};
};
2 changes: 2 additions & 0 deletions src/Service/Font/Service_Font.rei
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type font = {
measuredHeight: float,
descenderHeight: float,
smoothing: Revery.Font.Smoothing.t,
features: list(Revery.Font.Feature.t),
};

let toString: font => string;
Expand All @@ -32,6 +33,7 @@ module Sub: {
~uniqueId: string,
~fontFamily: string,
~fontSize: float,
~fontLigatures: bool,
~fontSmoothing: ConfigurationValues.fontSmoothing
) =>
Isolinear.Sub.t(msg);
Expand Down
7 changes: 7 additions & 0 deletions src/Store/StoreThread.re
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,18 @@ let start =
c => c.editorFontSmoothing,
state.configuration,
);
let fontLigatures =
Oni_Core.Configuration.getValue(
c => c.editorFontLigatures,
state.configuration,
);
let editorFontSubscription =
Service_Font.Sub.font(
~uniqueId="editorFont",
~fontFamily,
~fontSize,
~fontSmoothing,
~fontLigatures,
)
|> Isolinear.Sub.map(msg => Model.Actions.EditorFont(msg));

Expand All @@ -283,6 +289,7 @@ let start =
~fontFamily=terminalFontFamily,
~fontSize=terminalFontSize,
~fontSmoothing=terminalFontSmoothing,
~fontLigatures,
)
|> Isolinear.Sub.map(msg => Model.Actions.TerminalFont(msg));

Expand Down

0 comments on commit 982b28f

Please sign in to comment.