| 12 | { |
| 13 | |
| 14 | sp<BitmapFont> ApocalypseFont::loadFont(const UString &fontDescPath) |
| 15 | { |
| 16 | auto file = fw().data->fs.open(fontDescPath); |
| 17 | if (!file) |
| 18 | { |
| 19 | LogWarning("Failed to open font file at path \"%s\"", fontDescPath); |
| 20 | return nullptr; |
| 21 | } |
| 22 | |
| 23 | auto data = file.readAll(); |
| 24 | if (!data) |
| 25 | { |
| 26 | LogWarning("Failed to read font file at path \"%s\"", fontDescPath); |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
| 30 | pugi::xml_document doc; |
| 31 | |
| 32 | auto parseResult = doc.load_buffer(data.get(), file.size()); |
| 33 | |
| 34 | if (!parseResult) |
| 35 | { |
| 36 | LogWarning("Failed to parse font file at \"%s\" - \"%s\" at \"%llu\"", fontDescPath, |
| 37 | parseResult.description(), (unsigned long long)parseResult.offset); |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | auto openapocNode = doc.child("openapoc"); |
| 42 | if (!openapocNode) |
| 43 | { |
| 44 | LogWarning("Failed to find \"openapoc\" root node in font file at \"%s\"", fontDescPath); |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
| 48 | auto fontNode = openapocNode.child("apocfont"); |
| 49 | if (!fontNode) |
| 50 | { |
| 51 | LogWarning("Failed to find \"openapoc::apocfont\" node in font file at \"%s\"", |
| 52 | fontDescPath); |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | int spacewidth = 0; |
| 57 | int height = 0; |
| 58 | int kerning = 0; |
| 59 | UString fontName; |
| 60 | |
| 61 | std::map<char32_t, UString> charMap; |
| 62 | |
| 63 | fontName = fontNode.attribute("name").value(); |
| 64 | if (fontName.empty()) |
| 65 | { |
| 66 | LogError("apocfont element with no \"name\" attribute"); |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | height = fontNode.attribute("height").as_int(); |
| 71 | if (height <= 0) |