| 1173 | } |
| 1174 | |
| 1175 | bool LoadFonts() override |
| 1176 | { |
| 1177 | // read file data into buffer |
| 1178 | const char *pFilename = "fonts/index.json"; |
| 1179 | void *pFileData; |
| 1180 | unsigned JsonFileSize; |
| 1181 | if(!Storage()->ReadFile(pFilename, IStorage::TYPE_ALL, &pFileData, &JsonFileSize)) |
| 1182 | { |
| 1183 | log_error("textrender", "Failed to open/read font index file '%s'", pFilename); |
| 1184 | return false; |
| 1185 | } |
| 1186 | |
| 1187 | // parse json data |
| 1188 | json_settings JsonSettings{}; |
| 1189 | char aError[256]; |
| 1190 | json_value *pJsonData = json_parse_ex(&JsonSettings, static_cast<const json_char *>(pFileData), JsonFileSize, aError); |
| 1191 | free(pFileData); |
| 1192 | if(pJsonData == nullptr) |
| 1193 | { |
| 1194 | log_error("textrender", "Failed to parse font index file '%s': %s", pFilename, aError); |
| 1195 | return false; |
| 1196 | } |
| 1197 | if(pJsonData->type != json_object) |
| 1198 | { |
| 1199 | log_error("textrender", "Font index malformed: root must be an object in file '%s'", pFilename); |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | bool Success = true; |
| 1204 | |
| 1205 | // extract font file definitions |
| 1206 | const json_value &FontFiles = (*pJsonData)["font files"]; |
| 1207 | if(FontFiles.type == json_array) |
| 1208 | { |
| 1209 | for(unsigned FontFileIndex = 0; FontFileIndex < FontFiles.u.array.length; ++FontFileIndex) |
| 1210 | { |
| 1211 | if(FontFiles[FontFileIndex].type != json_string) |
| 1212 | { |
| 1213 | log_error("textrender", "Font index malformed: 'font files' must be an array of strings (error at index %d)", FontFileIndex); |
| 1214 | Success = false; |
| 1215 | continue; |
| 1216 | } |
| 1217 | |
| 1218 | char aFontName[IO_MAX_PATH_LENGTH]; |
| 1219 | str_format(aFontName, sizeof(aFontName), "fonts/%s", FontFiles[FontFileIndex].u.string.ptr); |
| 1220 | void *pFontData; |
| 1221 | unsigned FontDataSize; |
| 1222 | if(Storage()->ReadFile(aFontName, IStorage::TYPE_ALL, &pFontData, &FontDataSize)) |
| 1223 | { |
| 1224 | if(LoadFontCollection(aFontName, static_cast<FT_Byte *>(pFontData), (FT_Long)FontDataSize)) |
| 1225 | { |
| 1226 | m_vpFontData.push_back(pFontData); |
| 1227 | } |
| 1228 | else |
| 1229 | { |
| 1230 | free(pFontData); |
| 1231 | } |
| 1232 | } |
no test coverage detected