X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FGlyphMaker.java;h=e5fcc522da927fb2c8788b98f8a5075815389f38;hb=c4cefaa04ec122fc02efb6542451a31fdf722c32;hp=2a6610bbec4c8cedff479a6962b8502757bac874;hpb=218d18dbda14a7bf482d6c07bed66f16bcd6a6ba;p=nikiroo-utils.git diff --git a/src/jexer/backend/GlyphMaker.java b/src/jexer/backend/GlyphMaker.java index 2a6610b..e5fcc52 100644 --- a/src/jexer/backend/GlyphMaker.java +++ b/src/jexer/backend/GlyphMaker.java @@ -29,6 +29,7 @@ package jexer.backend; import java.awt.Font; +import java.awt.FontFormatException; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; @@ -38,6 +39,7 @@ import java.io.IOException; import java.util.HashMap; import jexer.bits.Cell; +import jexer.bits.StringUtils; /** * GlyphMakerFont creates glyphs as bitmaps from a font. @@ -135,18 +137,28 @@ class GlyphMakerFont { * @param fontSize the size of font to use */ public GlyphMakerFont(final String filename, final int fontSize) { + + if (filename.length() == 0) { + // Fallback font + font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize - 2); + return; + } + Font fontRoot = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); InputStream in = loader.getResourceAsStream(filename); fontRoot = Font.createFont(Font.TRUETYPE_FONT, in); - font = fontRoot.deriveFont(Font.PLAIN, fontSize); - } catch (java.awt.FontFormatException e) { - e.printStackTrace(); - font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize); - } catch (java.io.IOException e) { - e.printStackTrace(); - font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize); + font = fontRoot.deriveFont(Font.PLAIN, fontSize - 2); + } catch (FontFormatException e) { + // Ideally we would report an error here, either via System.err + // or TExceptionDialog. However, I do not want GlyphMaker to + // know about available backends, so we quietly fallback to + // whatever is available as MONO. + font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize - 2); + } catch (IOException e) { + // See comment above. + font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize - 2); } } @@ -285,6 +297,17 @@ class GlyphMakerFont { gotFontDimensions = true; } + /** + * Checks if this maker's Font has a glyph for the specified character. + * + * @param codePoint the character (Unicode code point) for which a glyph + * is needed. + * @return true if this Font has a glyph for the character; false + * otherwise. + */ + public boolean canDisplay(final int codePoint) { + return font.canDisplay(codePoint); + } } /** @@ -312,6 +335,11 @@ public class GlyphMaker { */ private static final String emojiFontFilename = "OpenSansEmoji.ttf"; + /** + * The fallback font resource filename. + */ + private static final String fallbackFontFilename = ""; + // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- // ------------------------------------------------------------------------ @@ -341,6 +369,11 @@ public class GlyphMaker { */ private GlyphMakerFont makerEmoji; + /** + * The instance that has the fallback font. + */ + private GlyphMakerFont makerFallback; + // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -360,6 +393,9 @@ public class GlyphMaker { fontFilename = System.getProperty("jexer.emojiFont.filename", emojiFontFilename); makerEmoji = new GlyphMakerFont(fontFilename, fontSize); + fontFilename = System.getProperty("jexer.fallbackFont.filename", + fallbackFontFilename); + makerFallback = new GlyphMakerFont(fontFilename, fontSize); } // ------------------------------------------------------------------------ @@ -410,15 +446,28 @@ public class GlyphMaker { final int cellHeight, final boolean blinkVisible) { int ch = cell.getChar(); - if ((ch >= 0x2e80) && (ch <= 0x9fff)) { - return makerCjk.getImage(cell, cellWidth, cellHeight, blinkVisible); + if (StringUtils.isCjk(ch)) { + if (makerCjk.canDisplay(ch)) { + return makerCjk.getImage(cell, cellWidth, cellHeight, + blinkVisible); + } } - if ((ch >= 0x1f004) && (ch <= 0x1f9c0)) { - return makerEmoji.getImage(cell, cellWidth, cellHeight, blinkVisible); + if (StringUtils.isEmoji(ch)) { + if (makerEmoji.canDisplay(ch)) { + // System.err.println("emoji: " + String.format("0x%x", ch)); + return makerEmoji.getImage(cell, cellWidth, cellHeight, + blinkVisible); + } } // When all else fails, use the default. - return makerMono.getImage(cell, cellWidth, cellHeight, blinkVisible); + if (makerMono.canDisplay(ch)) { + return makerMono.getImage(cell, cellWidth, cellHeight, + blinkVisible); + } + + return makerFallback.getImage(cell, cellWidth, cellHeight, + blinkVisible); } }