LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - StandardFontsFile.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 76.5 % 204 156
Test Date: 2026-11-21 03:51:50 Functions: 75.0 % 12 9
Legend: Lines: hit not hit

            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           93 :     StandardFontsFile::~StandardFontsFile()
      15              :     {
      16           93 :         m_characterBlocks.clear();
      17           93 :     }
      18              : 
      19           90 :     bool StandardFontsFile::Parse(std::filesystem::path path)
      20              :     {
      21           90 :         std::ifstream standardFile;
      22           90 :         int lineNumber = 0;
      23           90 :         std::string line;
      24              : 
      25              :         try
      26              :         {
      27           90 :             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           90 :             auto fontsStandardFile = path / "standard";
      34           90 :             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           89 :             standardFile.open(fontsStandardFile, std::ios::in);
      41           89 :             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           89 :             std::shared_ptr<CharacterBlock> pCurrentCharacterBlock;
      48           89 :             std::shared_ptr<CharacterBlock::LineRecord> pCurrentLineRecord;
      49           89 :             bool beginTokenFound = false;
      50              : 
      51        65264 :             while (std::getline(standardFile, line))
      52              :             {
      53        65175 :                 lineNumber++;
      54              : 
      55              :                 // trim whitespace from beginning and end of line
      56        65175 :                 Utils::str_trim(line);
      57        65175 :                 if (!line.empty())
      58              :                 {
      59        56693 :                     std::stringstream lineStream(line);
      60        56693 :                     if (line.find(Constants::COMMENT_TOKEN) == 0)
      61              :                     {
      62              :                         // comment line
      63              :                     }
      64        56693 :                     else if (line.find("XSIZE") == 0)
      65              :                     {
      66           89 :                         std::string token;
      67           89 :                         if (!(lineStream >> token))
      68              :                         {
      69            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      70              :                         }
      71              : 
      72           89 :                         if (token != "XSIZE")
      73              :                         {
      74            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      75              :                         }
      76              : 
      77           89 :                         if (!(lineStream >> token))
      78              :                         {
      79            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      80              :                         }
      81              : 
      82           89 :                         m_xSize = std::stof(token);
      83           89 :                     }
      84        56604 :                     else if (line.find("YSIZE") == 0)
      85              :                     {
      86           89 :                         std::string token;
      87           89 :                         if (!(lineStream >> token))
      88              :                         {
      89            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      90              :                         }
      91              : 
      92           89 :                         if (token != "YSIZE")
      93              :                         {
      94            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      95              :                         }
      96              : 
      97           89 :                         if (!(lineStream >> token))
      98              :                         {
      99            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     100              :                         }
     101              : 
     102           89 :                         m_ySize = std::stof(token);
     103           89 :                     }
     104        56515 :                     else if (line.find("OFFSET") == 0)
     105              :                     {
     106           89 :                         std::string token;
     107           89 :                         if (!(lineStream >> token))
     108              :                         {
     109            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     110              :                         }
     111              : 
     112           89 :                         if (token != "OFFSET")
     113              :                         {
     114            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     115              :                         }
     116              : 
     117           89 :                         if (!(lineStream >> token))
     118              :                         {
     119            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     120              :                         }
     121              : 
     122           89 :                         m_offset = std::stof(token);
     123           89 :                     }
     124        56426 :                     else if (line.find(CharacterBlock::BEGIN_TOKEN) == 0)
     125              :                     {
     126         8366 :                         pCurrentCharacterBlock = std::make_shared<CharacterBlock>();
     127         8366 :                         beginTokenFound = true;
     128              : 
     129         8366 :                         std::string token;
     130         8366 :                         if (!(lineStream >> token))
     131              :                         {
     132            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     133              :                         }
     134              : 
     135         8366 :                         if (token != CharacterBlock::BEGIN_TOKEN)
     136              :                         {
     137            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     138              :                         }
     139              : 
     140         8366 :                         if (!(lineStream >> token))
     141              :                         {
     142            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     143              :                         }
     144              : 
     145         8366 :                         Utils::str_trim(token);
     146         8366 :                         if (token.length() != 1)
     147              :                         {
     148            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     149              :                         }
     150              : 
     151         8366 :                         pCurrentCharacterBlock->character = token;
     152         8366 :                     }
     153        48060 :                     else if (line.find(CharacterBlock::END_TOKEN) == 0)
     154              :                     {
     155         8366 :                         if (pCurrentCharacterBlock != nullptr && beginTokenFound)
     156              :                         {
     157         8366 :                             m_characterBlocks.push_back(pCurrentCharacterBlock);
     158         8366 :                             beginTokenFound = false;
     159         8366 :                             pCurrentCharacterBlock.reset();
     160              :                         }
     161              :                         else
     162              :                         {
     163            0 :                             throw_parse_error(m_path, line, "", lineNumber);
     164              :                         }
     165              :                     }
     166        39694 :                     else if (line.find(CharacterBlock::LineRecord::RECORD_TOKEN) == 0)
     167              :                     {
     168        39694 :                         std::string token;
     169        39694 :                         if (!(lineStream >> token))
     170              :                         {
     171            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     172              :                         }
     173              : 
     174        39694 :                         if (token != CharacterBlock::LineRecord::RECORD_TOKEN)
     175              :                         {
     176            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     177              :                         }
     178              : 
     179        39694 :                         if (pCurrentCharacterBlock == nullptr || !beginTokenFound)
     180              :                         {
     181            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     182              :                         }
     183              : 
     184        39694 :                         auto pLineRecord = std::make_shared<CharacterBlock::LineRecord>();
     185              : 
     186        39694 :                         if (!(lineStream >> token))
     187              :                         {
     188            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     189              :                         }
     190        39694 :                         pLineRecord->xStart = std::stof(token);
     191              : 
     192        39694 :                         if (!(lineStream >> token))
     193              :                         {
     194            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     195              :                         }
     196        39694 :                         pLineRecord->yStart = std::stof(token);
     197              : 
     198        39694 :                         if (!(lineStream >> token))
     199              :                         {
     200            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     201              :                         }
     202        39694 :                         pLineRecord->xEnd = std::stof(token);
     203              : 
     204        39694 :                         if (!(lineStream >> token))
     205              :                         {
     206            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     207              :                         }
     208        39694 :                         pLineRecord->yEnd = std::stof(token);
     209              : 
     210              :                         // polarity
     211        39694 :                         if (!(lineStream >> token))
     212              :                         {
     213            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     214              :                         }
     215        39694 :                         Utils::str_trim(token);
     216              : 
     217        39694 :                         if (token.length() != 1)
     218              :                         {
     219            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     220              :                         }
     221              : 
     222        39694 :                         switch (token[0])
     223              :                         {
     224        39694 :                         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        39694 :                         if (!(lineStream >> token))
     231              :                         {
     232            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     233              :                         }
     234        39694 :                         Utils::str_trim(token);
     235              : 
     236        39694 :                         if (token.length() != 1)
     237              :                         {
     238            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     239              :                         }
     240              : 
     241        39694 :                         switch (token[0])
     242              :                         {
     243        39694 :                         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        39694 :                         if (!(lineStream >> token))
     250              :                         {
     251            0 :                             throw_parse_error(m_path, line, token, lineNumber);
     252              :                         }
     253        39694 :                         pLineRecord->width = std::stof(token);
     254              : 
     255        39694 :                         pCurrentCharacterBlock->m_lineRecords.push_back(pLineRecord);
     256        39694 :                     }
     257              :                     else
     258              :                     {
     259            0 :                         logwarn("unrecognized line: " + line);
     260            0 :                         throw_parse_error(m_path, line, "", lineNumber);
     261              :                     }
     262        56693 :                 }
     263              :             }
     264              : 
     265           89 :             standardFile.close();
     266           90 :         }
     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           89 :                 return true;
     286           91 :         }
     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         7144 :     StandardFontsFile::CharacterBlock::~CharacterBlock()
     320              :     {
     321         7144 :         m_lineRecords.clear();
     322         7144 :     }
     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              : }
        

Generated by: LCOV version 2.0-1