font.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Models a font that has been loaded from disk and kept in storage
  2. class Font < Store
  3. extend Store::Forward
  4. # Loads a truetype or opentype font
  5. def self.load_ttf(name, vpath, height, flags = 0)
  6. load_something(forward_name(name), vpath, self) do | nid |
  7. Eruta::Store.load_ttf_font(nid, vpath, height, flags=0)
  8. end
  9. end
  10. # Loads a truetype or opentype font and stretches it
  11. def self.load_ttf_stretch(name, vpath, height, width, flags)
  12. load_something(forward_name(name), vpath, self) do | nid |
  13. Eruta::Store.load_ttf_stretch(nid, vpath, height, width, flags)
  14. end
  15. end
  16. # Loads a bitmap font
  17. def self.load_bitmap(name, vpath)
  18. load_something(forward_name(name), vpath, self) do | nid |
  19. Eruta::Store.load_bitmap_font(nid, flags)
  20. end
  21. end
  22. # Loads a bitmap font with flags
  23. def self.load_bitmap_flags(name, vpath, flags)
  24. load_something(forward_name(name), vpath, self) do | nid |
  25. Eruta::Store.load_bitmap_font_flags(nid, vpath, flags)
  26. end
  27. end
  28. # Loads a font. If the extension is .ttf or .otf, this calls
  29. # load_ttf or load_ttf_stretch, otherwise it calls load_bitmap or
  30. # load_bitmap_flags
  31. def self.load(name, vpath, *args)
  32. ext = vpath.split('.').last.downcase
  33. case ext
  34. when 'ttf', 'otf', 'afm', 'pfb', 'pfm', 'pcf', 'gz', 'sfd'
  35. if args.size >= 3
  36. height, width, flags = *args
  37. return load_ttf_strech(name, vpath, height, width, flags)
  38. else
  39. height, flags = *args
  40. return load_ttf(name, vpath, height, flags)
  41. end
  42. when 'bmp', 'pcx', 'tga', 'jpeg', 'jpg', 'png'
  43. if args.size >= 1
  44. flags = *args
  45. return load_bitmap_flags(name, vpath, flags)
  46. else
  47. return load_bitmap(name, vpath)
  48. end
  49. else
  50. raise "Unknown font file name extension."
  51. end
  52. end
  53. # Ascent height of the font.
  54. def ascent
  55. return Eruta::Store.font_ascent(@id)
  56. end
  57. # Descent of the font.
  58. def descent
  59. return Eruta::Store.font_descent(@id)
  60. end
  61. # Line height of the font.
  62. def line_height
  63. return Eruta::Store.font_line_height(@id)
  64. end
  65. # Text dimensions of the given text in the font.
  66. def text_dimensions(text)
  67. return Eruta::Store.text_dimensions(@id, text)
  68. end
  69. # Width of the given text in the font.
  70. def text_width(text)
  71. return Eruta::Store.text_width(@id, text)
  72. end
  73. end