1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <math.h>
20 #include <cairo/cairo.h>
21 #include <pango/pango.h>
22 #include <pango/pangocairo.h>
23 
draw_text(cairo_t * cr,PangoLayout * pl,const char * txt)24 static void draw_text (cairo_t* cr, PangoLayout* pl, const char* txt) {
25 	int tw, th;
26 	cairo_save (cr);
27 	cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
28 	pango_layout_set_text (pl, txt, -1);
29 	// rotate 90deg ? maybe
30 	pango_layout_get_pixel_size (pl, &tw, &th);
31 	cairo_translate (cr, -tw / 2.0, -th / 2.0);
32 	pango_cairo_layout_path (cr, pl);
33 	cairo_fill (cr);
34 	cairo_restore (cr);
35 }
36 
draw_swinger(cairo_t * cr,int w,int h,int n_steps)37 static void draw_swinger (cairo_t* cr, int w, int h, int n_steps) {
38 	int i;
39 
40 	PangoLayout* pl = pango_cairo_create_layout (cr);
41 	PangoFontDescription *desc = pango_font_description_from_string ("Sans 14px"); // XXX use MOD font
42 	pango_layout_set_font_description (pl, desc);
43 	pango_font_description_free (desc);
44 
45 	cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 3.0, h);
46 #if 0
47 	cairo_pattern_add_color_stop_rgba (pat, 0.00, 1.0, 0.0, 0.0, 1.0);
48 	cairo_pattern_add_color_stop_rgba (pat, 0.33, 1.0, 0.6, 0.2, 1.0);
49 	cairo_pattern_add_color_stop_rgba (pat, 0.60, 0.0, 1.0, 0.0, 1.0);
50 	cairo_pattern_add_color_stop_rgba (pat, 1.00, 0.0, 0.0, 1.0, 1.0);
51 #else
52 	cairo_pattern_add_color_stop_rgba (pat, 0.00, 1.0, 0.6, 0.2, 1.0);
53 	cairo_pattern_add_color_stop_rgba (pat, 0.33, 1.0, 0.6, 0.2, 1.0);
54 	cairo_pattern_add_color_stop_rgba (pat, 1.00, 0.5, 0.3, 0.1, 1.0);
55 #endif
56 
57 	const int nsm1 = n_steps - 1;
58 	for (i = 0; i < n_steps; ++i) {
59 		cairo_save (cr);
60 		cairo_translate (cr, i * w, 0);
61 		cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
62 		cairo_rectangle (cr, 0, 0, w, h);
63 		cairo_fill (cr);
64 
65 		float vh = h * i / (n_steps - 1.f);
66 		cairo_rectangle (cr, 0, h - vh, w, vh);
67 		cairo_set_source (cr, pat);
68 		cairo_fill (cr);
69 
70 		cairo_translate (cr, w * .5, h * .5);
71 		if (i == 0) {
72 			draw_text (cr, pl, "1:1");
73 		}
74 		if (  6 * nsm1 == 15 * i) {
75 			draw_text (cr, pl, "3:2");
76 		}
77 		if ( 10 * nsm1 == 15 * i) {
78 			draw_text (cr, pl, "2:1");
79 		}
80 		if ( 15 * nsm1 == 15 * i) {
81 			draw_text (cr, pl, "3:1");
82 		}
83 
84 		cairo_restore (cr);
85 	}
86 
87 	g_object_unref (pl);
88 
89 }
90 
main(int argc,char ** argv)91 int main (int argc, char **argv) {
92 	int w = 30;
93 	int h = 42;
94 	/* need to be able to represent
95 	 * 0, 0.2, 1/3 and 0.5
96 	 * common denominator 30:
97 	 * 0, 6, 10, 15
98 	 */
99 	int n_steps = 31;
100 
101 	cairo_surface_t* cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, n_steps * w, h);
102 	cairo_t* cr = cairo_create (cs);
103 
104 	draw_swinger (cr, w, h, n_steps);
105 
106 	cairo_surface_write_to_png (cs, "../modgui/swinger.png");
107 	cairo_destroy (cr);
108 	cairo_surface_destroy (cs);
109 	return 0;
110 }
111