Skip to content

Commit acfda40

Browse files
committed
Add the fontVariations config
1 parent 609c4f6 commit acfda40

File tree

9 files changed

+283
-189
lines changed

9 files changed

+283
-189
lines changed

src/vs/base/browser/fastDomNode.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class FastDomNode<T extends HTMLElement> {
1717
private _fontSize: string = '';
1818
private _fontStyle: string = '';
1919
private _fontFeatureSettings: string = '';
20+
private _fontVariationSettings: string = '';
2021
private _textDecoration: string = '';
2122
private _lineHeight: string = '';
2223
private _letterSpacing: string = '';
@@ -110,12 +111,7 @@ export class FastDomNode<T extends HTMLElement> {
110111
return;
111112
}
112113
this._fontWeight = fontWeight;
113-
if (fontWeight !== 'normal' && fontWeight !== 'bold') {
114-
const fontWeightAsNumber = parseInt(fontWeight, 10);
115-
this.domNode.style.fontVariationSettings = `"wght" ${fontWeightAsNumber}`;
116-
} else {
117-
this.domNode.style.fontWeight = this._fontWeight;
118-
}
114+
this.domNode.style.fontWeight = this._fontWeight;
119115
}
120116

121117
public setFontSize(_fontSize: number | string): void {
@@ -143,6 +139,14 @@ export class FastDomNode<T extends HTMLElement> {
143139
this.domNode.style.fontFeatureSettings = this._fontFeatureSettings;
144140
}
145141

142+
public setFontVariationSettings(fontVariationSettings: string): void {
143+
if (this._fontVariationSettings === fontVariationSettings) {
144+
return;
145+
}
146+
this._fontVariationSettings = fontVariationSettings;
147+
this.domNode.style.fontVariationSettings = this._fontVariationSettings;
148+
}
149+
146150
public setTextDecoration(textDecoration: string): void {
147151
if (this._textDecoration === textDecoration) {
148152
return;

src/vs/editor/browser/config/domFontInfo.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ export function applyFontInfo(domNode: FastDomNode<HTMLElement> | HTMLElement, f
1212
domNode.setFontWeight(fontInfo.fontWeight);
1313
domNode.setFontSize(fontInfo.fontSize);
1414
domNode.setFontFeatureSettings(fontInfo.fontFeatureSettings);
15+
domNode.setFontVariationSettings(fontInfo.fontVariationSettings);
1516
domNode.setLineHeight(fontInfo.lineHeight);
1617
domNode.setLetterSpacing(fontInfo.letterSpacing);
1718
} else {
1819
domNode.style.fontFamily = fontInfo.getMassagedFontFamily();
19-
if (fontInfo.fontWeight !== 'normal' && fontInfo.fontWeight !== 'bold') {
20-
const fontWeightAsNumber = parseInt(fontInfo.fontWeight, 10);
21-
domNode.style.fontWeight = `"wght" ${fontWeightAsNumber}`;
22-
} else {
23-
domNode.style.fontWeight = fontInfo.fontWeight;
24-
}
20+
domNode.style.fontWeight = fontInfo.fontWeight;
2521
domNode.style.fontSize = fontInfo.fontSize + 'px';
2622
domNode.style.fontFeatureSettings = fontInfo.fontFeatureSettings;
23+
domNode.style.fontVariationSettings = fontInfo.fontVariationSettings;
2724
domNode.style.lineHeight = fontInfo.lineHeight + 'px';
2825
domNode.style.letterSpacing = fontInfo.letterSpacing + 'px';
2926
}

src/vs/editor/browser/config/fontMeasurements.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface ISerializedFontInfo {
2020
readonly fontWeight: string;
2121
readonly fontSize: number;
2222
readonly fontFeatureSettings: string;
23+
readonly fontVariationSettings: string;
2324
readonly lineHeight: number;
2425
readonly letterSpacing: number;
2526
readonly isMonospace: boolean;
@@ -128,6 +129,7 @@ class FontMeasurementsImpl extends Disposable {
128129
fontWeight: readConfig.fontWeight,
129130
fontSize: readConfig.fontSize,
130131
fontFeatureSettings: readConfig.fontFeatureSettings,
132+
fontVariationSettings: readConfig.fontVariationSettings,
131133
lineHeight: readConfig.lineHeight,
132134
letterSpacing: readConfig.letterSpacing,
133135
isMonospace: readConfig.isMonospace,
@@ -219,6 +221,7 @@ class FontMeasurementsImpl extends Disposable {
219221
fontWeight: bareFontInfo.fontWeight,
220222
fontSize: bareFontInfo.fontSize,
221223
fontFeatureSettings: bareFontInfo.fontFeatureSettings,
224+
fontVariationSettings: bareFontInfo.fontVariationSettings,
222225
lineHeight: bareFontInfo.lineHeight,
223226
letterSpacing: bareFontInfo.letterSpacing,
224227
isMonospace: isMonospace,

src/vs/editor/common/config/editorOptions.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ export interface IEditorOptions {
226226
* Defaults to false.
227227
*/
228228
fontLigatures?: boolean | string;
229+
/**
230+
* Enable font variations.
231+
* Defaults to false.
232+
*/
233+
fontVariations?: boolean | string;
229234
/**
230235
* Disable the use of `transform: translate3d(0px, 0px, 0px)` for the editor margin and lines layers.
231236
* The usage of `transform: translate3d(0px, 0px, 0px)` acts as a hint for browsers to create an extra layer.
@@ -1583,6 +1588,60 @@ export class EditorFontLigatures extends BaseEditorOption<EditorOption.fontLigat
15831588

15841589
//#endregion
15851590

1591+
//#region fontVariations
1592+
1593+
/**
1594+
* @internal
1595+
*/
1596+
export class EditorFontVariations extends BaseEditorOption<EditorOption.fontVariations, boolean | string, string> {
1597+
// Text is laid out using default settings.
1598+
public static OFF = '"normal"';
1599+
1600+
// Translate `fontWeight` config to `font-variation-settings` CSS property.
1601+
public static TRANSLATE = 'translate';
1602+
1603+
constructor() {
1604+
super(
1605+
EditorOption.fontVariations, 'fontVariations', EditorFontVariations.OFF,
1606+
{
1607+
anyOf: [
1608+
{
1609+
type: 'boolean',
1610+
description: nls.localize('fontVariations', "Enables/Disables the translation from font-weight to font-variation-settings. Change this to a string for fine-grained control of the 'font-variation-settings' CSS property."),
1611+
},
1612+
{
1613+
type: 'string',
1614+
description: nls.localize('fontVariationSettings', "Explicit 'font-variation-settings' CSS property. A boolean can be passed instead if one only needs to translate font-weight to font-variation-settings.")
1615+
}
1616+
],
1617+
description: nls.localize('fontVariationsGeneral', "Configures font variations. Can be either a boolean to enable/disable the translation from font-weight to font-variation-settings or a string for the value of the CSS 'font-variation-settings' property."),
1618+
default: false
1619+
}
1620+
);
1621+
}
1622+
1623+
public validate(input: any): string {
1624+
if (typeof input === 'undefined') {
1625+
return this.defaultValue;
1626+
}
1627+
if (typeof input === 'string') {
1628+
if (input === 'false') {
1629+
return EditorFontVariations.OFF;
1630+
}
1631+
if (input === 'true') {
1632+
return EditorFontVariations.TRANSLATE;
1633+
}
1634+
return input;
1635+
}
1636+
if (Boolean(input)) {
1637+
return EditorFontVariations.TRANSLATE;
1638+
}
1639+
return EditorFontVariations.OFF;
1640+
}
1641+
}
1642+
1643+
//#endregion
1644+
15861645
//#region fontInfo
15871646

15881647
class EditorFontInfo extends ComputedEditorOption<EditorOption.fontInfo, FontInfo> {
@@ -4460,6 +4519,7 @@ export const enum EditorOption {
44604519
fontLigatures,
44614520
fontSize,
44624521
fontWeight,
4522+
fontVariations,
44634523
formatOnPaste,
44644524
formatOnType,
44654525
glyphMargin,
@@ -4808,6 +4868,7 @@ export const EditorOptions = {
48084868
fontLigatures2: register(new EditorFontLigatures()),
48094869
fontSize: register(new EditorFontSize()),
48104870
fontWeight: register(new EditorFontWeight()),
4871+
fontVariations: register(new EditorFontVariations()),
48114872
formatOnPaste: register(new EditorBooleanOption(
48124873
EditorOption.formatOnPaste, 'formatOnPaste', false,
48134874
{ description: nls.localize('formatOnPaste', "Controls whether the editor should automatically format the pasted content. A formatter must be available and the formatter should be able to format a range in a document.") }

src/vs/editor/common/config/fontInfo.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as platform from 'vs/base/common/platform';
7-
import { EditorOptions, EditorOption, FindComputedEditorOptionValueById, EDITOR_FONT_DEFAULTS } from 'vs/editor/common/config/editorOptions';
7+
import { EditorFontVariations, EditorOptions, EditorOption, FindComputedEditorOptionValueById, EDITOR_FONT_DEFAULTS } from 'vs/editor/common/config/editorOptions';
88
import { EditorZoom } from 'vs/editor/common/config/editorZoom';
99

1010
/**
@@ -36,28 +36,30 @@ export class BareFontInfo {
3636
const fontWeight = options.get(EditorOption.fontWeight);
3737
const fontSize = options.get(EditorOption.fontSize);
3838
const fontFeatureSettings = options.get(EditorOption.fontLigatures);
39+
const fontVariationSettings = options.get(EditorOption.fontVariations);
3940
const lineHeight = options.get(EditorOption.lineHeight);
4041
const letterSpacing = options.get(EditorOption.letterSpacing);
41-
return BareFontInfo._create(fontFamily, fontWeight, fontSize, fontFeatureSettings, lineHeight, letterSpacing, pixelRatio, ignoreEditorZoom);
42+
return BareFontInfo._create(fontFamily, fontWeight, fontSize, fontFeatureSettings, fontVariationSettings, lineHeight, letterSpacing, pixelRatio, ignoreEditorZoom);
4243
}
4344

4445
/**
4546
* @internal
4647
*/
47-
public static createFromRawSettings(opts: { fontFamily?: string; fontWeight?: string; fontSize?: number; fontLigatures?: boolean | string; lineHeight?: number; letterSpacing?: number }, pixelRatio: number, ignoreEditorZoom: boolean = false): BareFontInfo {
48+
public static createFromRawSettings(opts: { fontFamily?: string; fontWeight?: string; fontSize?: number; fontLigatures?: boolean | string; fontVariations?: boolean | string; lineHeight?: number; letterSpacing?: number }, pixelRatio: number, ignoreEditorZoom: boolean = false): BareFontInfo {
4849
const fontFamily = EditorOptions.fontFamily.validate(opts.fontFamily);
4950
const fontWeight = EditorOptions.fontWeight.validate(opts.fontWeight);
5051
const fontSize = EditorOptions.fontSize.validate(opts.fontSize);
5152
const fontFeatureSettings = EditorOptions.fontLigatures2.validate(opts.fontLigatures);
53+
const fontVariationSettings = EditorOptions.fontVariations.validate(opts.fontVariations);
5254
const lineHeight = EditorOptions.lineHeight.validate(opts.lineHeight);
5355
const letterSpacing = EditorOptions.letterSpacing.validate(opts.letterSpacing);
54-
return BareFontInfo._create(fontFamily, fontWeight, fontSize, fontFeatureSettings, lineHeight, letterSpacing, pixelRatio, ignoreEditorZoom);
56+
return BareFontInfo._create(fontFamily, fontWeight, fontSize, fontFeatureSettings, fontVariationSettings, lineHeight, letterSpacing, pixelRatio, ignoreEditorZoom);
5557
}
5658

5759
/**
5860
* @internal
5961
*/
60-
private static _create(fontFamily: string, fontWeight: string, fontSize: number, fontFeatureSettings: string, lineHeight: number, letterSpacing: number, pixelRatio: number, ignoreEditorZoom: boolean): BareFontInfo {
62+
private static _create(fontFamily: string, fontWeight: string, fontSize: number, fontFeatureSettings: string, fontVariationSettings: string, lineHeight: number, letterSpacing: number, pixelRatio: number, ignoreEditorZoom: boolean): BareFontInfo {
6163
if (lineHeight === 0) {
6264
lineHeight = GOLDEN_LINE_HEIGHT_RATIO * fontSize;
6365
} else if (lineHeight < MINIMUM_LINE_HEIGHT) {
@@ -75,12 +77,23 @@ export class BareFontInfo {
7577
fontSize *= editorZoomLevelMultiplier;
7678
lineHeight *= editorZoomLevelMultiplier;
7779

80+
if (fontVariationSettings === EditorFontVariations.TRANSLATE) {
81+
if (fontWeight === 'normal' || fontWeight === 'bold') {
82+
fontVariationSettings = EditorFontVariations.OFF;
83+
} else {
84+
const fontWeightAsNumber = parseInt(fontWeight, 10);
85+
fontVariationSettings = `'wght' ${fontWeightAsNumber}`;
86+
fontWeight = 'normal';
87+
}
88+
}
89+
7890
return new BareFontInfo({
7991
pixelRatio: pixelRatio,
8092
fontFamily: fontFamily,
8193
fontWeight: fontWeight,
8294
fontSize: fontSize,
8395
fontFeatureSettings: fontFeatureSettings,
96+
fontVariationSettings,
8497
lineHeight: lineHeight,
8598
letterSpacing: letterSpacing
8699
});
@@ -91,6 +104,7 @@ export class BareFontInfo {
91104
readonly fontWeight: string;
92105
readonly fontSize: number;
93106
readonly fontFeatureSettings: string;
107+
readonly fontVariationSettings: string;
94108
readonly lineHeight: number;
95109
readonly letterSpacing: number;
96110

@@ -103,6 +117,7 @@ export class BareFontInfo {
103117
fontWeight: string;
104118
fontSize: number;
105119
fontFeatureSettings: string;
120+
fontVariationSettings: string;
106121
lineHeight: number;
107122
letterSpacing: number;
108123
}) {
@@ -111,6 +126,7 @@ export class BareFontInfo {
111126
this.fontWeight = String(opts.fontWeight);
112127
this.fontSize = opts.fontSize;
113128
this.fontFeatureSettings = opts.fontFeatureSettings;
129+
this.fontVariationSettings = opts.fontVariationSettings;
114130
this.lineHeight = opts.lineHeight | 0;
115131
this.letterSpacing = opts.letterSpacing;
116132
}
@@ -119,7 +135,7 @@ export class BareFontInfo {
119135
* @internal
120136
*/
121137
public getId(): string {
122-
return `${this.pixelRatio}-${this.fontFamily}-${this.fontWeight}-${this.fontSize}-${this.fontFeatureSettings}-${this.lineHeight}-${this.letterSpacing}`;
138+
return `${this.pixelRatio}-${this.fontFamily}-${this.fontWeight}-${this.fontSize}-${this.fontFeatureSettings}-${this.fontVariationSettings}-${this.lineHeight}-${this.letterSpacing}`;
123139
}
124140

125141
/**
@@ -173,6 +189,7 @@ export class FontInfo extends BareFontInfo {
173189
fontWeight: string;
174190
fontSize: number;
175191
fontFeatureSettings: string;
192+
fontVariationSettings: string;
176193
lineHeight: number;
177194
letterSpacing: number;
178195
isMonospace: boolean;
@@ -205,6 +222,7 @@ export class FontInfo extends BareFontInfo {
205222
&& this.fontWeight === other.fontWeight
206223
&& this.fontSize === other.fontSize
207224
&& this.fontFeatureSettings === other.fontFeatureSettings
225+
&& this.fontVariationSettings === other.fontVariationSettings
208226
&& this.lineHeight === other.lineHeight
209227
&& this.letterSpacing === other.letterSpacing
210228
&& this.typicalHalfwidthCharacterWidth === other.typicalHalfwidthCharacterWidth

0 commit comments

Comments
 (0)