aboutsummaryrefslogtreecommitdiff
path: root/graphics/crystalspace-devel/files/patch-plugins_font_server_freefnt2_freefnt2.cpp
blob: 486843d8769a94f53b241ffc5a41b1d7c26c3257 (plain) (blame)
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
--- plugins/font/server/freefnt2/freefnt2.cpp.orig	Thu Oct 12 12:48:44 2006
+++ plugins/font/server/freefnt2/freefnt2.cpp	Thu Oct 12 12:54:51 2006
@@ -30,10 +30,11 @@
 #include "iutil/comp.h"
 
 #include <ft2build.h>
-#include FT_INTERNAL_OBJECTS_H
 #include FT_FREETYPE_H
 #include FT_GLYPH_H
 #include FT_MODULE_H
+#include FT_SIZES_H
+#include FT_OUTLINE_H
 
 #include "freefnt2.h"
 
@@ -372,6 +373,20 @@
   return true;
 }
 
+static void GridFitCbox (FT_BBox& cbox, FT_UInt& glyphW, FT_UInt& glyphH)
+{
+  /* compute the control box, and grid fit it */
+#define FT_PIX_FLOOR( x )     ( (x) & ~63 )
+#define FT_PIX_CEIL( x )      FT_PIX_FLOOR( (x) + 63 )
+  cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
+  cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
+  cbox.xMax = FT_PIX_CEIL( cbox.xMax );
+  cbox.yMax = FT_PIX_CEIL( cbox.yMax );
+
+  glyphW = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 );
+  glyphH = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
+}
+
 csPtr<iDataBuffer> csFreeType2Font::GetGlyphBitmap (utf32_char c,
 						    csBitmapMetrics& metrics)
 {
@@ -381,37 +396,65 @@
   if ((c != CS_FONT_DEFAULT_GLYPH) && (ci == 0)) return 0;
 
   if (server->FreetypeError (FT_Load_Glyph (face->face, ci, 
-    FT_LOAD_RENDER | FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO),
+    FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO),
     "Could not load glyph %u for %s", ci, name))
   {
     return 0;
   }
 
-  int stride = (face->face->glyph->bitmap.width + 7) / 8;
+  /* Work around an FT2.2.1 issue where rendering of a glyph fails if the width
+   * or height is 0 */
+  FT_Outline* outline = &face->face->glyph->outline;
+  FT_BBox cbox;
+  FT_Outline_Get_CBox( outline, &cbox );
+  FT_UInt glyphW, glyphH;
+  GridFitCbox (cbox, glyphW, glyphH);
   int maxrows = (size->metrics.height + 63) >> 6;
-  int bitmapsize = maxrows*stride;
-  uint8* bitmap = new uint8 [bitmapsize];
-  memset (bitmap, 0, bitmapsize);
+  int stride, bitmapsize;
+  uint8* bitmap;
+  if ((glyphW > 0) && (glyphH > 0))
+  {
+    if (server->FreetypeError (FT_Render_Glyph (face->face->glyph, 
+      FT_RENDER_MODE_MONO),
+      "Could not render glyph %u for %s", ci, name))
+    {
+      return 0;
+    }
+    stride = (face->face->glyph->bitmap.width + 7) / 8;
+    bitmapsize = maxrows*stride;
+  }
+  else
+    bitmapsize = 0;
 
   int descend = (-size->metrics.descender + 63) >> 6;;
 
-  int startrow = maxrows - (descend + face->face->glyph->bitmap_top);
-
-  int endrow = startrow + face->face->glyph->bitmap.rows;
+  if (bitmapsize > 0)
+  {
+    int startrow = maxrows - (descend + face->face->glyph->bitmap_top);
+    int endrow = startrow + face->face->glyph->bitmap.rows;
 
-  if (startrow < 0) startrow = 0;
-  if (endrow > maxrows) endrow = maxrows;
+    if (startrow < 0) startrow = 0;
+    if (endrow > maxrows) endrow = maxrows;
 
-  int n, i;
-  for (n = 0, i = startrow; i < endrow; i++, n++)
-    memcpy (bitmap + stride*i, 
-            face->face->glyph->bitmap.buffer + 
-	    n * face->face->glyph->bitmap.pitch,
-            MIN(stride, face->face->glyph->bitmap.pitch));
+    bitmap = new uint8 [bitmapsize];
+    memset (bitmap, 0, bitmapsize);
+    int n, i;
+    for (n = 0, i = startrow; i < endrow; i++, n++)
+      memcpy (bitmap + stride*i, 
+              face->face->glyph->bitmap.buffer + 
+	      n * face->face->glyph->bitmap.pitch,
+              MIN(stride, face->face->glyph->bitmap.pitch));
+    metrics.width = face->face->glyph->bitmap.width;
+    metrics.left = face->face->glyph->bitmap_left;
+  }
+  else
+  {
+    bitmap = 0;
+    metrics.width = glyphW >> 6;
+    metrics.left = (FT_Int)( cbox.xMin >> 6 );
+  }
 
-  metrics.width = face->face->glyph->bitmap.width;
   metrics.height = maxrows;
-  metrics.left = face->face->glyph->bitmap_left;
   metrics.top = maxrows - descend;
 
   return (csPtr<iDataBuffer> (new csDataBuffer ((char*)bitmap, bitmapsize, 
@@ -427,42 +470,70 @@
   if ((c != CS_FONT_DEFAULT_GLYPH) && (ci == 0)) return 0;
 
   if (server->FreetypeError (FT_Load_Glyph (face->face, ci, 
-    FT_LOAD_RENDER | FT_RENDER_MODE_NORMAL),
+    FT_LOAD_TARGET_NORMAL),
     "Could not load glyph %u for %s", ci, name))
   {
     return 0;
   }
 
-  if (face->face->glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
-    // That's not what we want.
-    return 0;
-
-  int stride = face->face->glyph->bitmap.width;
+  /* Work around an FT2.2.1 issue where rendering of a glyph fails if the width
+   * or height is 0 */
+  FT_Outline* outline = &face->face->glyph->outline;
+  FT_BBox cbox;
+  FT_Outline_Get_CBox( outline, &cbox );
+  FT_UInt glyphW, glyphH;
+  GridFitCbox (cbox, glyphW, glyphH);
   int maxrows = (size->metrics.height + 63) >> 6;
-  int bitmapsize = maxrows * stride;
-  // Allocate at least 1 byte
-  uint8* bitmap = (bitmapsize > 0) ? new uint8 [bitmapsize] : new uint8[1];
-  memset (bitmap, 0, bitmapsize);
+  int stride, bitmapsize;
+  uint8* bitmap;
+  if ((glyphW > 0) && (glyphH > 0))
+  {
+    if (server->FreetypeError (FT_Render_Glyph (face->face->glyph, 
+      FT_RENDER_MODE_NORMAL),
+      "Could not render glyph %u for %s", ci, name))
+    {
+      return 0;
+    }
+    if (face->face->glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
+    {
+      // That's not what we want.
+      return 0;
+    }
+    stride = face->face->glyph->bitmap.width;
+    bitmapsize = maxrows*stride;
+    metrics.width = face->face->glyph->bitmap.width;
+    metrics.left = face->face->glyph->bitmap_left;
+  }
+  else
+  {
+    bitmapsize = 0;
+    metrics.width = glyphW >> 6;
+    metrics.left = (FT_Int)( cbox.xMin >> 6 );
+  }
 
   int descend = (-size->metrics.descender + 63) >> 6;
 
-  int startrow = maxrows - (descend + face->face->glyph->bitmap_top);
-
-  int endrow = startrow + face->face->glyph->bitmap.rows;
+  if (bitmapsize > 0)
+  {
+    int startrow = maxrows - (descend + face->face->glyph->bitmap_top);
+    int endrow = startrow + face->face->glyph->bitmap.rows;
 
-  if (startrow < 0) startrow = 0;
-  if (endrow > maxrows) endrow = maxrows;
+    if (startrow < 0) startrow = 0;
+    if (endrow > maxrows) endrow = maxrows;
 
-  int n, i;
-  for (n = 0, i = startrow; i < endrow; i++, n++)
-    memcpy (bitmap + stride*i, 
-            face->face->glyph->bitmap.buffer + 
-	    n * face->face->glyph->bitmap.pitch,
-            MIN(stride, face->face->glyph->bitmap.pitch));
+    bitmap = new uint8 [bitmapsize];
+    memset (bitmap, 0, bitmapsize);
+    int n, i;
+    for (n = 0, i = startrow; i < endrow; i++, n++)
+      memcpy (bitmap + stride*i, 
+              face->face->glyph->bitmap.buffer + 
+	      n * face->face->glyph->bitmap.pitch,
+              MIN(stride, face->face->glyph->bitmap.pitch));
+  }
+  else
+    bitmap = 0;
 
-  metrics.width = face->face->glyph->bitmap.width;
   metrics.height = maxrows;
-  metrics.left = face->face->glyph->bitmap_left;
   metrics.top = maxrows - descend;
 
   return (csPtr<iDataBuffer> (new csDataBuffer ((char*)bitmap, bitmapsize,