• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / libsiedler2 / 14537997660

18 Apr 2025 04:06PM UTC coverage: 81.697% (-0.3%) from 82.012%
14537997660

push

github

Flamefire
Require Boost 1.73

An undefined `PTHREAD_STACK_MIN` causes a compile failure otherwise.

5200 of 6365 relevant lines covered (81.7%)

4237.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/ArchivItem_Font.cpp
1
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "ArchivItem_Font.h"
6
#include "ErrorCodes.h"
7
#include "prototypen.h"
8
#include "libendian/EndianIStreamAdapter.h"
9
#include "libendian/EndianOStreamAdapter.h"
10
#include <iostream>
11
#include <sstream>
12
#include <stdexcept>
13

14
/** @class libsiedler2::ArchivItem_Font
15
 *
16
 *  Klasse für Fonts.
17
 */
18

19
/** @var libsiedler2::ArchivItem_Font::dx
20
 *
21
 *  X-Buchstabenabstand.
22
 */
23

24
/** @var libsiedler2::ArchivItem_Font::dy
25
 *
26
 *  Y-Buchstabenabstand.
27
 */
28

29
libsiedler2::ArchivItem_Font::ArchivItem_Font() : ArchivItem(BobType::Font), isUnicode(false), dx(0), dy(0) {}
3✔
30

31
/**
32
 *  lädt die Fontdaten aus einer Datei.
33
 *
34
 *  @param[in] file    Dateihandle der Datei
35
 *  @param[in] palette Grundpalette
36
 *
37
 *  @return liefert Null bei Erfolg, ungleich Null bei Fehler
38
 */
39
int libsiedler2::ArchivItem_Font::load(std::istream& file, const ArchivItem_Palette* palette)
2✔
40
{
41
    if(!file)
2✔
42
        return ErrorCode::FILE_NOT_ACCESSIBLE;
×
43
    if(!palette)
2✔
44
        return ErrorCode::PALETTE_MISSING;
×
45

46
    libendian::EndianIStreamAdapter<false, std::istream&> fs(file);
2✔
47
    // Spacing einlesen
48
    if(!(fs >> dx >> dy))
2✔
49
        return ErrorCode::UNEXPECTED_EOF;
×
50

51
    isUnicode = (dx == 255 && dy == 255);
2✔
52
    uint32_t numChars;
53
    if(isUnicode)
2✔
54
    {
55
        fs >> numChars >> dx >> dy;
1✔
56
        if(!fs)
1✔
57
            return ErrorCode::UNEXPECTED_EOF;
×
58
    } else
59
        numChars = 256;
1✔
60

61
    // Speicher für Buchstaben alloziieren
62
    alloc(numChars);
2✔
63

64
    // Buchstaben einlesen
65
    for(uint32_t i = 32; i < numChars; ++i)
1,238✔
66
    {
67
        int16_t bobtype_s;
68

69
        // bobtype des Items einlesen
70
        if(!(fs >> bobtype_s))
1,236✔
71
            return ErrorCode::UNEXPECTED_EOF;
×
72
        auto bobtype = static_cast<BobType>(bobtype_s);
1,236✔
73

74
        if(bobtype == BobType::None)
1,236✔
75
            continue;
1,215✔
76

77
        // Daten von Item auswerten
78
        std::unique_ptr<ArchivItem> item;
×
79
        int ec = loader::LoadType(bobtype, file, item, palette);
21✔
80
        if(ec)
21✔
81
            return ec;
×
82
        std::stringstream name;
21✔
83
        name << "U+" << std::hex << i;
21✔
84
        item->setName(name.str());
21✔
85
        set(i, std::move(item));
21✔
86
    }
87

88
    return (!file) ? ErrorCode::UNEXPECTED_EOF : ErrorCode::NONE;
2✔
89
}
90

91
/**
92
 *  schreibt die Fontdaten in eine Datei.
93
 *
94
 *  @param[in] file    Dateihandle der Datei
95
 *  @param[in] palette Grundpalette
96
 *
97
 *  @return liefert Null bei Erfolg, ungleich Null bei Fehler
98
 */
99
int libsiedler2::ArchivItem_Font::write(std::ostream& file, const ArchivItem_Palette* palette) const
2✔
100
{
101
    if(!file)
2✔
102
        return ErrorCode::FILE_NOT_ACCESSIBLE;
×
103
    if(!palette)
2✔
104
        return ErrorCode::PALETTE_MISSING;
×
105

106
    size_t numChars = size();
2✔
107
    if(numChars > 256 && !isUnicode)
2✔
108
        throw std::runtime_error("Trying to save a non-unicode font with more than 256 glyphs");
×
109

110
    libendian::EndianOStreamAdapter<false, std::ostream&> fs(file);
2✔
111

112
    if(isUnicode)
2✔
113
        fs << uint16_t(0xFFFF) << static_cast<uint32_t>(size());
1✔
114
    else
115
        numChars = 256;
1✔
116
    // Spacing schreiben
117
    fs << dx << dy;
2✔
118

119
    // Buchstaben schreiben
120
    for(size_t i = 32; i < numChars; ++i)
1,238✔
121
    {
122
        const ArchivItem* item = get(i);
1,236✔
123
        BobType bobtype = BobType::None;
1,236✔
124

125
        if(item)
1,236✔
126
            bobtype = item->getBobType();
21✔
127

128
        // bobtype des Items schreiben
129
        fs << (int16_t)bobtype;
1,236✔
130

131
        if(item == nullptr)
1,236✔
132
            continue;
1,215✔
133

134
        // Daten von Item auswerten
135
        if(int ec = loader::WriteType(bobtype, file, *item, palette))
21✔
136
            return ec;
×
137
    }
138

139
    return (!file) ? ErrorCode::UNEXPECTED_EOF : ErrorCode::NONE;
2✔
140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc