]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
does a bit better job at adding text to line area
authorKeiron Liddle <keiron@apache.org>
Mon, 11 Feb 2002 09:45:39 +0000 (09:45 +0000)
committerKeiron Liddle <keiron@apache.org>
Mon, 11 Feb 2002 09:45:39 +0000 (09:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194637 13f79535-47bb-0310-9956-ffa450edef68

src/org/apache/fop/layoutmgr/TextLayoutManager.java
src/org/apache/fop/render/pdf/CIDFont.java
src/org/apache/fop/render/pdf/Font.java
src/org/apache/fop/render/pdf/PDFRenderer.java
src/org/apache/fop/render/pdf/fonts/LazyFont.java
src/org/apache/fop/render/pdf/fonts/MultiByteFont.java
src/org/apache/fop/tools/AreaTreeBuilder.java

index 2aa848adaa38b21a8dea9c7a69115cb20fd39bdb..635aa8792318bd04c3cdc5fd34001dba9eda491b 100644 (file)
@@ -12,6 +12,7 @@ import org.apache.fop.fo.FOText; // For TextInfo: TODO: make independent!
 import org.apache.fop.area.Area;
 import org.apache.fop.area.Property;
 import org.apache.fop.area.inline.Word;
+import org.apache.fop.area.inline.Space;
 import org.apache.fop.util.CharUtilities;
 
 import org.apache.fop.fo.properties.*;
@@ -27,6 +28,14 @@ public class TextLayoutManager extends LeafNodeLayoutManager {
     private char[] chars;
     private FOText.TextInfo textInfo;
 
+    private static final char NEWLINE = '\n';
+    private static final char RETURN = '\r';
+    private static final char TAB = '\t';
+    private static final char LINEBREAK = '\u2028';
+    private static final char ZERO_WIDTH_SPACE = '\u200B';
+    // byte order mark
+    private static final char ZERO_WIDTH_NOBREAK_SPACE = '\uFEFF';
+
     /* values that prev (below) may take */
     protected static final int NOTHING = 0;
     protected static final int WHITESPACE = 1;
@@ -48,11 +57,11 @@ public class TextLayoutManager extends LeafNodeLayoutManager {
 
         // Iterate over characters and make text areas.
         // Add each one to parent. Handle word-space.
-//        Word curWordArea = new Word();
-//        curWordArea.setWord(new String(chars));
-//System.out.println("word:" + new String(chars));
+        //        Word curWordArea = new Word();
+        //        curWordArea.setWord(new String(chars));
+        //System.out.println("word:" + new String(chars));
         //parentLM.addChild(curWordArea);
-parseChars();
+        parseChars();
 
         //setCurrentArea(curWordArea);
         //flush();
@@ -65,62 +74,59 @@ parseChars();
 
         int wordStart = 0;
         int wordLength = 0;
-        int wordWidth = 0; 
+        int wordWidth = 0;
         int spaceWidth = 0;
 
         int prev = NOTHING;
 
-        boolean isText = false;
-
-        /* iterate over each character */ 
+        /* iterate over each character */
         for (int i = 0; i < chars.length; i++) {
             int charWidth;
             /* get the character */
             char c = chars[i];
-            if (!(CharUtilities.isSpace(c) || (c == '\n') || (c == '\r') || (c == '\t')
-                    || (c == '\u2028'))) {
+            if (!(CharUtilities.isSpace(c) || (c == NEWLINE) ||
+                    (c == RETURN) || (c == TAB) || (c == LINEBREAK))) {
                 charWidth = CharUtilities.getCharWidth(c, textInfo.fs);
-                isText = true; 
                 prev = TEXT;
-wordLength++;
-wordWidth += charWidth;
+                wordLength++;
+                wordWidth += charWidth;
                 // Add support for zero-width spaces
-                if (charWidth <= 0 && c != '\u200B' && c != '\uFEFF')
+                if (charWidth <= 0 && c != ZERO_WIDTH_SPACE &&
+                                 c != ZERO_WIDTH_NOBREAK_SPACE)
                     charWidth = whitespaceWidth;
             } else {
-                if ((c == '\n') || (c == '\r') || (c == '\t'))
+                if ((c == NEWLINE) || (c == RETURN) || (c == TAB))
                     charWidth = whitespaceWidth;
                 else
                     charWidth = CharUtilities.getCharWidth(c, textInfo.fs);
 
-                isText = false;
-
                 if (prev == WHITESPACE) {
 
                     // if current & previous are WHITESPACE
 
-                    if (textInfo.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE) {
-                        if (CharUtilities.isSpace(c)) { 
-                            spaceWidth += CharUtilities.getCharWidth(c, textInfo.fs);
-                        } else if (c == '\n' || c == '\u2028') {
-                            // force line break 
+                    if (textInfo.whiteSpaceCollapse ==
+                            WhiteSpaceCollapse.FALSE) {
+                        if (CharUtilities.isSpace(c)) {
+                            spaceWidth += CharUtilities.getCharWidth(c,
+                                          textInfo.fs);
+                        } else if (c == NEWLINE || c == LINEBREAK) {
+                            // force line break
                             if (spaceWidth > 0) {
-                                /*InlineSpace is = new InlineSpace(spaceWidth);
-                                addChild(is);*/
+                                Space is = new Space();
+                                is.setWidth(spaceWidth);
+                                parentLM.addChild(is);
                                 spaceWidth = 0;
                             }
-                        } else if (c == '\t') {
+                        } else if (c == TAB) {
                             spaceWidth += 8 * whitespaceWidth;
                         }
-                    } else if (c == '\u2028') {
+                    } else if (c == LINEBREAK) {
                         // Line separator
                         // Breaks line even if WhiteSpaceCollapse = True
                         if (spaceWidth > 0) {
-                            /*InlineSpace is = new InlineSpace(spaceWidth);
-                            is.setUnderlined(textState.getUnderlined());
-                            is.setOverlined(textState.getOverlined());
-                            is.setLineThrough(textState.getLineThrough());
-                            addChild(is);*/
+                            Space is = new Space();
+                            is.setWidth(spaceWidth);
+                            parentLM.addChild(is);
                             spaceWidth = 0;
                         }
                     }
@@ -133,17 +139,9 @@ wordWidth += charWidth;
                     // was some)
 
                     if (spaceWidth > 0) {
-                        /*InlineSpace is = new InlineSpace(spaceWidth);
-                        if (prevUlState) {
-                            is.setUnderlined(textState.getUnderlined());
-                        }
-                        if (prevOlState) {
-                            is.setOverlined(textState.getOverlined());
-                        }
-                        if (prevLTState) {
-                            is.setLineThrough(textState.getLineThrough());
-                        }
-                        addChild(is);*/
+                        Space is = new Space();
+                        is.setWidth(spaceWidth);
+                        parentLM.addChild(is);
                         spaceWidth = 0;
                     }
 
@@ -151,17 +149,18 @@ wordWidth += charWidth;
 
                     if (wordLength > 0) {
                         // The word might contain nonbreaking
-                        // spaces. Split the word and add InlineSpace
+                        // spaces. Split the word and add Space
                         // as necessary. All spaces inside the word
                         // Have a fixed width.
-        Word curWordArea = new Word(); 
-curWordArea.setWidth(wordWidth);
-        curWordArea.setWord(new String(chars, wordStart, wordLength + 1));
-Property prop = new Property();
-prop.propType = Property.FONT_STATE;
-prop.data = textInfo.fs;
-curWordArea.addProperty(prop);
-        parentLM.addChild(curWordArea);
+                        Word curWordArea = new Word();
+                        curWordArea.setWidth(wordWidth);
+                        curWordArea.setWord( new String(chars, wordStart + 1,
+                                                        wordLength));
+                        Property prop = new Property();
+                        prop.propType = Property.FONT_STATE;
+                        prop.data = textInfo.fs;
+                        curWordArea.addProperty(prop);
+                        parentLM.addChild(curWordArea);
 
                         // reset word width
                         wordWidth = 0;
@@ -173,28 +172,32 @@ curWordArea.addProperty(prop);
 
                     spaceWidth = CharUtilities.getCharWidth(c, textInfo.fs);
 
-                    if (textInfo.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE) {
-                        if (c == '\n' || c == '\u2028') {
+                    if (textInfo.whiteSpaceCollapse ==
+                            WhiteSpaceCollapse.FALSE) {
+                        if (c == NEWLINE || c == LINEBREAK) {
                             // force a line break
-                        } else if (c == '\t') {
+                        } else if (c == TAB) {
                             spaceWidth = whitespaceWidth;
                         }
-                    } else if (c == '\u2028') {
+                    } else if (c == LINEBREAK) {
                     }
                 } else {
 
                     // if current is WHITESPACE and no previous
 
-                    if (textInfo.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE) {
+                    if (textInfo.whiteSpaceCollapse ==
+                            WhiteSpaceCollapse.FALSE) {
                         if (CharUtilities.isSpace(c)) {
                             prev = WHITESPACE;
-                            spaceWidth = CharUtilities.getCharWidth(c, textInfo.fs);
-                        } else if (c == '\n') {
+                            spaceWidth = CharUtilities.getCharWidth(c,
+                                                                    textInfo.fs);
+                        } else if (c == NEWLINE) {
                             // force line break
                             // textdecoration not used because spaceWidth is 0
-                            /*InlineSpace is = new InlineSpace(spaceWidth);
-                            addChild(is);*/
-                        } else if (c == '\t') {
+                            Space is = new Space();
+                            is.setWidth(spaceWidth);
+                            parentLM.addChild(is);
+                        } else if (c == TAB) {
                             prev = WHITESPACE;
                             spaceWidth = 8 * whitespaceWidth;
                         }
@@ -204,27 +207,28 @@ curWordArea.addProperty(prop);
                         wordStart++;
                     }
                 }
-                        wordStart = i;
-wordLength = 0;
+                wordStart = i;
+                wordLength = 0;
             }
         } // end of iteration over text
 
         if (wordLength > 0) {
             // The word might contain nonbreaking
-            // spaces. Split the word and add InlineSpace
+            // spaces. Split the word and add Space
             // as necessary. All spaces inside the word
             // Have a fixed width.
-if(wordStart + wordLength > chars.length - 1) {
-wordLength = chars.length - 1 - wordStart;
-}
+            if (wordStart + wordLength > chars.length - 1) {
+                wordLength = chars.length - 1 - wordStart;
+            }
 
             Word curWordArea = new Word();
-curWordArea.setWidth(wordWidth);
-            curWordArea.setWord(new String(chars, wordStart, wordLength + 1));
-Property prop = new Property();
-prop.propType = Property.FONT_STATE;
-prop.data = textInfo.fs;
-curWordArea.addProperty(prop);
+            curWordArea.setWidth(wordWidth);
+            curWordArea.setWord(
+              new String(chars, wordStart + 1, wordLength));
+            Property prop = new Property();
+            prop.propType = Property.FONT_STATE;
+            prop.data = textInfo.fs;
+            curWordArea.addProperty(prop);
             parentLM.addChild(curWordArea);
 
         }
index 0e5c089ac55a969e85b256986fad3880ceb312ca..92ad2a5e88b26965c0a7bd7005f6a53b37057952 100644 (file)
@@ -42,4 +42,7 @@ public abstract class CIDFont extends Font {
         return null;
     }
 
+    public boolean isMultiByte() {
+        return true;
+    }
 }
index d0142ef874664bcf49f75d63f3cc5d08f6f81b07..760200b22bc841368454f56fec8004c92e9c6b89 100644 (file)
@@ -48,7 +48,9 @@ public abstract class Font implements FontMetric {
         return c;
     }
 
-}
-
+    public boolean isMultiByte() {
+        return false;
+    }
 
+}
 
index 45bcee7888f6872ab943b394e2e1a2759e0f0969..a8ba5ad93dc77ccb21140fa863186baaa432171f 100644 (file)
@@ -237,15 +237,9 @@ public class PDFRenderer extends PrintRenderer {
             int size = fs.getFontSize();
 
             // This assumes that *all* CIDFonts use a /ToUnicode mapping
-            boolean useMultiByte = false;
             Font f = (Font)fs.getFontInfo().getFonts().get(name);
-            if (f instanceof LazyFont){
-                if(((LazyFont) f).getRealFont() instanceof CIDFont){
-                    useMultiByte = true;
-                }
-            } else if (f instanceof CIDFont){
-                useMultiByte = true;
-            }
+            boolean useMultiByte = f.isMultiByte();
+
             // String startText = useMultiByte ? "<FEFF" : "(";
             String startText = useMultiByte ? "<" : "(";
             String endText = useMultiByte ? "> " : ") ";
index ae453dbfd3285bf0bd6161eb6b8f64290198ec92..dd55d636e94e71b3da51ae10c382db37195e8f17 100644 (file)
@@ -54,6 +54,10 @@ public class LazyFont extends Font implements FontDescriptor {
         return realFont;
     }
     
+    public boolean isMultiByte() {
+        return realFont.isMultiByte();
+    }
+
     // Font
     public String encoding(){
         load();
index 9cc624fd13be811e7ffb4118fc8eca27553d72bb..a59239dc939df073ee6ff59398080e5bd847a4ff 100644 (file)
@@ -335,12 +335,3 @@ public class MultiByteFont extends CIDFont implements FontDescriptor {
 
 }
 
-
-
-
-
-
-
-
-
-
index dcf007b48c165b64a133db034aa1135ddc65c3c7..3528f8b977af3bca69c63cde5eec484c7fc6cb52 100644 (file)
@@ -490,7 +490,7 @@ class TreeLoader {
                       new FontState(fontInfo, "sans-serif", "normal",
                                     "normal", 12000, 0);
                 } catch (FOPException e) {
-
+                    e.printStackTrace();
                 }
 
                 ch.setWidth(currentFontState.width(ch.getChar()));
@@ -513,7 +513,18 @@ class TreeLoader {
                     list.add(leader);
                 }
             } else if (obj.getNodeName().equals("word")) {
+                try {
+                    currentFontState =
+                      new FontState(fontInfo, "sans-serif", "normal",
+                                    "normal", 12000, 0);
+                } catch (FOPException e) {
+                    e.printStackTrace();
+                }
                 Word word = getWord((Element) obj);
+                Property prop = new Property();
+                prop.propType = Property.FONT_STATE;
+                prop.data = currentFontState;
+                word.addProperty(prop);
                 if (word != null) {
                     list.add(word);
                 }