Coverage Report

Created: 2025-07-11 07:47

/src/xpdf-4.05/splash/SplashFont.cc
Line
Count
Source (jump to first uncovered line)
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
0
#define splashFontCacheAssoc   8
23
0
#define splashFontCacheMaxSets 8
24
0
#define splashFontCacheSize    (128*1024)
25
26
//------------------------------------------------------------------------
27
28
struct SplashFontCacheTag {
29
  int 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
0
           SplashCoord *textMatA, GBool aaA) {
41
0
  fontFile = fontFileA;
42
0
  fontFile->incRefCnt();
43
0
  mat[0] = matA[0];
44
0
  mat[1] = matA[1];
45
0
  mat[2] = matA[2];
46
0
  mat[3] = matA[3];
47
0
  textMat[0] = textMatA[0];
48
0
  textMat[1] = textMatA[1];
49
0
  textMat[2] = textMatA[2];
50
0
  textMat[3] = textMatA[3];
51
0
  aa = aaA;
52
53
0
  cache = NULL;
54
0
  cacheTags = NULL;
55
56
0
  xMin = yMin = xMax = yMax = 0;
57
0
}
58
59
0
void SplashFont::initCache() {
60
0
  int i;
61
62
  // this should be (max - min + 1), but we add some padding to
63
  // deal with rounding errors
64
0
  glyphW = xMax - xMin + 3;
65
0
  glyphH = yMax - yMin + 3;
66
0
  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
0
    glyphW = glyphH = 0;
71
0
    glyphSize = 0;
72
0
    cacheSets = 0;
73
0
    cacheAssoc = 0;
74
0
    return;
75
0
  }
76
0
  if (aa) {
77
0
    glyphSize = glyphW * glyphH;
78
0
  } else {
79
0
    glyphSize = ((glyphW + 7) >> 3) * glyphH;
80
0
  }
81
82
  // set up the glyph pixmap cache
83
0
  cacheAssoc = splashFontCacheAssoc;
84
0
  for (cacheSets = splashFontCacheMaxSets;
85
0
       cacheSets > 1 &&
86
0
   glyphSize > splashFontCacheSize / (cacheSets * cacheAssoc);
87
0
       cacheSets >>= 1) ;
88
0
  cache = (Guchar *)gmallocn(cacheSets * cacheAssoc, glyphSize);
89
0
  cacheTags = (SplashFontCacheTag *)gmallocn(cacheSets * cacheAssoc,
90
0
               sizeof(SplashFontCacheTag));
91
0
  for (i = 0; i < cacheSets * cacheAssoc; ++i) {
92
0
    cacheTags[i].mru = i & (cacheAssoc - 1);
93
0
  }
94
0
}
95
96
0
SplashFont::~SplashFont() {
97
0
  fontFile->decRefCnt();
98
0
  if (cache) {
99
0
    gfree(cache);
100
0
  }
101
0
  if (cacheTags) {
102
0
    gfree(cacheTags);
103
0
  }
104
0
}
105
106
GBool SplashFont::getGlyph(int c, int xFrac, int yFrac,
107
0
         SplashGlyphBitmap *bitmap) {
108
0
  SplashGlyphBitmap bitmap2;
109
0
  int size;
110
0
  Guchar *p;
111
0
  int i, j, k;
112
113
  // no fractional coordinates for large glyphs or non-anti-aliased
114
  // glyphs
115
0
  if (!aa || glyphH > 50) {
116
0
    xFrac = yFrac = 0;
117
0
  }
118
119
  // check the cache
120
0
  if (cache) {
121
0
    i = (c & (cacheSets - 1)) * cacheAssoc;
122
0
    for (j = 0; j < cacheAssoc; ++j) {
123
0
      if ((cacheTags[i+j].mru & 0x80000000) &&
124
0
    cacheTags[i+j].c == c &&
125
0
    (int)cacheTags[i+j].xFrac == xFrac &&
126
0
    (int)cacheTags[i+j].yFrac == yFrac) {
127
0
  bitmap->x = cacheTags[i+j].x;
128
0
  bitmap->y = cacheTags[i+j].y;
129
0
  bitmap->w = cacheTags[i+j].w;
130
0
  bitmap->h = cacheTags[i+j].h;
131
0
  for (k = 0; k < cacheAssoc; ++k) {
132
0
    if (k != j &&
133
0
        (cacheTags[i+k].mru & 0x7fffffff) <
134
0
        (cacheTags[i+j].mru & 0x7fffffff)) {
135
0
      ++cacheTags[i+k].mru;
136
0
    }
137
0
  }
138
0
  cacheTags[i+j].mru = 0x80000000;
139
0
  bitmap->aa = aa;
140
0
  bitmap->data = cache + (i+j) * glyphSize;
141
0
  bitmap->freeData = gFalse;
142
0
  return gTrue;
143
0
      }
144
0
    }
145
0
  } else {
146
0
    i = 0; // make gcc happy
147
0
  }
148
149
  // generate the glyph bitmap
150
0
  if (!makeGlyph(c, xFrac, yFrac, &bitmap2)) {
151
0
    return gFalse;
152
0
  }
153
154
  // if the glyph doesn't fit in the bounding box, return a temporary
155
  // uncached bitmap
156
0
  if (!cache || bitmap2.w > glyphW || bitmap2.h > glyphH) {
157
0
    *bitmap = bitmap2;
158
0
    return gTrue;
159
0
  }
160
161
  // insert glyph pixmap in cache
162
0
  if (aa) {
163
0
    size = bitmap2.w * bitmap2.h;
164
0
  } else {
165
0
    size = ((bitmap2.w + 7) >> 3) * bitmap2.h;
166
0
  }
167
0
  p = NULL; // make gcc happy
168
0
  for (j = 0; j < cacheAssoc; ++j) {
169
0
    if ((cacheTags[i+j].mru & 0x7fffffff) == cacheAssoc - 1) {
170
0
      cacheTags[i+j].mru = 0x80000000;
171
0
      cacheTags[i+j].c = c;
172
0
      cacheTags[i+j].xFrac = (short)xFrac;
173
0
      cacheTags[i+j].yFrac = (short)yFrac;
174
0
      cacheTags[i+j].x = bitmap2.x;
175
0
      cacheTags[i+j].y = bitmap2.y;
176
0
      cacheTags[i+j].w = bitmap2.w;
177
0
      cacheTags[i+j].h = bitmap2.h;
178
0
      p = cache + (i+j) * glyphSize;
179
0
      memcpy(p, bitmap2.data, size);
180
0
    } else {
181
0
      ++cacheTags[i+j].mru;
182
0
    }
183
0
  }
184
0
  *bitmap = bitmap2;
185
0
  bitmap->data = p;
186
0
  bitmap->freeData = gFalse;
187
0
  if (bitmap2.freeData) {
188
0
    gfree(bitmap2.data);
189
0
  }
190
0
  return gTrue;
191
0
}