aboutsummaryrefslogtreecommitdiffstats
path: root/src/org
diff options
context:
space:
mode:
authorarved <arved@unknown>2001-07-08 05:05:16 +0000
committerarved <arved@unknown>2001-07-08 05:05:16 +0000
commita78935e479cfcbc445e4d9c2bb4e85fea4574c06 (patch)
treee8f827796180f950cb57371968efd23b22c506a4 /src/org
parent7e0a43a07a6d5a4ff7970940d0b6fad7e3faabca (diff)
downloadxmlgraphics-fop-a78935e479cfcbc445e4d9c2bb4e85fea4574c06.tar.gz
xmlgraphics-fop-a78935e479cfcbc445e4d9c2bb4e85fea4574c06.zip
Supports page-number formatting
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194336 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/org')
-rw-r--r--src/org/apache/fop/fo/flow/PageNumber.java2
-rw-r--r--src/org/apache/fop/fo/pagination/PageNumberGenerator.java146
-rw-r--r--src/org/apache/fop/fo/pagination/PageSequence.java15
-rw-r--r--src/org/apache/fop/layout/Page.java11
4 files changed, 170 insertions, 4 deletions
diff --git a/src/org/apache/fop/fo/flow/PageNumber.java b/src/org/apache/fop/fo/flow/PageNumber.java
index 8138828ff..7ec9584ef 100644
--- a/src/org/apache/fop/fo/flow/PageNumber.java
+++ b/src/org/apache/fop/fo/flow/PageNumber.java
@@ -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
index 000000000..d00a5c66b
--- /dev/null
+++ b/src/org/apache/fop/fo/pagination/PageNumberGenerator.java
@@ -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();
+ }
+}
+
diff --git a/src/org/apache/fop/fo/pagination/PageSequence.java b/src/org/apache/fop/fo/pagination/PageSequence.java
index 107ab944d..0c56b3b7f 100644
--- a/src/org/apache/fop/fo/pagination/PageSequence.java
+++ b/src/org/apache/fop/fo/pagination/PageSequence.java
@@ -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);
diff --git a/src/org/apache/fop/layout/Page.java b/src/org/apache/fop/layout/Page.java
index eaf3d4e1e..fe1d16332 100644
--- a/src/org/apache/fop/layout/Page.java
+++ b/src/org/apache/fop/layout/Page.java
@@ -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);