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
|
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// 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_MANAGER2_INCLUDED
#define AGG_FONT_CACHE_MANAGER2_INCLUDED
#include <cassert>
#include <exception>
#include <string.h>
#include "agg_array.h"
namespace agg {
namespace fman {
//---------------------------------------------------------glyph_data_type
enum glyph_data_type
{
glyph_data_invalid = 0,
glyph_data_mono = 1,
glyph_data_gray8 = 2,
glyph_data_outline = 3
};
//-------------------------------------------------------------cached_glyph
struct cached_glyph
{
void * cached_font;
unsigned glyph_code;
unsigned glyph_index;
int8u* data;
unsigned data_size;
glyph_data_type data_type;
rect_i bounds;
double advance_x;
double advance_y;
};
//--------------------------------------------------------------cached_glyphs
class cached_glyphs
{
public:
enum block_size_e { block_size = 16384-16 };
//--------------------------------------------------------------------
cached_glyphs()
: m_allocator(block_size)
{ memset(m_glyphs, 0, sizeof(m_glyphs)); }
//--------------------------------------------------------------------
const cached_glyph* 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;
}
//--------------------------------------------------------------------
cached_glyph* cache_glyph(
void * cached_font,
unsigned glyph_code,
unsigned glyph_index,
unsigned data_size,
glyph_data_type data_type,
const rect_i& bounds,
double advance_x,
double advance_y)
{
unsigned msb = (glyph_code >> 8) & 0xFF;
if(m_glyphs[msb] == 0)
{
m_glyphs[msb] =
(cached_glyph**)m_allocator.allocate(sizeof(cached_glyph*) * 256,
sizeof(cached_glyph*));
memset(m_glyphs[msb], 0, sizeof(cached_glyph*) * 256);
}
unsigned lsb = glyph_code & 0xFF;
if(m_glyphs[msb][lsb]) return 0; // Already exists, do not overwrite
cached_glyph* glyph =
(cached_glyph*)m_allocator.allocate(sizeof(cached_glyph),
sizeof(double));
glyph->cached_font = cached_font;
glyph->glyph_code = glyph_code;
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:
block_allocator m_allocator;
cached_glyph** m_glyphs[256];
};
//------------------------------------------------------------------------
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;
struct cached_font
{
cached_font(
font_engine_type& engine,
typename FontEngine::loaded_face *face,
double height,
double width,
bool hinting,
glyph_rendering rendering )
: m_engine( engine )
, m_face( face )
, m_height( height )
, m_width( width )
, m_hinting( hinting )
, m_rendering( rendering )
{
select_face();
m_face_height=m_face->height();
m_face_width=m_face->width();
m_face_ascent=m_face->ascent();
m_face_descent=m_face->descent();
m_face_ascent_b=m_face->ascent_b();
m_face_descent_b=m_face->descent_b();
}
double height() const
{
return m_face_height;
}
double width() const
{
return m_face_width;
}
double ascent() const
{
return m_face_ascent;
}
double descent() const
{
return m_face_descent;
}
double ascent_b() const
{
return m_face_ascent_b;
}
double descent_b() const
{
return m_face_descent_b;
}
bool add_kerning( const cached_glyph *first, const cached_glyph *second, double* x, double* y)
{
if( !first || !second )
return false;
select_face();
return m_face->add_kerning(
first->glyph_index, second->glyph_index, x, y );
}
void select_face()
{
m_face->select_instance( m_height, m_width, m_hinting, m_rendering );
}
const cached_glyph *get_glyph(unsigned cp)
{
const cached_glyph *glyph=m_glyphs.find_glyph(cp);
if( glyph==0 )
{
typename FontEngine::prepared_glyph prepared;
select_face();
bool success=m_face->prepare_glyph(cp, &prepared);
if( success )
{
glyph=m_glyphs.cache_glyph(
this,
prepared.glyph_code,
prepared.glyph_index,
prepared.data_size,
prepared.data_type,
prepared.bounds,
prepared.advance_x,
prepared.advance_y );
assert( glyph!=0 );
m_face->write_glyph_to(&prepared,glyph->data);
}
}
return glyph;
}
font_engine_type& m_engine;
typename FontEngine::loaded_face *m_face;
double m_height;
double m_width;
bool m_hinting;
glyph_rendering m_rendering;
double m_face_height;
double m_face_width;
double m_face_ascent;
double m_face_descent;
double m_face_ascent_b;
double m_face_descent_b;
cached_glyphs m_glyphs;
};
//--------------------------------------------------------------------
font_cache_manager(font_engine_type& engine, unsigned max_fonts=32)
:m_engine(engine)
{ }
//--------------------------------------------------------------------
void init_embedded_adaptors(const cached_glyph* 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; }
private:
//--------------------------------------------------------------------
font_cache_manager(const self_type&);
const self_type& operator = (const self_type&);
font_engine_type& m_engine;
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
|