[a1614eb] | 1 | // |
---|
| 2 | // glbitmapfont.cc |
---|
| 3 | // |
---|
| 4 | // Draw text using glBitmap. |
---|
| 5 | // |
---|
[c7627d4] | 6 | // Copyright (C) 2011,2012,2013,2014 Olly Betts |
---|
[a1614eb] | 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 | |
---|
[614d60b] | 33 | #include "../lib/preload_font.h" |
---|
| 34 | |
---|
[a1614eb] | 35 | #define CHECK_GL_ERROR(M, F) do { \ |
---|
| 36 | GLenum error_code_ = glGetError(); \ |
---|
[1c8ab60] | 37 | if (error_code_ != GL_NO_ERROR) { \ |
---|
[a1614eb] | 38 | wxLogError(wxT(__FILE__":"STRING(__LINE__)": OpenGL error: %s " \ |
---|
| 39 | "(call "F" in method "M")"), \ |
---|
| 40 | wxString((const char *)gluErrorString(error_code_), \ |
---|
| 41 | wxConvUTF8).c_str()); \ |
---|
[1c8ab60] | 42 | } \ |
---|
[a1614eb] | 43 | } while (0) |
---|
| 44 | |
---|
| 45 | bool |
---|
[614d60b] | 46 | BitmapFont::load(const wxString & font_file_) |
---|
[a1614eb] | 47 | { |
---|
[614d60b] | 48 | font_file = font_file_; |
---|
[a1614eb] | 49 | |
---|
| 50 | if (!gllist_base) { |
---|
| 51 | gllist_base = glGenLists(BITMAPFONT_MAX_CHAR); |
---|
| 52 | } |
---|
| 53 | |
---|
[614d60b] | 54 | const unsigned char * p = fontdata_preloaded; |
---|
| 55 | const unsigned char * end = p + sizeof(fontdata_preloaded); |
---|
[a1614eb] | 56 | for (int ch = 0; ch < BITMAPFONT_MAX_CHAR; ++ch) { |
---|
| 57 | glNewList(gllist_base + ch, GL_COMPILE); |
---|
| 58 | CHECK_GL_ERROR("BitmapFont::load", "glNewList"); |
---|
[614d60b] | 59 | if (p == end) { |
---|
[17c483d] | 60 | return false; |
---|
| 61 | } |
---|
[614d60b] | 62 | unsigned int byte_width = *p++; |
---|
[a1614eb] | 63 | |
---|
[d4d6909] | 64 | char_width[ch] = (byte_width & 0x0f) + 2; |
---|
| 65 | byte_width >>= 6; |
---|
[a1614eb] | 66 | |
---|
| 67 | int start = 0; |
---|
| 68 | int n = 0; |
---|
| 69 | if (byte_width) { |
---|
[614d60b] | 70 | if (p == end) { |
---|
[17c483d] | 71 | return false; |
---|
| 72 | } |
---|
[614d60b] | 73 | unsigned int start_and_n = *p++; |
---|
[a1614eb] | 74 | start = start_and_n >> 4; |
---|
| 75 | n = (start_and_n & 15) + 1; |
---|
[614d60b] | 76 | |
---|
| 77 | if (end - p < n * byte_width) { |
---|
[17c483d] | 78 | return false; |
---|
| 79 | } |
---|
[a1614eb] | 80 | } |
---|
| 81 | |
---|
| 82 | // Even if there's nothing to display, we want to advance the |
---|
| 83 | // raster position. |
---|
[614d60b] | 84 | glBitmap(8 * byte_width, n, 0, -start, char_width[ch], 0, p); |
---|
[a1614eb] | 85 | CHECK_GL_ERROR("BitmapFont::load", "glBitmap"); |
---|
| 86 | glEndList(); |
---|
| 87 | CHECK_GL_ERROR("BitmapFont::load", "glEndList"); |
---|
[614d60b] | 88 | |
---|
| 89 | p += n * byte_width; |
---|
[a1614eb] | 90 | } |
---|
| 91 | |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | inline void call_lists(GLsizei n, const GLvoid * lists) |
---|
| 96 | { |
---|
[30b66b5] | 97 | #if SIZEOF_WXCHAR == 1 |
---|
| 98 | glCallLists(n, GL_UNSIGNED_BYTE, lists); |
---|
| 99 | #elif SIZEOF_WXCHAR == 2 |
---|
| 100 | glCallLists(n, GL_UNSIGNED_SHORT, lists); |
---|
| 101 | #elif SIZEOF_WXCHAR == 4 |
---|
| 102 | glCallLists(n, GL_UNSIGNED_INT, lists); |
---|
| 103 | #else |
---|
| 104 | # error "sizeof(wxChar) not 1, 2 or 4" |
---|
| 105 | #endif |
---|
[a1614eb] | 106 | } |
---|
| 107 | |
---|
[36b4cd7] | 108 | void |
---|
[cba8bf34] | 109 | BitmapFont::init_extra_chars() const |
---|
[36b4cd7] | 110 | { |
---|
[614d60b] | 111 | #ifdef __WXMSW__ |
---|
| 112 | FILE * fh = _wfopen(font_file.fn_str(), L"rb"); |
---|
| 113 | #else |
---|
| 114 | FILE * fh = fopen(font_file.mb_str(), "rb"); |
---|
| 115 | #endif |
---|
| 116 | |
---|
| 117 | long data_len = 0; |
---|
| 118 | if (fh && fseek(fh, 0, SEEK_END) >= 0) { |
---|
| 119 | data_len = ftell(fh); |
---|
| 120 | if (data_len == -1) |
---|
| 121 | data_len = 0; |
---|
| 122 | } |
---|
[cba8bf34] | 123 | unsigned char * data = new unsigned char [data_len]; |
---|
[614d60b] | 124 | if (data_len && (fseek(fh, 0, SEEK_SET) < 0 || |
---|
| 125 | fread(data, data_len, 1, fh) != 1)) { |
---|
| 126 | data_len = 0; |
---|
| 127 | // FIXME: do something better. wxGetApp().ReportError(m); |
---|
| 128 | // We have this message available: Error in format of font file “%s” |
---|
| 129 | // fprintf(stderr, "Couldn't load extended font.\n"); |
---|
[cba8bf34] | 130 | } |
---|
[614d60b] | 131 | if (fh) |
---|
| 132 | fclose(fh); |
---|
[cba8bf34] | 133 | |
---|
[614d60b] | 134 | extra_chars = new unsigned char * [0x10000 - BITMAPFONT_MAX_CHAR]; |
---|
[cba8bf34] | 135 | for (int i = 0; i < 0x10000 - BITMAPFONT_MAX_CHAR; ++i) { |
---|
| 136 | if (data_len <= 0) { |
---|
| 137 | extra_chars[i] = NULL; |
---|
| 138 | continue; |
---|
[36b4cd7] | 139 | } |
---|
[cba8bf34] | 140 | extra_chars[i] = data; |
---|
| 141 | unsigned int byte_width = *data++; |
---|
| 142 | byte_width >>= 6; |
---|
[36b4cd7] | 143 | |
---|
[cba8bf34] | 144 | if (byte_width) { |
---|
| 145 | unsigned int start_and_n = *data; |
---|
| 146 | int n = (start_and_n & 15) + 1; |
---|
| 147 | data += n * byte_width + 1; |
---|
| 148 | data_len -= n * byte_width + 1; |
---|
[36b4cd7] | 149 | } |
---|
| 150 | } |
---|
[cba8bf34] | 151 | } |
---|
| 152 | |
---|
| 153 | int |
---|
| 154 | BitmapFont::glyph_width(wxChar ch) const |
---|
| 155 | { |
---|
| 156 | #if SIZEOF_WXCHAR > 2 |
---|
| 157 | if (ch >= 0x10000) return 0; |
---|
| 158 | #endif |
---|
| 159 | if (!extra_chars) |
---|
| 160 | init_extra_chars(); |
---|
| 161 | |
---|
| 162 | int width = 8; |
---|
| 163 | |
---|
| 164 | const unsigned char * p = extra_chars[ch - BITMAPFONT_MAX_CHAR]; |
---|
| 165 | if (p) { |
---|
[614d60b] | 166 | unsigned int byte_width = *p; |
---|
[cba8bf34] | 167 | width = (byte_width & 0x0f) + 2; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return width; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void |
---|
| 174 | BitmapFont::write_glyph(wxChar ch) const |
---|
| 175 | { |
---|
| 176 | #if SIZEOF_WXCHAR > 2 |
---|
| 177 | if (ch >= 0x10000) return; |
---|
| 178 | #endif |
---|
| 179 | if (!extra_chars) |
---|
| 180 | init_extra_chars(); |
---|
[36b4cd7] | 181 | |
---|
[d4d6909] | 182 | unsigned int byte_width = 0; |
---|
[36b4cd7] | 183 | int start = 0; |
---|
| 184 | int n = 0; |
---|
| 185 | int width = 8; |
---|
[d4d6909] | 186 | |
---|
| 187 | const unsigned char * p = extra_chars[ch - BITMAPFONT_MAX_CHAR]; |
---|
| 188 | if (p) { |
---|
| 189 | byte_width = *p++; |
---|
| 190 | width = (byte_width & 0x0f) + 2; |
---|
| 191 | byte_width >>= 6; |
---|
| 192 | |
---|
| 193 | if (byte_width) { |
---|
| 194 | unsigned int start_and_n = *p++; |
---|
| 195 | start = start_and_n >> 4; |
---|
| 196 | n = (start_and_n & 15) + 1; |
---|
| 197 | } |
---|
[36b4cd7] | 198 | } |
---|
| 199 | |
---|
| 200 | // Even if there's nothing to display, we want to advance the |
---|
| 201 | // raster position. |
---|
| 202 | glBitmap(8 * byte_width, n, 0, -start, width, 0, p); |
---|
[614d60b] | 203 | CHECK_GL_ERROR("BitmapFont::write_glyph", "glBitmap"); |
---|
[36b4cd7] | 204 | } |
---|
| 205 | |
---|
[a1614eb] | 206 | void |
---|
| 207 | BitmapFont::write_string(const wxChar *s, size_t len) const |
---|
| 208 | { |
---|
[36b4cd7] | 209 | if (!gllist_base) return; |
---|
[a1614eb] | 210 | glListBase(gllist_base); |
---|
[30b66b5] | 211 | #if SIZEOF_WXCHAR == 1 |
---|
| 212 | call_lists(len, s); |
---|
| 213 | #elif SIZEOF_WXCHAR == 2 || SIZEOF_WXCHAR == 4 |
---|
| 214 | while (len) { |
---|
| 215 | size_t n; |
---|
| 216 | for (n = 0; n < len; ++n) |
---|
| 217 | if (int(s[n]) >= BITMAPFONT_MAX_CHAR) |
---|
| 218 | break; |
---|
| 219 | call_lists(n, s); |
---|
| 220 | s += n; |
---|
| 221 | len -= n; |
---|
[c7627d4] | 222 | while (len && int(*s) >= BITMAPFONT_MAX_CHAR) { |
---|
| 223 | write_glyph(*s); |
---|
[30b66b5] | 224 | ++s; |
---|
| 225 | --len; |
---|
[a1614eb] | 226 | } |
---|
| 227 | } |
---|
[30b66b5] | 228 | #else |
---|
| 229 | # error "sizeof(wxChar) not 1, 2 or 4" |
---|
| 230 | #endif |
---|
[a1614eb] | 231 | } |
---|