LayoutStdLR does basic standard layout of text in LR direction.
(txtSty *styles.Text, fontSty *styles.FontRender, ctxt *units.Context, size math32.Vector2)
| 172 | |
| 173 | // LayoutStdLR does basic standard layout of text in LR direction. |
| 174 | func (tr *Text) LayoutStdLR(txtSty *styles.Text, fontSty *styles.FontRender, ctxt *units.Context, size math32.Vector2) math32.Vector2 { |
| 175 | if len(tr.Spans) == 0 { |
| 176 | return math32.Vector2{} |
| 177 | } |
| 178 | |
| 179 | // pr := profile.Start("TextLayout") |
| 180 | // defer pr.End() |
| 181 | // |
| 182 | tr.Dir = styles.LRTB |
| 183 | fontSty.Font = OpenFont(fontSty, ctxt) |
| 184 | fht := fontSty.Face.Metrics.Height |
| 185 | tr.FontHeight = fht |
| 186 | dsc := math32.FromFixed(fontSty.Face.Face.Metrics().Descent) |
| 187 | lspc := txtSty.EffLineHeight(fht) |
| 188 | tr.LineHeight = lspc |
| 189 | lpad := (lspc - fht) / 2 // padding above / below text box for centering in line |
| 190 | |
| 191 | maxw := float32(0) |
| 192 | |
| 193 | // first pass gets rune positions and wraps text as needed, and gets max width |
| 194 | si := 0 |
| 195 | for si < len(tr.Spans) { |
| 196 | sr := &(tr.Spans[si]) |
| 197 | if err := sr.IsValid(); err != nil { |
| 198 | si++ |
| 199 | continue |
| 200 | } |
| 201 | if sr.LastPos.X == 0 { // don't re-do unless necessary |
| 202 | sr.SetRunePosLR(txtSty.LetterSpacing.Dots, txtSty.WordSpacing.Dots, fontSty.Face.Metrics.Ch, txtSty.TabSize) |
| 203 | } |
| 204 | if sr.IsNewPara() { |
| 205 | sr.RelPos.X = txtSty.Indent.Dots |
| 206 | } else { |
| 207 | sr.RelPos.X = 0 |
| 208 | } |
| 209 | ssz := sr.SizeHV() |
| 210 | ssz.X += sr.RelPos.X |
| 211 | if size.X > 0 && ssz.X > size.X && txtSty.HasWordWrap() { |
| 212 | for { |
| 213 | wp := sr.FindWrapPosLR(size.X, ssz.X) |
| 214 | if wp > 0 && wp < len(sr.Text)-1 { |
| 215 | nsr := sr.SplitAtLR(wp) |
| 216 | tr.InsertSpan(si+1, nsr) |
| 217 | ssz = sr.SizeHV() |
| 218 | ssz.X += sr.RelPos.X |
| 219 | if ssz.X > maxw { |
| 220 | maxw = ssz.X |
| 221 | } |
| 222 | si++ |
| 223 | if si >= len(tr.Spans) { |
| 224 | break |
| 225 | } |
| 226 | sr = &(tr.Spans[si]) // keep going with nsr |
| 227 | sr.SetRunePosLR(txtSty.LetterSpacing.Dots, txtSty.WordSpacing.Dots, fontSty.Face.Metrics.Ch, txtSty.TabSize) |
| 228 | ssz = sr.SizeHV() |
| 229 | |
| 230 | // fixup links |
| 231 | for li := range tr.Links { |
no test coverage detected