Coverage Report

Created: 2026-06-10 06:37

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