bitmap.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package render
  2. import (
  3. "bytes"
  4. "image"
  5. "image/draw"
  6. _ "image/jpeg" // load image formats for users of the API
  7. _ "image/png"
  8. "github.com/go-text/typesetting/shaping"
  9. scale "golang.org/x/image/draw"
  10. _ "golang.org/x/image/tiff" // load image formats for users of the API
  11. "github.com/go-text/typesetting/opentype/api"
  12. )
  13. func (r *Renderer) drawBitmap(g shaping.Glyph, bitmap api.GlyphBitmap, img draw.Image, x, y float32) error {
  14. switch bitmap.Format {
  15. case api.BlackAndWhite:
  16. rec := image.Rect(0, 0, bitmap.Width, bitmap.Height)
  17. sub := image.NewNRGBA(rec)
  18. i := 0
  19. for y2 := 0; y2 < bitmap.Height; y2++ {
  20. for x2 := 0; x2 < bitmap.Width; x2 += 8 {
  21. v := bitmap.Data[i]
  22. if v&1 > 0 {
  23. sub.Set(x2+7, y2, r.Color)
  24. }
  25. if v&2 > 0 {
  26. sub.Set(x2+6, y2, r.Color)
  27. }
  28. if v&4 > 0 {
  29. sub.Set(x2+5, y2, r.Color)
  30. }
  31. if v&8 > 0 {
  32. sub.Set(x2+4, y2, r.Color)
  33. }
  34. if v&16 > 0 {
  35. sub.Set(x2+3, y2, r.Color)
  36. }
  37. if v&32 > 0 {
  38. sub.Set(x2+2, y2, r.Color)
  39. }
  40. if v&64 > 0 {
  41. sub.Set(x2+1, y2, r.Color)
  42. }
  43. if v&128 > 0 {
  44. sub.Set(x2, y2, r.Color)
  45. }
  46. i++
  47. }
  48. }
  49. rect := image.Rect(int(x), int(y), int(x+fixed266ToFloat(g.Width)*r.PixScale), int(y-fixed266ToFloat(g.Height)*r.PixScale))
  50. scale.NearestNeighbor.Scale(img, rect, sub, sub.Bounds(), draw.Over, nil)
  51. case api.JPG, api.PNG, api.TIFF:
  52. pix, _, err := image.Decode(bytes.NewReader(bitmap.Data))
  53. if err != nil {
  54. return err
  55. }
  56. rect := image.Rect(int(x), int(y), int(x+fixed266ToFloat(g.Width)*r.PixScale), int(y-fixed266ToFloat(g.Height)*r.PixScale))
  57. scale.BiLinear.Scale(img, rect, pix, pix.Bounds(), draw.Over, nil)
  58. }
  59. if bitmap.Outline != nil {
  60. r.drawOutline(g, *bitmap.Outline, r.filler, r.fillerScale, x, y)
  61. }
  62. return nil
  63. }