summaryrefslogtreecommitdiff
path: root/text.c
blob: 1e512af7521d6cef3decef865984c214878f5e0b (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
/*
 * Copyright © 2004 David Reveman, Peter Nilsson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the names of
 * David Reveman and Peter Nilsson not be used in advertising or
 * publicity pertaining to distribution of the software without
 * specific, written prior permission. David Reveman and Peter Nilsson
 * makes no representations about the suitability of this software for
 * any purpose. It is provided "as is" without express or implied warranty.
 *
 * DAVID REVEMAN AND PETER NILSSON DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DAVID REVEMAN AND
 * PETER NILSSON BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors: David Reveman <c99drn@cs.umu.se>
 *          Peter Nilsson <c99pnn@cs.umu.se>
 */

#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

static float font_scale1, font_scale2,
	     font_rotate1, font_rotate2,
	     font_alpha1, font_alpha2,
	     font_speed1, font_speed2,
	     font_x1, font_y1,
	     font_x2, font_y2;

static cairo_surface_t *bg_surface;
static int bg_width, bg_height;

void
text_setup (cairo_surface_t *target, int w, int h)
{
    cairo_surface_t *image;
    cairo_t *cr;

    image = cairo_image_surface_create_from_png ("orion.png");
    if (cairo_surface_status (image)) {
	printf ("error reading orion.png: %s\n",
		cairo_status_to_string (cairo_surface_status (image)));
	exit(1);
    }

    bg_width = cairo_image_surface_get_width (image);
    bg_height = cairo_image_surface_get_height (image);
    bg_surface = cairo_surface_create_similar (target,
	    CAIRO_CONTENT_COLOR_ALPHA,
	    bg_width, bg_height);

    cr = cairo_create (bg_surface);
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_surface (cr, image, 0.0, 0.0);
    cairo_paint (cr);
    cairo_destroy (cr);

    cairo_surface_destroy (image);

    drand48 ();
    drand48 ();

    font_rotate1 = (float) (drand48 () * 3.14);
    font_rotate2 = (float) (drand48 () * 3.14);
    font_scale1 =  (float) (drand48 () * 250.0);
    font_scale2 =  (float) (drand48 () * 250.0);
    font_speed1 = (float) (drand48 () + 0.5);
    font_speed2 = (float) (drand48 () + 0.5);
    font_alpha1 = 0.0;
    font_alpha2 = 0.0;
    font_x1 =  200.0 + (float) (drand48 () * (w - 200.0));
    font_y1 =  100.0 + (float) (drand48 () * (h - 100.0));
    font_x2 =  200.0 + (float) (drand48 () * (w - 200.0));
    font_y2 =  100.0 + (float) (drand48 () * (h - 100.0));
}

static void
text_path_render_text1 (cairo_t *cr)
{
    cairo_save (cr);
    cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, font_alpha1);

    cairo_set_font_size (cr, font_scale1);

    cairo_move_to (cr, font_x1 - font_scale1, font_y1);
    cairo_rotate(cr, font_rotate1);
    cairo_text_path (cr, "CAIRO");
    cairo_fill_preserve (cr);
    cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, font_alpha1);
    cairo_stroke (cr);
    cairo_restore (cr);
}

static void
text_path_render_text2 (cairo_t *cr)
{
    cairo_save (cr);
    cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, font_alpha2);
    cairo_set_font_size (cr, font_scale2);
    cairo_move_to (cr, font_x2 - font_scale2, font_y2);
    cairo_rotate(cr, font_rotate2);
    cairo_text_path (cr, "GEARS");
    cairo_fill_preserve (cr);
    cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, font_alpha2);
    cairo_stroke (cr);
    cairo_restore (cr);
}

void
text_render (cairo_t *cr, int w, int h)
{
    double scale_x, scale_y;

    cairo_select_font_face (cr,
	    "arial",
	    CAIRO_FONT_SLANT_NORMAL,
	    CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size (cr, 1.0);

    cairo_save (cr);

    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_move_to (cr, 0, 0);

    scale_x = ((double) w) / (double) bg_width;
    scale_y = ((double) h) / (double) bg_height;

    cairo_scale (cr, scale_x, scale_y);

    cairo_set_source_surface (cr, bg_surface, 0.0, 0.0);
    cairo_paint (cr);

    cairo_set_operator (cr, CAIRO_OPERATOR_OVER);

    if (font_scale1 < font_scale2) {
	text_path_render_text1 (cr);
	text_path_render_text2 (cr);
    } else {
	text_path_render_text2 (cr);
	text_path_render_text1 (cr);
    }

    if (font_scale1 > 250) {
	font_scale1 = (float) (drand48 () * 50.0);
	font_x1 =  ((float) (drand48 () * (w - 400.0))) + 200.0;
	font_y1 =  ((float) (drand48 () * (h - 200.0))) + 100.0;
	font_rotate1 = (float) (drand48 () * 3.14);
	font_speed1 = (float) (drand48 () * 10.0);
	font_alpha1 = 0;
    }

    if (font_scale2 > 250) {
	font_scale2 = (float) (drand48 () * 50.0);
	font_x2 =  ((float) (drand48 () * (w - 400.0))) + 200.0;
	font_y2 =  ((float) (drand48 () * (h - 200.0))) + 100.0;
	font_rotate2 = (float) (drand48 () * 3.14);
	font_speed2 = (float) (drand48 () * 10.0);
	font_alpha2 = 0;
    }

    font_scale1 > 150? font_scale1 += 2: font_scale1++;
    font_scale2 > 150? font_scale2 += 2: font_scale2++;
    font_scale1 < 175?  (font_alpha1 += 0.01): (font_alpha1 -= 0.05);
    font_scale2 < 175?  (font_alpha2 += 0.01): (font_alpha2 -= 0.05);
    font_scale1 += font_speed1;
    font_scale2 += font_speed2;

    cairo_restore (cr);
}