]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Supports page-number formatting
authorarved <arved@unknown>
Sun, 8 Jul 2001 05:05:16 +0000 (05:05 +0000)
committerarved <arved@unknown>
Sun, 8 Jul 2001 05:05:16 +0000 (05:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194336 13f79535-47bb-0310-9956-ffa450edef68

src/org/apache/fop/fo/flow/PageNumber.java
src/org/apache/fop/fo/pagination/PageNumberGenerator.java [new file with mode: 0644]
src/org/apache/fop/fo/pagination/PageSequence.java
src/org/apache/fop/layout/Page.java

index 8138828ff7ed485473c31bdba749fd4dc8c421f4..7ec9584efac528865ed755289aedfc0bd17ba7eb 100644 (file)
@@ -110,7 +110,7 @@ public class PageNumber extends FObj {
                                                area.getIDReferences().initializeID(id, area);
                                }
 
-                               String p = Integer.toString(area.getPage().getNumber());
+                               String p = area.getPage().getFormattedNumber();
                                this.marker = FOText.addText((BlockArea) area, propMgr.getFontState(area.getFontInfo()), red, green, blue,
                                                                                        wrapOption, null, whiteSpaceCollapse, p.toCharArray(),
                                                                                        0, p.length(), ts, VerticalAlign.BASELINE);
diff --git a/src/org/apache/fop/fo/pagination/PageNumberGenerator.java b/src/org/apache/fop/fo/pagination/PageNumberGenerator.java
new file mode 100644 (file)
index 0000000..d00a5c6
--- /dev/null
@@ -0,0 +1,146 @@
+/*-- $Id$ --
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.fo.pagination;
+
+import org.apache.fop.fo.properties.*;
+import org.apache.fop.messaging.MessageHandler;
+
+// Java
+import java.util.*;
+
+/**
+ * This class uses the 'format', 'groupingSeparator', 'groupingSize',
+ * and 'letterValue' properties on fo:page-sequence to return a String
+ * corresponding to the supplied integer page number.
+ */
+public class PageNumberGenerator {
+
+       private String format;
+       private char groupingSeparator;
+       private int groupingSize;
+       private int letterValue;
+
+       //constants
+       private int DECIMAL = 1;        // '0*1'
+       private int LOWERALPHA = 2;     // 'a'
+       private int UPPERALPHA = 3;     // 'A'
+       private int LOWERROMAN = 4;     // 'i'
+       private int UPPERROMAN = 5;     // 'I'
+       
+       // flags
+       private int formatType = DECIMAL;
+       private int minPadding = 0;     // for decimal formats
+       
+       // preloaded strings of zeros
+       private String zeros[] = { "", "0", "00", "000", "0000", "00000" };
+       
+       public PageNumberGenerator( String format,
+       char groupingSeparator, int groupingSize, int letterValue ) {
+               this.format = format;
+               this.groupingSeparator = groupingSeparator;
+               this.groupingSize = groupingSize;
+               this.letterValue = letterValue;
+               
+               // the only accepted format strings are currently '0*1' 'a', 'A', 'i'
+               // and 'I'
+               int fmtLen = format.length();
+               if (fmtLen == 1) {
+                       if (format.equals("1")) {
+                               formatType = DECIMAL;
+                               minPadding = 0;
+                       } else if (format.equals("a")) {
+                               formatType = LOWERALPHA;
+                       } else if (format.equals("A")) {
+                               formatType = UPPERALPHA;
+                       } else if (format.equals("i")) {
+                               formatType = LOWERROMAN;
+                       } else if (format.equals("I")) {
+                               formatType = UPPERROMAN;
+                       } else {
+                               // token not handled
+                               MessageHandler.log("'format' token not recognized; using '1'");
+                               formatType = DECIMAL;
+                               minPadding = 0;                         
+                       }
+               }
+               else {
+                       // only accepted token is '0+1'at this stage.    Because of the
+                       // wonderful regular expression support in Java, we will resort to a
+                       // loop
+                       for (int i = 0; i < fmtLen-1; i++) {
+                               if (format.charAt(i) != '0') {
+                                       MessageHandler.log("'format' token not recognized; using '1'");
+                                       formatType = DECIMAL;
+                                       minPadding = 0;                         
+                               } else {
+                                       minPadding = fmtLen - 1;
+                               }
+                       }
+               }
+       }
+       
+       public String makeFormattedPageNumber(int number) {
+               String pn = null;
+               if (formatType == DECIMAL) {
+                       pn = Integer.toString(number);
+                       if (minPadding >= pn.length()) {
+                               int nz = minPadding - pn.length() + 1;
+                               pn = zeros[nz] + pn;
+                       }
+               } else if ((formatType == LOWERROMAN) || (formatType == UPPERROMAN)) {
+                       pn = makeRoman(number);
+                       if (formatType == UPPERROMAN)
+                               pn = pn.toUpperCase();
+               } else {
+                       // alphabetic
+                       pn = makeAlpha(number);
+                       if (formatType == UPPERALPHA)
+                               pn = pn.toUpperCase();
+               }
+               return pn;
+       }
+       
+       private String makeRoman( int num ) {
+       int arabic[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
+               String roman[] = { "m", "cm", "d", "cd", "c", "xc", "l", "xl", "x",
+               "ix", "v", "iv", "i" };
+
+       int i = 0;
+               StringBuffer romanNumber = new StringBuffer();
+               
+               while (num > 0) {
+               while (num >= arabic[i]) {
+                               num = num - arabic[i];
+                               romanNumber.append( roman[i] );
+                       }
+                       i = i + 1;
+               }
+               return romanNumber.toString();
+       }
+       
+       private String makeAlpha( int num ) {
+               String letters = "abcdefghijklmnopqrstuvwxyz";
+               StringBuffer alphaNumber = new StringBuffer();
+               
+               int base = 26;
+               int rem = 0;
+               
+               num--;
+               if (num < base) {
+                       alphaNumber.append(letters.charAt(num));
+               } else {
+                       while (num >= base) {
+                               rem = num % base;
+                               alphaNumber.append(letters.charAt(rem));
+                               num = num / base;
+                       }
+                       alphaNumber.append(letters.charAt(num-1));
+               }
+               return alphaNumber.reverse().toString();
+       }
+}
+
index 107ab944de8418a2831bbb7ffaf65cecead8237a..0c56b3b7f8fa6cfdb9f3e933c525547375ef7b1a 100644 (file)
@@ -83,8 +83,10 @@ public class PageSequence extends FObj {
 
     private Page currentPage;
 
+       // page number and elated formatting variables
     private int currentPageNumber = 0;
-
+       private PageNumberGenerator pageNumberGenerator;
+       
     /** specifies page numbering type (auto|auto-even|auto-odd|explicit) */
     private int pageNumberType;
 
@@ -145,7 +147,13 @@ public class PageSequence extends FObj {
 
         masterName = this.properties.get("master-name").getString();
 
-
+               // get the 'format' properties
+               this.pageNumberGenerator =
+               new PageNumberGenerator( this.properties.get("format").getString(),
+                       this.properties.get("grouping-separator").getCharacter(),
+                       this.properties.get("grouping-size").getNumber().intValue(),
+                       this.properties.get("letter-value").getEnum()
+               );
     }
 
     public void addFlow(Flow flow) throws FOPException {
@@ -219,6 +227,9 @@ public class PageSequence extends FObj {
                                    tempIsFirstPage, isEmptyPage);
 
             currentPage.setNumber(this.currentPageNumber);
+                       String formattedPageNumber =
+                               pageNumberGenerator.makeFormattedPageNumber(this.currentPageNumber);
+                       currentPage.setFormattedNumber(formattedPageNumber);
             this.root.setRunningPageNumberCounter(this.currentPageNumber);
 
             MessageHandler.log(" [" + currentPageNumber);
index eaf3d4e1e269662f320d02183ae230c5a3f903f7..fe1d163326ee8321dba1cb1828fb01bc39896e1f 100644 (file)
@@ -30,7 +30,8 @@ public class Page {
     private AreaTree areaTree;
 
     protected int pageNumber = 0;
-
+       protected String formattedPageNumber;
+       
     protected Vector linkSets = new Vector();
 
     private Vector idList = new Vector();
@@ -51,6 +52,14 @@ public class Page {
        return this.pageNumber;
     }
 
+       public void setFormattedNumber(String number) {
+       this.formattedPageNumber = number;
+       }
+       
+    public String getFormattedNumber() {
+       return this.formattedPageNumber;
+    }
+
     void addAfter(AreaContainer area) {
        this.after = area;
        area.setPage(this);