File: raster_text.cpp

package info (click to toggle)
agg 2.5%2Bdfsg1-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,640 kB
  • sloc: cpp: 64,972; ansic: 11,272; makefile: 3,645; sh: 31
file content (178 lines) | stat: -rw-r--r-- 6,425 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include "agg_basics.h"
#include "agg_rendering_buffer.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_scanline_u.h"
#include "agg_renderer_scanline.h"
#include "agg_pixfmt_rgb.h"
#include "agg_span_allocator.h"
#include "agg_span_gradient.h"
#include "agg_span_interpolator_linear.h"
#include "agg_glyph_raster_bin.h"
#include "agg_renderer_raster_text.h"
#include "agg_embedded_raster_fonts.h"
#include "platform/agg_platform_support.h"
#include "ctrl/agg_slider_ctrl.h"
#include "ctrl/agg_cbox_ctrl.h"


enum flip_y_e { flip_y = true };




//------------------------------------------------------------------------
template<class GradientF> class gradient_sine_repeat_adaptor
{
public:
    gradient_sine_repeat_adaptor() : m_periods(agg::pi* 2.0) {}

    void periods(double p) { m_periods = p * agg::pi * 2.0; }

    int calculate(int x, int y, int d) const
    {
        return int((1.0 + sin(m_gradient.calculate(x, y, d) * m_periods / d)) * d/2);
    }

private:
    GradientF m_gradient;
    double    m_periods;
};




//------------------------------------------------------------------------
class the_application : public agg::platform_support
{
public:
    typedef agg::pixfmt_bgr24 pixfmt;
    typedef agg::renderer_base<pixfmt> ren_base;
    typedef agg::glyph_raster_bin<agg::rgba8> glyph_gen;

    the_application(agg::pix_format_e format, bool flip_y) :
        agg::platform_support(format, flip_y)
    {
    }


    virtual void on_draw()
    {
        struct font_type
        {
            const agg::int8u* font;
            const char* name;
        }
        fonts[] = 
        {
            { agg::gse4x6,                  "gse4x6"               },
            { agg::gse4x8,                  "gse4x8"               },
            { agg::gse5x7,                  "gse5x7"               },
            { agg::gse5x9,                  "gse5x9"               },
            { agg::gse6x9,                  "gse6x9"               },
            { agg::gse6x12,                 "gse6x12"              },
            { agg::gse7x11,                 "gse7x11"              },
            { agg::gse7x11_bold,            "gse7x11_bold"         },
            { agg::gse7x15,                 "gse7x15"              },
            { agg::gse7x15_bold,            "gse7x15_bold"         },
            { agg::gse8x16,                 "gse8x16"              },
            { agg::gse8x16_bold,            "gse8x16_bold"         },
            { agg::mcs11_prop,              "mcs11_prop"           },
            { agg::mcs11_prop_condensed,    "mcs11_prop_condensed" },
            { agg::mcs12_prop,              "mcs12_prop"           },
            { agg::mcs13_prop,              "mcs13_prop"           },
            { agg::mcs5x10_mono,            "mcs5x10_mono"         },
            { agg::mcs5x11_mono,            "mcs5x11_mono"         },
            { agg::mcs6x10_mono,            "mcs6x10_mono"         },
            { agg::mcs6x11_mono,            "mcs6x11_mono"         },
            { agg::mcs7x12_mono_high,       "mcs7x12_mono_high"    },
            { agg::mcs7x12_mono_low,        "mcs7x12_mono_low"     },
            { agg::verdana12,               "verdana12"            },
            { agg::verdana12_bold,          "verdana12_bold"       },
            { agg::verdana13,               "verdana13"            },
            { agg::verdana13_bold,          "verdana13_bold"       },
            { agg::verdana14,               "verdana14"            },
            { agg::verdana14_bold,          "verdana14_bold"       },
            { agg::verdana16,               "verdana16"            },
            { agg::verdana16_bold,          "verdana16_bold"       },
            { agg::verdana17,               "verdana17"            },
            { agg::verdana17_bold,          "verdana17_bold"       },
            { agg::verdana18,               "verdana18"            },
            { agg::verdana18_bold,          "verdana18_bold"       },
            0, 0
        };



        glyph_gen glyph(0);
        pixfmt pixf(rbuf_window());
        ren_base rb(pixf);
        rb.clear(agg::rgba(1,1,1));


        agg::renderer_raster_htext_solid<ren_base, glyph_gen> rt(rb, glyph);

        int i;
        double y = 5;
        rt.color(agg::rgba(0,0,0));
        for(i = 0; fonts[i].font; i++)
        {
            char buf[100];
            strcpy(buf, "A quick brown fox jumps over the lazy dog 0123456789: ");
            strcat(buf, fonts[i].name);

            // Testing "wide-char"
            unsigned wbuf[100];
            unsigned* wp = wbuf;
            const char* p = buf;
            while(*p) *wp++ = *(unsigned char*)p++;
            *wp++ = 0;

            glyph.font(fonts[i].font);
            rt.render_text(5, y, wbuf, !flip_y());
            y += glyph.height() + 1;
        }


        // Rendering raster text with a custom span generator, gradient

        typedef agg::span_interpolator_linear<> interpolator_type;
        typedef agg::span_allocator<agg::rgba8> span_alloc_type;
        typedef agg::span_gradient<agg::rgba8, 
                                   interpolator_type, 
                                   gradient_sine_repeat_adaptor<agg::gradient_circle>, 
                                   agg::gradient_linear_color<agg::rgba8> > span_gen_type;
        typedef agg::renderer_scanline_aa<ren_base, 
                                          span_alloc_type,
                                          span_gen_type> ren_type;

        agg::trans_affine mtx;
        gradient_sine_repeat_adaptor<agg::gradient_circle> grad_func;
        grad_func.periods(5);
        agg::gradient_linear_color<agg::rgba8> color_func;
        color_func.colors(agg::rgba(1.0,0,0), agg::rgba(0,0.5,0));
        interpolator_type inter(mtx);
        span_alloc_type sa;
        span_gen_type sg(inter, grad_func, color_func, 0, 150.0);
        ren_type ren(rb, sa, sg);

        agg::renderer_raster_htext<ren_type, glyph_gen> rt2(ren, glyph);
        rt2.render_text(5, 465, (unsigned char*)"RADIAL REPEATING GRADIENT: A quick brown fox jumps over the lazy dog", !flip_y());
    }

};


int agg_main(int argc, char* argv[])
{
    the_application app(agg::pix_format_bgr24, flip_y);
    app.caption("AGG Example. Raster Text");

    if(app.init(640, 480, agg::window_resize))
    {
        return app.run();
    }
    return 1;
}