NewTextLine is a simple text line using a single font face, a string (supporting new lines) and horizontal alignment (Left, Center, Right). The text's baseline will be drawn on the current coordinate.
(face *FontFace, s string, halign TextAlign)
| 314 | |
| 315 | // NewTextLine is a simple text line using a single font face, a string (supporting new lines) and horizontal alignment (Left, Center, Right). The text's baseline will be drawn on the current coordinate. |
| 316 | func NewTextLine(face *FontFace, s string, halign TextAlign) *Text { |
| 317 | t := &Text{ |
| 318 | fonts: map[*Font]bool{face.Font: true}, |
| 319 | Text: s, |
| 320 | } |
| 321 | |
| 322 | ascent, descent, spacing := face.Metrics().Ascent, face.Metrics().Descent, face.Metrics().LineGap |
| 323 | |
| 324 | i := 0 |
| 325 | y := 0.0 |
| 326 | skipNext := false |
| 327 | for j, r := range s + "\n" { |
| 328 | if text.IsParagraphSeparator(r) { |
| 329 | if skipNext { |
| 330 | skipNext = false |
| 331 | i++ |
| 332 | continue |
| 333 | } |
| 334 | if i < j { |
| 335 | x := 0.0 |
| 336 | ppem := face.PPEM(DefaultResolution) |
| 337 | line := line{y: y, spans: []TextSpan{}} |
| 338 | for _, item := range itemizeString(s[i:j]) { |
| 339 | direction, _ := scriptDirection(HorizontalTB, Natural, item.Script, item.Level, face.Direction) |
| 340 | glyphs := face.Font.shaper.Shape(item.Text, ppem, direction, face.Script, face.Language, face.Font.features, face.Font.variations) |
| 341 | width := face.textWidth(glyphs) |
| 342 | line.spans = append(line.spans, TextSpan{ |
| 343 | X: x, |
| 344 | Width: width, |
| 345 | Face: face, |
| 346 | Text: item.Text, |
| 347 | Glyphs: glyphs, |
| 348 | Direction: direction, |
| 349 | Level: item.Level, |
| 350 | }) |
| 351 | x += width |
| 352 | } |
| 353 | if halign == Center || halign == Middle { |
| 354 | for k := range line.spans { |
| 355 | line.spans[k].X = -x / 2.0 |
| 356 | } |
| 357 | } else if halign == Right { |
| 358 | for k := range line.spans { |
| 359 | line.spans[k].X = -x |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // reorder runs of RTL text |
| 364 | reorderSpans(line.spans) |
| 365 | |
| 366 | t.lines = append(t.lines, line) |
| 367 | } |
| 368 | y += ascent + descent + spacing |
| 369 | i = j + utf8.RuneLen(r) |
| 370 | skipNext = r == '\r' && j+1 < len(s) && s[j+1] == '\n' |
| 371 | } |
| 372 | } |
| 373 | if halign == Left { |