Skip to content

Commit

Permalink
Improve IME text handling and styling
Browse files Browse the repository at this point in the history
  • Loading branch information
akiyosi committed Mar 6, 2024
1 parent 1227fed commit 37b2e08
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 133 deletions.
55 changes: 34 additions & 21 deletions editor/imetooltip.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,43 +169,56 @@ func (i *IMETooltip) parsePreeditString(preeditStr string) {
start = i.s.tooltip.cursorPos - length
}

g := &Highlight{}
h := &Highlight{}
preeditHl := i.s.getHighlightByHlname("GuiImePreeditText")
currConvHl := i.s.getHighlightByHlname("GuiImeCurrentConversionText")

if i.s.ws.foreground == nil || i.s.ws.background == nil {
return
if preeditHl == nil {
if i.s.ws.foreground == nil || i.s.ws.background == nil {
return
}

preeditHl = &Highlight{}
preeditHl.foreground = i.s.ws.foreground
preeditHl.background = i.s.ws.background
}
g.foreground = i.s.ws.foreground
g.background = i.s.ws.background
if i.s.ws.screenbg == "light" {
h.foreground = warpColor(i.s.ws.background, -30)
h.background = warpColor(i.s.ws.foreground, -30)
} else {
h.foreground = warpColor(i.s.ws.foreground, 30)
h.background = warpColor(i.s.ws.background, 30)

if currConvHl == nil {
if i.s.ws.foreground == nil || i.s.ws.background == nil {
return
}

currConvHl = &Highlight{}

if i.s.ws.screenbg == "light" {
currConvHl.foreground = warpColor(i.s.ws.background, -30)
currConvHl.background = warpColor(i.s.ws.foreground, -30)
} else {
currConvHl.foreground = warpColor(i.s.ws.foreground, 30)
currConvHl.background = warpColor(i.s.ws.background, 30)
}
currConvHl.underline = true
}
h.underline = true

if preeditStr != "" {
r := []rune(preeditStr)

if length > 0 {
if start > 0 {
i.updateText(g, string(r[:start]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(preeditHl, string(r[:start]), i.s.ws.font.letterSpace, i.getFont().qfont)
if start+length < len(r) {
i.updateText(h, string(r[start:start+length]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(g, string(r[start+length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(currConvHl, string(r[start:start+length]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(preeditHl, string(r[start+length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
} else {
i.updateText(h, string(r[start:]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(currConvHl, string(r[start:]), i.s.ws.font.letterSpace, i.getFont().qfont)
}
} else if start == 0 && length < len(r) {
i.updateText(h, string(r[0:length]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(g, string(r[length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(currConvHl, string(r[0:length]), i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(preeditHl, string(r[length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
} else {
i.updateText(g, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(preeditHl, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
}
} else {
i.updateText(g, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
i.updateText(preeditHl, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
}
}
}
24 changes: 23 additions & 1 deletion editor/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,29 @@ func (s *Screen) makeHighlight(args interface{}) *Highlight {
return &highlight
}

func (s *Screen) getHighlight(name string) (hl *Highlight) {
func (s *Screen) getHighlightByHlname(name string) (hl *Highlight) {
keys := make([]int, 0, len(s.hlAttrDef))
for k := range s.hlAttrDef {
if s.hlAttrDef[k].hlName == "" {
continue
}
keys = append(keys, k)
}

sort.Sort(sort.Reverse(sort.IntSlice(keys)))

for _, k := range keys {
h := s.hlAttrDef[k]
if h.hlName == name {
hl = h
break
}
}

return
}

func (s *Screen) getHighlightByUiname(name string) (hl *Highlight) {
keys := make([]int, 0, len(s.hlAttrDef))
for k := range s.hlAttrDef {
if s.hlAttrDef[k].uiName == "" {
Expand Down
65 changes: 38 additions & 27 deletions editor/tooltip.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,23 @@ func (t *Tooltip) drawContent(p *gui.QPainter, getFont func() *Font) {

fg := chunk.hl.fg()
bg := chunk.hl.bg()
// bold := chunk.hl.bold
underline := chunk.hl.underline
// undercurl := chunk.hl.undercurl
// strikethrough := chunk.hl.strikethrough
sp := chunk.hl.special


bold := chunk.hl.bold
italic := chunk.hl.italic
underline := chunk.hl.underline
undercurl := chunk.hl.undercurl
underdouble := chunk.hl.underdouble
underdotted := chunk.hl.underdotted
underdashed := chunk.hl.underdashed
strikethrough := chunk.hl.strikethrough

// set foreground color
p.SetPen2(fg.QColor())

r := []rune(chunk.str)
for _, rr := range r {
// setFont(p)
p.SetFont(resolveFontFallback(getFont(), t.fallbackfonts, string(rr)).qfont)
font := p.Font()

Expand All @@ -89,12 +94,9 @@ func (t *Tooltip) drawContent(p *gui.QPainter, getFont func() *Font) {
bg.QColor(),
)

// set italic
if italic {
font.SetItalic(true)
} else {
font.SetItalic(false)
}
// set italic, bold
font.SetItalic(italic)
font.SetBold(bold)

p.DrawText(
core.NewQPointF3(
Expand All @@ -104,23 +106,32 @@ func (t *Tooltip) drawContent(p *gui.QPainter, getFont func() *Font) {
string(rr),
)

// draw underline
//
// set text decoration
//

if strikethrough {
drawStrikethrough(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
}

if underline {
var underlinePos float64 = 1
if t.s.ws.palette != nil && t.s.ws.palette.widget.IsVisible() {
underlinePos = 2
}

// draw underline
p.FillRect4(
core.NewQRectF4(
x,
y+height-underlinePos,
chunk.width,
underlinePos,
),
fg.QColor(),
)
drawUnderline(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
}

if undercurl {
drawUndercurl(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
}

if underdouble {
drawUnderdouble(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
}

if underdotted {
drawUnderdotted(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
}

if underdashed {
drawUnderdashed(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
}

x += chunk.width
Expand Down
Loading

0 comments on commit 37b2e08

Please sign in to comment.