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