Coverage Report

Created: 2026-03-15 06:39

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