render.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package render
  2. import (
  3. "image/color"
  4. "image/draw"
  5. "math"
  6. "github.com/go-text/typesetting/font"
  7. "github.com/go-text/typesetting/opentype/api"
  8. "github.com/go-text/typesetting/shaping"
  9. "github.com/srwiley/rasterx"
  10. "golang.org/x/image/math/fixed"
  11. )
  12. // Renderer defines a type that can render strings to a bitmap canvas.
  13. // The size and look of output depends on the various fields in this struct.
  14. // Developers should provide suitable output images for their draw requests.
  15. // This type is not thread safe so instances should be used from only 1 goroutine.
  16. type Renderer struct {
  17. // FontSize defines the point size of output text, commonly between 10 and 14 for regular text
  18. FontSize float32
  19. // PixScale is used to indicate the pixel density of your output target.
  20. // For example on a hi-DPI (or "retina") display this may be 2.0.
  21. // Default value is 1.0, meaning 1 pixel on the image for each render pixel.
  22. PixScale float32
  23. // Color is the pen colour for rendering
  24. Color color.Color
  25. shaper shaping.Shaper
  26. filler *rasterx.Filler
  27. fillerScale float32
  28. }
  29. // DrawString will rasterise the given string into the output image using the specified font face.
  30. // The text will be drawn starting at the left edge, down from the image top by the
  31. // font ascent value, so that the text is all visible.
  32. // The return value is the X pixel position of the end of the drawn string.
  33. func (r *Renderer) DrawString(str string, img draw.Image, face font.Face) int {
  34. if r.PixScale == 0 {
  35. r.PixScale = 1
  36. }
  37. in := shaping.Input{
  38. Text: []rune(str),
  39. RunStart: 0,
  40. RunEnd: len(str),
  41. Face: face,
  42. Size: fixed.I(int(r.FontSize)),
  43. }
  44. out := r.cachedShaper().Shape(in)
  45. return r.DrawShapedRunAt(out, img, 0, out.LineBounds.Ascent.Ceil())
  46. }
  47. // DrawStringAt will rasterise the given string into the output image using the specified font face.
  48. // The text will be drawn starting at the x, y pixel position.
  49. // Note that x and y are not multiplied by the `PixScale` value as they refer to output coordinates.
  50. // The return value is the X pixel position of the end of the drawn string.
  51. func (r *Renderer) DrawStringAt(str string, img draw.Image, x, y int, face font.Face) int {
  52. if r.PixScale == 0 {
  53. r.PixScale = 1
  54. }
  55. in := shaping.Input{
  56. Text: []rune(str),
  57. RunStart: 0,
  58. RunEnd: len(str),
  59. Face: face,
  60. Size: fixed.I(int(r.FontSize)),
  61. }
  62. return r.DrawShapedRunAt(r.cachedShaper().Shape(in), img, x, y)
  63. }
  64. // DrawShapedRunAt will rasterise the given shaper run into the output image using font face referenced in the shaping.
  65. // The text will be drawn starting at the startX, startY pixel position.
  66. // Note that startX and startY are not multiplied by the `PixScale` value as they refer to output coordinates.
  67. // The return value is the X pixel position of the end of the drawn string.
  68. func (r *Renderer) DrawShapedRunAt(run shaping.Output, img draw.Image, startX, startY int) int {
  69. if r.PixScale == 0 {
  70. r.PixScale = 1
  71. }
  72. scale := r.FontSize * r.PixScale / float32(run.Face.Upem())
  73. r.fillerScale = scale
  74. b := img.Bounds()
  75. scanner := rasterx.NewScannerGV(b.Dx(), b.Dy(), img, b)
  76. f := rasterx.NewFiller(b.Dx(), b.Dy(), scanner)
  77. r.filler = f
  78. f.SetColor(r.Color)
  79. x := float32(startX)
  80. y := float32(startY)
  81. for _, g := range run.Glyphs {
  82. xPos := x + fixed266ToFloat(g.XOffset)*r.PixScale
  83. yPos := y - fixed266ToFloat(g.YOffset)*r.PixScale
  84. data := run.Face.GlyphData(g.GlyphID)
  85. switch format := data.(type) {
  86. case api.GlyphOutline:
  87. r.drawOutline(g, format, f, scale, xPos, yPos)
  88. case api.GlyphBitmap:
  89. _ = r.drawBitmap(g, format, img, xPos, yPos)
  90. case api.GlyphSVG:
  91. _ = r.drawSVG(g, format, img, xPos, yPos)
  92. }
  93. x += fixed266ToFloat(g.XAdvance) * r.PixScale
  94. }
  95. f.Draw()
  96. r.filler = nil
  97. return int(math.Ceil(float64(x)))
  98. }
  99. func (r *Renderer) cachedShaper() shaping.Shaper {
  100. if r.shaper == nil {
  101. r.shaper = &shaping.HarfbuzzShaper{}
  102. }
  103. return r.shaper
  104. }
  105. func (r *Renderer) drawOutline(g shaping.Glyph, bitmap api.GlyphOutline, f *rasterx.Filler, scale float32, x, y float32) {
  106. for _, s := range bitmap.Segments {
  107. switch s.Op {
  108. case api.SegmentOpMoveTo:
  109. f.Start(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
  110. case api.SegmentOpLineTo:
  111. f.Line(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
  112. case api.SegmentOpQuadTo:
  113. f.QuadBezier(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)},
  114. fixed.Point26_6{X: floatToFixed266(s.Args[1].X*scale + x), Y: floatToFixed266(-s.Args[1].Y*scale + y)})
  115. case api.SegmentOpCubeTo:
  116. f.CubeBezier(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)},
  117. fixed.Point26_6{X: floatToFixed266(s.Args[1].X*scale + x), Y: floatToFixed266(-s.Args[1].Y*scale + y)},
  118. fixed.Point26_6{X: floatToFixed266(s.Args[2].X*scale + x), Y: floatToFixed266(-s.Args[2].Y*scale + y)})
  119. }
  120. }
  121. f.Stop(true)
  122. }
  123. func fixed266ToFloat(i fixed.Int26_6) float32 {
  124. return float32(float64(i) / 64)
  125. }
  126. func floatToFixed266(f float32) fixed.Int26_6 {
  127. return fixed.Int26_6(int(float64(f) * 64))
  128. }