thanks, jk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

  1. //! Textium is a small library that wires Rusttype up to OpenGL.
  2. //!
  3. //! Textium provides a glyph atlas, atlas packers, a glyph rasterizer, and the necessary
  4. //! shaders and infrastructure to easily connect to (at the moment) Glium.
  5. //!
  6. //! A future goal is making rendering backends generic.
  7. extern crate rusttype;
  8. #[macro_use]
  9. extern crate glium;
  10. mod error;
  11. pub mod texture;
  12. pub mod geometry;
  13. pub mod rasterizer;
  14. pub mod packer;
  15. pub mod cache;
  16. use std::collections::HashMap;
  17. use std::hash::{Hash, Hasher};
  18. use glium::texture::Texture2d;
  19. pub use error::Error;
  20. pub use geometry::Rect;
  21. pub use rasterizer::Font;
  22. /// Information used to render a glyph in a font.
  23. #[derive(Debug, Clone)]
  24. pub struct GlyphMetadata {
  25. /// Bounding box in the packed texture
  26. bounding_box: Rect<usize>,
  27. /// Scale that the character was rendered at
  28. scale: f32,
  29. /// Distance between the bottom of the glyph and the baseline, in ems.
  30. // baseline_distance: f32,
  31. /// Distance to advance in layout after this character
  32. advance_width: f32,
  33. /// Horizontal offset between glyph origin and leftmost point of the glyph
  34. left_bearing: f32,
  35. }
  36. /// Maps glyphs to their size, location, and rendering properties in a glyph atlas.
  37. #[derive(Debug)]
  38. pub struct FontAtlas {
  39. pub glyph_metadata: HashMap<char, GlyphMetadata>
  40. }
  41. impl FontAtlas {
  42. pub fn info(&self, chr: char) -> Option<GlyphMetadata> {
  43. self.glyph_metadata.get(&chr).cloned()
  44. }
  45. }
  46. /// A GPU-backed font texture, plus a [`FontAtlas`].
  47. ///
  48. /// [`FontAtlas`]: struct.FontAtlas.html
  49. pub struct FontTexture {
  50. /// The GPU texture of packed glyphs.
  51. pub data: Texture2d,
  52. /// Atlas describing where glyphs are.
  53. pub atlas: FontAtlas,
  54. }
  55. #[derive(Debug, Clone, Copy)]
  56. struct Scale(f32);
  57. impl Scale {
  58. fn canonicalize(&self) -> i64 {
  59. (self.0 * 1024.0 * 1024.0).round() as i64
  60. }
  61. }
  62. impl PartialEq for Scale {
  63. fn eq(&self, other: &Self) -> bool {
  64. self.canonicalize() == other.canonicalize()
  65. }
  66. }
  67. impl Eq for Scale {}
  68. impl Hash for Scale {
  69. fn hash<H>(&self, state: &mut H) where H: Hasher {
  70. self.canonicalize().hash(state);
  71. }
  72. }