[FOray-commit] SF.net SVN: foray:[12485] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2022-01-27 16:52:21
|
Revision: 12485
http://sourceforge.net/p/foray/code/12485
Author: victormote
Date: 2022-01-27 16:52:18 +0000 (Thu, 27 Jan 2022)
Log Message:
-----------
Improvements to rendering of bookmarks in PDF.
Modified Paths:
--------------
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/PdfItem.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutline4a.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineItem4a.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineParent4a.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfRoot.java
trunk/foray/foray-render/src/main/java/org/foray/render/pdf/PdfRenderer.java
Modified: trunk/foray/foray-pdf/src/main/java/org/foray/pdf/PdfItem.java
===================================================================
--- trunk/foray/foray-pdf/src/main/java/org/foray/pdf/PdfItem.java 2022-01-27 13:22:30 UTC (rev 12484)
+++ trunk/foray/foray-pdf/src/main/java/org/foray/pdf/PdfItem.java 2022-01-27 16:52:18 UTC (rev 12485)
@@ -45,20 +45,16 @@
* etc.
*/
public abstract class PdfItem {
+
/** The String that should be used to end a line in the PDF document.
- * Note that this always uses the Unix-style line ending, as it is more
- * compact, and as the PDF generated should be identical, regardless of
- * the platform from which it is generated. */
+ * Note that this always uses the Unix-style line ending, as it is more compact, and as the PDF generated should be
+ * identical, regardless of the platform from which it is generated. */
public static final String EOL = StringUtils.EMPTY_STRING + Basic_Latin_Block.CONTROL_LINE_FEED;
- /**
- * The String that should be used to end a cross-reference line in the
- * PDF document.
- * According to the PDF Reference, 3rd Edition, Section 3.4.3, each
- * cross-reference entry must be exactly 20 bytes long, including the
- * end-of-line character(s).
- * If a 1-character eol is used (such as a newline), then an extra character
- * must be padded.
+ /** The String that should be used to end a cross-reference line in the PDF document.
+ * According to the PDF Reference, 3rd Edition, Section 3.4.3, each cross-reference entry must be exactly 20 bytes
+ * long, including the end-of-line character(s).
+ * If a 1-character eol is used (such as a newline), then an extra character must be padded.
* According to the Reference, that character must be a space. */
public static final String XREF_EOL = StringUtils.SINGLE_SPACE + Basic_Latin_Block.CONTROL_LINE_FEED;
@@ -66,12 +62,6 @@
public static final String MSG_EOL = System.getProperty("line.separator");
/**
- * No-parameter constructor.
- */
- public PdfItem() {
- }
-
- /**
* Converts a StringBuilder buffer to an array of bytes.
* @param buffer The input StringBuilder.
* @return A properly-encoded array of bytes.
@@ -99,8 +89,7 @@
* @param bytes The array to be written.
* @throws PdfException For IO errors during output.
*/
- protected void byteArrayToStream(final byte[] bytes,
- final OutputStream stream) throws PdfException {
+ protected void byteArrayToStream(final byte[] bytes, final OutputStream stream) throws PdfException {
try {
stream.write(bytes);
} catch (final IOException e) {
@@ -108,5 +97,4 @@
}
}
-
}
Modified: trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutline4a.java
===================================================================
--- trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutline4a.java 2022-01-27 13:22:30 UTC (rev 12484)
+++ trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutline4a.java 2022-01-27 16:52:18 UTC (rev 12485)
@@ -48,7 +48,7 @@
@Override
public String toPDF(final PdfDocument4a doc) {
- if (this.getChildren().size() < 1) {
+ if (getChildren().size() < 1) {
return StringUtils.EMPTY_STRING;
}
final StringBuilder result = new StringBuilder();
Modified: trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineItem4a.java
===================================================================
--- trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineItem4a.java 2022-01-27 13:22:30 UTC (rev 12484)
+++ trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineItem4a.java 2022-01-27 16:52:18 UTC (rev 12485)
@@ -32,6 +32,8 @@
*/
package org.foray.pdf.object;
+import org.axsl.pdf.PdfOutlineItem;
+
import java.awt.Color;
import java.util.List;
@@ -39,8 +41,7 @@
* An Outline Item Dictionary in PDF.
*/
-public class PdfOutlineItem4a extends PdfOutlineParent4a
- implements org.axsl.pdf.PdfOutlineItem {
+public class PdfOutlineItem4a extends PdfOutlineParent4a implements PdfOutlineItem {
/** The parent object. */
private PdfOutlineParent4a parent;
@@ -49,14 +50,14 @@
private PdfTextString title;
/**
- * BitUtils 1 of the flags entry. See PDF Reference, 3rd Edition, Table 8.5 and
- * the discussion near it for the meaning.
+ * Bit 1 of the flags entry.
+ * @see "PDF Reference, 3rd Edition, Table 8.5 and the discussion near it."
*/
private boolean italic = false;
/**
- * BitUtils 2 of the flags entry. See PDF Reference, 3rd Edition, Table 8.5 and
- * the discussion near it for the meaning.
+ * Bit 2 of the flags entry.
+ * @see "PDF Reference, 3rd Edition, Table 8.5 and the discussion near it."
*/
private boolean bold = false;
@@ -84,8 +85,7 @@
private PdfAction action;
/**
- * Private constructor to be used only by the public constructors in this
- * class.
+ * Private constructor to be used only by the public constructors in this class.
* @param parent The parent outline item.
* @param title The title or text of the outline item.
* @param color The color of the outline item.
@@ -94,11 +94,9 @@
* @param bold Indciates whether the outline item should be displayed in a
* bold font.
*/
- private PdfOutlineItem4a(final PdfOutlineParent4a parent,
- final String title, final Color color, final boolean italic,
- final boolean bold) {
+ private PdfOutlineItem4a(final PdfOutlineParent4a parent, final String title, final Color color,
+ final boolean italic, final boolean bold) {
this.parent = parent;
- parent.addChild(this);
this.title = new PdfTextString(title);
if (color != null) {
this.color = new PdfColor4a(color);
@@ -111,16 +109,12 @@
* Constructor to access a named destination.
* @param parent The parent outline item.
* @param title The title or text of the outline item.
- * @param destinationString The name of the named destination that should
- * be accessed by this Outline Item.
+ * @param destinationString The name of the named destination that should be accessed by this Outline Item.
* @param color The color of the outline item.
- * @param italic Indicates whether the outline item should be displayed
- * in an italic font.
- * @param bold Indciates whether the outline item should be displayed in a
- * bold font.
+ * @param italic Indicates whether the outline item should be displayed in an italic font.
+ * @param bold Indciates whether the outline item should be displayed in a bold font.
*/
- public PdfOutlineItem4a(final PdfOutlineParent4a parent,
- final String title, final String destinationString,
+ public PdfOutlineItem4a(final PdfOutlineParent4a parent, final String title, final String destinationString,
final Color color, final boolean italic, final boolean bold) {
this(parent, title, color, italic, bold);
this.destinationString = new PdfTextString(destinationString);
@@ -130,16 +124,12 @@
* Constructor to access a named destination.
* @param parent The parent outline item.
* @param title The title or text of the outline item.
- * @param destination The PdfDestination instance that should be accessed
- * by this Outline Item.
+ * @param destination The PdfDestination instance that should be accessed by this Outline Item.
* @param color The color of the outline item.
- * @param italic Indicates whether the outline item should be displayed
- * in an italic font.
- * @param bold Indciates whether the outline item should be displayed in a
- * bold font.
+ * @param italic Indicates whether the outline item should be displayed in an italic font.
+ * @param bold Indciates whether the outline item should be displayed in a bold font.
*/
- public PdfOutlineItem4a(final PdfOutlineParent4a parent,
- final String title, final PdfDestination4a destination,
+ public PdfOutlineItem4a(final PdfOutlineParent4a parent, final String title, final PdfDestination4a destination,
final Color color, final boolean italic, final boolean bold) {
this(parent, title, color, italic, bold);
this.destination = destination;
@@ -149,17 +139,13 @@
* Constructor to access an action.
* @param parent The parent outline item.
* @param title The title or text of the outline item.
- * @param action The PdfAction instance that should be accessed
- * by this Outline Item.
+ * @param action The PdfAction instance that should be accessed by this Outline Item.
* @param color The color of the outline item.
- * @param italic Indicates whether the outline item should be displayed
- * in an italic font.
- * @param bold Indciates whether the outline item should be displayed in a
- * bold font.
+ * @param italic Indicates whether the outline item should be displayed in an italic font.
+ * @param bold Indciates whether the outline item should be displayed in a bold font.
*/
- public PdfOutlineItem4a(final PdfOutlineParent4a parent,
- final String title, final PdfAction action, final Color color,
- final boolean italic, final boolean bold) {
+ public PdfOutlineItem4a(final PdfOutlineParent4a parent, final String title, final PdfAction action,
+ final Color color, final boolean italic, final boolean bold) {
this(parent, title, color, italic, bold);
this.action = action;
}
@@ -180,35 +166,26 @@
/**
* Return the sibling outline item immediately before this one.
- * @return The sibling outline item immediately before this one, or null if
- * there is none.
+ * @return The sibling outline item immediately before this one, or null if there is none.
*/
private PdfOutlineItem4a getPrevious() {
- if (this.parent == null) {
+ final int myIndex = this.parent.getChildren().indexOf(this);
+ if (myIndex < 1) {
return null;
}
- final int siblingIndex = getSiblings().indexOf(this);
- if (siblingIndex < 1 || siblingIndex >= getSiblings().size()) {
- return null;
- }
- return getSiblings().get(siblingIndex - 1);
+ return this.parent.getChildren().get(myIndex - 1);
}
/**
* Return the sibling outline item immediately after this one.
- * @return The sibling outline item immediately after this one, or null if
- * there is none.
+ * @return The sibling outline item immediately after this one, or null if there is none.
*/
private PdfOutlineItem4a getNext() {
- if (this.parent == null) {
+ final int myIndex = this.parent.getChildren().indexOf(this);
+ if (myIndex >= getSiblings().size() - 1) {
return null;
}
- final int siblingIndex = getSiblings().indexOf(this);
- if (siblingIndex < 0
- || siblingIndex >= getSiblings().size() - 1) {
- return null;
- }
- return getSiblings().get(siblingIndex + 1);
+ return this.parent.getChildren().get(myIndex + 1);
}
@Override
@@ -254,11 +231,13 @@
result.append(" /First " + getFirst().pdfReference(doc) + EOL);
result.append(" /Last " + getLast().pdfReference(doc) + EOL);
}
- if (getPrevious() != null) {
- result.append(" /Prev " + getPrevious().pdfReference(doc) + EOL);
+ final PdfOutlineItem4a previous = getPrevious();
+ if (previous != null) {
+ result.append(" /Prev " + previous.pdfReference(doc) + EOL);
}
- if (getNext() != null) {
- result.append(" /Next " + getNext().pdfReference(doc) + EOL);
+ final PdfOutlineItem4a next = getNext();
+ if (next != null) {
+ result.append(" /Next " + next.pdfReference(doc) + EOL);
}
if (countDescendants > 0) {
result.append(" /Count -" + countDescendants + EOL);
Modified: trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineParent4a.java
===================================================================
--- trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineParent4a.java 2022-01-27 13:22:30 UTC (rev 12484)
+++ trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfOutlineParent4a.java 2022-01-27 16:52:18 UTC (rev 12485)
@@ -28,6 +28,8 @@
package org.foray.pdf.object;
+import org.axsl.pdf.PdfOutlineParent;
+
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
@@ -37,21 +39,12 @@
* object. This includes the classes PdfOutline and PdfOutlineItem4a.
*/
-public abstract class PdfOutlineParent4a extends PdfObject
- implements org.axsl.pdf.PdfOutlineParent {
+public abstract class PdfOutlineParent4a extends PdfObject implements PdfOutlineParent {
/** Collection of child PdfOutline objects. */
private List<PdfOutlineItem4a> children = new ArrayList<PdfOutlineItem4a>();
/**
- * Add a child to this parent.
- * @param child The child to be added.
- */
- public void addChild(final PdfOutlineItem4a child) {
- this.children.add(child);
- }
-
- /**
* Return the children of this parent.
* @return The children of this parent.
*/
@@ -90,10 +83,12 @@
protected abstract int count();
@Override
- public PdfOutlineItem4a createPdfOutlineItem(
- final String titleText, final String internalDestination,
+ public PdfOutlineItem4a createPdfOutlineItem(final String titleText, final String internalDestination,
final Color color, final boolean italic, final boolean bold) {
- return new PdfOutlineItem4a(this, titleText, internalDestination, color, italic, bold);
+ final PdfOutlineItem4a newItem =
+ new PdfOutlineItem4a(this, titleText, internalDestination, color, italic, bold);
+ this.children.add(newItem);
+ return newItem;
}
}
Modified: trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfRoot.java
===================================================================
--- trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfRoot.java 2022-01-27 13:22:30 UTC (rev 12484)
+++ trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfRoot.java 2022-01-27 16:52:18 UTC (rev 12485)
@@ -131,6 +131,7 @@
public PdfOutline4a getOutlineRoot() {
if (this.outline == null) {
this.outline = new PdfOutline4a();
+ this.document.registerIndirectObject(this.outline);
}
return this.outline;
}
Modified: trunk/foray/foray-render/src/main/java/org/foray/render/pdf/PdfRenderer.java
===================================================================
--- trunk/foray/foray-render/src/main/java/org/foray/render/pdf/PdfRenderer.java 2022-01-27 13:22:30 UTC (rev 12484)
+++ trunk/foray/foray-render/src/main/java/org/foray/render/pdf/PdfRenderer.java 2022-01-27 16:52:18 UTC (rev 12485)
@@ -647,39 +647,37 @@
}
@Override
- public void render(final BookmarkTreeG5 area) {
- for (int i = 0; i < area.qtyAreaNodeChildren(); i++) {
- final BookmarkG5 child = area.areaNodeChildAt(i);
- renderBookmark(child, this.pdfDoc.getOutlineRoot());
+ public void render(final BookmarkTreeG5 bookmarkTree) {
+ for (int i = 0; i < bookmarkTree.qtyAreaNodeChildren(); i++) {
+ final BookmarkG5 bookmark = bookmarkTree.areaNodeChildAt(i);
+ renderBookmark(bookmark, this.pdfDoc.getOutlineRoot());
}
}
/**
* Render a bookmark, using recursion to render child bookmarks as well.
- * @param area The bookmark area to be rendered.
- * @param parent The PDF outline object to which the rendered bookmarks
- * should be added.
+ * @param bookmark The bookmark area to be rendered.
+ * @param parent The PDF outline object to which the rendered bookmarks should be added.
*/
- public void renderBookmark(final BookmarkG5 area, final PdfOutlineParent parent) {
- if (area == null || parent == null) {
+ public void renderBookmark(final BookmarkG5 bookmark, final PdfOutlineParent parent) {
+ if (bookmark == null || parent == null) {
return;
}
- final Color color = area.traitColor();
+ final Color color = bookmark.traitColor();
boolean italic = false;
- if (area.traitFontStyle() == Font.Style.ITALIC) {
+ if (bookmark.traitFontStyle() == Font.Style.ITALIC) {
italic = true;
}
boolean bold = false;
- if (area.traitFontWeight().getNumericWeight()
- >= Font.Weight.BOLD.getNumericWeight()) {
+ if (bookmark.traitFontWeight().getNumericWeight() >= Font.Weight.BOLD.getNumericWeight()) {
bold = true;
}
PdfOutlineItem pdfOutline = null;
- pdfOutline = parent.createPdfOutlineItem(area.getTitleText(),
- area.getInternalDestination(), color, italic, bold);
+ pdfOutline = parent.createPdfOutlineItem(bookmark.getTitleText(), bookmark.getInternalDestination(), color,
+ italic, bold);
// Recursively handle child bookmarks
- for (int i = 0; i < area.qtyAreaNodeChildren(); i++) {
- final BookmarkG5 child = area.areaNodeChildAt(i);
+ for (int i = 0; i < bookmark.qtyAreaNodeChildren(); i++) {
+ final BookmarkG5 child = bookmark.areaNodeChildAt(i);
renderBookmark(child, pdfOutline);
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|