func NewText(text string, font *ui.Font) *Text { t := new(Text) t.Wrap = true t.Text = text t.Font = font t.layout = ui.NewTextLayout(text, font, 0) t.lText = text t.lFont = font return t }
func (a HistogramAreaHandler) Draw(area *ui.Area, dp *ui.AreaDrawParams) { fntNum := ui.LoadClosestFont(&ui.FontDescriptor{Family: "Droid Sans", Size: 10, Weight: ui.TextWeightBold}) fntVolume := ui.LoadClosestFont(&ui.FontDescriptor{Family: "Droid Sans", Size: 12, Italic: ui.TextItalicItalic}) brush := &ui.Brush{Type: ui.Solid, R: 0.1, G: 0.5, B: 0.7, A: 1} columnWidth := dp.AreaWidth / float64(len(a.Data)) barWidth := columnWidth * 0.8 for i, volume := range a.Data { x1 := (float64(i) + 0.1) * columnWidth x2 := x1 + barWidth y1 := dp.AreaHeight - 16 y2 := y1 - (dp.AreaHeight-32.0)*float64(volume)/100.0 layoutNum := ui.NewTextLayout(strconv.Itoa(i), fntNum, columnWidth) w1, _ := layoutNum.Extents() dp.Context.Text(x1+(barWidth-w1)/2, y1, layoutNum) layoutVolume := ui.NewTextLayout(strconv.Itoa(volume), fntVolume, columnWidth) w2, h2 := layoutVolume.Extents() dp.Context.Text(x1+(barWidth-w2)/2, y2-h2, layoutVolume) dp.Context.Save() p := ui.NewPath(ui.Winding) p.NewFigure(x1, y1) p.LineTo(x2, y1) p.LineTo(x2, y2) p.LineTo(x1, y2) p.CloseFigure() p.End() dp.Context.Fill(p, brush) dp.Context.Restore() } }
func (e *Text) Layout(width float64) (w float64, h float64) { if !e.Wrap { width = math.MaxFloat64 } if e.Text != e.lText || e.Font != e.lFont { e.layout.Free() e.layout = ui.NewTextLayout(e.Text, e.Font, width) e.lText = e.Text e.lFont = e.Font } else { e.layout.SetWidth(width) } return e.layout.Extents() }