Line data Source code
1 : #include "StandardFontsFile.h"
2 : #include "Logger.h"
3 : #include <fstream>
4 : #include "str_utils.h"
5 : #include "../../Constants.h"
6 : #include "../../Text/Utf8Sanitizer.h"
7 : #include "../parse_error.h"
8 : #include "../invalid_odb_error.h"
9 : #include "enums.pb.h"
10 :
11 :
12 : namespace Odb::Lib::FileModel::Design
13 : {
14 96 : StandardFontsFile::~StandardFontsFile()
15 : {
16 96 : m_characterBlocks.clear();
17 96 : }
18 :
19 93 : bool StandardFontsFile::Parse(std::filesystem::path path)
20 : {
21 93 : std::ifstream standardFile;
22 93 : int lineNumber = 0;
23 93 : std::string line;
24 :
25 : try
26 : {
27 93 : if (!OdbFile::Parse(path))
28 : {
29 0 : auto message = "fonts directory does not exist: [" + path.string() + "]";
30 0 : throw invalid_odb_error(message);
31 0 : }
32 :
33 93 : auto fontsStandardFile = path / "standard";
34 93 : if (!std::filesystem::exists(fontsStandardFile))
35 : {
36 1 : auto message = "fonts/standard file does not exist: [" + fontsStandardFile.string() + "]";
37 1 : throw invalid_odb_error(message);
38 1 : }
39 :
40 92 : standardFile.open(fontsStandardFile, std::ios::in);
41 92 : if (!standardFile.is_open())
42 : {
43 0 : auto message = "unable to open fonts/standard file: [" + fontsStandardFile.string() + "]";
44 0 : throw invalid_odb_error(message);
45 0 : }
46 :
47 92 : std::shared_ptr<CharacterBlock> pCurrentCharacterBlock;
48 92 : std::shared_ptr<CharacterBlock::LineRecord> pCurrentLineRecord;
49 92 : bool beginTokenFound = false;
50 :
51 67464 : while (std::getline(standardFile, line))
52 : {
53 67372 : lineNumber++;
54 :
55 : // trim whitespace from beginning and end of line
56 67372 : Utils::str_trim(line);
57 67372 : if (!line.empty())
58 : {
59 58604 : std::stringstream lineStream(line);
60 58604 : if (line.find(Constants::COMMENT_TOKEN) == 0)
61 : {
62 : // comment line
63 : }
64 58604 : else if (line.find("XSIZE") == 0)
65 : {
66 92 : std::string token;
67 92 : if (!(lineStream >> token))
68 : {
69 0 : throw_parse_error(m_path, line, token, lineNumber);
70 : }
71 :
72 92 : if (token != "XSIZE")
73 : {
74 0 : throw_parse_error(m_path, line, token, lineNumber);
75 : }
76 :
77 92 : if (!(lineStream >> token))
78 : {
79 0 : throw_parse_error(m_path, line, token, lineNumber);
80 : }
81 :
82 92 : m_xSize = std::stof(token);
83 92 : }
84 58512 : else if (line.find("YSIZE") == 0)
85 : {
86 92 : std::string token;
87 92 : if (!(lineStream >> token))
88 : {
89 0 : throw_parse_error(m_path, line, token, lineNumber);
90 : }
91 :
92 92 : if (token != "YSIZE")
93 : {
94 0 : throw_parse_error(m_path, line, token, lineNumber);
95 : }
96 :
97 92 : if (!(lineStream >> token))
98 : {
99 0 : throw_parse_error(m_path, line, token, lineNumber);
100 : }
101 :
102 92 : m_ySize = std::stof(token);
103 92 : }
104 58420 : else if (line.find("OFFSET") == 0)
105 : {
106 92 : std::string token;
107 92 : if (!(lineStream >> token))
108 : {
109 0 : throw_parse_error(m_path, line, token, lineNumber);
110 : }
111 :
112 92 : if (token != "OFFSET")
113 : {
114 0 : throw_parse_error(m_path, line, token, lineNumber);
115 : }
116 :
117 92 : if (!(lineStream >> token))
118 : {
119 0 : throw_parse_error(m_path, line, token, lineNumber);
120 : }
121 :
122 92 : m_offset = std::stof(token);
123 92 : }
124 58328 : else if (line.find(CharacterBlock::BEGIN_TOKEN) == 0)
125 : {
126 8648 : pCurrentCharacterBlock = std::make_shared<CharacterBlock>();
127 8648 : beginTokenFound = true;
128 :
129 8648 : std::string token;
130 8648 : if (!(lineStream >> token))
131 : {
132 0 : throw_parse_error(m_path, line, token, lineNumber);
133 : }
134 :
135 8648 : if (token != CharacterBlock::BEGIN_TOKEN)
136 : {
137 0 : throw_parse_error(m_path, line, token, lineNumber);
138 : }
139 :
140 8648 : if (!(lineStream >> token))
141 : {
142 0 : throw_parse_error(m_path, line, token, lineNumber);
143 : }
144 :
145 8648 : Utils::str_trim(token);
146 8648 : if (token.length() != 1)
147 : {
148 0 : throw_parse_error(m_path, line, token, lineNumber);
149 : }
150 :
151 8648 : pCurrentCharacterBlock->character = token;
152 8648 : }
153 49680 : else if (line.find(CharacterBlock::END_TOKEN) == 0)
154 : {
155 8648 : if (pCurrentCharacterBlock != nullptr && beginTokenFound)
156 : {
157 8648 : m_characterBlocks.push_back(pCurrentCharacterBlock);
158 8648 : beginTokenFound = false;
159 8648 : pCurrentCharacterBlock.reset();
160 : }
161 : else
162 : {
163 0 : throw_parse_error(m_path, line, "", lineNumber);
164 : }
165 : }
166 41032 : else if (line.find(CharacterBlock::LineRecord::RECORD_TOKEN) == 0)
167 : {
168 41032 : std::string token;
169 41032 : if (!(lineStream >> token))
170 : {
171 0 : throw_parse_error(m_path, line, token, lineNumber);
172 : }
173 :
174 41032 : if (token != CharacterBlock::LineRecord::RECORD_TOKEN)
175 : {
176 0 : throw_parse_error(m_path, line, token, lineNumber);
177 : }
178 :
179 41032 : if (pCurrentCharacterBlock == nullptr || !beginTokenFound)
180 : {
181 0 : throw_parse_error(m_path, line, token, lineNumber);
182 : }
183 :
184 41032 : auto pLineRecord = std::make_shared<CharacterBlock::LineRecord>();
185 :
186 41032 : if (!(lineStream >> token))
187 : {
188 0 : throw_parse_error(m_path, line, token, lineNumber);
189 : }
190 41032 : pLineRecord->xStart = std::stof(token);
191 :
192 41032 : if (!(lineStream >> token))
193 : {
194 0 : throw_parse_error(m_path, line, token, lineNumber);
195 : }
196 41032 : pLineRecord->yStart = std::stof(token);
197 :
198 41032 : if (!(lineStream >> token))
199 : {
200 0 : throw_parse_error(m_path, line, token, lineNumber);
201 : }
202 41032 : pLineRecord->xEnd = std::stof(token);
203 :
204 41032 : if (!(lineStream >> token))
205 : {
206 0 : throw_parse_error(m_path, line, token, lineNumber);
207 : }
208 41032 : pLineRecord->yEnd = std::stof(token);
209 :
210 : // polarity
211 41032 : if (!(lineStream >> token))
212 : {
213 0 : throw_parse_error(m_path, line, token, lineNumber);
214 : }
215 41032 : Utils::str_trim(token);
216 :
217 41032 : if (token.length() != 1)
218 : {
219 0 : throw_parse_error(m_path, line, token, lineNumber);
220 : }
221 :
222 41032 : switch (token[0])
223 : {
224 41032 : case 'P': pLineRecord->polarity = Polarity::Positive; break;
225 0 : case 'N': pLineRecord->polarity = Polarity::Negative; break;
226 0 : default: throw_parse_error(m_path, line, token, lineNumber);
227 : }
228 :
229 : // shape
230 41032 : if (!(lineStream >> token))
231 : {
232 0 : throw_parse_error(m_path, line, token, lineNumber);
233 : }
234 41032 : Utils::str_trim(token);
235 :
236 41032 : if (token.length() != 1)
237 : {
238 0 : throw_parse_error(m_path, line, token, lineNumber);
239 : }
240 :
241 41032 : switch (token[0])
242 : {
243 41032 : case 'R': pLineRecord->shape = LineShape::Round; break;
244 0 : case 'S': pLineRecord->shape = LineShape::Square; break;
245 0 : default: throw_parse_error(m_path, line, token, lineNumber);
246 : }
247 :
248 : // width
249 41032 : if (!(lineStream >> token))
250 : {
251 0 : throw_parse_error(m_path, line, token, lineNumber);
252 : }
253 41032 : pLineRecord->width = std::stof(token);
254 :
255 41032 : pCurrentCharacterBlock->m_lineRecords.push_back(pLineRecord);
256 41032 : }
257 : else
258 : {
259 0 : logwarn("unrecognized line: " + line);
260 0 : throw_parse_error(m_path, line, "", lineNumber);
261 : }
262 58604 : }
263 : }
264 :
265 92 : standardFile.close();
266 93 : }
267 1 : catch (parse_error& pe)
268 : {
269 0 : auto m = pe.toString("Parse Error:");
270 0 : logerror(m);
271 : // cleanup file
272 0 : standardFile.close();
273 0 : throw pe;
274 0 : }
275 1 : catch (invalid_odb_error& ioe)
276 : {
277 1 : parse_info pi(m_path, line, lineNumber);
278 1 : const auto m = pi.toString();
279 2 : logexception_msg(ioe, m);
280 : // cleanup file
281 1 : standardFile.close();
282 1 : throw ioe;
283 3 : }
284 :
285 92 : return true;
286 94 : }
287 :
288 98 : std::unique_ptr<Odb::Lib::Protobuf::StandardFontsFile> StandardFontsFile::to_protobuf() const
289 : {
290 98 : auto pStandardFontsFileMessage = std::make_unique<Odb::Lib::Protobuf::StandardFontsFile>();
291 98 : pStandardFontsFileMessage->set_xsize(m_xSize);
292 98 : pStandardFontsFileMessage->set_ysize(m_ySize);
293 98 : pStandardFontsFileMessage->set_offset(m_offset);
294 8934 : for (const auto& characterBlock : m_characterBlocks)
295 : {
296 8836 : pStandardFontsFileMessage->add_m_characterblocks()->CopyFrom(*characterBlock->to_protobuf());
297 : }
298 98 : return pStandardFontsFileMessage;
299 0 : }
300 :
301 7 : void StandardFontsFile::from_protobuf(const Odb::Lib::Protobuf::StandardFontsFile& message)
302 : {
303 7 : m_xSize = message.xsize();
304 7 : m_ySize = message.ysize();
305 7 : m_offset = message.offset();
306 477 : for (const auto& characterBlockMessage : message.m_characterblocks())
307 : {
308 470 : auto pCharacterBlock = std::make_shared<CharacterBlock>();
309 470 : pCharacterBlock->from_protobuf(characterBlockMessage);
310 470 : m_characterBlocks.push_back(pCharacterBlock);
311 470 : }
312 7 : }
313 :
314 0 : bool StandardFontsFile::Save(std::ostream& os)
315 : {
316 0 : return true;
317 : }
318 :
319 7426 : StandardFontsFile::CharacterBlock::~CharacterBlock()
320 : {
321 7426 : m_lineRecords.clear();
322 7426 : }
323 :
324 8836 : std::unique_ptr<Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock> StandardFontsFile::CharacterBlock::to_protobuf() const
325 : {
326 8836 : std::unique_ptr<Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock> pCharacterBlockMessage(new Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock);
327 17672 : pCharacterBlockMessage->set_character(Odb::Lib::Text::ToUtf8(character));
328 50760 : for (const auto& lineRecord : m_lineRecords)
329 : {
330 41924 : pCharacterBlockMessage->add_m_linerecords()->CopyFrom(*lineRecord->to_protobuf());
331 : }
332 8836 : return pCharacterBlockMessage;
333 0 : }
334 :
335 470 : void StandardFontsFile::CharacterBlock::from_protobuf(const Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock& message)
336 : {
337 470 : if (! message.character().empty()) character = message.character();
338 :
339 2700 : for (const auto& lineRecordMessage : message.m_linerecords())
340 : {
341 2230 : auto pLineRecord = std::make_shared<LineRecord>();
342 2230 : pLineRecord->from_protobuf(lineRecordMessage);
343 2230 : m_lineRecords.push_back(pLineRecord);
344 2230 : }
345 470 : }
346 :
347 41924 : std::unique_ptr<Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock::LineRecord> StandardFontsFile::CharacterBlock::LineRecord::to_protobuf() const
348 : {
349 41924 : std::unique_ptr<Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock::LineRecord> pLineRecordMessage(new Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock::LineRecord);
350 41924 : pLineRecordMessage->set_xstart(xStart);
351 41924 : pLineRecordMessage->set_ystart(yStart);
352 41924 : pLineRecordMessage->set_xend(xEnd);
353 41924 : pLineRecordMessage->set_yend(yEnd);
354 41924 : pLineRecordMessage->set_polarity(static_cast<Odb::Lib::Protobuf::Polarity>(polarity));
355 41924 : pLineRecordMessage->set_shape(static_cast<Odb::Lib::Protobuf::LineShape>(shape));
356 41924 : pLineRecordMessage->set_width(width);
357 41924 : return pLineRecordMessage;
358 0 : }
359 :
360 2230 : void StandardFontsFile::CharacterBlock::LineRecord::from_protobuf(const Odb::Lib::Protobuf::StandardFontsFile::CharacterBlock::LineRecord& message)
361 : {
362 2230 : xStart = message.xstart();
363 2230 : yStart = message.ystart();
364 2230 : xEnd = message.xend();
365 2230 : yEnd = message.yend();
366 2230 : polarity = static_cast<Odb::Lib::Polarity>(message.polarity());
367 2230 : shape = static_cast<LineShape>(message.shape());
368 2230 : width = message.width();
369 2230 : }
370 :
371 : }
|