use of com.loohp.interactivechatdiscordsrvaddon.resources.fonts.LegacyUnicodeFont.GlyphSize in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class FontManager method loadDirectory.
@Override
protected void loadDirectory(String namespace, ResourcePackFile root) {
if (!root.exists() || !root.isDirectory()) {
throw new IllegalArgumentException(root.getAbsolutePath() + " is not a directory.");
}
Map<String, ResourcePackFile> fileList = files.get(namespace);
if (fileList == null) {
files.put(namespace, fileList = new HashMap<>());
}
JSONParser parser = new JSONParser();
Map<String, FontProvider> fonts = new HashMap<>(this.fonts);
Collection<ResourcePackFile> files = root.listFilesRecursively();
for (ResourcePackFile file : files) {
fileList.put(file.getName(), file);
}
for (ResourcePackFile file : files) {
if (file.getName().endsWith(".json")) {
try {
String key = namespace + ":" + file.getName();
key = key.substring(0, key.lastIndexOf("."));
InputStreamReader reader = new InputStreamReader(new BOMInputStream(file.getInputStream()), StandardCharsets.UTF_8);
JSONObject rootJson = (JSONObject) parser.parse(reader);
reader.close();
List<MinecraftFont> providedFonts = new ArrayList<>();
int index = -1;
for (Object obj : (JSONArray) rootJson.get("providers")) {
index++;
JSONObject fontJson = (JSONObject) obj;
try {
switch(fontJson.get("type").toString()) {
case "bitmap":
String resourceLocation = fontJson.get("file").toString();
int height = ((Number) fontJson.getOrDefault("height", 8)).intValue();
int ascent = ((Number) fontJson.get("ascent")).intValue();
List<String> chars = (List<String>) ((JSONArray) fontJson.get("chars")).stream().map(each -> each.toString()).collect(Collectors.toList());
providedFonts.add(new BitmapFont(manager, null, resourceLocation, height, ascent, chars));
break;
case "legacy_unicode":
String template = fontJson.get("template").toString();
DataInputStream sizesInput = new DataInputStream(new BufferedInputStream(getFontResource(fontJson.get("sizes").toString()).getFile().getInputStream()));
Int2ObjectOpenHashMap<GlyphSize> sizes = new Int2ObjectOpenHashMap<>();
for (int i = 0; ; i++) {
try {
byte b = sizesInput.readByte();
byte start = (byte) ((b >> 4) & 15);
byte end = (byte) (b & 15);
sizes.put(i, new GlyphSize(start, end));
} catch (EOFException e) {
break;
}
}
sizesInput.close();
providedFonts.add(new LegacyUnicodeFont(manager, null, sizes, template));
break;
case "ttf":
resourceLocation = fontJson.get("file").toString();
JSONArray shiftArray = (JSONArray) fontJson.get("shift");
float leftShift = ((Number) shiftArray.get(0)).floatValue();
float downShift = ((Number) shiftArray.get(1)).floatValue();
AffineTransform shift = AffineTransform.getTranslateInstance(-leftShift, downShift);
float size = ((Number) fontJson.get("size")).floatValue();
float oversample = ((Number) fontJson.get("oversample")).floatValue();
String skip = fontJson.getOrDefault("skip", "").toString();
providedFonts.add(new TrueTypeFont(manager, null, resourceLocation, shift, size, oversample, skip));
break;
}
} catch (Exception e) {
new ResourceLoadingException("Unable to load font provider " + index + " in " + file.getAbsolutePath(), e).printStackTrace();
}
}
FontProvider existingProvider = fonts.get(key);
if (existingProvider == null) {
providedFonts.add(new BackingEmptyFont(manager, null));
FontProvider provider = new FontProvider(key, providedFonts);
for (MinecraftFont mcFont : provider.getProviders()) {
mcFont.setProvider(provider);
}
fonts.put(key, provider);
} else {
for (MinecraftFont mcFont : providedFonts) {
mcFont.setProvider(existingProvider);
}
existingProvider.prependProviders(providedFonts);
}
} catch (Exception e) {
new ResourceLoadingException("Unable to load font " + file.getAbsolutePath(), e).printStackTrace();
}
}
}
this.fonts.clear();
this.fonts.putAll(fonts);
}
Aggregations