Skip to content

Commit d8bdf09

Browse files
committed
Add support manual font fallback
1 parent 0ca24ad commit d8bdf09

File tree

14 files changed

+506
-262
lines changed

14 files changed

+506
-262
lines changed

editor/config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type editorConfig struct {
3636
WindowSeparatorTheme string
3737
NvimInWsl string
3838
WSLDist string
39+
FontWeight string
3940
ModeEnablingIME []string
4041
IndentGuideIgnoreFtList []string
4142
CharsScaledLineHeight []string
@@ -46,8 +47,9 @@ type editorConfig struct {
4647
DiffChangePattern int
4748
CacheSize int
4849
Linespace int
49-
Letterspace float64
50+
Letterspace int
5051
FontSize int
52+
FontStretch int
5153
Margin int
5254
Gap int
5355
Height int
@@ -78,7 +80,7 @@ type editorConfig struct {
7880
BorderlessWindow bool
7981
RestoreWindowGeometry bool
8082
ExtCmdline bool
81-
NoFontMerge bool
83+
ManualFontFallback bool
8284
WindowGeometryBasedOnFontmetrics bool
8385
IgnoreFirstMouseClickWhenAppInactivated bool
8486
HideTitlebar bool
@@ -316,6 +318,10 @@ func (c *gonvimConfig) init() {
316318
c.Editor.MouseScrollingUnit = "line"
317319
}
318320
c.Editor.FontSize = 12
321+
c.Editor.FontWeight = "normal"
322+
// Horizontal stretch ratio
323+
c.Editor.FontStretch = 100
324+
c.Editor.FontSize = 12
319325
c.Editor.Linespace = 6
320326

321327
c.Editor.ExtCmdline = false

editor/cursor.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ type Cursor struct {
1414
widgets.QWidget
1515
charCache *Cache
1616
font *Font
17+
fallbackfonts []*Font
1718
fontwide *Font
19+
fallbackfontwides []*Font
1820
bg *RGBA
1921
fg *RGBA
2022
ws *Workspace
@@ -182,12 +184,12 @@ func (c *Cursor) drawForeground(p *gui.QPainter, sx, sy, dx, dy float64, text st
182184
)
183185
} else {
184186
if !c.normalWidth && c.fontwide != nil {
185-
p.SetFont(c.fontwide.fontNew)
187+
p.SetFont(resolveFontFallback(c.fontwide, c.fallbackfontwides, text).qfont)
186188
if c.fontwide.lineHeight > font.lineHeight {
187189
shift += c.fontwide.ascent - font.ascent
188190
}
189191
} else {
190-
p.SetFont(font.fontNew)
192+
p.SetFont(resolveFontFallback(c.font, c.fallbackfonts, text).qfont)
191193
}
192194
p.SetPen2(c.fg.QColor())
193195

@@ -206,6 +208,12 @@ func (c *Cursor) drawForeground(p *gui.QPainter, sx, sy, dx, dy float64, text st
206208
func (c *Cursor) newCharCache(text string, fg *RGBA, isNormalWidth bool) *gui.QImage {
207209
font := c.font
208210

211+
if !isNormalWidth && c.fontwide != nil {
212+
font = resolveFontFallback(c.fontwide, c.fallbackfontwides, text)
213+
} else {
214+
font = resolveFontFallback(c.font, c.fallbackfonts, text)
215+
}
216+
209217
width := float64(len(text)) * font.italicWidth
210218
if !isNormalWidth {
211219
// width = math.Ceil(c.ws.screen.runeTextWidth(font, text))
@@ -216,20 +224,15 @@ func (c *Cursor) newCharCache(text string, fg *RGBA, isNormalWidth bool) *gui.QI
216224
// So we set the correct device pixel ratio
217225
image := gui.NewQImage3(
218226
int(c.devicePixelRatio*width),
219-
int(c.devicePixelRatio*float64(font.height)),
227+
int(c.devicePixelRatio*float64(c.font.height)),
220228
gui.QImage__Format_ARGB32_Premultiplied,
221229
)
222230
image.SetDevicePixelRatio(c.devicePixelRatio)
223231
image.Fill3(core.Qt__transparent)
224232

225233
pi := gui.NewQPainter2(image)
226234
pi.SetPen2(fg.QColor())
227-
228-
if !isNormalWidth && font == nil && c.fontwide != nil {
229-
pi.SetFont(c.fontwide.fontNew)
230-
} else {
231-
pi.SetFont(font.fontNew)
232-
}
235+
pi.SetFont(font.qfont)
233236

234237
// TODO
235238
// Set bold, italic styles
@@ -342,7 +345,7 @@ func (c *Cursor) move() {
342345
c.Move2(iX, iY)
343346
}
344347

345-
func (c *Cursor) updateFont(targetWin *Window, font *Font) {
348+
func (c *Cursor) updateFont(targetWin *Window, font *Font, fallbackfonts []*Font) {
346349
win := targetWin
347350
ok := false
348351
if win == nil {
@@ -353,12 +356,16 @@ func (c *Cursor) updateFont(targetWin *Window, font *Font) {
353356
}
354357

355358
if win == nil {
359+
c.font = font
360+
c.fallbackfonts = fallbackfonts
356361
return
357362
}
358363
if win.font == nil {
359364
c.font = font
365+
c.fallbackfonts = fallbackfonts
360366
} else {
361367
c.font = win.font
368+
c.fallbackfonts = win.fallbackfonts
362369
}
363370
}
364371

editor/editor.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type Editor struct {
109109
notifyStartPos *core.QPoint
110110
colors *ColorPalette
111111
notify chan *Notify
112-
fontCh chan *Font
112+
fontCh chan []*Font
113113
cbChan chan *string
114114
chUiPrepared chan bool
115115
geometryUpdateTimer *time.Timer
@@ -125,6 +125,7 @@ type Editor struct {
125125
config gonvimConfig
126126
opts Options
127127
font *Font
128+
fallbackfonts []*Font
128129
notifications []*Notification
129130
workspaces []*Workspace
130131
args []string
@@ -258,16 +259,16 @@ func InitEditor(options Options, args []string) {
258259

259260
e.extFontFamily = e.config.Editor.FontFamily
260261
e.extFontSize = e.config.Editor.FontSize
261-
e.fontCh = make(chan *Font, 2)
262+
e.fontCh = make(chan []*Font, 100)
262263
go func() {
263-
font := initFontNew(
264+
e.fontCh <- parseFont(
264265
editor.extFontFamily,
265-
float64(editor.extFontSize),
266-
0,
266+
editor.extFontSize,
267+
e.config.Editor.FontWeight,
268+
e.config.Editor.FontStretch,
269+
e.config.Editor.Linespace,
267270
e.config.Editor.Letterspace,
268271
)
269-
270-
e.fontCh <- font
271272
}()
272273

273274
e.initSVGS()
@@ -414,6 +415,38 @@ func (e *Editor) addDockMenu() {
414415
menu.SetAsDockMenu()
415416
}
416417

418+
func parseFont(families string, size int, weight string, stretch, linespace, letterspace int) (fonts []*Font) {
419+
weight = strings.ToLower(weight)
420+
var fontWeight gui.QFont__Weight
421+
switch weight {
422+
case "thin":
423+
fontWeight = gui.QFont__Thin
424+
case "extralight", "ultralight":
425+
fontWeight = gui.QFont__ExtraLight
426+
case "light":
427+
fontWeight = gui.QFont__Light
428+
case "normal", "regular":
429+
fontWeight = gui.QFont__Normal
430+
case "demibold", "semibold":
431+
fontWeight = gui.QFont__DemiBold
432+
case "bold":
433+
fontWeight = gui.QFont__Bold
434+
case "extrabold", "ultrabold":
435+
fontWeight = gui.QFont__ExtraBold
436+
case "black", "heavy":
437+
fontWeight = gui.QFont__Black
438+
}
439+
440+
for _, f := range strings.Split(families, ",") {
441+
fonts = append(
442+
fonts,
443+
initFontNew(strings.TrimSpace(f), float64(size), fontWeight, stretch, linespace, letterspace),
444+
)
445+
}
446+
447+
return
448+
}
449+
417450
// setAppDirPath
418451
// set application working directory path
419452
// TODO: This process is problematic and needs a better way to set up CWD
@@ -524,7 +557,11 @@ func (e *Editor) initWorkspaces(ctx context.Context, signal *neovimSignal, redra
524557
ws.initUI()
525558

526559
if i == 0 {
527-
e.font = <-e.fontCh
560+
fonts := <-e.fontCh
561+
e.font = fonts[0]
562+
if len(fonts) > 1 {
563+
e.fallbackfonts = fonts[1:]
564+
}
528565
}
529566

530567
ws.initFont()

0 commit comments

Comments
 (0)