summaryrefslogtreecommitdiff
path: root/agg/inc/agg_font_cache_manager.h
blob: 55c4d39db75d6711e4209493395c2e4748c3b86c (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------

#ifndef AGG_FONT_CACHE_MANAGER_INCLUDED
#define AGG_FONT_CACHE_MANAGER_INCLUDED

#include <string.h>
#include "agg_array.h"

namespace agg
{

    //---------------------------------------------------------glyph_data_type
    enum glyph_data_type
    {
        glyph_data_invalid = 0,
        glyph_data_mono    = 1,
        glyph_data_gray8   = 2,
        glyph_data_outline = 3
    };


    //-------------------------------------------------------------glyph_cache
    struct glyph_cache
    {
        unsigned        glyph_index;
        int8u*          data;
        unsigned        data_size;
        glyph_data_type data_type;
        rect            bounds;
        double          advance_x;
        double          advance_y;
    };


    //--------------------------------------------------------------font_cache
    class font_cache
    {
    public:
        enum { block_size = 16384-16 };

        //--------------------------------------------------------------------
        font_cache(const char* font_signature) :
            m_allocator(block_size),
            m_font_signature(0)
        {
            m_font_signature = (char*)m_allocator.allocate(strlen(font_signature) + 1);
            strcpy(m_font_signature, font_signature);
            memset(m_glyphs, 0, sizeof(m_glyphs));
        }

        //--------------------------------------------------------------------
        bool font_is(const char* font_signature) const
        {
            return strcmp(font_signature, m_font_signature) == 0;
        }

        //--------------------------------------------------------------------
        const glyph_cache* find_glyph(unsigned glyph_code) const
        {
            unsigned msb = (glyph_code >> 8) & 0xFF;
            if(m_glyphs[msb])
            {
                return m_glyphs[msb][glyph_code & 0xFF];
            }
            return 0;
        }

        //--------------------------------------------------------------------
        glyph_cache* cache_glyph(unsigned        glyph_code,
                                 unsigned        glyph_index,
                                 unsigned        data_size,
                                 glyph_data_type data_type,
                                 const rect&     bounds,
                                 double          advance_x,
                                 double          advance_y)
        {
            unsigned msb = (glyph_code >> 8) & 0xFF;
            if(m_glyphs[msb] == 0)
            {
                m_glyphs[msb] =
                    (glyph_cache**)m_allocator.allocate(sizeof(glyph_cache*) * 256,
                                                        sizeof(glyph_cache*));
                memset(m_glyphs[msb], 0, sizeof(glyph_cache*) * 256);
            }

            unsigned lsb = glyph_code & 0xFF;
            if(m_glyphs[msb][lsb]) return 0; // Already exists, do not overwrite

            glyph_cache* glyph =
                (glyph_cache*)m_allocator.allocate(sizeof(glyph_cache),
                                                   sizeof(double));

            glyph->glyph_index = glyph_index;
            glyph->data        = m_allocator.allocate(data_size);
            glyph->data_size   = data_size;
            glyph->data_type   = data_type;
            glyph->bounds      = bounds;
            glyph->advance_x   = advance_x;
            glyph->advance_y   = advance_y;
            return m_glyphs[msb][lsb] = glyph;
        }

    private:
        pod_allocator   m_allocator;
        glyph_cache**   m_glyphs[256];
        char*           m_font_signature;
    };







    //---------------------------------------------------------font_cache_pool
    class font_cache_pool
    {
    public:
        //--------------------------------------------------------------------
        ~font_cache_pool()
        {
            unsigned i;
            for(i = 0; i < m_num_fonts; ++i)
            {
                delete m_fonts[i];
            }
            delete [] m_fonts;
        }

        //--------------------------------------------------------------------
        font_cache_pool(unsigned max_fonts=32) :
            m_fonts(new font_cache* [max_fonts]),
            m_max_fonts(max_fonts),
            m_num_fonts(0),
            m_cur_font(0)
        {}


        //--------------------------------------------------------------------
        void font(const char* font_signature, bool reset_cache = false)
        {
            int idx = find_font(font_signature);
            if(idx >= 0)
            {
                if(reset_cache)
                {
                    delete m_fonts[idx];
                    m_fonts[idx] = new font_cache(font_signature);
                }
                m_cur_font = m_fonts[idx];
            }
            else
            {
                if(m_num_fonts >= m_max_fonts)
                {
                    delete m_fonts[0];
                    memcpy(m_fonts,
                           m_fonts + 1,
                           (m_max_fonts - 1) * sizeof(font_cache*));
                    m_num_fonts = m_max_fonts - 1;
                }
                m_fonts[m_num_fonts] = new font_cache(font_signature);
                m_cur_font = m_fonts[m_num_fonts];
                ++m_num_fonts;
            }
        }

        //--------------------------------------------------------------------
        const font_cache* font() const
        {
            return m_cur_font;
        }

        //--------------------------------------------------------------------
        const glyph_cache* find_glyph(unsigned glyph_code) const
        {
            if(m_cur_font) return m_cur_font->find_glyph(glyph_code);
            return 0;
        }

        //--------------------------------------------------------------------
        glyph_cache* cache_glyph(unsigned        glyph_code,
                                 unsigned        glyph_index,
                                 unsigned        data_size,
                                 glyph_data_type data_type,
                                 const rect&     bounds,
                                 double          advance_x,
                                 double          advance_y)
        {
            if(m_cur_font)
            {
                return m_cur_font->cache_glyph(glyph_code,
                                               glyph_index,
                                               data_size,
                                               data_type,
                                               bounds,
                                               advance_x,
                                               advance_y);
            }
            return 0;
        }


        //--------------------------------------------------------------------
        int find_font(const char* font_signature)
        {
            unsigned i;
            for(i = 0; i < m_num_fonts; i++)
            {
                if(m_fonts[i]->font_is(font_signature)) return int(i);
            }
            return -1;
        }

    private:
        font_cache** m_fonts;
        unsigned     m_max_fonts;
        unsigned     m_num_fonts;
        font_cache*  m_cur_font;
    };




    //------------------------------------------------------------------------
    enum glyph_rendering
    {
        glyph_ren_native_mono,
        glyph_ren_native_gray8,
        glyph_ren_outline,
        glyph_ren_agg_mono,
        glyph_ren_agg_gray8
    };




    //------------------------------------------------------font_cache_manager
    template<class FontEngine> class font_cache_manager
    {
    public:
        typedef FontEngine font_engine_type;
        typedef font_cache_manager<FontEngine> self_type;
        typedef typename font_engine_type::path_adaptor_type   path_adaptor_type;
        typedef typename font_engine_type::gray8_adaptor_type  gray8_adaptor_type;
        typedef typename gray8_adaptor_type::embedded_scanline gray8_scanline_type;
        typedef typename font_engine_type::mono_adaptor_type   mono_adaptor_type;
        typedef typename mono_adaptor_type::embedded_scanline  mono_scanline_type;

        //--------------------------------------------------------------------
        font_cache_manager(font_engine_type& engine, unsigned max_fonts=32) :
            m_fonts(max_fonts),
            m_engine(engine),
            m_change_stamp(-1),
            m_prev_glyph(0),
            m_last_glyph(0)
        {}

        //--------------------------------------------------------------------
        const glyph_cache* glyph(unsigned glyph_code)
        {
            synchronize();
            const glyph_cache* gl = m_fonts.find_glyph(glyph_code);
            if(gl)
            {
                m_prev_glyph = m_last_glyph;
                return m_last_glyph = gl;
            }
            else
            {
                if(m_engine.prepare_glyph(glyph_code))
                {
                    m_prev_glyph = m_last_glyph;
                    m_last_glyph = m_fonts.cache_glyph(glyph_code,
                                                       m_engine.glyph_index(),
                                                       m_engine.data_size(),
                                                       m_engine.data_type(),
                                                       m_engine.bounds(),
                                                       m_engine.advance_x(),
                                                       m_engine.advance_y());
                    m_engine.write_glyph_to(m_last_glyph->data);
                    return m_last_glyph;
                }
            }
            return 0;
        }

        //--------------------------------------------------------------------
        void init_embedded_adaptors(const glyph_cache* gl,
                                    double x, double y,
                                    double scale=1.0)
        {
            if(gl)
            {
                switch(gl->data_type)
                {
                default: return;
                case glyph_data_mono:
                    m_mono_adaptor.init(gl->data, gl->data_size, x, y);
                    break;

                case glyph_data_gray8:
                    m_gray8_adaptor.init(gl->data, gl->data_size, x, y);
                    break;

                case glyph_data_outline:
                    m_path_adaptor.init(gl->data, gl->data_size, x, y, scale);
                    break;
                }
            }
        }


        //--------------------------------------------------------------------
        path_adaptor_type&   path_adaptor()   { return m_path_adaptor;   }
        gray8_adaptor_type&  gray8_adaptor()  { return m_gray8_adaptor;  }
        gray8_scanline_type& gray8_scanline() { return m_gray8_scanline; }
        mono_adaptor_type&   mono_adaptor()   { return m_mono_adaptor;   }
        mono_scanline_type&  mono_scanline()  { return m_mono_scanline;  }

        //--------------------------------------------------------------------
        const glyph_cache* perv_glyph() const { return m_prev_glyph; }
        const glyph_cache* last_glyph() const { return m_last_glyph; }

        //--------------------------------------------------------------------
        bool add_kerning(double* x, double* y)
        {
            if(m_prev_glyph && m_last_glyph)
            {
                return m_engine.add_kerning(m_prev_glyph->glyph_index,
                                            m_last_glyph->glyph_index,
                                            x, y);
            }
            return false;
        }

        //--------------------------------------------------------------------
        void precache(unsigned from, unsigned to)
        {
            for(; from <= to; ++from) glyph(from);
        }

        //--------------------------------------------------------------------
        void reset_cache()
        {
            m_fonts.font(m_engine.font_signature(), true);
            m_change_stamp = m_engine.change_stamp();
            m_prev_glyph = m_last_glyph = 0;
        }

    private:
        //--------------------------------------------------------------------
        font_cache_manager(const self_type&);
        const self_type& operator = (const self_type&);

        //--------------------------------------------------------------------
        void synchronize()
        {
            if(m_change_stamp != m_engine.change_stamp())
            {
                m_fonts.font(m_engine.font_signature());
                m_change_stamp = m_engine.change_stamp();
                m_prev_glyph = m_last_glyph = 0;
            }
        }

        font_cache_pool     m_fonts;
        font_engine_type&   m_engine;
        int                 m_change_stamp;
        double              m_dx;
        double              m_dy;
        const glyph_cache*  m_prev_glyph;
        const glyph_cache*  m_last_glyph;
        path_adaptor_type   m_path_adaptor;
        gray8_adaptor_type  m_gray8_adaptor;
        gray8_scanline_type m_gray8_scanline;
        mono_adaptor_type   m_mono_adaptor;
        mono_scanline_type  m_mono_scanline;
    };

}

#endif