summaryrefslogtreecommitdiff
path: root/pics.c
blob: d9b571edd7e2157748b6ca7041891bdc744cddf8 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <cairo.h>

#define CHECK_SIZE 16

static cairo_t *
get_cairo (int width, int height)
{
  cairo_surface_t *target = 
    cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
  cairo_t *cr;
  int i, j;

  cr = cairo_create (target);

  for (i = 0; i < width; i += CHECK_SIZE)
    for (j = 0; j < height; j += CHECK_SIZE)
      {
	int light = !!(((i / CHECK_SIZE) ^ (j / CHECK_SIZE)) & 1);

	if (light)
	  cairo_set_source_rgb (cr, 0.95, 0.95, 0.95);
	else
	  cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);

	cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
	cairo_fill (cr);
      }

  cairo_push_group (cr);

  return cr;
}

static void
finish (cairo_t *cr, const char *filename)
{
  cairo_surface_t *target = cairo_get_target (cr);

  cairo_pop_group_to_source (cr);
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  cairo_paint (cr);

  cairo_surface_write_to_png (target, filename);
  cairo_surface_destroy (target);
  cairo_destroy (cr);
}

static void
draw_ampersand (cairo_t *cr)
{
  cairo_rel_move_to (cr, 5, 70.0);
  cairo_select_font_face (cr, "Sans", 
			  CAIRO_FONT_SLANT_NORMAL, 
			  CAIRO_FONT_WEIGHT_BOLD);
  cairo_set_font_size (cr, 80);
  cairo_text_path (cr, "&");
  cairo_set_source_rgb (cr, 0.2, 0.2, 0.4);
  cairo_fill (cr);
}

static void
draw_polygon (cairo_t *cr)
{
  cairo_rel_move_to (cr, 10, 10);
  cairo_rel_line_to (cr, 65, 35);
  cairo_rel_line_to (cr, -38, 35);
  
  cairo_set_source_rgb (cr, 0.8, 0.3, 0.2);
  cairo_fill (cr);
}

static void
create_ampersand (void)
{
  cairo_t *cr = get_cairo (90, 90);

  cairo_move_to (cr, 0, 0);
  draw_ampersand (cr);

  finish (cr, "ampersand.png");
}

static void
create_polygon (void)
{
  cairo_t *cr = get_cairo (90, 90);

  cairo_move_to (cr, 0, 0);
  draw_polygon (cr);

  finish (cr, "polygon.png");
}

static struct {
  char name[16];
  cairo_operator_t op;
} ops[] =
  {
    "Source", CAIRO_OPERATOR_SOURCE,
    "Over", CAIRO_OPERATOR_OVER,
    "Atop", CAIRO_OPERATOR_ATOP,
    "In", CAIRO_OPERATOR_IN,
    "Out", CAIRO_OPERATOR_OUT,

    "Dest", CAIRO_OPERATOR_DEST,
    "Dest Over", CAIRO_OPERATOR_DEST_OVER,
    "Dest Atop", CAIRO_OPERATOR_DEST_ATOP,
    "Dest In", CAIRO_OPERATOR_DEST_IN,
    "Dest Out", CAIRO_OPERATOR_DEST_OUT,

    "Clear", CAIRO_OPERATOR_CLEAR,
    "Xor", CAIRO_OPERATOR_XOR,
  };

static void
draw_source (cairo_t *cr)
{
  draw_polygon (cr);
}

static void
draw_dest (cairo_t *cr)
{
  draw_ampersand (cr);
}

static void
centered_text (cairo_t *cr, double x, double y, const char *text)
{
  double x1, y1, x2, y2;

  cairo_save (cr);
  cairo_select_font_face (cr, "Sans", 
			  CAIRO_FONT_SLANT_NORMAL, 
			  CAIRO_FONT_WEIGHT_NORMAL);
  cairo_new_path (cr);
  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  cairo_set_font_size (cr, 14.0);
  cairo_text_path (cr, text);
  cairo_path_extents (cr, &x1, &y1, &x2, &y2);
  cairo_new_path (cr);
  printf ("in x %f %f %s\n", x, x2 - x1, text);
  cairo_move_to (cr, x - (x2 - x1) / 2.0, y);
  cairo_show_text (cr, text);
  cairo_restore (cr);
}

#define CELL_WIDTH 120
#define CELL_HEIGHT 130
#define XPAD (CELL_WIDTH / 4)
#define YPAD (CELL_WIDTH / 4)

static void
draw_cell (cairo_t *cr, 
	   double x, double y,
	   cairo_operator_t op, 
	   const char *name)
{
  cairo_save (cr);
      
  printf ("move to %f %f for %s\n", x, y, name);
  cairo_rectangle (cr, x - 10, y, CELL_WIDTH + 20, CELL_HEIGHT);
  cairo_clip (cr);
      
  /* Cairo's source and clear are clip-to-self, but we
   * want to draw the actual Porter/Duff operator, 
   * so those need to be special cased.
   */
  if (op == CAIRO_OPERATOR_SOURCE)
    {
      cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
      cairo_move_to (cr, x, y);
      draw_source (cr);
    }
  else if (op != CAIRO_OPERATOR_CLEAR)
    {
      cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
      cairo_move_to (cr, x, y);
      draw_dest (cr);
      cairo_move_to (cr, x, y);
      cairo_set_operator (cr, op);
      draw_source (cr);
    }

  centered_text (cr, 
		 x + CELL_WIDTH / 2.0 - 20.0, 
		 y + CELL_HEIGHT - 30, name);

  cairo_restore (cr);
}

static void
create_porter_duff_table (void)
{
  cairo_t *cr = get_cairo (CELL_WIDTH * 5 + XPAD,
			   (CELL_HEIGHT) * 3 + 2 * YPAD);
  int i;
  
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint (cr);

  for (i = 0; i < 12; ++i)
    {
      double x = CELL_WIDTH * (i % 5) + XPAD;
      double y = CELL_HEIGHT * (i / 5) + YPAD;

      draw_cell (cr, x, y, ops[i].op, ops[i].name);
    }
    
  finish (cr, "table.png");
}

static void
create_op (cairo_operator_t op, const char *name, const char *filename)
{
  cairo_t *cr = get_cairo (CELL_WIDTH + 20, CELL_HEIGHT);
  char fn[64] = { 0 };

  cairo_move_to (cr, 0, 0);

  draw_cell (cr, 30, 10, op, name);

  snprintf (fn, 63, "%s.png", filename);
  finish (cr, fn);
}

int
main (int argc, char *argv)
{
  create_ampersand ();
  create_polygon ();
  create_op (CAIRO_OPERATOR_COLOR_DODGE, "Color Dodge", "colordodge.png");
  create_op (CAIRO_OPERATOR_DEST_ATOP, "Dest Atop", "destatop.png");
  create_op (CAIRO_OPERATOR_OVER, "Over", "over.png");
  create_porter_duff_table ();
}