source: git/src/glbitmapfont.cc @ c142664

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since c142664 was 17c483d, checked in by Olly Betts <olly@…>, 11 years ago

src/gla-gl.cc,src/glbitmapfont.cc: Add checks for errors when reading
the font file.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1//
2//  glbitmapfont.cc
3//
4//  Draw text using glBitmap.
5//
6//  Copyright (C) 2011,2012,2013 Olly Betts
7//
8//  This program is free software; you can redistribute it and/or modify
9//  it under the terms of the GNU General Public License as published by
10//  the Free Software Foundation; either version 2 of the License, or
11//  (at your option) any later version.
12//
13//  This program is distributed in the hope that it will be useful,
14//  but WITHOUT ANY WARRANTY; without even the implied warranty of
15//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//  GNU General Public License for more details.
17//
18//  You should have received a copy of the GNU General Public License
19//  along with this program; if not, write to the Free Software
20//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21//
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include "glbitmapfont.h"
28
29#include "aventypes.h"
30#include "useful.h"
31#include "wx.h"
32
33#define CHECK_GL_ERROR(M, F) do { \
34    GLenum error_code_ = glGetError(); \
35    if (error_code_ != GL_NO_ERROR) { \
36        wxLogError(wxT(__FILE__":"STRING(__LINE__)": OpenGL error: %s " \
37                   "(call "F" in method "M")"), \
38                   wxString((const char *)gluErrorString(error_code_), \
39                            wxConvUTF8).c_str()); \
40    } \
41} while (0)
42
43bool
44BitmapFont::load(const wxString & font_file)
45{
46    if (fh) fclose(fh);
47#ifdef __WXMSW__
48    fh = _wfopen(font_file.fn_str(), L"rb");
49#else
50    fh = fopen(font_file.mb_str(), "rb");
51#endif
52
53    if (!fh) {
54        return false;
55    }
56
57    if (!gllist_base) {
58        gllist_base = glGenLists(BITMAPFONT_MAX_CHAR);
59    }
60
61    unsigned char buf[32];
62
63    for (int ch = 0; ch < BITMAPFONT_MAX_CHAR; ++ch) {
64        glNewList(gllist_base + ch, GL_COMPILE);
65        CHECK_GL_ERROR("BitmapFont::load", "glNewList");
66        int b = GETC(fh);
67        if (b == EOF) {
68            fclose(fh);
69            return false;
70        }
71        unsigned int byte_width = b;
72
73        char_width[ch] = (byte_width & 0x0f) + 2;
74        byte_width >>= 6;
75
76        int start = 0;
77        int n = 0;
78        if (byte_width) {
79            b = GETC(fh);
80            if (b == EOF) {
81                fclose(fh);
82                return false;
83            }
84            unsigned int start_and_n = b;
85            start = start_and_n >> 4;
86            n = (start_and_n & 15) + 1;
87            if (fread(buf, n * byte_width, 1, fh) != 1) {
88                fclose(fh);
89                return false;
90            }
91        }
92
93        // Even if there's nothing to display, we want to advance the
94        // raster position.
95        glBitmap(8 * byte_width, n, 0, -start, char_width[ch], 0, buf);
96        CHECK_GL_ERROR("BitmapFont::load", "glBitmap");
97        glEndList();
98        CHECK_GL_ERROR("BitmapFont::load", "glEndList");
99    }
100
101    return true;
102}
103
104inline void call_lists(GLsizei n, const GLvoid * lists)
105{
106    // Each test is a compile-time constant, so this should get collapsed by
107    // the compiler.
108    if (sizeof(wxChar) == 1) {
109        glCallLists(n, GL_UNSIGNED_BYTE, lists);
110    } else if (sizeof(wxChar) == 2) {
111        glCallLists(n, GL_UNSIGNED_SHORT, lists);
112    } else if (sizeof(wxChar) == 4) {
113        glCallLists(n, GL_UNSIGNED_INT, lists);
114    }
115}
116
117void
118BitmapFont::write_glyph(wxChar ch) const
119{
120    if (ch >= 0x10000) return;
121    if (!extra_chars) {
122        long here = ftell(fh);
123        if (here == -1 || fseek(fh, 0, SEEK_END) < 0)
124            return;
125        long data_len = ftell(fh);
126        if (data_len == -1)
127            return;
128        data_len -= here;
129        unsigned char * data = new unsigned char [data_len];
130        if (fseek(fh, here, SEEK_SET) < 0 ||
131            fread(data, data_len, 1, fh) != 1) {
132            delete data;
133            return;
134        }
135        extra_chars = new unsigned char * [0x10000 - BITMAPFONT_MAX_CHAR];
136
137        for (int i = 0; i < 0x10000 - BITMAPFONT_MAX_CHAR; ++i) {
138            if (data_len <= 0) {
139                extra_chars[i] = NULL;
140                continue;
141            }
142            extra_chars[i] = data;
143            unsigned int byte_width = *data++;
144            byte_width >>= 6;
145
146            if (byte_width) {
147                unsigned int start_and_n = *data;
148                int n = (start_and_n & 15) + 1;
149                data += n * byte_width + 1;
150                data_len -= n * byte_width + 1;
151            }
152        }
153        fclose(fh);
154        fh = NULL;
155    }
156
157    unsigned int byte_width = 0;
158    int start = 0;
159    int n = 0;
160    int width = 8;
161
162    const unsigned char * p = extra_chars[ch - BITMAPFONT_MAX_CHAR];
163    if (p) {
164        byte_width = *p++;
165        width = (byte_width & 0x0f) + 2;
166        byte_width >>= 6;
167
168        if (byte_width) {
169            unsigned int start_and_n = *p++;
170            start = start_and_n >> 4;
171            n = (start_and_n & 15) + 1;
172        }
173    }
174
175    // Even if there's nothing to display, we want to advance the
176    // raster position.
177    glBitmap(8 * byte_width, n, 0, -start, width, 0, p);
178}
179
180void
181BitmapFont::write_string(const wxChar *s, size_t len) const
182{
183    if (!gllist_base) return;
184    glListBase(gllist_base);
185    // Each test is a compile-time constant, so this should get collapsed by
186    // the compiler.
187    if (sizeof(wxChar) == 1) {
188        call_lists(len, s);
189    } else if (sizeof(wxChar) == 2 || sizeof(wxChar) == 4) {
190        while (len) {
191            size_t n;
192            for (n = 0; n < len; ++n)
193                if (int(s[n]) >= BITMAPFONT_MAX_CHAR)
194                    break;
195            call_lists(n, s);
196            s += n;
197            len -= n;
198            while (len && int(s[n]) >= BITMAPFONT_MAX_CHAR) {
199                write_glyph(s[n]);
200                ++s;
201                --len;
202            }
203        }
204    }
205}
Note: See TracBrowser for help on using the repository browser.