length.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package svg
  2. type Unit string
  3. // Relative units are:
  4. // em: the 'font-size' of the relevant font
  5. // ex: the 'x-height' of the relevant font
  6. // px: pixels, relative to the viewing device
  7. // ebisvg uses exactly one ebiten pixel for px unit.
  8. // const UnitEx = Unit(ex)
  9. // As the CSS2 standard states "1px thus corresponds to about 0.28 mm"
  10. // This then means that there are 3.571428571428571 pixels per mm.
  11. // ebisvg uses this scale factor for the absolute SVG units.
  12. //
  13. const MM2PX = 3.571428571428571
  14. const IN2MM = 254
  15. const PT2IN = 1 / 72
  16. const PC2IN = 1 / 6
  17. const UnitNo = Unit("")
  18. const UnitPx = Unit("px")
  19. const UnitEm = Unit("em")
  20. const UnitEx = Unit("ex")
  21. const UnitIn = Unit("in")
  22. const UnitCm = Unit("cm")
  23. const UnitMm = Unit("mm")
  24. const UnitPt = Unit("pt")
  25. const UnitPc = Unit("pc")
  26. const UnitPe = Unit("%")
  27. type Length struct {
  28. Magnitude float32
  29. Unit
  30. }
  31. func (l Length) Pixels(parent ...float32) float32 {
  32. switch l.Unit {
  33. case UnitNo, UnitPx:
  34. return l.Magnitude
  35. case UnitEx, UnitEm, UnitPe:
  36. if len(parent) > 0 {
  37. return l.Magnitude * parent[0]
  38. } else {
  39. return l.Magnitude
  40. }
  41. case UnitPc:
  42. return l.Magnitude * PC2IN * IN2MM * MM2PX
  43. case UnitPt:
  44. return l.Magnitude * PT2IN * IN2MM * MM2PX
  45. case UnitMm:
  46. return l.Magnitude * MM2PX
  47. case UnitCm:
  48. return l.Magnitude * MM2PX * 100
  49. case UnitIn:
  50. return l.Magnitude * IN2MM * MM2PX
  51. default:
  52. return l.Magnitude
  53. }
  54. }