| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// SplashFont.cc |
| 4 |
// |
| 5 |
// Copyright 2003-2013 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#include <aconf.h> |
| 10 |
|
| 11 |
#include <string.h> |
| 12 |
#include "gmem.h" |
| 13 |
#include "gmempp.h" |
| 14 |
#include "SplashMath.h" |
| 15 |
#include "SplashGlyphBitmap.h" |
| 16 |
#include "SplashFontFile.h" |
| 17 |
#include "SplashFont.h" |
| 18 |
|
| 19 |
//------------------------------------------------------------------------ |
| 20 |
|
| 21 |
// font cache size parameters |
| 22 |
#define splashFontCacheAssoc 8 |
| 23 |
#define splashFontCacheMaxSets 8 |
| 24 |
#define splashFontCacheSize (128*1024) |
| 25 |
|
| 26 |
//------------------------------------------------------------------------ |
| 27 |
|
| 28 |
struct SplashFontCacheTag { |
| 29 |
Guint c; |
| 30 |
short xFrac, yFrac; // x and y fractions |
| 31 |
int mru; // valid bit (0x80000000) and MRU index |
| 32 |
int x, y, w, h; // offset and size of glyph |
| 33 |
}; |
| 34 |
|
| 35 |
//------------------------------------------------------------------------ |
| 36 |
// SplashFont |
| 37 |
//------------------------------------------------------------------------ |
| 38 |
|
| 39 |
SplashFont::SplashFont(SplashFontFile *fontFileA, SplashCoord *matA, |
| 40 |
SplashCoord *textMatA, GBool aaA) { |
| 41 |
fontFile = fontFileA; |
| 42 |
fontFile->incRefCnt(); |
| 43 |
mat[0] = matA[0]; |
| 44 |
mat[1] = matA[1]; |
| 45 |
mat[2] = matA[2]; |
| 46 |
mat[3] = matA[3]; |
| 47 |
textMat[0] = textMatA[0]; |
| 48 |
textMat[1] = textMatA[1]; |
| 49 |
textMat[2] = textMatA[2]; |
| 50 |
textMat[3] = textMatA[3]; |
| 51 |
aa = aaA; |
| 52 |
|
| 53 |
cache = NULL; |
| 54 |
cacheTags = NULL; |
| 55 |
|
| 56 |
xMin = yMin = xMax = yMax = 0; |
| 57 |
} |
| 58 |
|
| 59 |
void SplashFont::initCache() { |
| 60 |
int i; |
| 61 |
|
| 62 |
// this should be (max - min + 1), but we add some padding to |
| 63 |
// deal with rounding errors |
| 64 |
glyphW = xMax - xMin + 3; |
| 65 |
glyphH = yMax - yMin + 3; |
| 66 |
if (glyphW > 1000 || glyphH > 1000) { |
| 67 |
// if the glyphs are too large, don't cache them -- setting the |
| 68 |
// cache bitmap size to something tiny will cause getGlyph() to |
| 69 |
// fall back to the uncached case |
| 70 |
glyphW = glyphH = 0; |
| 71 |
glyphSize = 0; |
| 72 |
cacheSets = 0; |
| 73 |
cacheAssoc = 0; |
| 74 |
return; |
| 75 |
} |
| 76 |
if (aa) { |
| 77 |
glyphSize = glyphW * glyphH; |
| 78 |
} else { |
| 79 |
glyphSize = ((glyphW + 7) >> 3) * glyphH; |
| 80 |
} |
| 81 |
|
| 82 |
// set up the glyph pixmap cache |
| 83 |
cacheAssoc = splashFontCacheAssoc; |
| 84 |
for (cacheSets = splashFontCacheMaxSets; |
| 85 |
cacheSets > 1 && |
| 86 |
glyphSize > splashFontCacheSize / (cacheSets * cacheAssoc); |
| 87 |
cacheSets >>= 1) ; |
| 88 |
cache = (Guchar *)gmallocn(cacheSets * cacheAssoc, glyphSize); |
| 89 |
cacheTags = (SplashFontCacheTag *)gmallocn(cacheSets * cacheAssoc, |
| 90 |
sizeof(SplashFontCacheTag)); |
| 91 |
for (i = 0; i < cacheSets * cacheAssoc; ++i) { |
| 92 |
cacheTags[i].mru = i & (cacheAssoc - 1); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
SplashFont::~SplashFont() { |
| 97 |
fontFile->decRefCnt(); |
| 98 |
if (cache) { |
| 99 |
gfree(cache); |
| 100 |
} |
| 101 |
if (cacheTags) { |
| 102 |
gfree(cacheTags); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
GBool SplashFont::getGlyph(Guint c, int xFrac, int yFrac, |
| 107 |
SplashGlyphBitmap *bitmap) { |
| 108 |
SplashGlyphBitmap bitmap2; |
| 109 |
int size; |
| 110 |
Guchar *p; |
| 111 |
int i, j, k; |
| 112 |
|
| 113 |
// no fractional coordinates for large glyphs or non-anti-aliased |
| 114 |
// glyphs |
| 115 |
if (!aa || glyphH > 50) { |
| 116 |
xFrac = yFrac = 0; |
| 117 |
} |
| 118 |
|
| 119 |
// check the cache |
| 120 |
if (cache) { |
| 121 |
i = (c & (cacheSets - 1)) * cacheAssoc; |
| 122 |
for (j = 0; j < cacheAssoc; ++j) { |
| 123 |
if ((cacheTags[i+j].mru & 0x80000000) && |
| 124 |
cacheTags[i+j].c == c && |
| 125 |
(int)cacheTags[i+j].xFrac == xFrac && |
| 126 |
(int)cacheTags[i+j].yFrac == yFrac) { |
| 127 |
bitmap->x = cacheTags[i+j].x; |
| 128 |
bitmap->y = cacheTags[i+j].y; |
| 129 |
bitmap->w = cacheTags[i+j].w; |
| 130 |
bitmap->h = cacheTags[i+j].h; |
| 131 |
for (k = 0; k < cacheAssoc; ++k) { |
| 132 |
if (k != j && |
| 133 |
(cacheTags[i+k].mru & 0x7fffffff) < |
| 134 |
(cacheTags[i+j].mru & 0x7fffffff)) { |
| 135 |
++cacheTags[i+k].mru; |
| 136 |
} |
| 137 |
} |
| 138 |
cacheTags[i+j].mru = 0x80000000; |
| 139 |
bitmap->aa = aa; |
| 140 |
bitmap->data = cache + (i+j) * glyphSize; |
| 141 |
bitmap->freeData = gFalse; |
| 142 |
return gTrue; |
| 143 |
} |
| 144 |
} |
| 145 |
} else { |
| 146 |
i = 0; // make gcc happy |
| 147 |
} |
| 148 |
|
| 149 |
// generate the glyph bitmap |
| 150 |
if (!makeGlyph(c, xFrac, yFrac, &bitmap2)) { |
| 151 |
return gFalse; |
| 152 |
} |
| 153 |
|
| 154 |
// if the glyph doesn't fit in the bounding box, return a temporary |
| 155 |
// uncached bitmap |
| 156 |
if (!cache || bitmap2.w > glyphW || bitmap2.h > glyphH) { |
| 157 |
*bitmap = bitmap2; |
| 158 |
return gTrue; |
| 159 |
} |
| 160 |
|
| 161 |
// insert glyph pixmap in cache |
| 162 |
if (aa) { |
| 163 |
size = bitmap2.w * bitmap2.h; |
| 164 |
} else { |
| 165 |
size = ((bitmap2.w + 7) >> 3) * bitmap2.h; |
| 166 |
} |
| 167 |
p = NULL; // make gcc happy |
| 168 |
for (j = 0; j < cacheAssoc; ++j) { |
| 169 |
if ((cacheTags[i+j].mru & 0x7fffffff) == cacheAssoc - 1) { |
| 170 |
cacheTags[i+j].mru = 0x80000000; |
| 171 |
cacheTags[i+j].c = c; |
| 172 |
cacheTags[i+j].xFrac = (short)xFrac; |
| 173 |
cacheTags[i+j].yFrac = (short)yFrac; |
| 174 |
cacheTags[i+j].x = bitmap2.x; |
| 175 |
cacheTags[i+j].y = bitmap2.y; |
| 176 |
cacheTags[i+j].w = bitmap2.w; |
| 177 |
cacheTags[i+j].h = bitmap2.h; |
| 178 |
p = cache + (i+j) * glyphSize; |
| 179 |
memcpy(p, bitmap2.data, size); |
| 180 |
} else { |
| 181 |
++cacheTags[i+j].mru; |
| 182 |
} |
| 183 |
} |
| 184 |
*bitmap = bitmap2; |
| 185 |
bitmap->data = p; |
| 186 |
bitmap->freeData = gFalse; |
| 187 |
if (bitmap2.freeData) { |
| 188 |
gfree(bitmap2.data); |
| 189 |
} |
| 190 |
return gTrue; |
| 191 |
} |