| 1 | #include <cvd/gl_helpers.h> |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <cassert> |
| 5 | #include <cmath> |
| 6 | #include <map> |
| 7 | |
| 8 | using namespace std; |
| 9 | |
| 10 | namespace CVD |
| 11 | { |
| 12 | |
| 13 | namespace Internal |
| 14 | { |
| 15 | |
| 16 | struct Point |
| 17 | { |
| 18 | float x, y; |
| 19 | }; |
| 20 | |
| 21 | struct Font |
| 22 | { |
| 23 | typedef unsigned short Index; |
| 24 | |
| 25 | struct Char |
| 26 | { |
| 27 | Index vertexOffset; |
| 28 | Index triangleOffset; |
| 29 | Index outlineOffset; |
| 30 | GLsizei numTriangles; |
| 31 | GLsizei numOutlines; |
| 32 | float advance; |
| 33 | }; |
| 34 | |
| 35 | Point* vertices; |
| 36 | Index* triangles; |
| 37 | Index* outlines; |
| 38 | Char* characters; |
| 39 | string glyphs; |
| 40 | |
| 41 | const Char* findChar(const char c) const |
| 42 | { |
| 43 | size_t ind = glyphs.find(c); |
| 44 | if(ind == string::npos) |
| 45 | return NULL; |
| 46 | return characters + ind; |
| 47 | } |
| 48 | |
| 49 | float getAdvance(const char c) const |
| 50 | { |
| 51 | const Char* ch = findChar(c); |
| 52 | if(!ch) |
| 53 | return 0; |
| 54 | return ch->advance; |
| 55 | } |
| 56 | |
| 57 | void fill(const char c) const |
| 58 | { |
| 59 | const Char* ch = findChar(c); |
| 60 | if(!ch || !ch->numTriangles) |
| 61 | return; |
| 62 | glVertexPointer(2, GL_FLOAT, 0, vertices + ch->vertexOffset); |
| 63 | glDrawElements(GL_TRIANGLES, ch->numTriangles, GL_UNSIGNED_SHORT, triangles + ch->triangleOffset); |
| 64 | } |
| 65 | |
| 66 | void outline(const char c) const |
| 67 | { |
| 68 | const Char* ch = findChar(c); |
| 69 | if(!ch || !ch->numOutlines) |
| 70 | return; |
| 71 | glVertexPointer(2, GL_FLOAT, 0, vertices + ch->vertexOffset); |
| 72 | glDrawElements(GL_LINES, ch->numOutlines, GL_UNSIGNED_SHORT, outlines + ch->outlineOffset); |
| 73 | } |
| 74 | |
| 75 | void draw(const char c) const |
| 76 | { |
| 77 | const Char* ch = findChar(c); |
| 78 | if(!ch || !ch->numTriangles || !ch->numOutlines) |
| 79 | return; |
| 80 | glVertexPointer(2, GL_FLOAT, 0, vertices + ch->vertexOffset); |
| 81 | glDrawElements(GL_TRIANGLES, ch->numTriangles, GL_UNSIGNED_SHORT, triangles + ch->triangleOffset); |
| 82 | glDrawElements(GL_LINES, ch->numOutlines, GL_UNSIGNED_SHORT, outlines + ch->outlineOffset); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | // the fonts defined in these headers are derived from Bitstream Vera fonts. See http://www.gnome.org/fonts/ for license and details |
| 87 | #include "mono.h" |
| 88 | #include "sans.h" |
| 89 | #include "serif.h" |
| 90 | |
| 91 | struct FontData |
| 92 | { |
| 93 | |
| 94 | typedef map<string, Font*> FontMap; |
| 95 | |
| 96 | FontData() |
| 97 | { |
| 98 | fonts["sans"] = &sans_font; |
| 99 | fonts["mono"] = &mono_font; |
| 100 | fonts["serif"] = &serif_font; |
| 101 | glSetFont("sans"); |
| 102 | } |
| 103 | inline Font* currentFont() |
| 104 | { |
| 105 | return fonts[currentFontName]; |
| 106 | } |
| 107 | |
| 108 | string currentFontName; |
| 109 | FontMap fonts; |
| 110 | }; |
| 111 | |
| 112 | static struct FontData data; |
| 113 | |
| 114 | } // namespace Internal |
| 115 | |
| 116 | void glSetFont(const std::string& fontname) |
| 117 | { |
| 118 | if(Internal::data.fonts.count(fontname) > 0) |
| 119 | Internal::data.currentFontName = fontname; |
| 120 | } |
| 121 | |
| 122 | const std::string& glGetFont() |
| 123 | { |
| 124 | return Internal::data.currentFontName; |
| 125 | } |
| 126 | |
| 127 | std::pair<double, double> glDrawText(const std::string& text, enum TEXT_STYLE style, double spacing, double kerning) |
| 128 | { |
| 129 | glPushMatrix(); |
| 130 | if(style == NICE) |
| 131 | { |
| 132 | glPushAttrib(GL_COLOR_BUFFER_BIT | GL_LINE_BIT); |
| 133 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 134 | glEnable(GL_BLEND); |
| 135 | glEnable(GL_LINE_SMOOTH); |
| 136 | glLineWidth(1); |
| 137 | } |
| 138 | glEnableClientState(GL_VERTEX_ARRAY); |
| 139 | |
| 140 | // figure out which operation to do on the Char (yes, this is a pointer to member function :) |
| 141 | void (Internal::Font::*operation)(const char c) const; |
| 142 | switch(style) |
| 143 | { |
| 144 | case FILL: |
| 145 | operation = &Internal::Font::fill; |
| 146 | break; |
| 147 | case OUTLINE: |
| 148 | operation = &Internal::Font::outline; |
| 149 | break; |
| 150 | case NICE: |
| 151 | operation = &Internal::Font::draw; |
| 152 | break; |
| 153 | default: |
| 154 | assert(false); |
| 155 | } |
| 156 | |
| 157 | int lines = 0; |
| 158 | double max_total = 0; |
| 159 | double total = 0; |
| 160 | const Internal::Font* font = Internal::data.currentFont(); |
| 161 | const Internal::Font::Char* space = font->findChar(' '); |
| 162 | const double tab_width = 8 * ((space) ? (space->advance) : 1); |
| 163 | for(size_t i = 0; i < text.length(); ++i) |
| 164 | { |
| 165 | char c = text[i]; |
| 166 | if(c == '\n') |
| 167 | { |
| 168 | glTranslated(-total, -spacing, 0); |
| 169 | max_total = std::max(max_total, total); |
| 170 | total = 0; |
| 171 | ++lines; |
| 172 | continue; |
| 173 | } |
| 174 | if(c == '\t') |
| 175 | { |
| 176 | const double advance = tab_width - std::fmod(total, tab_width); |
| 177 | total += advance; |
| 178 | glTranslated(advance, 0, 0); |
| 179 | continue; |
| 180 | } |
| 181 | const Internal::Font::Char* ch = font->findChar(c); |
| 182 | if(!ch) |
| 183 | { |
| 184 | c = static_cast<char>(toupper(c)); |
| 185 | ch = font->findChar(c); |
| 186 | if(!ch) |
| 187 | { |
| 188 | c = '?'; |
| 189 | ch = font->findChar(c); |
| 190 | } |
| 191 | } |
| 192 | if(!ch) |
| 193 | continue; |
| 194 | (font->*operation)(c); |
| 195 | |
| 196 | double w = ch->advance + kerning; |
| 197 | glTranslated(w, 0, 0); |
| 198 | total += w; |
| 199 | } |
| 200 | |
| 201 | glDisableClientState(GL_VERTEX_ARRAY); |
| 202 | if(style == NICE) |
| 203 | { |
| 204 | glPopAttrib(); |
| 205 | } |
| 206 | glPopMatrix(); |
| 207 | |
| 208 | max_total = std::max(total, max_total); |
| 209 | return std::make_pair(max_total, (lines + 1) * spacing); |
| 210 | } |
| 211 | |
| 212 | std::pair<double, double> glGetExtends(const std::string& text, double spacing, double kerning) |
| 213 | { |
| 214 | int lines = 0; |
| 215 | double max_total = 0; |
| 216 | double total = 0; |
| 217 | const Internal::Font* font = Internal::data.currentFont(); |
| 218 | for(size_t i = 0; i < text.length(); ++i) |
| 219 | { |
| 220 | char c = text[i]; |
| 221 | if(c == '\n') |
| 222 | { |
| 223 | max_total = std::max(max_total, total); |
| 224 | total = 0; |
| 225 | ++lines; |
| 226 | continue; |
| 227 | } |
| 228 | const Internal::Font::Char* ch = font->findChar(c); |
| 229 | if(!ch) |
| 230 | { |
| 231 | c = static_cast<char>(toupper(c)); |
| 232 | ch = font->findChar(c); |
| 233 | if(!ch) |
| 234 | { |
| 235 | c = '?'; |
| 236 | ch = font->findChar(c); |
| 237 | } |
| 238 | } |
| 239 | if(!ch) |
| 240 | continue; |
| 241 | total += ch->advance + kerning; |
| 242 | } |
| 243 | max_total = std::max(total, max_total); |
| 244 | return std::make_pair(max_total, (lines + 1) * spacing); |
| 245 | } |
| 246 | |
| 247 | } // namespace CVD |