draw.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use crate::data::Config;
  2. use crate::file::ThymeFile;
  3. pub struct Pattern {
  4. pub thyme: ThymeFile,
  5. // pub image: Image,
  6. pub config: Config,
  7. // pub mapping: Mapping,
  8. }
  9. impl Pattern {
  10. pub fn draw(&self, ctx: cairo::Context) -> Result<(), Box<dyn std::error::Error>> {
  11. let width = self.thyme.width;
  12. let height = self.thyme.height;
  13. ctx.set_source_rgb(1.0, 1.0, 1.0);
  14. ctx.paint()?;
  15. let layout = pangocairo::functions::create_layout(&ctx).unwrap();
  16. let font = pango::FontDescription::from_string(&self.config.font);
  17. layout.set_width(2880);
  18. layout.set_font_description(Some(&font));
  19. ctx.set_source_rgb(0.0, 0.0, 0.0);
  20. for i in 0..=width {
  21. ctx.move_to(self.config.scale(i), 0.0);
  22. ctx.line_to(self.config.scale(i), self.config.scale(height));
  23. ctx.set_line_width(if i % self.config.grid_every == 0 {
  24. self.config.major_line_weight
  25. } else {
  26. self.config.line_weight
  27. });
  28. ctx.stroke()?;
  29. }
  30. for j in 0..=height {
  31. ctx.move_to(0.0, self.config.scale(j));
  32. ctx.line_to(self.config.scale(width), self.config.scale(j));
  33. ctx.set_line_width(if j % self.config.grid_every == 0 {
  34. self.config.major_line_weight
  35. } else {
  36. self.config.line_weight
  37. });
  38. ctx.stroke()?;
  39. }
  40. ctx.set_source_rgb(0.0, 0.0, 0.0);
  41. for ((x, y), stitch) in self.thyme.iter() {
  42. if let Some((_, color)) = stitch {
  43. ctx.move_to(self.config.scale(x) + 5.0, self.config.scale(y) + 3.0);
  44. layout.set_text(&color.symbol);
  45. pangocairo::functions::show_layout(&ctx, &layout);
  46. }
  47. }
  48. Ok(())
  49. }
  50. }