* include/freetype/internal/ftobjs.h, src/base/ftutil.c (ft_highpow2), src/pfr/pfrload.c, src/pfr/pfrobjs.c, src/pfr/pfrtypes.h: implement FT_OPTIMIZE_MEMORY, the kerning table is not loaded into the heap anymore.
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
diff --git a/include/freetype/internal/ftobjs.h b/include/freetype/internal/ftobjs.h
index 805c3ae..1a6130c 100644
--- a/include/freetype/internal/ftobjs.h
+++ b/include/freetype/internal/ftobjs.h
@@ -80,6 +80,12 @@ FT_BEGIN_HEADER
#define FT_PIX_CEIL( x ) FT_PIX_FLOOR( (x) + 63 )
+ /* returns the highest power of 2 that is <= value, this correspond to
+ * the highest bit in a given 32-bit value
+ */
+ FT_BASE( FT_UInt32 )
+ ft_highpow2( FT_UInt32 value );
+
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
diff --git a/src/base/ftutil.c b/src/base/ftutil.c
index ac3760f..317cd95 100644
--- a/src/base/ftutil.c
+++ b/src/base/ftutil.c
@@ -398,4 +398,24 @@
}
+ FT_BASE( FT_UInt32 )
+ ft_highpow2( FT_UInt32 value )
+ {
+ FT_UInt32 value2;
+
+ /* we simply clear the lowest bit in each iteration. when
+ * we reach 0, we now that the previous value was our result
+ */
+ for ( ;; )
+ {
+ value2 = value & (value-1); /* clear lowest bit */
+ if ( value2 == 0 )
+ break;
+
+ value = value2;
+ }
+ return value;
+ }
+
+
/* END */
diff --git a/src/pfr/pfrload.c b/src/pfr/pfrload.c
index 1435336..38a10c1 100644
--- a/src/pfr/pfrload.c
+++ b/src/pfr/pfrload.c
@@ -609,6 +609,7 @@
}
+#ifndef FT_OPTIMIZE_MEMORY
/*
* The kerning data embedded in a PFR font are (charcode,charcode)
* pairs; we need to translate them to (gindex,gindex) and sort
@@ -747,7 +748,7 @@
return error;
}
-
+#endif /* !FT_OPTIMIZE_MEMORY */
static const PFR_ExtraItemRec pfr_phy_font_extra_items[] =
{
@@ -826,7 +827,10 @@
FT_FREE( phy_font->blue_values );
phy_font->num_blue_values = 0;
+#ifndef FT_OPTIMIZE_MEMORY
FT_FREE( phy_font->kern_pairs );
+#endif
+
{
PFR_KernItem item, next;
@@ -1065,8 +1069,10 @@
phy_font->bct_offset = FT_STREAM_POS();
phy_font->cursor = NULL;
+#ifndef FT_OPTIMIZE_MEMORY
/* now sort kerning pairs */
error = pfr_sort_kerning_pairs( stream, phy_font );
+#endif
Exit:
return error;
diff --git a/src/pfr/pfrobjs.c b/src/pfr/pfrobjs.c
index 8b318e9..68e1bd9 100644
--- a/src/pfr/pfrobjs.c
+++ b/src/pfr/pfrobjs.c
@@ -48,7 +48,7 @@
/* we don't want dangling pointers */
pfrface->family_name = NULL;
pfrface->style_name = NULL;
-
+
/* finalize the physical font record */
pfr_phy_font_done( &face->phy_font, FT_FACE_MEMORY( face ) );
@@ -174,11 +174,11 @@
FT_Bitmap_Size* size;
PFR_Strike strike;
FT_Memory memory = pfrface->stream->memory;
-
-
+
+
if ( FT_NEW_ARRAY( pfrface->available_sizes, count ) )
goto Exit;
-
+
size = pfrface->available_sizes;
strike = phy_font->strikes;
for ( n = 0; n < count; n++, size++, strike++ )
@@ -361,6 +361,8 @@
metrics->vertBearingX = 0;
metrics->vertBearingY = 0;
+#if 0 /* some fonts seem to be broken here !! */
+
/* Apply the font matrix, if any. */
/* TODO: Test existing fonts with unusual matrix */
/* whether we have to adjust Units per EM. */
@@ -375,6 +377,7 @@
FT_Outline_Transform( outline, &font_matrix );
}
+#endif
/* scale when needed */
if ( scaling )
@@ -419,6 +422,123 @@
/*************************************************************************/
/*************************************************************************/
+#ifdef FT_OPTIMIZE_MEMORY
+ FT_LOCAL_DEF( FT_Error )
+ pfr_face_get_kerning( FT_Face pfrface, /* PFR_Face */
+ FT_UInt glyph1,
+ FT_UInt glyph2,
+ FT_Vector* kerning )
+ {
+ PFR_Face face = (PFR_Face)pfrface;
+ FT_Error error = PFR_Err_Ok;
+ PFR_PhyFont phy_font = &face->phy_font;
+ FT_UInt32 code1, code2, pair;
+
+ kerning->x = 0;
+ kerning->y = 0;
+
+ if ( glyph1 > 0 )
+ glyph1--;
+
+ if ( glyph2 > 0 )
+ glyph2--;
+
+ /* convert glyph indices to character codes */
+ if ( glyph1 > phy_font->num_chars ||
+ glyph2 > phy_font->num_chars )
+ goto Exit;
+
+ code1 = phy_font->chars[glyph1].char_code;
+ code2 = phy_font->chars[glyph2].char_code;
+ pair = PFR_KERN_INDEX(code1,code2);
+
+ /* now search the list of kerning items */
+ {
+ PFR_KernItem item = phy_font->kern_items;
+ FT_Stream stream = pfrface->stream;
+
+ for ( ; item; item = item->next )
+ {
+ if ( pair >= item->pair1 && pair <= item->pair2 )
+ goto FoundPair;
+ }
+ goto Exit;
+
+ FoundPair: /* we found an item, now parse it and find the value if any */
+ if ( FT_STREAM_SEEK( item->offset ) ||
+ FT_FRAME_ENTER( item->pair_count*item->pair_size ) )
+ goto Exit;
+
+ {
+ FT_UInt count = item->pair_count;
+ FT_UInt size = item->pair_size;
+ FT_UInt power = (FT_UInt)ft_highpow2( (FT_UInt32)count );
+ FT_UInt probe = power*size;
+ FT_UInt extra = count - power;
+ FT_Byte* base = stream->cursor;
+ FT_Bool twobytes = item->flags & 1;
+ FT_Byte* p;
+ FT_UInt32 cpair;
+
+ if ( extra > 0 )
+ {
+ p = base + extra*size;
+ if ( twobytes )
+ cpair = FT_NEXT_ULONG(p);
+ else
+ cpair = PFR_NEXT_KPAIR(p);
+
+ if ( cpair == pair )
+ goto Found;
+
+ if ( cpair < pair )
+ base = p;
+ }
+
+ while ( probe > size )
+ {
+ probe >>= 1;
+ p = base + probe;
+ if ( twobytes )
+ cpair = FT_NEXT_ULONG(p);
+ else
+ cpair = PFR_NEXT_KPAIR(p);
+
+ if ( cpair == pair )
+ goto Found;
+
+ if ( cpair < pair )
+ base += probe;
+ }
+
+ p = base;
+ if ( twobytes )
+ cpair = FT_NEXT_ULONG(p);
+ else
+ cpair = PFR_NEXT_KPAIR(p);
+
+ if ( cpair == pair )
+ {
+ FT_Int value;
+
+ Found:
+ if ( item->flags & 2 )
+ value = FT_PEEK_SHORT(p);
+ else
+ value = p[0];
+
+ kerning->x = item->base_adj + value;
+ }
+ }
+
+ FT_FRAME_EXIT();
+ }
+
+ Exit:
+ return error;
+ }
+
+#else /* !FT_OPTIMIZE_MEMORY */
FT_LOCAL_DEF( FT_Error )
pfr_face_get_kerning( FT_Face pfrface, /* PFR_Face */
FT_UInt glyph1,
@@ -438,20 +558,20 @@
min = 0;
max = phy_font->num_kern_pairs;
-
+
while ( min < max )
{
FT_UInt mid = ( min + max ) >> 1;
PFR_KernPair pair = pairs + mid;
FT_UInt32 pidx = PFR_KERN_PAIR_INDEX( pair );
-
+
if ( pidx == idx )
{
kerning->x = pair->kerning;
break;
}
-
+
if ( pidx < idx )
min = mid + 1;
else
@@ -460,6 +580,6 @@
return error;
}
-
+#endif /* !FT_OPTIMIZE_MEMORY */
/* END */
diff --git a/src/pfr/pfrtypes.h b/src/pfr/pfrtypes.h
index a10a2b2..db593f0 100644
--- a/src/pfr/pfrtypes.h
+++ b/src/pfr/pfrtypes.h
@@ -196,10 +196,10 @@ FT_BEGIN_HEADER
typedef struct PFR_KernItemRec_
{
PFR_KernItem next;
- FT_UInt pair_count;
+ FT_Byte pair_count;
+ FT_Byte flags;
+ FT_Short base_adj;
FT_UInt pair_size;
- FT_Int base_adj;
- FT_UInt flags;
FT_UInt32 offset;
FT_UInt32 pair1;
FT_UInt32 pair2;
@@ -212,6 +212,8 @@ FT_BEGIN_HEADER
#define PFR_KERN_PAIR_INDEX( pair ) \
PFR_KERN_INDEX( (pair)->glyph1, (pair)->glyph2 )
+#define PFR_NEXT_KPAIR(p) ( p+=2, ((FT_UInt32)p[-2] << 16) | p[-1] )
+
typedef struct PFR_KernPairRec_
{
FT_UInt glyph1;
@@ -261,7 +263,9 @@ FT_BEGIN_HEADER
FT_UInt num_kern_pairs;
PFR_KernItem kern_items;
PFR_KernItem* kern_items_tail;
+#ifndef FT_OPTIMIZE_MEMORY
PFR_KernPair kern_pairs;
+#endif
/* not part of the spec, but used during load */
FT_UInt32 bct_offset;