Skip to content

Commit 24eeaea

Browse files
committed
refactor: introduce HlKey for highlight-based screen caching in drawText
1 parent 89f780b commit 24eeaea

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
lines changed

editor/cursor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (c *Cursor) drawForeground(p *gui.QPainter, sx, sy, dx, dy float64, text st
163163
charCache := *c.charCache
164164
imagev, err := charCache.get(HlTextKey{
165165
text: text,
166-
fg: c.fg,
166+
fg: *(c.fg),
167167
italic: false,
168168
bold: false,
169169
})
@@ -265,7 +265,7 @@ func (c *Cursor) setCharCache(text string, fg *RGBA, image *gui.QImage) {
265265
c.charCache.set(
266266
HlTextKey{
267267
text: text,
268-
fg: c.fg,
268+
fg: *(c.fg),
269269
italic: false,
270270
bold: false,
271271
},

editor/window.go

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ type Highlight struct {
4949
underdashed bool
5050
}
5151

52+
// HlText is used in screen cache
53+
type HlKey struct {
54+
fg RGBA
55+
italic bool
56+
bold bool
57+
}
58+
5259
// HlText is used in screen cache
5360
type HlTextKey struct {
54-
fg *RGBA
61+
fg RGBA
5562
text string
5663
italic bool
5764
bold bool
@@ -2303,7 +2310,7 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
23032310
}
23042311

23052312
line := w.content[y]
2306-
chars := map[*Highlight][]int{}
2313+
chars := map[HlKey][]int{}
23072314
specialChars := []int{}
23082315
cellBasedDrawing := editor.config.Editor.DisableLigatures || (editor.config.Editor.Letterspace > 0)
23092316
wsfontLineHeight := y * wsfont.lineHeight
@@ -2367,7 +2374,11 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
23672374
int(float64(x)*wsfont.cellwidth)+horScrollPixels,
23682375
wsfontLineHeight+verScrollPixels,
23692376
line[x].char,
2370-
line[x].highlight,
2377+
HlKey{
2378+
fg: *(line[x].highlight.fg()),
2379+
bold: line[x].highlight.bold,
2380+
italic: line[x].highlight.italic,
2381+
},
23712382
true,
23722383
line[x].scaled,
23732384
)
@@ -2381,19 +2392,24 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
23812392
highlight.background = highlight.background.copy()
23822393
}
23832394

2384-
colorSlice, ok := chars[highlight]
2395+
hlkey := HlKey{
2396+
fg: *(highlight.fg()),
2397+
italic: highlight.italic,
2398+
bold: highlight.bold,
2399+
}
2400+
colorSlice, ok := chars[hlkey]
23852401
if !ok {
23862402
colorSlice = []int{}
23872403
}
23882404
colorSlice = append(colorSlice, x)
2389-
chars[highlight] = colorSlice
2405+
chars[hlkey] = colorSlice
23902406
}
23912407
}
23922408

23932409
// This is the normal rendering process for goneovim,
23942410
// we draw a word snippet of the same highlight on the screen for each of the highlights.
23952411
if !cellBasedDrawing {
2396-
for highlight, colorSlice := range chars {
2412+
for hlkey, colorSlice := range chars {
23972413
var buffer bytes.Buffer
23982414
slice := colorSlice
23992415

@@ -2409,7 +2425,7 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
24092425
if editor.config.Editor.LineToScroll == 1 {
24102426
verScrollPixels += w.scrollPixels[1]
24112427
}
2412-
if highlight.isSignColumn() {
2428+
if line[x].highlight.isSignColumn() {
24132429
horScrollPixels = 0
24142430
}
24152431
if x < w.viewportMargins[2] || x > w.cols-w.viewportMargins[3]-1 {
@@ -2483,7 +2499,7 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
24832499
int(float64(x-pos)*wsfont.cellwidth)+horScrollPixels,
24842500
wsfontLineHeight+verScrollPixels,
24852501
buffer.String(),
2486-
highlight,
2502+
hlkey,
24872503
true,
24882504
false,
24892505
)
@@ -2541,7 +2557,11 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
25412557
int(float64(x)*wsfont.cellwidth)+horScrollPixels,
25422558
wsfontLineHeight+verScrollPixels,
25432559
line[x].char,
2544-
line[x].highlight,
2560+
HlKey{
2561+
fg: *(line[x].highlight.fg()),
2562+
bold: line[x].highlight.bold,
2563+
italic: line[x].highlight.italic,
2564+
},
25452565
false,
25462566
line[x].scaled,
25472567
)
@@ -2550,7 +2570,7 @@ func (w *Window) drawText(p *gui.QPainter, y int, col int, cols int) {
25502570
}
25512571
}
25522572

2553-
func (w *Window) drawTextInPos(p *gui.QPainter, x, y int, text string, highlight *Highlight, isNormalWidth bool, scaled bool) {
2573+
func (w *Window) drawTextInPos(p *gui.QPainter, x, y int, text string, hlkey HlKey, isNormalWidth bool, scaled bool) {
25542574
wsfont := w.getFont()
25552575

25562576
// var horScrollPixels int
@@ -2566,7 +2586,7 @@ func (w *Window) drawTextInPos(p *gui.QPainter, x, y int, text string, highlight
25662586
x, //+horScrollPixels,
25672587
y+wsfont.shift,
25682588
text,
2569-
highlight,
2589+
hlkey,
25702590
isNormalWidth,
25712591
scaled,
25722592
)
@@ -2576,14 +2596,14 @@ func (w *Window) drawTextInPos(p *gui.QPainter, x, y int, text string, highlight
25762596
x, //+horScrollPixels,
25772597
y,
25782598
text,
2579-
highlight,
2599+
hlkey,
25802600
isNormalWidth,
25812601
scaled,
25822602
)
25832603
}
25842604
}
25852605

2586-
func (w *Window) drawTextInPosWithNoCache(p *gui.QPainter, x, y int, text string, highlight *Highlight, isNormalWidth bool, scaled bool) {
2606+
func (w *Window) drawTextInPosWithNoCache(p *gui.QPainter, x, y int, text string, hlkey HlKey, isNormalWidth bool, scaled bool) {
25872607
if text == "" {
25882608
return
25892609
}
@@ -2602,24 +2622,15 @@ func (w *Window) drawTextInPosWithNoCache(p *gui.QPainter, x, y int, text string
26022622
p.SetFont(fontfallbacked.qfont)
26032623

26042624
font := p.Font()
2605-
fg := highlight.fg()
2625+
fg := &(hlkey.fg)
26062626
p.SetPen2(fg.QColor())
26072627

2608-
if highlight.bold {
2609-
font.SetBold(true)
2610-
} else {
2611-
font.SetBold(false)
2612-
}
2613-
if highlight.italic {
2614-
font.SetItalic(true)
2615-
} else {
2616-
font.SetItalic(false)
2617-
}
2618-
// p.DrawText(point, text)
2628+
font.SetBold(hlkey.bold)
2629+
font.SetItalic(hlkey.italic)
26192630
p.DrawText3(x, y, text)
26202631
}
26212632

2622-
func (w *Window) drawTextInPosWithCache(p *gui.QPainter, x, y int, text string, highlight *Highlight, isNormalWidth bool, scaled bool) {
2633+
func (w *Window) drawTextInPosWithCache(p *gui.QPainter, x, y int, text string, hlkey HlKey, isNormalWidth bool, scaled bool) {
26232634
if text == "" {
26242635
return
26252636
}
@@ -2628,15 +2639,15 @@ func (w *Window) drawTextInPosWithCache(p *gui.QPainter, x, y int, text string,
26282639
var image *gui.QImage
26292640
imagev, err := cache.get(HlTextKey{
26302641
text: text,
2631-
fg: highlight.fg(),
2632-
italic: highlight.italic,
2633-
bold: highlight.bold,
2642+
fg: hlkey.fg,
2643+
italic: hlkey.italic,
2644+
bold: hlkey.bold,
26342645
})
26352646

26362647
if err != nil {
2637-
image = w.newTextCache(text, highlight, isNormalWidth)
2648+
image = w.newTextCache(text, hlkey, isNormalWidth)
26382649
if image != nil {
2639-
w.setTextCache(text, highlight, image)
2650+
w.setTextCache(text, hlkey, image)
26402651
}
26412652
} else {
26422653
image = imagev.(*gui.QImage)
@@ -2745,15 +2756,15 @@ func (w *Window) newDecorationCache(char string, highlight *Highlight, isNormalW
27452756
return image
27462757
}
27472758

2748-
func (w *Window) setTextCache(text string, highlight *Highlight, image *gui.QImage) {
2759+
func (w *Window) setTextCache(text string, hlkey HlKey, image *gui.QImage) {
27492760
if w.font != nil {
27502761
// If window has own font setting
27512762
w.cache.set(
27522763
HlTextKey{
27532764
text: text,
2754-
fg: highlight.fg(),
2755-
italic: highlight.italic,
2756-
bold: highlight.bold,
2765+
fg: hlkey.fg,
2766+
italic: hlkey.italic,
2767+
bold: hlkey.bold,
27572768
},
27582769
image,
27592770
)
@@ -2762,9 +2773,9 @@ func (w *Window) setTextCache(text string, highlight *Highlight, image *gui.QIma
27622773
w.s.cache.set(
27632774
HlTextKey{
27642775
text: text,
2765-
fg: highlight.fg(),
2766-
italic: highlight.italic,
2767-
bold: highlight.bold,
2776+
fg: hlkey.fg,
2777+
italic: hlkey.italic,
2778+
bold: hlkey.bold,
27682779
},
27692780
image,
27702781
)
@@ -2784,7 +2795,7 @@ func (w *Window) destroyImagePainter() {
27842795
}
27852796
}
27862797

2787-
func (w *Window) newTextCache(text string, highlight *Highlight, isNormalWidth bool) *gui.QImage {
2798+
func (w *Window) newTextCache(text string, hlkey HlKey, isNormalWidth bool) *gui.QImage {
27882799
// * Ref: https://stackoverflow.com/questions/40458515/a-best-way-to-draw-a-lot-of-independent-characters-in-qt5/40476430#40476430
27892800
editor.putLog("start creating word cache:", text)
27902801

@@ -2813,11 +2824,11 @@ func (w *Window) newTextCache(text string, highlight *Highlight, isNormalWidth b
28132824
}
28142825

28152826
width := float64(len(text))*font.cellwidth + 1
2816-
if highlight.italic {
2827+
if hlkey.italic {
28172828
width = float64(len(text))*font.italicWidth + 1
28182829
}
28192830

2820-
fg := highlight.fg()
2831+
fg := hlkey.fg
28212832
if !isNormalWidth {
28222833
advance := fontfallbacked.fontMetrics.HorizontalAdvance(text, -1)
28232834
if advance > 0 {
@@ -2859,12 +2870,11 @@ func (w *Window) newTextCache(text string, highlight *Highlight, isNormalWidth b
28592870
w.imagePainter.SetPen2(fg.QColor())
28602871
w.imagePainter.SetFont(fontfallbacked.qfont)
28612872

2862-
if highlight.bold {
2863-
w.imagePainter.Font().SetBold(true)
2864-
// w.imagePainter.Font().SetWeight(font.qfont.Weight() + 50)
2873+
if hlkey.bold {
2874+
w.imagePainter.Font().SetBold(hlkey.bold)
28652875
}
2866-
if highlight.italic {
2867-
w.imagePainter.Font().SetItalic(true)
2876+
if hlkey.italic {
2877+
w.imagePainter.Font().SetItalic(hlkey.italic)
28682878
}
28692879

28702880
w.imagePainter.DrawText6(

0 commit comments

Comments
 (0)