[FOray-commit] SF.net SVN: foray:[10733] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2009-04-21 17:28:15
|
Revision: 10733
http://foray.svn.sourceforge.net/foray/?rev=10733&view=rev
Author: victormote
Date: 2009-04-21 17:28:06 +0000 (Tue, 21 Apr 2009)
Log Message:
-----------
Conform to aXSL changes which effectively move the responsibility for writing text into a PDF from the PDF system to the Font system.
Modified Paths:
--------------
trunk/foray/foray-font/src/java/org/foray/font/FontUse4a.java
trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java
trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java
trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFString.java
Modified: trunk/foray/foray-font/src/java/org/foray/font/FontUse4a.java
===================================================================
--- trunk/foray/foray-font/src/java/org/foray/font/FontUse4a.java 2009-04-21 15:20:36 UTC (rev 10732)
+++ trunk/foray/foray-font/src/java/org/foray/font/FontUse4a.java 2009-04-21 17:28:06 UTC (rev 10733)
@@ -41,6 +41,7 @@
import org.apache.commons.logging.Log;
+import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
@@ -61,6 +62,10 @@
*/
public class FontUse4a implements org.axsl.font.FontUse {
+ /** Constant indicating the number of chars needed to depict one 16-bit
+ * Unicode character using the PDF ASCII Hex notation. */
+ private static final int UNICODE_STRING_SIZE = 4;
+
/** The parent ConsumerFont. */
private ConsumerFont4a consumerFont;
@@ -494,4 +499,162 @@
return true;
}
+ /**
+ * Converts a String to the content that should be written into a PDF to
+ * show that String.
+ * @param theString The input String, that is, the text that needs to be
+ * shown in the PDF.
+ * @param kerningActive Indicates whether kerning should be applied when
+ * writing this text.
+ * @return The String that should actually be written into the PDF to show
+ * the input text.
+ */
+ public String textToPdf(final CharSequence theString, final boolean kerningActive) {
+ final Font font = this.getFont();
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("[");
+ buffer.append(startTextDelimiter(font));
+ for (int i = 0; i < theString.length(); i++) {
+ final int codePoint = Character.codePointAt(theString, i);
+ if (Character.isSupplementaryCodePoint(codePoint)) {
+ i++;
+ }
+ if (codePoint == '\u200B') {
+ /* Zero-width space. Until we learn better, we just omit it
+ * from the output. */
+ continue;
+ }
+ final int ch = encodeCharacter(codePoint);
+ addCharToBuffer(buffer, ch, font);
+ if (i + 1 < theString.length()
+ && kerningActive) {
+ addKerning(font, theString.charAt(i), theString.charAt(i + 1),
+ buffer);
+ }
+ }
+ buffer.append(endTextDelimiter(font));
+ buffer.append("]TJ" + WKConstants.LINEFEED_STRING);
+ return buffer.toString();
+ }
+
+ /**
+ * Add a character to the buffer.
+ * @param buffer The buffer to which the character should be added.
+ * @param glyphIndex The glyph index to be added.
+ * @param font The font being used.
+ */
+ private void addCharToBuffer(final StringBuilder buffer,
+ final int glyphIndex, final Font font) {
+ if (font.getFontComplexity() == Font.Complexity.COMPOSITE) {
+ buffer.append(getUnicodeString(glyphIndex));
+ return;
+ }
+ /* Font is a simple font. */
+ if (glyphIndex > WKConstants.MAX_7_BIT_UNSIGNED_VALUES - 1) {
+ buffer.append("\\");
+ buffer.append(Integer.toOctalString(glyphIndex));
+ return;
+ }
+ int glyphIndexToUse = glyphIndex;
+ if (glyphIndexToUse < 0) {
+ glyphIndexToUse = ' ';
+ }
+ final char c = (char) glyphIndexToUse;
+ switch (c) {
+ case '(':
+ case ')':
+ case '\\': {
+ buffer.append("\\");
+ break;
+ }
+ }
+ buffer.append(c);
+ }
+
+ /**
+ * Computes how much kerning is needed, and, if any, writes it into the
+ * output.
+ * @param font The Font containing the kerning information to be used.
+ * @param char1 The Unicode code point of the first (left in a left-to-right
+ * system) character.
+ * @param char2 The Unicode code point of the second (right in left-to-right
+ * system) character.
+ * @param buf The StringBuffer to which any kerning information should be
+ * written.
+ */
+ private void addKerning(final Font font, final char char1,
+ final char char2, final StringBuilder buf) {
+ /* Note that kerning is written into a PDF file in units of 1/1000 of a text space unit,
+ * which is exactly what the font system returns. Therefore no consideration needs to be
+ * given here to the size of the font. */
+ final int kernAmount = font.kern(char1, char2);
+ if (kernAmount == 0) {
+ return;
+ }
+ buf.append(endTextDelimiter(font));
+ buf.append(- kernAmount);
+ buf.append(startTextDelimiter(font));
+ }
+
+ /**
+ * Returns the char that denotes the start of text.
+ * @param font The font being used.
+ * @return The char that denotes the start of text.
+ */
+ private char startTextDelimiter(final Font font) {
+ if (font.getFontComplexity() == Font.Complexity.COMPOSITE) {
+ return '<';
+ }
+ return '(';
+ }
+
+ /**
+ * Returns the char that denotes the end of text.
+ * @param font The font being used.
+ * @return The char that denotes the end of text.
+ */
+ private char endTextDelimiter(final Font font) {
+ if (font.getFontComplexity() == Font.Complexity.COMPOSITE) {
+ return '>';
+ }
+ return ')';
+ }
+
+ /**
+ * Convert a char to a multibyte hex representation.
+ * @param c The character to be converted.
+ * @return The string representation of the character.
+ */
+ public String getUnicodeString(final int c) {
+ StringBuilder buf = new StringBuilder(FontUse4a.UNICODE_STRING_SIZE);
+ byte[] uniBytes = null;
+ int codePoint = c;
+ /* TODO: Handle Unicode characters > 0xFFFF better. */
+ if (codePoint < Character.MIN_VALUE
+ || codePoint > Character.MAX_VALUE) {
+ codePoint = ' ';
+ }
+ final char[] a = {(char) codePoint};
+ try {
+ uniBytes = new String(a).getBytes("UnicodeBigUnmarked");
+ } catch (final UnsupportedEncodingException e) {
+ uniBytes = new String(a).getBytes();
+ }
+
+ for (int i = 0; i < uniBytes.length; i++) {
+ int b = uniBytes[i];
+ if (uniBytes[i] < 0) {
+ b += WKConstants.MAX_8_BIT_UNSIGNED_VALUES;
+ }
+
+ final String hexString = Integer.toHexString(b);
+ if (hexString.length() == 1) {
+ buf = buf.append("0" + hexString);
+ } else {
+ buf = buf.append(hexString);
+ }
+ }
+ return buf.toString();
+ }
+
}
Modified: trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java
===================================================================
--- trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java 2009-04-21 15:20:36 UTC (rev 10732)
+++ trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java 2009-04-21 17:28:06 UTC (rev 10733)
@@ -39,7 +39,6 @@
import org.foray.common.WKConstants;
import org.foray.common.ps.PsColor;
import org.foray.common.ps.PsUtil;
-import org.foray.pdf.object.PDFString;
import org.axsl.font.FontConsumer;
import org.axsl.font.FontException;
@@ -720,7 +719,7 @@
this.write(matrixString + "cm");
this.write("1 0 0 -1 0 0 Tm ");
- final String outputString = PDFString.textToPdf(s, font.getFontUse(), true);
+ final String outputString = font.getFontUse().textToPdf(s, true);
this.write(outputString);
this.write("ET");
Modified: trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java
===================================================================
--- trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java 2009-04-21 15:20:36 UTC (rev 10732)
+++ trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java 2009-04-21 17:28:06 UTC (rev 10733)
@@ -119,7 +119,7 @@
return;
}
openTextObject();
- final String stringOut = PDFString.textToPdf(text, this.getGS().getFont().getFont().getFontUse(), kern);
+ final String stringOut = this.getGS().getFont().getFont().getFontUse().textToPdf(text, kern);
add(stringOut);
}
Modified: trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFString.java
===================================================================
--- trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFString.java 2009-04-21 15:20:36 UTC (rev 10732)
+++ trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFString.java 2009-04-21 17:28:06 UTC (rev 10733)
@@ -28,13 +28,8 @@
package org.foray.pdf.object;
-import org.foray.common.WKConstants;
-
-import org.axsl.font.Font;
import org.axsl.font.FontUse;
-import java.io.UnsupportedEncodingException;
-
/**
* A PDF String Object, as defined in PDF Reference, 2nd edition, Section 3.2.3.
* PDF string objects are used in content streams, and are specifically
@@ -44,10 +39,6 @@
*/
public class PDFString extends PDFObject {
- /** Constant indicating the number of chars needed to depict one 16-bit
- * Unicode character using the PDF ASCII Hex notation. */
- private static final int UNICODE_STRING_SIZE = 4;
-
/** The encapsulated String. */
private String theString;
@@ -77,170 +68,10 @@
* {@inheritDoc}
*/
public String toPDF() {
- return textToPdf(this.theString, this.font, this.kerningActive);
+ return this.font.textToPdf(this.theString, this.kerningActive);
}
/**
- * Converts a String to the content that should be written into a PDF to
- * show that String.
- * @param theString The input String, that is, the text that needs to be
- * shown in the PDF.
- * @param fontUse The font used to show the text.
- * @param kerningActive Indicates whether kerning should be applied when
- * writing this text.
- * @return The String that should actually be written into the PDF to show
- * the input text.
- */
- public static String textToPdf(final CharSequence theString,
- final FontUse fontUse, final boolean kerningActive) {
- final Font font = fontUse.getFont();
- final StringBuilder buffer = new StringBuilder();
- buffer.append("[");
- buffer.append(startTextDelimiter(font));
- for (int i = 0; i < theString.length(); i++) {
- final int codePoint = Character.codePointAt(theString, i);
- if (Character.isSupplementaryCodePoint(codePoint)) {
- i++;
- }
- if (codePoint == '\u200B') {
- /* Zero-width space. Until we learn better, we just omit it
- * from the output. */
- continue;
- }
- final int ch = fontUse.encodeCharacter(codePoint);
- addCharToBuffer(buffer, ch, font);
- if (i + 1 < theString.length()
- && kerningActive) {
- addKerning(font, theString.charAt(i), theString.charAt(i + 1),
- buffer);
- }
- }
- buffer.append(endTextDelimiter(font));
- buffer.append("]TJ" + EOL);
- return buffer.toString();
- }
-
- /**
- * Add a character to the buffer.
- * @param buffer The buffer to which the character should be added.
- * @param glyphIndex The glyph index to be added.
- * @param font The font being used.
- */
- private static void addCharToBuffer(final StringBuilder buffer,
- final int glyphIndex, final Font font) {
- if (font.getFontComplexity() == Font.Complexity.COMPOSITE) {
- buffer.append(PDFString.getUnicodeString(glyphIndex));
- return;
- }
- /* Font is a simple font. */
- if (glyphIndex > WKConstants.MAX_7_BIT_UNSIGNED_VALUES - 1) {
- buffer.append("\\");
- buffer.append(Integer.toOctalString(glyphIndex));
- return;
- }
- int glyphIndexToUse = glyphIndex;
- if (glyphIndexToUse < 0) {
- glyphIndexToUse = ' ';
- }
- final char c = (char) glyphIndexToUse;
- switch (c) {
- case '(':
- case ')':
- case '\\': {
- buffer.append("\\");
- break;
- }
- }
- buffer.append(c);
- }
-
- /**
- * Computes how much kerning is needed, and, if any, writes it into the
- * output.
- * @param font The Font containing the kerning information to be used.
- * @param char1 The Unicode code point of the first (left in a left-to-right
- * system) character.
- * @param char2 The Unicode code point of the second (right in left-to-right
- * system) character.
- * @param buf The StringBuffer to which any kerning information should be
- * written.
- */
- private static void addKerning(final Font font, final char char1,
- final char char2, final StringBuilder buf) {
- /* Note that kerning is written into a PDF file in units of 1/1000 of a text space unit,
- * which is exactly what the font system returns. Therefore no consideration needs to be
- * given here to the size of the font. */
- final int kernAmount = font.kern(char1, char2);
- if (kernAmount == 0) {
- return;
- }
- buf.append(endTextDelimiter(font));
- buf.append(- kernAmount);
- buf.append(startTextDelimiter(font));
- }
-
- /**
- * Returns the char that denotes the start of text.
- * @param font The font being used.
- * @return The char that denotes the start of text.
- */
- private static char startTextDelimiter(final Font font) {
- if (font.getFontComplexity() == Font.Complexity.COMPOSITE) {
- return '<';
- }
- return '(';
- }
-
- /**
- * Returns the char that denotes the end of text.
- * @param font The font being used.
- * @return The char that denotes the end of text.
- */
- private static char endTextDelimiter(final Font font) {
- if (font.getFontComplexity() == Font.Complexity.COMPOSITE) {
- return '>';
- }
- return ')';
- }
-
- /**
- * Convert a char to a multibyte hex representation.
- * @param c The character to be converted.
- * @return The string representation of the character.
- */
- public static String getUnicodeString(final int c) {
- StringBuilder buf = new StringBuilder(PDFString.UNICODE_STRING_SIZE);
- byte[] uniBytes = null;
- int codePoint = c;
- /* TODO: Handle Unicode characters > 0xFFFF better. */
- if (codePoint < Character.MIN_VALUE
- || codePoint > Character.MAX_VALUE) {
- codePoint = ' ';
- }
- final char[] a = {(char) codePoint};
- try {
- uniBytes = new String(a).getBytes("UnicodeBigUnmarked");
- } catch (final UnsupportedEncodingException e) {
- uniBytes = new String(a).getBytes();
- }
-
- for (int i = 0; i < uniBytes.length; i++) {
- int b = uniBytes[i];
- if (uniBytes[i] < 0) {
- b += WKConstants.MAX_8_BIT_UNSIGNED_VALUES;
- }
-
- final String hexString = Integer.toHexString(b);
- if (hexString.length() == 1) {
- buf = buf.append("0" + hexString);
- } else {
- buf = buf.append(hexString);
- }
- }
- return buf.toString();
- }
-
- /**
* Indicates whether this string has any content or not.
* @return True iff this string has no content.
*/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|