Bubba-3D  0.9.0
Awesome game engine!
Font.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 //
18 // Created by simon on 2016-03-11.
19 //
20 
21 #include <ft2build.h>
22 #include <stdexcept>
23 #include FT_FREETYPE_H
24 
25 #include "Font.h"
26 
27 Font::GlyphData::GlyphData(int offsetX, FT_GlyphSlot ft_glyphSlot):
28  advanceX(ft_glyphSlot->advance.x),
29  advanceY(ft_glyphSlot->advance.y),
30  bitmapWidth(ft_glyphSlot->bitmap.width),
31  bitmapHeight(ft_glyphSlot->bitmap.rows),
32  bitmapLeft(ft_glyphSlot->bitmap_left),
33  bitmapTop(ft_glyphSlot->bitmap_top),
34  offsetX(offsetX)
35 {
36 }
37 
38 Font::GlyphData Font::getCharacter(unsigned char character) {
39  checkInvalidCharacter(character);
40  return glyphs[character-32];
41 }
42 
43 void Font::checkInvalidCharacter(unsigned char character) {
44  if(character < 32 || character >= 128) {
45  throw std::invalid_argument("The font only support characters with"
46  " ascii value from 32 inclusive to 128 exclusive.");
47  }
48 }
49 
50 void Font::addGlyph(FT_GlyphSlot glyph, int offsetX, unsigned char character) {
51  checkInvalidCharacter(character);
52  glyphs[character-32] = GlyphData(offsetX,glyph);
53 }
54 
55 int Font::getPixelSize() {
56  return pixelSize;
57 }
58 
59 Font::Font(int pixelSize) : pixelSize(pixelSize) {
60 }