[FOray-commit] SF.net SVN: foray:[10857] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2009-05-01 21:54:39
|
Revision: 10857
http://foray.svn.sourceforge.net/foray/?rev=10857&view=rev
Author: victormote
Date: 2009-05-01 21:54:37 +0000 (Fri, 01 May 2009)
Log Message:
-----------
Move EncodingVector4a from FOrayPs to FOrayCommon, so that it can be used by other packages.
Modified Paths:
--------------
trunk/foray/foray-ps/src/java/org/foray/ps/PsServer4a.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCE.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCustom.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpert.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpertSubset.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingFOrayLatinExtra.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingISOLatin1.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacExpert.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacRoman.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacStandard.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingPDFDoc.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingStandard.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingSymbol.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingWinAnsi.java
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingZapfDingbats.java
Added Paths:
-----------
trunk/foray/foray-common/src/java/org/foray/common/ps/EncodingVector4a.java
Removed Paths:
-------------
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingVector4a.java
Copied: trunk/foray/foray-common/src/java/org/foray/common/ps/EncodingVector4a.java (from rev 10856, trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingVector4a.java)
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/ps/EncodingVector4a.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/ps/EncodingVector4a.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -0,0 +1,473 @@
+/*
+ * Copyright 2004 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/*
+ * $LastChangedRevision$
+ * $LastChangedDate$
+ * $LastChangedBy$
+ */
+
+package org.foray.common.ps;
+
+import org.axsl.ps.Encoding;
+import org.axsl.ps.EncodingVector;
+import org.axsl.ps.GlyphList;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * <p>Encoding instances provide some standard encoding resources for
+ * PostScript.
+ * When a PostScript file is parsed, the characters in a string are really
+ * indices into an array of glyph names, which are in turn the keys to entries
+ * in a glyph dictionary.
+ * Input to a PostScript interpreter is not really characters, but rather
+ * character indexes.
+ * (The two might be the same, especially for ASCII characters).</p>
+ *
+ * <p>So the PostScript interpreter uses a two-step process, as follows:
+ * 1. It conceptually maps character codes (integer values) to name objects
+ * like /A or /Uacute. This map is known as an "encoding".
+ * There are several standard encoding schemes defined in the PostScript
+ * standard that each interpreter must know how to handle. Those are provided
+ * as Encoding subclasses.
+ * 2. The name objects created in step 1 are then used as the key into a
+ * dictionary (contained in the Font dictionary) whose values contain the
+ * PostScript instructions needed to actually paint the glyph.</p>
+ *
+ * <p>Nowhere in the above scheme is Unicode encoding contemplated or supported.
+ * Applications that <em>create </em> PostScript output from Unicode input need
+ * a way to convert from that Unicode input to the indexes that are expected
+ * for by the selected encoding.
+ * The closest resource that we have for this purpose is the standard glyph
+ * lists supplied by Adobe.</p>
+ *
+ * <p>The canonical method for mapping a Unicode code point to a PostScript
+ * encoding index would be:</p>
+ * <ol>
+ * <li>Use the Adobe Glyph List (or another glyph list) to obtain the glyph name for a given Unicode
+ * code point.</li>
+ * <li>Use an Encoding instance to obtain the index in that Encoding for the
+ * glyph name obtained above.</li>
+ * </ol>
+ *
+ * <p>However, for efficiency, an Encoding combines the above two steps and
+ * provides a direct mapping from a Unicode code point to the encoded index.
+ * Two parallel arrays are used to make this process efficient.
+ * The first is a an array of all Unicode code points supported in the Encoding.
+ * This array is sorted for fast searching.
+ * The second array provides the encoded index for the Unicode code point in
+ * the first array.
+ * Methods are provided to encode and decode characters.</p>
+ */
+public abstract class EncodingVector4a implements EncodingVector, Serializable {
+
+ /** The name of the undefined glyph. */
+ public static final String NOTDEF = ".notdef";
+
+ /** Constant needed for serialization. */
+ private static final long serialVersionUID = 6967489975814026068L;
+
+ /** The name of this encoding. */
+ private transient String name;
+
+ /** The GlyphList instances, in order, which were used to create this encoding. */
+ private transient List<GlyphList> sourceGlyphLists;
+
+ /** Array of Unicode code points, sorted. Elements in this array are keys
+ * to elements in {@link #codePointIndexes}. */
+ private transient char[] codePoints;
+
+ /** Array of encoded indexes for the glyphs that correspond to elements
+ * in {@link #codePoints}. */
+ private transient char[] codePointIndexes;
+
+ /**
+ * Create a new Encoding instance.
+ * @param name The name of this encoding.
+ * This name is not used by the system, but may be useful for debugging.
+ * @param sourceGlyphLists The GlyphList instances that were used to create this encoding.
+ * @param codePoints The array of Unicode code points supported by this encoding.
+ * @param codePointIndexes The array of encoded indexes that is parallel to codePoints.
+ * codePointIndexes[n] should contain the encoded index that corresponds to the Unicode code point at codePoints[n].
+ */
+ public EncodingVector4a(final String name, final List<GlyphList> sourceGlyphLists,
+ final char[] codePoints, final char[] codePointIndexes) {
+ this.name = name;
+ this.sourceGlyphLists = sourceGlyphLists;
+ this.codePoints = codePoints;
+ this.codePointIndexes = codePointIndexes;
+ sortCodePoints(codePoints, codePointIndexes);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int encodeCharacter(final int codePoint) {
+ if (this.codePoints == null
+ || this.codePointIndexes == null) {
+ return 0;
+ }
+ final int index = Arrays.binarySearch(this.codePoints,
+ (char) codePoint);
+ if (index < 0) {
+ return 0;
+ }
+ return this.codePointIndexes[index];
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int decodeCharacter(final int encodedIndex) {
+ /* This is not expected to be used much or at all, so the
+ * codePointIndexes array has not been copied and sorted and
+ * referenced.*/
+ if (this.codePoints == null
+ || this.codePointIndexes == null) {
+ return INVALID_UNICODE_CHAR;
+ }
+ for (int i = 0; i < this.codePointIndexes.length; i++) {
+ if (this.codePointIndexes[i] == encodedIndex) {
+ return this.codePoints[i];
+ }
+ }
+ return INVALID_UNICODE_CHAR;
+ }
+
+ /**
+ * Returns the number of entries in this encoding.
+ * @return The number of entries in this encoding.
+ */
+ public int size() {
+ return this.codePoints.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String[] getGlyphNames() {
+ final int listSize = getLastIndex() + 1;
+ final String[] glyphList = new String[listSize];
+ // Initialize all items to ".notdef".
+ for (int i = 0; i < glyphList.length; i++) {
+ glyphList[i] = EncodingVector4a.NOTDEF;
+ }
+ for (int i = 0; i < this.codePoints.length; i++) {
+ final char codePoint = this.codePoints[i];
+ final String glyphName = mapCodePointToGlyphName(this.sourceGlyphLists, codePoint);
+ final int encodingIndex = this.codePointIndexes[i];
+ glyphList[encodingIndex] = glyphName;
+ }
+ return glyphList;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getFirstIndex() {
+ char firstIndex = Character.MAX_VALUE;
+ for (int i = 0; i < this.codePointIndexes.length; i++) {
+ if (this.codePointIndexes[i] < firstIndex) {
+ firstIndex = this.codePointIndexes[i];
+ }
+ }
+ return firstIndex;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLastIndex() {
+ char lastIndex = Character.MIN_VALUE;
+ for (int i = 0; i < this.codePointIndexes.length; i++) {
+ if (this.codePointIndexes[i] > lastIndex) {
+ lastIndex = this.codePointIndexes[i];
+ }
+ }
+ return lastIndex;
+ }
+
+ /**
+ * Sorts the codePoints array by its contents, and sorts the parallel
+ * codePointIndexes arrays in a parallel manner, keeping its elements
+ * synchronized with those in codePoints.
+ * @param codePoints The array of Unicode code points to be sorted.
+ * @param codePointIndexes The array of encoding indexes that whose elements
+ * correspond to elements in codePoints.
+ */
+ public void sortCodePoints(final char[] codePoints,
+ final char[] codePointIndexes) {
+ boolean anyChanges = true;
+ while (anyChanges) {
+ anyChanges = false;
+ for (int i = 0; i < codePoints.length - 1; i++) {
+ if (codePoints[i] > codePoints[i + 1]) {
+ /* They are out of order. Switch corresponding elements
+ * in both arrays.*/
+ char saveChar = codePoints[i];
+ codePoints[i] = codePoints[i + 1];
+ codePoints[i + 1] = saveChar;
+ saveChar = codePointIndexes[i];
+ codePointIndexes[i] = codePointIndexes[i + 1];
+ codePointIndexes[i + 1] = saveChar;
+ anyChanges = true;
+ }
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String asPostScript(final org.axsl.ps.Encoding baseEncoding) {
+ /* TODO: The baseEncoding is ignored for now. We need to consider it. */
+ final int maxCharsPerLine = 80;
+ final String indent = " ";
+ final String[] glyphNames = this.getGlyphNames();
+ if (glyphNames == null) {
+ return null;
+ }
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("/Differences\n");
+ buffer.append("[ 0 \n");
+ buffer.append(indent);
+ int lineLength = indent.length();
+ for (int i = 0; i < glyphNames.length; i++) {
+ final String glyphName = glyphNames[i];
+ /* If line length will exceed maxCharsPerLine, add linefeed. */
+ final int sizeToAdd = glyphName.length() + 2;
+ if (lineLength + sizeToAdd > maxCharsPerLine) {
+ buffer.append("\n");
+ buffer.append(indent);
+ lineLength = indent.length();
+ } else {
+ if (lineLength > indent.length()) {
+ buffer.append(" ");
+ lineLength += 1;
+ }
+ }
+ buffer.append("/");
+ buffer.append(glyphName);
+ lineLength += glyphName.length() + 1;
+ }
+ /* Add line-feed for the last line. */
+ buffer.append("\n");
+ buffer.append("]\n");
+ return buffer.toString();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String mapCodePointToGlyphName(final int codePoint) {
+ if (this.sourceGlyphLists == null) {
+ return null;
+ }
+ if (encodeCharacter(codePoint) == 0) {
+ // codePoint is not in this encoding.
+ return null;
+ }
+ return mapCodePointToGlyphName(this.sourceGlyphLists, codePoint);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public org.axsl.ps.EncodingVector bestBaseEncodingPdf() {
+ /* Predefined encodings do not need to be written at all. */
+ if (this.isPredefinedPdf()) {
+ return null;
+ }
+ /* TODO: Write a standard implementation of this that compares this
+ * encoding to the predefined PDF encodings and returns one of them if
+ * that is appropriate. */
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isSubsetOf(final EncodingVector otherVector) {
+ for (int i = 0; i < this.codePoints.length; i++) {
+ final int thisEncodedValue = this.encodeCharacter(
+ this.codePoints[i]);
+ final int otherEncodedValue = otherVector.encodeCharacter(
+ this.codePoints[i]);
+ if (thisEncodedValue != otherEncodedValue) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Indicates whether the data in this encoding vector is static, that is,
+ * tied entirely to the class itself and not built dynamically.
+ * This is used primarily to determine whether the instance should actually
+ * be serialized.
+ * @return True iff all data in this encoding vector is static and does not
+ * need to be serialized.
+ */
+ public abstract boolean isStatic();
+
+ /**
+ * Serialize this {@link EncodingVector4a} instance.
+ * @param stream The output stream to which the serialized data is written.
+ * @throws IOException For I/O errors.
+ * @serialData
+ * <ol>
+ * <li>The name of the encoding (String).</li>
+ * <li>The source {@link GlyphList4a} instances used to build this encoding
+ * (GlyphList[]), in order.</li>
+ * <li>The code points (char[]).</li>
+ * <li>The code point indexes (char[]).</li>
+ * </ol>
+ */
+ private void writeObject(final ObjectOutputStream stream)
+ throws IOException {
+ if (this.isStatic()) {
+ /* The static instances do not need to be serialized as they are
+ * derived entirely from static data. */
+ return;
+ }
+ stream.defaultWriteObject();
+ stream.writeObject(this.name);
+ stream.writeObject(this.sourceGlyphLists);
+ stream.writeObject(this.codePoints);
+ stream.writeObject(this.codePointIndexes);
+ }
+
+ /**
+ * Deserialize this {@link EncodingVector4a} instance.
+ * @param stream The input stream from which the serialized data is read.
+ * @throws IOException For I/O errors reading the input.
+ * @throws ClassNotFoundException For classes in the input that cannot be
+ * found.
+ */
+ @SuppressWarnings("unchecked")
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ if (this.isStatic()) {
+ /* The static instances do not need to be serialized as they are
+ * derived entirely from static data. */
+ return;
+ }
+ stream.defaultReadObject();
+ this.name = (String) stream.readObject();
+ this.sourceGlyphLists = (List<GlyphList>) stream.readObject();
+ this.codePoints = (char[]) stream.readObject();
+ this.codePointIndexes = (char[]) stream.readObject();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isPredefinedPs() {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isPredefinedPdf() {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEncode(final int codePoint) {
+ return this.encodeCharacter(codePoint) != 0;
+ }
+
+ /**
+ * Finds the glyph name for a given Unicode code point by searching
+ * an array of GlyphList instances for it.
+ * @param glyphLists An array of GlyphList instances which should be
+ * tried.
+ * @param codePoint The Unicode code point for which a glyph name is sought.
+ * @return The glyph name which corresponds to codePoint, or null if there
+ * is none.
+ */
+ public String mapCodePointToGlyphName(final List<GlyphList> glyphLists, final int codePoint) {
+ if (glyphLists == null) {
+ return null;
+ }
+ for (int i = 0; i < glyphLists.size(); i++) {
+ final GlyphList list = glyphLists.get(i);
+ if (list == null) {
+ continue;
+ }
+ final String glyphName = list.mapCodePointToGlyphName(codePoint);
+ if (glyphName == null) {
+ continue;
+ }
+ /* Make sure it maps back. Otherwise it must have come from a
+ * different list. */
+ final char roundTripCodePoint = mapGlyphNameToCodePoint(glyphLists,
+ glyphName);
+ if (roundTripCodePoint == codePoint) {
+ return glyphName;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Finds the Unicode code for a glyphName by searching an array of
+ * GlyphList instances for it.
+ * @param glyphLists An array of GlyphList instances which should be
+ * tried.
+ * @param glyphName The glyph name for which a Unicode code point is sought.
+ * @return The Unicode code point which corresponds to glyphName, or
+ * {@link Encoding#INVALID_UNICODE_CHAR} if there is none.
+ */
+ public char mapGlyphNameToCodePoint(final List<GlyphList> glyphLists, final String glyphName) {
+ if (glyphLists == null) {
+ return Encoding.INVALID_UNICODE_CHAR;
+ }
+ for (int i = 0; i < glyphLists.size(); i++) {
+ final GlyphList list = glyphLists.get(i);
+ if (list == null) {
+ continue;
+ }
+ final char c = list.mapGlyphNameToCodePoint(glyphName);
+ if (c != Encoding.INVALID_UNICODE_CHAR) {
+ return c;
+ }
+ }
+ return Encoding.INVALID_UNICODE_CHAR;
+ }
+
+}
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/PsServer4a.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/PsServer4a.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/PsServer4a.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,7 @@
package org.foray.ps;
+import org.foray.common.ps.EncodingVector4a;
import org.foray.ps.encode.CMap04;
import org.foray.ps.encode.CMap12;
import org.foray.ps.encode.CharSet4a;
@@ -45,7 +46,6 @@
import org.foray.ps.encode.EncodingParser;
import org.foray.ps.encode.EncodingStandard;
import org.foray.ps.encode.EncodingSymbol;
-import org.foray.ps.encode.EncodingVector4a;
import org.foray.ps.encode.EncodingWinAnsi;
import org.foray.ps.encode.EncodingZapfDingbats;
import org.foray.ps.encode.GlyphList4a;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCE.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCE.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCE.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCustom.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCustom.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingCustom.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import org.axsl.ps.GlyphList;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpert.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpert.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpert.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpertSubset.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpertSubset.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingExpertSubset.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingFOrayLatinExtra.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingFOrayLatinExtra.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingFOrayLatinExtra.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingISOLatin1.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingISOLatin1.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingISOLatin1.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacExpert.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacExpert.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacExpert.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacRoman.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacRoman.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacRoman.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacStandard.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacStandard.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingMacStandard.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.ObjectStreamException;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingPDFDoc.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingPDFDoc.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingPDFDoc.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -32,6 +32,7 @@
import org.foray.common.Logging;
import org.foray.common.StringUtil;
import org.foray.common.WKConstants;
+import org.foray.common.ps.EncodingVector4a;
import org.foray.common.url.URLFactory;
import org.axsl.ps.GlyphList;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingStandard.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingStandard.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingStandard.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingSymbol.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingSymbol.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingSymbol.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Deleted: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingVector4a.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingVector4a.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingVector4a.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -1,477 +0,0 @@
-/*
- * Copyright 2004 The FOray Project.
- * http://www.foray.org
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * This work is in part derived from the following work(s), used with the
- * permission of the licensor:
- * Apache FOP, licensed by the Apache Software Foundation
- *
- */
-
-/*
- * $LastChangedRevision$
- * $LastChangedDate$
- * $LastChangedBy$
- */
-
-package org.foray.ps.encode;
-
-import org.axsl.ps.Encoding;
-import org.axsl.ps.EncodingVector;
-import org.axsl.ps.GlyphList;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * <p>Encoding instances provide some standard encoding resources for
- * PostScript.
- * When a PostScript file is parsed, the characters in a string are really
- * indices into an array of glyph names, which are in turn the keys to entries
- * in a glyph dictionary.
- * Input to a PostScript interpreter is not really characters, but rather
- * character indexes.
- * (The two might be the same, especially for ASCII characters).</p>
- *
- * <p>So the PostScript interpreter uses a two-step process, as follows:
- * 1. It conceptually maps character codes (integer values) to name objects
- * like /A or /Uacute. This map is known as an "encoding".
- * There are several standard encoding schemes defined in the PostScript
- * standard that each interpreter must know how to handle. Those are provided
- * as Encoding subclasses.
- * 2. The name objects created in step 1 are then used as the key into a
- * dictionary (contained in the Font dictionary) whose values contain the
- * PostScript instructions needed to actually paint the glyph.</p>
- *
- * <p>Nowhere in the above scheme is Unicode encoding contemplated or supported.
- * Applications that <em>create </em> PostScript output from Unicode input need
- * a way to convert from that Unicode input to the indexes that are expected
- * for by the selected encoding.
- * The closest resource that we have for this purpose is the standard glyph
- * lists supplied by Adobe.
- * The main glyph list is encapsulated in the {@link GlyphListAGL} and
- * {@link GlyphListAGL2} classes.</p>
- *
- * <p>The canonical method for mapping a Unicode code point to a PostScript
- * encoding index would be:</p>
- * <ol>
- * <li>Use {@link GlyphListAGL} to obtain the glyph name for a given Unicode
- * code point.</li>
- * <li>Use an Encoding instance to obtain the index in that Encoding for the
- * glyph name obtained above.</li>
- * </ol>
- *
- * <p>However, for efficiency, an Encoding combines the above two steps and
- * provides a direct mapping from a Unicode code point to the encoded index.
- * Two parallel arrays are used to make this process efficient.
- * The first is a an array of all Unicode code points supported in the Encoding.
- * This array is sorted for fast searching.
- * The second array provides the encoded index for the Unicode code point in
- * the first array.
- * Methods are provided to encode and decode characters.</p>
- */
-public abstract class EncodingVector4a implements EncodingVector, Serializable {
-
- /** The name of the undefined glyph. */
- public static final String NOTDEF = ".notdef";
-
- /** Constant needed for serialization. */
- private static final long serialVersionUID = 6967489975814026068L;
-
- /** The name of this encoding. */
- private transient String name;
-
- /** The GlyphList instances, in order, which were used to create this encoding. */
- private transient List<GlyphList> sourceGlyphLists;
-
- /** Array of Unicode code points, sorted. Elements in this array are keys
- * to elements in {@link #codePointIndexes}. */
- private transient char[] codePoints;
-
- /** Array of encoded indexes for the glyphs that correspond to elements
- * in {@link #codePoints}. */
- private transient char[] codePointIndexes;
-
- /**
- * Create a new Encoding instance.
- * @param name The name of this encoding.
- * This name is not used by the system, but may be useful for debugging.
- * @param sourceGlyphLists The GlyphList instances that were used to create this encoding.
- * @param codePoints The array of Unicode code points supported by this encoding.
- * @param codePointIndexes The array of encoded indexes that is parallel to codePoints.
- * codePointIndexes[n] should contain the encoded index that corresponds to the Unicode code point at codePoints[n].
- */
- public EncodingVector4a(final String name, final List<GlyphList> sourceGlyphLists,
- final char[] codePoints, final char[] codePointIndexes) {
- this.name = name;
- this.sourceGlyphLists = sourceGlyphLists;
- this.codePoints = codePoints;
- this.codePointIndexes = codePointIndexes;
- sortCodePoints(codePoints, codePointIndexes);
- }
-
- /**
- * {@inheritDoc}
- */
- public String getName() {
- return this.name;
- }
-
- /**
- * {@inheritDoc}
- */
- public int encodeCharacter(final int codePoint) {
- if (this.codePoints == null
- || this.codePointIndexes == null) {
- return 0;
- }
- final int index = Arrays.binarySearch(this.codePoints,
- (char) codePoint);
- if (index < 0) {
- return 0;
- }
- return this.codePointIndexes[index];
- }
-
- /**
- * {@inheritDoc}
- */
- public int decodeCharacter(final int encodedIndex) {
- /* This is not expected to be used much or at all, so the
- * codePointIndexes array has not been copied and sorted and
- * referenced.*/
- if (this.codePoints == null
- || this.codePointIndexes == null) {
- return INVALID_UNICODE_CHAR;
- }
- for (int i = 0; i < this.codePointIndexes.length; i++) {
- if (this.codePointIndexes[i] == encodedIndex) {
- return this.codePoints[i];
- }
- }
- return INVALID_UNICODE_CHAR;
- }
-
- /**
- * Returns the number of entries in this encoding.
- * @return The number of entries in this encoding.
- */
- public int size() {
- return this.codePoints.length;
- }
-
- /**
- * {@inheritDoc}
- */
- public String[] getGlyphNames() {
- final int listSize = getLastIndex() + 1;
- final String[] glyphList = new String[listSize];
- // Initialize all items to ".notdef".
- for (int i = 0; i < glyphList.length; i++) {
- glyphList[i] = EncodingVector4a.NOTDEF;
- }
- for (int i = 0; i < this.codePoints.length; i++) {
- final char codePoint = this.codePoints[i];
- final String glyphName = mapCodePointToGlyphName(this.sourceGlyphLists, codePoint);
- final int encodingIndex = this.codePointIndexes[i];
- glyphList[encodingIndex] = glyphName;
- }
- return glyphList;
- }
-
- /**
- * {@inheritDoc}
- */
- public int getFirstIndex() {
- char firstIndex = Character.MAX_VALUE;
- for (int i = 0; i < this.codePointIndexes.length; i++) {
- if (this.codePointIndexes[i] < firstIndex) {
- firstIndex = this.codePointIndexes[i];
- }
- }
- return firstIndex;
- }
-
- /**
- * {@inheritDoc}
- */
- public int getLastIndex() {
- char lastIndex = Character.MIN_VALUE;
- for (int i = 0; i < this.codePointIndexes.length; i++) {
- if (this.codePointIndexes[i] > lastIndex) {
- lastIndex = this.codePointIndexes[i];
- }
- }
- return lastIndex;
- }
-
- /**
- * Sorts the codePoints array by its contents, and sorts the parallel
- * codePointIndexes arrays in a parallel manner, keeping its elements
- * synchronized with those in codePoints.
- * @param codePoints The array of Unicode code points to be sorted.
- * @param codePointIndexes The array of encoding indexes that whose elements
- * correspond to elements in codePoints.
- */
- public void sortCodePoints(final char[] codePoints,
- final char[] codePointIndexes) {
- boolean anyChanges = true;
- while (anyChanges) {
- anyChanges = false;
- for (int i = 0; i < codePoints.length - 1; i++) {
- if (codePoints[i] > codePoints[i + 1]) {
- /* They are out of order. Switch corresponding elements
- * in both arrays.*/
- char saveChar = codePoints[i];
- codePoints[i] = codePoints[i + 1];
- codePoints[i + 1] = saveChar;
- saveChar = codePointIndexes[i];
- codePointIndexes[i] = codePointIndexes[i + 1];
- codePointIndexes[i + 1] = saveChar;
- anyChanges = true;
- }
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public String asPostScript(final org.axsl.ps.Encoding baseEncoding) {
- /* TODO: The baseEncoding is ignored for now. We need to consider it. */
- final int maxCharsPerLine = 80;
- final String indent = " ";
- final String[] glyphNames = this.getGlyphNames();
- if (glyphNames == null) {
- return null;
- }
- final StringBuilder buffer = new StringBuilder();
- buffer.append("/Differences\n");
- buffer.append("[ 0 \n");
- buffer.append(indent);
- int lineLength = indent.length();
- for (int i = 0; i < glyphNames.length; i++) {
- final String glyphName = glyphNames[i];
- /* If line length will exceed maxCharsPerLine, add linefeed. */
- final int sizeToAdd = glyphName.length() + 2;
- if (lineLength + sizeToAdd > maxCharsPerLine) {
- buffer.append("\n");
- buffer.append(indent);
- lineLength = indent.length();
- } else {
- if (lineLength > indent.length()) {
- buffer.append(" ");
- lineLength += 1;
- }
- }
- buffer.append("/");
- buffer.append(glyphName);
- lineLength += glyphName.length() + 1;
- }
- /* Add line-feed for the last line. */
- buffer.append("\n");
- buffer.append("]\n");
- return buffer.toString();
- }
-
- /**
- * {@inheritDoc}
- */
- public String mapCodePointToGlyphName(final int codePoint) {
- if (this.sourceGlyphLists == null) {
- return null;
- }
- if (encodeCharacter(codePoint) == 0) {
- // codePoint is not in this encoding.
- return null;
- }
- return mapCodePointToGlyphName(this.sourceGlyphLists, codePoint);
- }
-
- /**
- * {@inheritDoc}
- */
- public org.axsl.ps.EncodingVector bestBaseEncodingPdf() {
- /* Predefined encodings do not need to be written at all. */
- if (this.isPredefinedPdf()) {
- return null;
- }
- /* TODO: Write a standard implementation of this that compares this
- * encoding to the predefined PDF encodings and returns one of them if
- * that is appropriate. */
- return null;
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isSubsetOf(final EncodingVector otherVector) {
- for (int i = 0; i < this.codePoints.length; i++) {
- final int thisEncodedValue = this.encodeCharacter(
- this.codePoints[i]);
- final int otherEncodedValue = otherVector.encodeCharacter(
- this.codePoints[i]);
- if (thisEncodedValue != otherEncodedValue) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * Indicates whether the data in this encoding vector is static, that is,
- * tied entirely to the class itself and not built dynamically.
- * This is used primarily to determine whether the instance should actually
- * be serialized.
- * @return True iff all data in this encoding vector is static and does not
- * need to be serialized.
- */
- public abstract boolean isStatic();
-
- /**
- * Serialize this {@link EncodingVector4a} instance.
- * @param stream The output stream to which the serialized data is written.
- * @throws IOException For I/O errors.
- * @serialData
- * <ol>
- * <li>The name of the encoding (String).</li>
- * <li>The source {@link GlyphList4a} instances used to build this encoding
- * (GlyphList[]), in order.</li>
- * <li>The code points (char[]).</li>
- * <li>The code point indexes (char[]).</li>
- * </ol>
- */
- private void writeObject(final ObjectOutputStream stream)
- throws IOException {
- if (this.isStatic()) {
- /* The static instances do not need to be serialized as they are
- * derived entirely from static data. */
- return;
- }
- stream.defaultWriteObject();
- stream.writeObject(this.name);
- stream.writeObject(this.sourceGlyphLists);
- stream.writeObject(this.codePoints);
- stream.writeObject(this.codePointIndexes);
- }
-
- /**
- * Deserialize this {@link EncodingVector4a} instance.
- * @param stream The input stream from which the serialized data is read.
- * @throws IOException For I/O errors reading the input.
- * @throws ClassNotFoundException For classes in the input that cannot be
- * found.
- */
- @SuppressWarnings("unchecked")
- private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
- if (this.isStatic()) {
- /* The static instances do not need to be serialized as they are
- * derived entirely from static data. */
- return;
- }
- stream.defaultReadObject();
- this.name = (String) stream.readObject();
- this.sourceGlyphLists = (List<GlyphList>) stream.readObject();
- this.codePoints = (char[]) stream.readObject();
- this.codePointIndexes = (char[]) stream.readObject();
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isPredefinedPs() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isPredefinedPdf() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean canEncode(final int codePoint) {
- return this.encodeCharacter(codePoint) != 0;
- }
-
- /**
- * Finds the glyph name for a given Unicode code point by searching
- * an array of GlyphList instances for it.
- * @param glyphLists An array of GlyphList instances which should be
- * tried.
- * @param codePoint The Unicode code point for which a glyph name is sought.
- * @return The glyph name which corresponds to codePoint, or null if there
- * is none.
- * @see GlyphList4a#mapCodePointToGlyphName(int)
- */
- public String mapCodePointToGlyphName(final List<GlyphList> glyphLists, final int codePoint) {
- if (glyphLists == null) {
- return null;
- }
- for (int i = 0; i < glyphLists.size(); i++) {
- final GlyphList list = glyphLists.get(i);
- if (list == null) {
- continue;
- }
- final String glyphName = list.mapCodePointToGlyphName(codePoint);
- if (glyphName == null) {
- continue;
- }
- /* Make sure it maps back. Otherwise it must have come from a
- * different list. */
- final char roundTripCodePoint = mapGlyphNameToCodePoint(glyphLists,
- glyphName);
- if (roundTripCodePoint == codePoint) {
- return glyphName;
- }
- }
- return null;
- }
-
- /**
- * Finds the Unicode code for a glyphName by searching an array of
- * GlyphList instances for it.
- * @param glyphLists An array of GlyphList instances which should be
- * tried.
- * @param glyphName The glyph name for which a Unicode code point is sought.
- * @return The Unicode code point which corresponds to glyphName, or
- * {@link Encoding#INVALID_UNICODE_CHAR} if there is none.
- * @see GlyphList4a#mapGlyphNameToCodePoint(String)
- */
- public char mapGlyphNameToCodePoint(final List<GlyphList> glyphLists, final String glyphName) {
- if (glyphLists == null) {
- return Encoding.INVALID_UNICODE_CHAR;
- }
- for (int i = 0; i < glyphLists.size(); i++) {
- final GlyphList list = glyphLists.get(i);
- if (list == null) {
- continue;
- }
- final char c = list.mapGlyphNameToCodePoint(glyphName);
- if (c != Encoding.INVALID_UNICODE_CHAR) {
- return c;
- }
- }
- return Encoding.INVALID_UNICODE_CHAR;
- }
-
-}
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingWinAnsi.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingWinAnsi.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingWinAnsi.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import java.io.Serializable;
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingZapfDingbats.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingZapfDingbats.java 2009-05-01 21:51:12 UTC (rev 10856)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingZapfDingbats.java 2009-05-01 21:54:37 UTC (rev 10857)
@@ -28,6 +28,8 @@
package org.foray.ps.encode;
+import org.foray.common.ps.EncodingVector4a;
+
import org.axsl.ps.EncodingVector;
import org.axsl.ps.GlyphList;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|