summaryrefslogtreecommitdiffstatsci
path: root/src/cairo-tests/text.cpp
blob: 406456b8238d31edfa2bce270f23f8ae74c3587b (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
/*
 * Copyright 2014 © Samsung Research America, Silicon Valley
 *
 * Use of this source code is governed by the 3-Clause BSD license
 * specified in the COPYING file included with this source code.
 */
#include <config.h>

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

#include "caskbench.h"
#include "caskbench_context.h"
#include "cairo-shapes.h"

static int max_dim;
static char rand_text_array[19][100];

void gen_random(char *s, const int len) {
    static const char alphanum[] =
        "ABCD EFG HIJKL MNOPQ RSTUVW XYZ";

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = 0;
}

int
ca_setup_text(caskbench_context_t *ctx)
{
    max_dim = MIN (ctx->canvas_width, ctx->canvas_height)/2;
    for(int i = 0;i<19;i++)
    {
        gen_random (rand_text_array[i],i+18);
    }
    return 1;
}

void
ca_teardown_text(void)
{
}

int
ca_test_text(caskbench_context_t *ctx)
{
    cairo_t *cr = ctx->cairo_cr;

    for (int i = 0; i < ctx->size; i++)
    {
        double font_size = 18;
        int ypos = 0, xpos = 0;

        cairo_set_source_rgb (cr, 0, 0, 0);
        cairo_rectangle (cr, 0, 0, ctx->canvas_width ,ctx->canvas_height);
        cairo_fill (cr);

        while (ypos <= ctx->canvas_height/2)
        {
            cairo_set_font_size (cr, font_size);
            cairoRandomizeColor(ctx);

            cairo_status_t status;
            cairo_scaled_font_t *font;
            font = cairo_get_scaled_font (cr);
            cairo_glyph_t *glyphs = NULL;
            int num_glyphs;
            cairo_text_cluster_t *clusters = NULL;
            int num_clusters;
            cairo_text_cluster_flags_t cluster_flags;

            char text[(int)font_size];
            gen_random (text,font_size);

            status = cairo_scaled_font_text_to_glyphs (font,
                                                       xpos, ypos,
                                                       text, font_size,
                                                       &glyphs, &num_glyphs,
                                                       &clusters, &num_clusters, &cluster_flags);

            if (status == CAIRO_STATUS_SUCCESS) {
                cairo_show_text_glyphs (cr,
                                        rand_text_array[(int)font_size-18],font_size,
                                        glyphs, num_glyphs,
                                        clusters, num_clusters, cluster_flags);
            }
            font_size = font_size+0.5;
            if(font_size>=36)
                font_size=36;
            ypos += (font_size/2);
        }
        while (ypos <= ctx->canvas_height)
        {
            cairo_set_font_size (cr, font_size);
            cairoRandomizeColor(ctx);

            cairo_status_t status;
            cairo_scaled_font_t *font;
            font = cairo_get_scaled_font (cr);
            cairo_glyph_t *glyphs = NULL;
            int num_glyphs;
            cairo_text_cluster_t *clusters = NULL;
            int num_clusters;
            cairo_text_cluster_flags_t cluster_flags;

            char text[(int)font_size];
            gen_random (text,font_size);

            status = cairo_scaled_font_text_to_glyphs (font,
                                                       xpos, ypos,
                                                       text, font_size,
                                                       &glyphs, &num_glyphs,
                                                       &clusters, &num_clusters, &cluster_flags);

            if (status == CAIRO_STATUS_SUCCESS) {
            cairo_show_text_glyphs (cr,
                                    rand_text_array[(int)font_size-18],font_size,
                                    glyphs, num_glyphs,
                                    clusters, num_clusters, cluster_flags);
            }
            font_size = font_size-0.5;
            if(font_size<=18)
                font_size=18;
            ypos += (font_size/2);
        }
    }
    return 1;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :