From: Jeremias Maerki Date: Sat, 3 Apr 2004 13:36:08 +0000 (+0000) Subject: New function to determine whether a particular character is available for this font. X-Git-Tag: Root_Temp_KnuthStylePageBreaking~774 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a3ae5cb800f737b9b719e5a68a9f60aff46b2ba5;p=xmlgraphics-fop.git New function to determine whether a particular character is available for this font. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197488 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/codegen/font-file.xsl b/src/codegen/font-file.xsl index 09fbd2225..93f70cdff 100644 --- a/src/codegen/font-file.xsl +++ b/src/codegen/font-file.xsl @@ -121,6 +121,10 @@ public class extends Typeface { } } + public boolean hasChar(char c) { + return (mapping.mapChar(c) > 0); + } + } diff --git a/src/java/org/apache/fop/fonts/Font.java b/src/java/org/apache/fop/fonts/Font.java index 9cce65d74..d2e58c775 100644 --- a/src/java/org/apache/fop/fonts/Font.java +++ b/src/java/org/apache/fop/fonts/Font.java @@ -154,6 +154,20 @@ public class Font { return c; } + + /** + * Determines whether this font contains a particular character/glyph. + * @param c character to check + * @return True if the character is supported, Falso otherwise + */ + public boolean hasChar(char c) { + if (metric instanceof org.apache.fop.fonts.Typeface) { + return ((org.apache.fop.fonts.Typeface)metric).hasChar(c); + } else { + // Use default CodePointMapping + return (CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c) > 0); + } + } /** * @see java.lang.Object#toString() @@ -182,7 +196,6 @@ public class Font { * This also performs some guessing on widths on various * versions of space that might not exists in the font. * @param c character to inspect - * @param fs FontState to use * @return the width of the character */ public int getCharWidth(char c) { @@ -257,10 +270,13 @@ public class Font { /** * Calculates the word width. + * @param word text to get width for + * @return the width of the text */ public int getWordWidth(String word) { - if (word == null) + if (word == null) { return 0; + } int wordLength = word.length(); int width = 0; char[] characters = new char[wordLength]; diff --git a/src/java/org/apache/fop/fonts/LazyFont.java b/src/java/org/apache/fop/fonts/LazyFont.java index b16f6b027..149b15c15 100644 --- a/src/java/org/apache/fop/fonts/LazyFont.java +++ b/src/java/org/apache/fop/fonts/LazyFont.java @@ -92,13 +92,21 @@ public class LazyFont extends Typeface implements FontDescriptor { } /** - * @see org.apache.fop.fonts.Font#mapChar(char) + * @see org.apache.fop.fonts.Typeface#mapChar(char) */ public char mapChar(char c) { load(); return realFont.mapChar(c); } + /** + * @see org.apache.fop.fonts.Typeface#hasChar(char) + */ + public boolean hasChar(char c) { + load(); + return realFont.hasChar(c); + } + /** * @see org.apache.fop.fonts.Typeface#isMultiByte() */ diff --git a/src/java/org/apache/fop/fonts/MultiByteFont.java b/src/java/org/apache/fop/fonts/MultiByteFont.java index 62f5be961..566636c37 100644 --- a/src/java/org/apache/fop/fonts/MultiByteFont.java +++ b/src/java/org/apache/fop/fonts/MultiByteFont.java @@ -125,12 +125,7 @@ public class MultiByteFont extends CIDFont { * @see org.apache.fop.fonts.FontDescriptor#isEmbeddable() */ public boolean isEmbeddable() { - if (getEmbedFileName() == null - && embedResourceName == null) { - return false; - } else { - return true; - } + return !(getEmbedFileName() == null && embedResourceName == null); } /** @@ -196,20 +191,27 @@ public class MultiByteFont extends CIDFont { } */ - /** - * @see org.apache.fop.fonts.Font#mapChar(char) - */ - public char mapChar(char c) { + private int findGlyphIndex(char c) { int idx = (int)c; int retIdx = 0; for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) { if (bfentries[i].getUnicodeStart() <= idx && bfentries[i].getUnicodeEnd() >= idx) { - retIdx = bfentries[i].getGlyphStartIndex() + idx - - bfentries[i].getUnicodeStart(); + + retIdx = bfentries[i].getGlyphStartIndex() + + idx + - bfentries[i].getUnicodeStart(); } } + return retIdx; + } + + /** + * @see org.apache.fop.fonts.Typeface#mapChar(char) + */ + public char mapChar(char c) { + int retIdx = findGlyphIndex(c); if (isEmbeddable()) { // Reencode to a new subset font or get @@ -231,6 +233,14 @@ public class MultiByteFont extends CIDFont { return (char)retIdx; } + /** + * @see org.apache.fop.fonts.Typeface#hasChar(char) + */ + public boolean hasChar(char c) { + return (findGlyphIndex(c) > 0); + } + + /** * Sets the bfentries. * @param bfentries The bfentries to set diff --git a/src/java/org/apache/fop/fonts/SingleByteFont.java b/src/java/org/apache/fop/fonts/SingleByteFont.java index e28bc653a..26bba54a6 100644 --- a/src/java/org/apache/fop/fonts/SingleByteFont.java +++ b/src/java/org/apache/fop/fonts/SingleByteFont.java @@ -67,7 +67,7 @@ public class SingleByteFont extends CustomFont { } /** - * @see org.apache.fop.fonts.Font#mapChar(char) + * @see org.apache.fop.fonts.Typeface#mapChar(char) */ public char mapChar(char c) { char d = mapping.mapChar(c); @@ -77,6 +77,13 @@ public class SingleByteFont extends CustomFont { return '#'; } } + + /** + * @see org.apache.fop.fonts.Typeface#hasChar(char) + */ + public boolean hasChar(char c) { + return (mapping.mapChar(c) > 0); + } /* ---- single byte font specific setters --- */ diff --git a/src/java/org/apache/fop/fonts/Typeface.java b/src/java/org/apache/fop/fonts/Typeface.java index f3e24cf98..f39a80d40 100644 --- a/src/java/org/apache/fop/fonts/Typeface.java +++ b/src/java/org/apache/fop/fonts/Typeface.java @@ -39,6 +39,13 @@ public abstract class Typeface implements FontMetrics { */ public abstract char mapChar(char c); + /** + * Determines whether this font contains a particular character/glyph. + * @param c character to check + * @return True if the character is supported, Falso otherwise + */ + public abstract boolean hasChar(char c); + /** * Determines whether the font is a multibyte font. * @return True if it is multibyte