Debian Codegraph @

tests/performance/test-text-perf.c

raw
1 #include <clutter/clutter.h>
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include "test-common.h"
6
7 #define STAGE_WIDTH 800
8 #define STAGE_HEIGHT 600
9
10 static int font_size;
11 static int n_chars;
12 static int rows, cols;
13
14 static gboolean
15 queue_redraw (gpointer stage)
16 {
17 clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
18
19 return G_SOURCE_CONTINUE;
20 }
21
22 static gunichar
23 get_character (int ch)
24 {
25 int total_letters = 0;
26 int i;
27
28 static const struct
29 {
30 gunichar first_letter;
31 int n_letters;
32 }
33 ranges[] =
34 {
35 { 'a', 26 }, /* lower case letters */
36 { 'A', 26 }, /* upper case letters */
37 { '0', 10 }, /* digits */
38 { 0x410, 0x40 }, /* cyrillic alphabet */
39 { 0x3b1, 18 } /* greek alphabet */
40 };
41
42 for (i = 0; i < G_N_ELEMENTS (ranges); i++)
43 total_letters += ranges[i].n_letters;
44
45 ch %= total_letters;
46
47 for (i = 0; i < G_N_ELEMENTS (ranges) - 1; i++)
48 if (ch < ranges[i].n_letters)
49 return ch + ranges[i].first_letter;
50 else
51 ch -= ranges[i].n_letters;
52
53 return ch + ranges[i].first_letter;
54 }
55
56 static ClutterActor *
57 create_label (void)
58 {
59 ClutterColor label_color = { 0xff, 0xff, 0xff, 0xff };
60 ClutterActor *label;
61 char *font_name;
62 GString *str;
63 int i;
64
65 font_name = g_strdup_printf ("Monospace %dpx", font_size);
66
67 str = g_string_new (NULL);
68 for (i = 0; i < n_chars; i++)
69 g_string_append_unichar (str, get_character (i));
70
71 label = clutter_text_new_with_text (font_name, str->str);
72 clutter_text_set_color (CLUTTER_TEXT (label), &label_color);
73
74 g_free (font_name);
75 g_string_free (str, TRUE);
76
77 return label;
78 }
79
80 int
81 main (int argc, char *argv[])
82 {
83 ClutterActor *stage;
84 ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff };
85 ClutterActor *label;
86 int w, h;
87 int row, col;
88 float scale = 1.0f;
89
90 clutter_perf_fps_init ();
91
92 if (CLUTTER_INIT_SUCCESS != clutter_init (&argc, &argv))
93 g_error ("Failed to initialize Clutter");
94
95 if (argc != 3)
96 {
97 //g_printerr ("Usage test-text-perf FONT_SIZE N_CHARS\n");
98 //exit (1);
99 font_size = 30;
100 n_chars = 400;
101 }
102 else
103 {
104 font_size = atoi (argv[1]);
105 n_chars = atoi (argv[2]);
106 }
107
108 g_print ("Monospace %dpx, string length = %d\n", font_size, n_chars);
109
110 stage = clutter_stage_new ();
111 clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
112 clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
113 clutter_stage_set_title (CLUTTER_STAGE (stage), "Text Performance");
114 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
115
116 label = create_label ();
117 w = clutter_actor_get_width (label);
118 h = clutter_actor_get_height (label);
119
120 /* If the label is too big to fit on the stage then scale it so that
121 it will fit */
122 if (w > STAGE_WIDTH || h > STAGE_HEIGHT)
123 {
124 float x_scale = STAGE_WIDTH / (float) w;
125 float y_scale = STAGE_HEIGHT / (float) h;
126
127 if (x_scale < y_scale)
128 {
129 scale = x_scale;
130 cols = 1;
131 rows = STAGE_HEIGHT / (h * scale);
132 }
133 else
134 {
135 scale = y_scale;
136 cols = STAGE_WIDTH / (w * scale);
137 rows = 1;
138 }
139
140 g_print ("Text scaled by %f to fit on the stage\n", scale);
141 }
142 else
143 {
144 cols = STAGE_WIDTH / w;
145 rows = STAGE_HEIGHT / h;
146 }
147
148 clutter_actor_destroy (label);
149
150 for (row=0; row<rows; row++)
151 for (col=0; col<cols; col++)
152 {
153 label = create_label();
154 clutter_actor_set_scale (label, scale, scale);
155 clutter_actor_set_position (label, w * col * scale, h * row * scale);
156 clutter_container_add_actor (CLUTTER_CONTAINER (stage), label);
157 }
158
159 clutter_actor_show_all (stage);
160
161 clutter_perf_fps_start (CLUTTER_STAGE (stage));
162 clutter_threads_add_idle (queue_redraw, stage);
163 clutter_main ();
164 clutter_perf_fps_report ("test-text-perf");
165
166 return 0;
167 }