aboutsummaryrefslogtreecommitdiff
path: root/src/image.c
blob: d9ee5fa20b1a643189ee13aaebdd04941d219b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdbool.h>
#include <stdlib.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include "draw.h"



// Draw an image at a specific point
int draw_image(cairo_t *c, char *path, double x, double y) {
   cairo_surface_t *image;

   // Checks if the file exists. If it doesn't, warn the user that it was not found.
   // A file not existing will not terminate the program, but simply not render the image.
   FILE *file;
   if((file = fopen(path, "r")) != NULL)
   {
      // file exists
      fclose(file);
   }
   else
   {
      printf("Warning: image \'%s\' does not exist.\n", path);
      return 1;
   }

   image = cairo_image_surface_create_from_png(path);

   double width = cairo_image_surface_get_width(image);
   double height = cairo_image_surface_get_height(image);

   double fake_width = 100;
   double fake_height = 100;

   double width_scale = fake_width / width;
   double height_scale = fake_height / height;

   cairo_scale(c, width_scale, height_scale);


   cairo_set_source_surface(c, image, x, y);
   cairo_paint(c);

   cairo_surface_destroy (image);
   return 0;
}




// Draw month art
int draw_month_art(cairo_t *c, struct dimensions *d, struct extents *he, struct alignment *pl, char *path) {
   cairo_save(c);
   double width = he->width;
   // TODO: Make this relative to text height
   double height = he->height - (pl->margin * 3.0);

   double cursor_x = pl->margin;
   double cursor_y = d->margin;
   cairo_rectangle (c, cursor_x, cursor_y, width, height);
   cairo_clip(c);
   
   draw_image(c, path, cursor_x, cursor_y);
   cairo_restore(c);
   return 0;
}