Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OImageFont.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "OImageFont.h"
21 #include <cassert>
22 #include <fstream>
24 
26 
27 bool OImageFont::load(const std::string& filename)
28 {
29 
30  std::string line;
31  char spaces[100];
32  int ascii = 0, xOffset = 0, yOffset = 0, page = 0, first = 0, second = 0, kerning = 0;
33 
34  char* charLinePattern =
35  "char id=%d%[ ]x=%d%[ ]y=%d%[ ]width=%d%[ ]height=%d%[ ]xoffset=%d%[ ]yoffset=%d%[ "
36  "]xadvance=%d%[ ]page=%d%[ ]\n";
37 
38  // Read the file with the description of the chararacters and their kernings (.fnt)
39  std::ifstream ifs;
40  ifs.open(filename.c_str(), std::ios::in | std::ios::binary);
41  if (!ifs)
42  {
43  return false;
44  }
45 
46  // Skip the header
47  std::getline(ifs, line);
48  std::getline(ifs, line);
49  std::getline(ifs, line);
50  std::getline(ifs, line);
51 
52  // Read the first char (which must be the invalid chararacter!)
53  std::getline(ifs, line);
54  if (sscanf_s(line.c_str(), charLinePattern, &ascii, spaces, (unsigned)_countof(spaces), &_invalid_char.x,
55  spaces, (unsigned)_countof(spaces), &_invalid_char.y, spaces, (unsigned)_countof(spaces),
56  &_invalid_char.w, spaces, (unsigned)_countof(spaces), &_invalid_char.h, spaces,
57  (unsigned)_countof(spaces), &xOffset, spaces, (unsigned)_countof(spaces), &yOffset, spaces,
58  (unsigned)_countof(spaces), &_invalid_char.advance, spaces, (unsigned)_countof(spaces),
59  &page, spaces, (unsigned)_countof(spaces)) != 18)
60  return false;
61 
62  // Initialize the char vector with the invalid_char
63  _char = std::vector<OGLFontChar>(256, _invalid_char);
64 
65  // Read the rest of the file
66  while (std::getline(ifs, line))
67  {
68 
69  // Match char lines
70  OGLFontChar c;
71  if (sscanf_s(line.c_str(), charLinePattern, &ascii, spaces, (unsigned)_countof(spaces), &c.x, spaces,
72  (unsigned)_countof(spaces), &c.y, spaces, (unsigned)_countof(spaces), &c.w, spaces,
73  (unsigned)_countof(spaces), &c.h, spaces, (unsigned)_countof(spaces), &xOffset, spaces,
74  (unsigned)_countof(spaces), &yOffset, spaces, (unsigned)_countof(spaces), &c.advance,
75  spaces, (unsigned)_countof(spaces), &page, spaces, (unsigned)_countof(spaces)) == 18)
76  _char[ascii] = c;
77 
78  // Match kerning lines
79  if (sscanf_s(line.c_str(), "kerning first=%d%[ ]second=%d%[ ]amount=%d%[ ]", &first, spaces,
80  (unsigned)_countof(spaces), &second, spaces, (unsigned)_countof(spaces), &kerning,
81  spaces, (unsigned)_countof(spaces)))
82  _kernings[first][second] = kerning;
83  }
84  ifs.close();
85 
86  // Read the texture (.tga)
87  std::string tgaFilename = filename.substr(0, filename.find_last_of('.')) + ".tga";
88  ifs.open(tgaFilename.c_str(), std::ios::in | std::ios::binary);
89  if (!ifs)
90  {
91  return false;
92  }
93 
94  // Skip the first 12 bytes of the header
95  ifs.seekg(12, std::ios::beg);
96 
97  // Read the width and height of the texture in pixels (2 bytes each)
98  ifs.read((char*)&_sizeX, 2);
99  ifs.read((char*)&_sizeY, 2);
100 
101  // Skip the first 12 bytes of the header
102  ifs.seekg(2, std::ios::cur);
103 
104  // Read the texture's data (1 byte per pixel)
105  int textureSize = _sizeX * _sizeY;
106  _data = new unsigned char[textureSize];
107  ifs.read((char*)_data, textureSize);
108 
109  _depth = 8;
110 
111  ifs.close();
112  return true;
113 }
114 
115 const OImageFont::OGLFontChar& OImageFont::getChar(unsigned char c) const
116 {
117  if (c >= 0 && c < 256)
118  return _char.at(c);
119  else
120  return _invalid_char;
121 }
122 
123 const int OImageFont::getKerning(unsigned char first, unsigned char second) const
124 {
125  if (first >= 0 && first < 256 && second >= 0 && second < 256)
126  return _kernings[first][second];
127  else
128  return 0;
129 }
NxReal c
Definition: NxVec3.cpp:317
int _kernings[256][256]
Definition: OImageFont.h:63
OGLFontChar _invalid_char
Definition: OImageFont.h:64
const int getKerning(unsigned char first, unsigned char second) const
Definition: OImageFont.cpp:123
std::vector< OGLFontChar > _char
Definition: OImageFont.h:62
virtual bool load(const std::string &filename)
Definition: OImageFont.cpp:27
const OGLFontChar & getChar(unsigned char c) const
Definition: OImageFont.cpp:115
virtual ~OImageFont()
Definition: OImageFont.cpp:25
Definition: OImage.h:34
unsigned int _sizeX
Definition: OImage.h:92
unsigned int _depth
Definition: OImage.h:89
unsigned int _sizeY
Definition: OImage.h:93
unsigned char * _data
Definition: OImage.h:86