Search in sources :

Example 1 with GlyphSize

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);
}
Also used : HashMap(java.util.HashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ArrayList(java.util.ArrayList) BOMInputStream(com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream) ResourceLoadingException(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException) BufferedInputStream(java.io.BufferedInputStream) EOFException(java.io.EOFException) ArrayList(java.util.ArrayList) List(java.util.List) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InputStreamReader(java.io.InputStreamReader) GlyphSize(com.loohp.interactivechatdiscordsrvaddon.resources.fonts.LegacyUnicodeFont.GlyphSize) JSONArray(com.loohp.interactivechat.libs.org.json.simple.JSONArray) DataInputStream(java.io.DataInputStream) ResourceLoadingException(com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException) EOFException(java.io.EOFException) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject) AffineTransform(java.awt.geom.AffineTransform) ResourcePackFile(com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackFile) JSONParser(com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser) JSONObject(com.loohp.interactivechat.libs.org.json.simple.JSONObject)

Aggregations

BOMInputStream (com.loohp.interactivechat.libs.org.apache.commons.io.input.BOMInputStream)1 JSONArray (com.loohp.interactivechat.libs.org.json.simple.JSONArray)1 JSONObject (com.loohp.interactivechat.libs.org.json.simple.JSONObject)1 JSONParser (com.loohp.interactivechat.libs.org.json.simple.parser.JSONParser)1 ResourceLoadingException (com.loohp.interactivechatdiscordsrvaddon.resources.ResourceLoadingException)1 ResourcePackFile (com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackFile)1 GlyphSize (com.loohp.interactivechatdiscordsrvaddon.resources.fonts.LegacyUnicodeFont.GlyphSize)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 AffineTransform (java.awt.geom.AffineTransform)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1