feat: support multi text draw

This commit is contained in:
vicanso 2022-05-25 23:09:33 +08:00
parent 4201c7d439
commit 1dcd50ba9a
2 changed files with 74 additions and 12 deletions

View file

@ -57,6 +57,15 @@ type TicksOption struct {
Length int Length int
Orient string Orient string
Count int Count int
Unit int
}
type MultiTextOption struct {
TextList []string
Orient string
Unit int
Position string
Align string
} }
// PainterPaddingOption sets the padding of draw painter // PainterPaddingOption sets the padding of draw painter
@ -223,6 +232,9 @@ func (p *Painter) SetDrawingStyle(style Style) *Painter {
} }
func (p *Painter) SetTextStyle(style Style) *Painter { func (p *Painter) SetTextStyle(style Style) *Painter {
if style.Font == nil {
style.Font = p.font
}
style.WriteTextOptionsToRenderer(p.render) style.WriteTextOptionsToRenderer(p.render)
return p return p
} }
@ -494,18 +506,25 @@ func (p *Painter) Ticks(opt TicksOption) *Painter {
if opt.Count <= 0 || opt.Length <= 0 { if opt.Count <= 0 || opt.Length <= 0 {
return p return p
} }
count := opt.Count - 1 count := opt.Count
width := p.Width() width := p.Width()
height := p.Height() height := p.Height()
unit := 1
if opt.Unit > 1 {
unit = opt.Unit
}
var values []int var values []int
if opt.Orient == OrientHorizontal { isVertical := opt.Orient == OrientVertical
if isVertical {
values = autoDivide(height, count) values = autoDivide(height, count)
} else { } else {
values = autoDivide(width, count) values = autoDivide(width, count)
} }
for index, value := range values {
for _, value := range values { if index%unit != 0 {
if opt.Orient == OrientVertical { continue
}
if isVertical {
p.LineStroke([]Point{ p.LineStroke([]Point{
{ {
X: 0, X: 0,
@ -529,6 +548,49 @@ func (p *Painter) Ticks(opt TicksOption) *Painter {
}) })
} }
} }
return p
}
func (p *Painter) MultiText(opt MultiTextOption) *Painter {
if len(opt.TextList) == 0 {
return p
}
count := len(opt.TextList)
positionCenter := true
if opt.Position == PositionLeft {
positionCenter = false
}
width := p.Width()
height := p.Height()
var values []int
isVertical := opt.Orient == OrientVertical
if isVertical {
values = autoDivide(height, count)
} else {
values = autoDivide(width, count)
}
for index, text := range opt.TextList {
box := p.MeasureText(text)
start := values[index]
if positionCenter {
start = (values[index] + values[index+1]) >> 1
}
x := 0
y := 0
if isVertical {
y = start - box.Height()>>1
switch opt.Align {
case AlignRight:
x = width - box.Width()
case AlignCenter:
x = width - box.Width()>>1
default:
x = 0
}
} else {
x = start - box.Width()>>1
}
p.Text(text, x, y)
}
return p return p
} }

View file

@ -60,12 +60,12 @@ func TestAutoDivide(t *testing.T) {
assert.Equal([]int{ assert.Equal([]int{
0, 0,
86, 85,
172, 171,
258, 257,
344, 342,
430, 428,
515, 514,
600, 600,
}, autoDivide(600, 7)) }, autoDivide(600, 7))
} }