Skip to content

Commit 7ee1a06

Browse files
committed
fix: Avoid caching and drawing invalid QImage in text rendering
1 parent 001e76e commit 7ee1a06

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

editor/window.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,13 +2638,15 @@ func (w *Window) drawTextInPosWithCache(p *gui.QPainter, x, y int, text string,
26382638

26392639
if err != nil {
26402640
image = w.newTextCache(text, highlight, isNormalWidth)
2641-
w.setTextCache(text, highlight, image)
2641+
if image != nil {
2642+
w.setTextCache(text, highlight, image)
2643+
}
26422644
} else {
26432645
image = imagev.(*gui.QImage)
26442646
}
26452647

26462648
// return if image is invalid
2647-
if image.Width() == 0 && image.Height() == 0 {
2649+
if image == nil {
26482650
return
26492651
}
26502652

@@ -2826,6 +2828,12 @@ func (w *Window) newTextCache(text string, highlight *Highlight, isNormalWidth b
28262828
}
28272829
}
28282830

2831+
imageWidth := int(math.Ceil(w.devicePixelRatio * width))
2832+
imageHeight := int(w.devicePixelRatio * float64(font.lineHeight))
2833+
if imageWidth <= 0 || imageHeight <= 0 {
2834+
return nil
2835+
}
2836+
28292837
// QImage default device pixel ratio is 1.0,
28302838
// So we set the correct device pixel ratio
28312839

@@ -2839,8 +2847,8 @@ func (w *Window) newTextCache(text string, highlight *Highlight, isNormalWidth b
28392847
// gui.QImage__Format_ARGB32_Premultiplied,
28402848
// )
28412849
image := gui.NewQImage3(
2842-
int(math.Ceil(w.devicePixelRatio*width)),
2843-
int(w.devicePixelRatio*float64(font.lineHeight)),
2850+
imageWidth,
2851+
imageHeight,
28442852
gui.QImage__Format_ARGB32_Premultiplied,
28452853
)
28462854

0 commit comments

Comments
 (0)