foray-commit Mailing List for FOray (Page 79)
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
You can subscribe to this list here.
| 2006 |
Jan
|
Feb
|
Mar
(139) |
Apr
(98) |
May
(250) |
Jun
(394) |
Jul
(84) |
Aug
(13) |
Sep
(420) |
Oct
(186) |
Nov
(1) |
Dec
(3) |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2007 |
Jan
(108) |
Feb
(202) |
Mar
(291) |
Apr
(247) |
May
(374) |
Jun
(227) |
Jul
(231) |
Aug
(60) |
Sep
(31) |
Oct
(45) |
Nov
(18) |
Dec
|
| 2008 |
Jan
(38) |
Feb
(71) |
Mar
(142) |
Apr
|
May
(59) |
Jun
(6) |
Jul
(10) |
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2009 |
Jan
(12) |
Feb
(4) |
Mar
(88) |
Apr
(121) |
May
(17) |
Jun
(30) |
Jul
|
Aug
(5) |
Sep
|
Oct
(1) |
Nov
|
Dec
|
| 2010 |
Jan
(11) |
Feb
(76) |
Mar
(11) |
Apr
|
May
(11) |
Jun
|
Jul
|
Aug
(44) |
Sep
(14) |
Oct
(7) |
Nov
|
Dec
|
| 2011 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(9) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(10) |
Nov
|
Dec
|
| 2012 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(3) |
Jul
(4) |
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2016 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
(168) |
| 2017 |
Jan
(77) |
Feb
(11) |
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2018 |
Jan
|
Feb
|
Mar
(1) |
Apr
(6) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2019 |
Jan
|
Feb
(88) |
Mar
(118) |
Apr
(1) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2020 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(6) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
(141) |
| 2021 |
Jan
(170) |
Feb
(20) |
Mar
|
Apr
|
May
|
Jun
|
Jul
(1) |
Aug
|
Sep
|
Oct
(62) |
Nov
(189) |
Dec
(162) |
| 2022 |
Jan
(201) |
Feb
(118) |
Mar
(8) |
Apr
|
May
(2) |
Jun
(47) |
Jul
(19) |
Aug
(14) |
Sep
(3) |
Oct
|
Nov
(28) |
Dec
(235) |
| 2023 |
Jan
(112) |
Feb
(23) |
Mar
(2) |
Apr
(2) |
May
|
Jun
(1) |
Jul
|
Aug
(70) |
Sep
(92) |
Oct
(20) |
Nov
(1) |
Dec
(1) |
| 2024 |
Jan
|
Feb
|
Mar
(1) |
Apr
(1) |
May
(14) |
Jun
(11) |
Jul
(1) |
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2025 |
Jan
(10) |
Feb
(29) |
Mar
|
Apr
(162) |
May
(245) |
Jun
(83) |
Jul
|
Aug
(1) |
Sep
|
Oct
|
Nov
(4) |
Dec
|
|
From: <vic...@us...> - 2021-10-23 15:03:38
|
Revision: 11920
http://sourceforge.net/p/foray/code/11920
Author: victormote
Date: 2021-10-23 15:03:35 +0000 (Sat, 23 Oct 2021)
Log Message:
-----------
Steps toward an improved node iterator.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java
trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java 2021-10-23 14:54:38 UTC (rev 11919)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java 2021-10-23 15:03:35 UTC (rev 11920)
@@ -29,6 +29,7 @@
package org.foray.common.data;
import org.foray.common.primitive.CharacterUtils;
+import org.foray.common.sequence.CharArrayBuilder;
import org.foray.common.sequence.IntArrayBuilder;
import org.slf4j.Logger;
@@ -113,7 +114,7 @@
/** Format string for {@link #toString()}. */
private static final String TO_STRING_FORMAT =
- "index: %5d, low: %5d, equal: %5d, high: %5d, key: %4x %s, parent: %5d";
+ "index: %5d, low: %5d, equal: %5d, high: %5d, key char: %4x %s, parent: %5d, key: %6s";
/** The index for this node. */
private int index;
@@ -133,17 +134,22 @@
/** The index to the parent of this node. */
private int parent = -1;
+ /** The key, i.e. the cumulative keyChar items for this node. */
+ private CharSequence key;
+
/**
* Constructor.
* @param index The index for which a notional nodes is wanted.
+ * @param key The cumulative keyChar for this node.
* @param withParents The {@link TernaryNodes.WithParents} instance. This can be null.
*/
- public Node(final int index, final WithParents withParents) {
+ public Node(final int index, final CharSequence key, final WithParents withParents) {
this.index = index;
this.low = TernaryNodes.this.getLow(index);
this.equal = TernaryNodes.this.getEqual(index);
this.high = TernaryNodes.this.getHigh(index);
this.keyChar = TernaryNodes.this.getKeyChar(index);
+ this.key = key;
if (withParents != null) {
this.parent = withParents.getParent(index);
}
@@ -197,6 +203,14 @@
return this.parent;
}
+ /**
+ * Returns the key.
+ * @return The key. This can be null if the node was not created by the iterator.
+ */
+ public CharSequence getKey() {
+ return this.key;
+ }
+
@Override
public String toString() {
char printableChar = this.keyChar;
@@ -205,7 +219,8 @@
printableChar = ' ';
}
return String.format(TO_STRING_FORMAT,
- this.index, this.low, this.equal, this.high, (int) this.keyChar, printableChar, this.parent);
+ this.index, this.low, this.equal, this.high, (int) this.keyChar, printableChar, this.parent,
+ this.key);
}
@Override
@@ -229,7 +244,7 @@
}
/**
- * Iterates over the nodes in depth-first order, going now first the left branch, then the equal branch, then the
+ * Iterates over the nodes in depth-first order, going first to the low branch, then the equal branch, then the
* high branch.
*/
public class DepthFirstIterator implements Iterator<TernaryNodes.Node> {
@@ -238,11 +253,16 @@
* the top. */
private IntArrayBuilder currentIndexStack = new IntArrayBuilder();
- /** Parallel stack to indexStack -- this should always have the same number of element in it as indexStack.
+ /** Parallel stack to indexStack -- this should always have the same number of elements in it as indexStack.
* This keeps track of which branch of the related index is being processed.
* Low is -1, equal is 0, and high is +1. */
private IntArrayBuilder currentBranchStack = new IntArrayBuilder();
+ /** Parallel stack to indexStack -- this should always have the same number of elements in it as indexStack.
+ * This keeps track of the "word" (sequence of characters) represented by the current location of the
+ * iterator. */
+ private CharArrayBuilder charStack = new CharArrayBuilder();
+
/** The next index to return. */
private int nextIndex = Integer.MIN_VALUE;
@@ -254,6 +274,7 @@
this.currentIndexStack.push(1);
this.nextIndex = 1;
this.currentBranchStack.push(-1 - 1);
+ this.charStack.push(TernaryNodes.this.keyChar[1]);
}
}
@@ -264,7 +285,9 @@
@Override
public Node next() {
- final int returnValue = this.nextIndex;
+ final Node returnValue = new Node(this.nextIndex, this.charStack.toString(), null);
+
+ /* Tee up the one after this, if any. */
this.nextIndex = -1;
indexLoop:
@@ -298,6 +321,7 @@
/* Push the found index onto the stack. */
this.currentIndexStack.push(branchIndex);
this.currentBranchStack.push(-1 - 1);
+ this.charStack.push(TernaryNodes.this.keyChar[branchIndex]);
/* The new index is the next item to be processed. */
this.nextIndex = branchIndex;
break indexLoop;
@@ -307,12 +331,13 @@
this.currentBranchStack.push(currentBranch);
}
}
- /* We have processed all of the branches on the current index. Pop both stacks. */
+ /* We have processed all of the branches on the current index. Pop all stacks. */
this.currentIndexStack.pop();
this.currentBranchStack.pop();
+ this.charStack.pop();
}
- return new Node(returnValue, null);
+ return returnValue;
}
@Override
@@ -612,11 +637,12 @@
* Use the getters and setters for ordinary processing.
* It is expected that this method should be used ONLY for testing and iteration.
* @param index The index to the node.
+ * @param key The chars making up the key to the node. This can be null.
* @param withParents An optional {@link TernaryNodes.WithParents}. This can be null.
* @return A new Node instance containing the data for the reqested node.
*/
- Node createNotionalNode(final int index, final WithParents withParents) {
- return new Node(index, withParents);
+ Node createNotionalNode(final int index, final CharSequence key, final WithParents withParents) {
+ return new Node(index, key, withParents);
}
/**
@@ -747,7 +773,7 @@
this.logger.error("Inconsistent Tree", e);
}
for (int index = 0; index < size(); index ++) {
- final Node node = createNotionalNode(index, withParents);
+ final Node node = createNotionalNode(index, null, withParents);
this.logger.info(node.toString());
}
this.logger.info("-----------------------------------------------------------------------------------------");
Modified: trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java
===================================================================
--- trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java 2021-10-23 14:54:38 UTC (rev 11919)
+++ trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java 2021-10-23 15:03:35 UTC (rev 11920)
@@ -259,7 +259,7 @@
*/
private void testNode(final TernaryTree map, final int index, final int expectedLow, final int expectedEqual,
final int expectedHigh, final char expectedKeyChar) {
- final TernaryNodes.Node node = map.getNodes().createNotionalNode(index, null);
+ final TernaryNodes.Node node = map.getNodes().createNotionalNode(index, null, null);
Assert.assertEquals(index, node.getIndex());
Assert.assertEquals(expectedLow, node.getLow());
Assert.assertEquals(expectedEqual, node.getEqual());
@@ -278,7 +278,7 @@
*/
private void testNode(final TernaryNodes nodes, final int index, final int expectedLow, final int expectedEqual,
final int expectedHigh, final char expectedKeyChar) {
- final TernaryNodes.Node node = nodes.createNotionalNode(index, null);
+ final TernaryNodes.Node node = nodes.createNotionalNode(index, null, null);
Assert.assertEquals(index, node.getIndex());
Assert.assertEquals(expectedLow, node.getLow());
Assert.assertEquals(expectedEqual, node.getEqual());
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-23 14:54:40
|
Revision: 11919
http://sourceforge.net/p/foray/code/11919
Author: victormote
Date: 2021-10-23 14:54:38 +0000 (Sat, 23 Oct 2021)
Log Message:
-----------
Minor javadoc changes.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java
trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesChar.java
trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesInt.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java 2021-10-23 14:46:52 UTC (rev 11918)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodes.java 2021-10-23 14:54:38 UTC (rev 11919)
@@ -39,7 +39,9 @@
import java.util.Iterator;
/**
- * <p>Interface for data structures that hold the tree data for a {@link TernaryTree}.</p>
+ * <p>Abstract superclass for data structures that hold the tree data for a {@link TernaryTree}.
+ * This abstraction allows different implementations to use different data structures, which can result in memory
+ * and performance benefits.</p>
*
* <p>We use four arrays of equal size to represent the nodes in this tree.
* A given node /n/ is represented by the values in the arrays low, high, equal, and keyChar.
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesChar.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesChar.java 2021-10-23 14:46:52 UTC (rev 11918)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesChar.java 2021-10-23 14:54:38 UTC (rev 11919)
@@ -33,7 +33,11 @@
import java.util.Arrays;
/**
- * Implementation of {@link TernaryNodes} that uses char for the node index values.
+ * Implementation of {@link TernaryNodes} that uses {@code char} for the node indexes (think "unsigned short").
+ * This will use half as much memory as one using {@code int} for the node indexes, but limits the number of nodes to
+ * 65,536.
+ * @see TernaryNodesInt for an implementation that uses {@code int} for the node indexes, using more memory and having
+ * more capacity.
*/
public class TernaryNodesChar extends TernaryNodes {
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesInt.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesInt.java 2021-10-23 14:46:52 UTC (rev 11918)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryNodesInt.java 2021-10-23 14:54:38 UTC (rev 11919)
@@ -31,7 +31,9 @@
import java.util.Arrays;
/**
- * Implementation of {@link TernaryNodes} that uses int for the node index values.
+ * Implementation of {@link TernaryNodes} that uses {@code int} for the node indexes.
+ * @see TernaryNodesChar for an implementation that uses {@code char} for the node indexes, using less memory and having
+ * less capacity.
*/
public class TernaryNodesInt extends TernaryNodes {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-23 14:46:55
|
Revision: 11918
http://sourceforge.net/p/foray/code/11918
Author: victormote
Date: 2021-10-23 14:46:52 +0000 (Sat, 23 Oct 2021)
Log Message:
-----------
Add test for invalid key in "put".
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java 2021-10-23 13:35:06 UTC (rev 11917)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java 2021-10-23 14:46:52 UTC (rev 11918)
@@ -75,9 +75,7 @@
* Sometimes it outperforms a hash function, and can usually determine a miss faster than a hash.</li>
* </ul>
*
- * <p>In this implementation the value is a char (think "unsigned short"), and is stored in the leaf nodes of the tree.
- * This limits the number of nodes to 65,536.
- * Branches that contain only one key are compressed to one node by storing a pointer to the trailer substring of the
+ * <p>Branches that contain only one key are compressed to one node by storing a pointer to the trailer substring of the
* key.
* A compressed branch needs only one node per key plus the size of the string key.
* Compressed branches are decompressed as needed when another key with same prefix is inserted.
@@ -210,7 +208,7 @@
* @param value The value.
*/
public void put(final CharSequence key, final int value) {
- this.put(key, 0, value);
+ put(key, 0, value);
}
/**
@@ -259,6 +257,16 @@
|| end < start) {
throw new IllegalArgumentException("Invalid end (" + end + ") in word: " + key);
}
+
+ for (int index = start; index < end; index ++) {
+ if (key.charAt(index) == Character.MIN_VALUE) {
+ throw new IllegalArgumentException("Illegal Unicode value \\0000 in key: " + key);
+ }
+ if (key.charAt(index) == Character.MAX_VALUE) {
+ throw new IllegalArgumentException("Illegal Unicode value \\FFFF in key: " + key);
+ }
+ }
+
int currentNodeIndex = startingNodeIndex;
final int len = end - start;
if (currentNodeIndex == 0) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-23 13:35:09
|
Revision: 11917
http://sourceforge.net/p/foray/code/11917
Author: victormote
Date: 2021-10-23 13:35:06 +0000 (Sat, 23 Oct 2021)
Log Message:
-----------
Add missing javadoc.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-23 13:31:54 UTC (rev 11916)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-23 13:35:06 UTC (rev 11917)
@@ -167,7 +167,7 @@
* @param input The input source encapsulating the document to be spell-checked.
* @param output The output stream to which the spelling errors should be written.
* @param orthographyConfigPath The path to the orthography configuration.
- * @throws HyphenationException
+ * @throws HyphenationException For errors during configuration of the hyphenation server.
*/
public SpellChecker(final InputSource input, final PrintStream output, final URL orthographyConfigPath)
throws HyphenationException {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-23 13:31:57
|
Revision: 11916
http://sourceforge.net/p/foray/code/11916
Author: victormote
Date: 2021-10-23 13:31:54 +0000 (Sat, 23 Oct 2021)
Log Message:
-----------
Rename instance variable for consistency.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java 2021-10-23 13:17:36 UTC (rev 11915)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java 2021-10-23 13:31:54 UTC (rev 11916)
@@ -50,7 +50,7 @@
private static final int DEFAULT_INITIAL_SIZE = 10;
/** The internal array. */
- private int[] array;
+ private int[] backingArray;
/** The number of ints in {@link #backingArray} actually used. */
private int length;
@@ -60,7 +60,7 @@
* @param initialCapacity The initial size of the internal array.
*/
public IntArrayBuilder(final int initialCapacity) {
- this.array = new int[initialCapacity];
+ this.backingArray = new int[initialCapacity];
}
/**
@@ -81,7 +81,7 @@
if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- return this.array[index];
+ return this.backingArray[index];
}
@Override
@@ -97,7 +97,7 @@
}
final IntArrayBuilder returnBuilder = new IntArrayBuilder(this.length);
for (int index = start; index < end; index ++) {
- returnBuilder.append(this.array[index]);
+ returnBuilder.append(this.backingArray[index]);
}
return returnBuilder;
}
@@ -105,7 +105,7 @@
@Override
public int[] toArray() {
final int[] returnArray = new int[this.length];
- System.arraycopy(this.array, 0, returnArray, 0, this.length);
+ System.arraycopy(this.backingArray, 0, returnArray, 0, this.length);
return returnArray;
}
@@ -153,7 +153,7 @@
* @see This code is based on AbstractStringBuilder#ensureCapacity(int).
*/
private void ensureCapacity(final int minimumCapacity) {
- if (minimumCapacity > this.array.length) {
+ if (minimumCapacity > this.backingArray.length) {
expandCapacity(minimumCapacity);
}
}
@@ -164,30 +164,30 @@
* @see This code is based on AbstractStringBuilder.expandCapacity(int).
*/
private void expandCapacity(final int minimumCapacity) {
- int newCapacity = (this.array.length + 1) * 2;
+ int newCapacity = (this.backingArray.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
- this.array = Arrays.copyOf(this.array, newCapacity);
+ this.backingArray = Arrays.copyOf(this.backingArray, newCapacity);
}
@Override
public void setIntAt(final int index, final int newInt) {
- this.array[index] = newInt;
+ this.backingArray[index] = newInt;
}
@Override
public void sort() {
- Arrays.sort(this.array);
+ Arrays.sort(this.backingArray);
}
@Override
public void swap(final int index1, final int index2) {
- final int temp = this.array[index1];
- this.array[index1] = this.array[index2];
- this.array[index2] = temp;
+ final int temp = this.backingArray[index1];
+ this.backingArray[index1] = this.backingArray[index2];
+ this.backingArray[index2] = temp;
}
/**
@@ -197,7 +197,7 @@
*/
public IntArrayBuilder append(final int intToAppend) {
ensureCapacity(this.length + 1);
- this.array[this.length] = intToAppend;
+ this.backingArray[this.length] = intToAppend;
this.length ++;
return this;
}
@@ -210,7 +210,7 @@
public IntArrayBuilder append(final int... intToAppend) {
ensureCapacity(this.length + intToAppend.length);
for (int index = 0; index < intToAppend.length; index ++) {
- this.array[this.length] = intToAppend[index];
+ this.backingArray[this.length] = intToAppend[index];
this.length ++;
}
return this;
@@ -224,7 +224,7 @@
public IntArrayBuilder append(final char... intToAppend) {
ensureCapacity(this.length + intToAppend.length);
for (int index = 0; index < intToAppend.length; index ++) {
- this.array[this.length] = intToAppend[index];
+ this.backingArray[this.length] = intToAppend[index];
this.length ++;
}
return this;
@@ -239,7 +239,7 @@
this.ensureCapacity(this.length + newContent.length());
final int startingLength = this.length;
for (int index = 0; index < newContent.length(); index ++) {
- this.array[startingLength + index] = newContent.intAt(index);
+ this.backingArray[startingLength + index] = newContent.intAt(index);
this.length ++;
}
return this;
@@ -254,10 +254,10 @@
*/
public IntArrayBuilder replace(final int index, final int newValue) throws IndexOutOfBoundsException {
ensureCapacity(index + 1);
- if ((index < 0) || (index >= this.array.length)) {
+ if ((index < 0) || (index >= this.backingArray.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- this.array[index] = newValue;
+ this.backingArray[index] = newValue;
return this;
}
@@ -271,7 +271,7 @@
if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- System.arraycopy(this.array, index + 1, this.array, index, this.length - index - 1);
+ System.arraycopy(this.backingArray, index + 1, this.backingArray, index, this.length - index - 1);
this.length--;
return this;
}
@@ -300,7 +300,7 @@
}
final int len = adjustedEnd - start;
if (len > 0) {
- System.arraycopy(this.array, start + len, this.array, start, this.length - adjustedEnd);
+ System.arraycopy(this.backingArray, start + len, this.backingArray, start, this.length - adjustedEnd);
this.length -= len;
}
return this;
@@ -336,18 +336,18 @@
@Override
public int binarySearch(final int value) {
- return Arrays.binarySearch(this.array, 0, this.length, value);
+ return Arrays.binarySearch(this.backingArray, 0, this.length, value);
}
@Override
public int capacity() {
- return this.array.length;
+ return this.backingArray.length;
}
@Override
public void trimToSize() {
- if (this.length < this.array.length) {
- this.array = Arrays.copyOf(this.array, this.length);
+ if (this.length < this.backingArray.length) {
+ this.backingArray = Arrays.copyOf(this.backingArray, this.length);
}
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java 2021-10-23 13:17:36 UTC (rev 11915)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java 2021-10-23 13:31:54 UTC (rev 11916)
@@ -52,7 +52,7 @@
private static final int DEFAULT_INITIAL_SIZE = 10;
/** The internal array. */
- private long[] array;
+ private long[] backingArray;
/** The number of longs in {@link #backingArray} actually used. */
private int length;
@@ -62,7 +62,7 @@
* @param initialCapacity The initial size of the internal array.
*/
public LongArrayBuilder(final int initialCapacity) {
- this.array = new long[initialCapacity];
+ this.backingArray = new long[initialCapacity];
}
/**
@@ -83,7 +83,7 @@
if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- return this.array[index];
+ return this.backingArray[index];
}
@Override
@@ -99,7 +99,7 @@
}
final LongArrayBuilder returnBuilder = new LongArrayBuilder(this.length);
for (int index = start; index < end; index ++) {
- returnBuilder.append(this.array[index]);
+ returnBuilder.append(this.backingArray[index]);
}
return returnBuilder;
}
@@ -107,7 +107,7 @@
@Override
public long[] toArray() {
final long[] returnArray = new long[this.length];
- System.arraycopy(this.array, 0, returnArray, 0, this.length);
+ System.arraycopy(this.backingArray, 0, returnArray, 0, this.length);
return returnArray;
}
@@ -155,7 +155,7 @@
* @see java.lang.AbstractStringBuilder#ensureCapacity(int)
*/
public void ensureCapacity(final int minimumCapacity) {
- if (minimumCapacity > this.array.length) {
+ if (minimumCapacity > this.backingArray.length) {
expandCapacity(minimumCapacity);
}
}
@@ -166,13 +166,13 @@
* @see This code is based on AbstractStringBuilder.expandCapacity(int).
*/
private void expandCapacity(final int minimumCapacity) {
- int newCapacity = (this.array.length + 1) * 2;
+ int newCapacity = (this.backingArray.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
- this.array = Arrays.copyOf(this.array, newCapacity);
+ this.backingArray = Arrays.copyOf(this.backingArray, newCapacity);
}
/**
@@ -182,7 +182,7 @@
*/
public LongArrayBuilder append(final long longToAppend) {
ensureCapacity(this.length + 1);
- this.array[this.length] = longToAppend;
+ this.backingArray[this.length] = longToAppend;
this.length ++;
return this;
}
@@ -195,7 +195,7 @@
public LongArrayBuilder append(final long... longsToAppend) {
ensureCapacity(this.length + longsToAppend.length);
for (int index = 0; index < longsToAppend.length; index ++) {
- this.array[this.length] = longsToAppend[index];
+ this.backingArray[this.length] = longsToAppend[index];
this.length ++;
}
return this;
@@ -209,7 +209,7 @@
public LongArrayBuilder append(final LongSequence newContent) {
this.ensureCapacity(length() + newContent.length());
for (int index = 0; index < newContent.length(); index ++) {
- this.array[this.length + index] = newContent.longAt(index);
+ this.backingArray[this.length + index] = newContent.longAt(index);
length ++;
}
return this;
@@ -224,10 +224,10 @@
*/
public LongArrayBuilder replace(final int index, final long newValue) throws IndexOutOfBoundsException {
ensureCapacity(index + 1);
- if ((index < 0) || (index >= this.array.length)) {
+ if ((index < 0) || (index >= this.backingArray.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- this.array[index] = newValue;
+ this.backingArray[index] = newValue;
return this;
}
@@ -241,7 +241,7 @@
if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- System.arraycopy(this.array, index + 1, this.array, index, this.length - index - 1);
+ System.arraycopy(this.backingArray, index + 1, this.backingArray, index, this.length - index - 1);
this.length--;
return this;
}
@@ -270,7 +270,7 @@
}
final int len = adjustedEnd - start;
if (len > 0) {
- System.arraycopy(this.array, start + len, this.array, start, this.length - adjustedEnd);
+ System.arraycopy(this.backingArray, start + len, this.backingArray, start, this.length - adjustedEnd);
this.length -= len;
}
return this;
@@ -321,35 +321,35 @@
@Override
public void setLongAt(final int index, final long newLong) {
- this.array[index] = newLong;
+ this.backingArray[index] = newLong;
}
@Override
public void sort() {
- Arrays.sort(this.array);
+ Arrays.sort(this.backingArray);
}
@Override
public void swap(final int index1, final int index2) {
- final long temp = this.array[index1];
- this.array[index1] = this.array[index2];
- this.array[index2] = temp;
+ final long temp = this.backingArray[index1];
+ this.backingArray[index1] = this.backingArray[index2];
+ this.backingArray[index2] = temp;
}
@Override
public int binarySearch(final long value) {
- return Arrays.binarySearch(this.array, 0, length(), value);
+ return Arrays.binarySearch(this.backingArray, 0, length(), value);
}
@Override
public int capacity() {
- return this.array.length;
+ return this.backingArray.length;
}
@Override
public void trimToSize() {
- if (this.length < this.array.length) {
- this.array = Arrays.copyOf(this.array, this.length);
+ if (this.length < this.backingArray.length) {
+ this.backingArray = Arrays.copyOf(this.backingArray, this.length);
}
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java 2021-10-23 13:17:36 UTC (rev 11915)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java 2021-10-23 13:31:54 UTC (rev 11916)
@@ -50,7 +50,7 @@
private static final int DEFAULT_INITIAL_SIZE = 10;
/** The internal array. */
- private short[] array;
+ private short[] backingArray;
/** The number of shorts in {@link #backingArray} actually used. */
private int length;
@@ -60,7 +60,7 @@
* @param initialCapacity The initial size of the internal array.
*/
public ShortArrayBuilder(final int initialCapacity) {
- this.array = new short[initialCapacity];
+ this.backingArray = new short[initialCapacity];
}
/**
@@ -81,7 +81,7 @@
if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- return this.array[index];
+ return this.backingArray[index];
}
@Override
@@ -97,7 +97,7 @@
}
final ShortArrayBuilder returnBuilder = new ShortArrayBuilder(this.length);
for (int index = start; index < end; index ++) {
- returnBuilder.append(this.array[index]);
+ returnBuilder.append(this.backingArray[index]);
}
return returnBuilder;
}
@@ -105,7 +105,7 @@
@Override
public short[] toArray() {
final short[] returnArray = new short[this.length];
- System.arraycopy(this.array, 0, returnArray, 0, this.length);
+ System.arraycopy(this.backingArray, 0, returnArray, 0, this.length);
return returnArray;
}
@@ -136,7 +136,7 @@
* @see java.lang.AbstractStringBuilder#ensureCapacity(int)
*/
public void ensureCapacity(final int minimumCapacity) {
- if (minimumCapacity > this.array.length) {
+ if (minimumCapacity > this.backingArray.length) {
expandCapacity(minimumCapacity);
}
}
@@ -147,13 +147,13 @@
* @see This code is based on AbstractStringBuilder.expandCapacity(int).
*/
private void expandCapacity(final int minimumCapacity) {
- int newCapacity = (this.array.length + 1) * 2;
+ int newCapacity = (this.backingArray.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
- this.array = Arrays.copyOf(this.array, newCapacity);
+ this.backingArray = Arrays.copyOf(this.backingArray, newCapacity);
}
/**
@@ -163,7 +163,7 @@
*/
public ShortArrayBuilder append(final short shortToAppend) {
ensureCapacity(this.length + 1);
- this.array[this.length] = shortToAppend;
+ this.backingArray[this.length] = shortToAppend;
this.length ++;
return this;
}
@@ -176,7 +176,7 @@
public ShortArrayBuilder append(final short... shortsToAppend) {
ensureCapacity(this.length + shortsToAppend.length);
for (int index = 0; index < shortsToAppend.length; index ++) {
- this.array[this.length] = shortsToAppend[index];
+ this.backingArray[this.length] = shortsToAppend[index];
this.length ++;
}
return this;
@@ -190,7 +190,7 @@
public ShortArrayBuilder append(final ShortSequence newContent) {
this.ensureCapacity(length() + newContent.length());
for (int index = 0; index < newContent.length(); index ++) {
- this.array[this.length + index] = newContent.shortAt(index);
+ this.backingArray[this.length + index] = newContent.shortAt(index);
length ++;
}
return this;
@@ -204,10 +204,10 @@
* @throws IndexOutOfBoundsException If the {@code index} is negative or greater than or equal to {@link #length()}.
*/
public ShortArrayBuilder replace(final int index, final short newValue) throws IndexOutOfBoundsException {
- if ((index < 0) || (index >= this.array.length)) {
+ if ((index < 0) || (index >= this.backingArray.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- this.array[index] = newValue;
+ this.backingArray[index] = newValue;
return this;
}
@@ -221,7 +221,7 @@
if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of range: " + index);
}
- System.arraycopy(this.array, index + 1, this.array, index, this.length - index - 1);
+ System.arraycopy(this.backingArray, index + 1, this.backingArray, index, this.length - index - 1);
this.length--;
return this;
}
@@ -250,7 +250,7 @@
}
final int len = adjustedEnd - start;
if (len > 0) {
- System.arraycopy(this.array, start + len, this.array, start, this.length - adjustedEnd);
+ System.arraycopy(this.backingArray, start + len, this.backingArray, start, this.length - adjustedEnd);
this.length -= len;
}
return this;
@@ -286,35 +286,35 @@
@Override
public void setShortAt(final int index, final short newShort) {
- this.array[index] = newShort;
+ this.backingArray[index] = newShort;
}
@Override
public void sort() {
- Arrays.sort(this.array);
+ Arrays.sort(this.backingArray);
}
@Override
public void swap(final int index1, final int index2) {
- final short temp = this.array[index1];
- this.array[index1] = this.array[index2];
- this.array[index2] = temp;
+ final short temp = this.backingArray[index1];
+ this.backingArray[index1] = this.backingArray[index2];
+ this.backingArray[index2] = temp;
}
@Override
public int binarySearch(final short value) {
- return Arrays.binarySearch(this.array, 0, length(), value);
+ return Arrays.binarySearch(this.backingArray, 0, length(), value);
}
@Override
public int capacity() {
- return this.array.length;
+ return this.backingArray.length;
}
@Override
public void trimToSize() {
- if (this.length < this.array.length) {
- this.array = Arrays.copyOf(this.array, this.length);
+ if (this.length < this.backingArray.length) {
+ this.backingArray = Arrays.copyOf(this.backingArray, this.length);
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-23 13:17:39
|
Revision: 11915
http://sourceforge.net/p/foray/code/11915
Author: victormote
Date: 2021-10-23 13:17:36 +0000 (Sat, 23 Oct 2021)
Log Message:
-----------
Add toString() method, mostly for debugging.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java 2021-10-22 14:40:33 UTC (rev 11914)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java 2021-10-23 13:17:36 UTC (rev 11915)
@@ -84,14 +84,16 @@
this.length = 0;
}
- /**
- * Returns the number of elements used in the backing array.
- * @return The number of elements used in the backing array.
- */
+ @Override
public int length() {
return this.length;
}
+ @Override
+ public String toString() {
+ return new String(backingArray, 0, this.length);
+ }
+
/**
* Returns the current capacity of the backing array, i.e. the number of items that the array can hold without
* needing to increase its size.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-22 14:40:36
|
Revision: 11914
http://sourceforge.net/p/foray/code/11914
Author: victormote
Date: 2021-10-22 14:40:33 +0000 (Fri, 22 Oct 2021)
Log Message:
-----------
Rename instance variable for consistency with sister classes.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java 2021-10-22 14:40:33 UTC (rev 11914)
@@ -75,8 +75,8 @@
/** The encapsulated array. */
private byte[] backingArray;
- /** Index to to the next free item in {@link #backingArray}. */
- private int nextIndex;
+ /** The number of bytes in {@link #backingArray} actually used. */
+ private int length;
/**
* No-argument constructor.
@@ -96,7 +96,7 @@
this.blockSize = ByteArrayBuilder.DEFAULT_BLOCK_SIZE;
}
this.backingArray = new byte[this.blockSize];
- this.nextIndex = 0;
+ this.length = 0;
}
/**
@@ -104,7 +104,7 @@
* @return The number of elements used in the backing array.
*/
public int length() {
- return this.nextIndex;
+ return this.length;
}
/**
@@ -119,15 +119,15 @@
@Override
public ByteArrayBuilder append(final byte value) {
ensureCapacity(1);
- set(this.nextIndex, value);
- this.nextIndex ++;
+ set(this.length, value);
+ this.length ++;
return this;
}
public ByteArrayBuilder append(final byte ... bytes) {
ensureCapacity(length() + bytes.length);
- System.arraycopy(bytes, 0, this.backingArray, this.nextIndex, bytes.length);
- this.nextIndex += bytes.length;
+ System.arraycopy(bytes, 0, this.backingArray, this.length, bytes.length);
+ this.length += bytes.length;
return this;
}
@@ -141,7 +141,7 @@
final byte[] array = IOUtils.toByteArray(stream);
if (length() < 1) {
this.backingArray = array;
- this.nextIndex = array.length;
+ this.length = array.length;
} else {
append(array);
}
@@ -221,8 +221,8 @@
* @return The index to the beginning of the allocated elements (bytes).
*/
public int alloc(final int size) {
- final int index = this.nextIndex;
- if (this.nextIndex + size >= this.backingArray.length) {
+ final int index = this.length;
+ if (this.length + size >= this.backingArray.length) {
final byte[] newArray = new byte[this.backingArray.length
+ this.blockSize];
System.arraycopy(this.backingArray, 0, newArray, 0,
@@ -229,7 +229,7 @@
this.backingArray.length);
this.backingArray = newArray;
}
- this.nextIndex += size;
+ this.length += size;
return index;
}
@@ -309,9 +309,9 @@
* unused capacity.
*/
public void trimToSize() {
- if (this.nextIndex < this.backingArray.length) {
- final byte[] aux = new byte[this.nextIndex];
- System.arraycopy(this.backingArray, 0, aux, 0, this.nextIndex);
+ if (this.length < this.backingArray.length) {
+ final byte[] aux = new byte[this.length];
+ System.arraycopy(this.backingArray, 0, aux, 0, this.length);
this.backingArray = aux;
}
}
@@ -318,7 +318,7 @@
@Override
public byte byteAt(final int index) {
- if ((index < 0) || (index >= this.nextIndex)) {
+ if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of bounds: " + index);
}
return this.backingArray[index];
@@ -340,7 +340,7 @@
@Override
public byte[] toArray() {
- return Arrays.copyOf(this.backingArray, this.nextIndex);
+ return Arrays.copyOf(this.backingArray, this.length);
}
// @Override
@@ -372,11 +372,11 @@
}
ensureCapacityInternal(newLength);
- while (this.nextIndex < newLength) {
- this.backingArray[this.nextIndex] = 0;
- this.nextIndex ++;
+ while (this.length < newLength) {
+ this.backingArray[this.length] = 0;
+ this.length ++;
}
- this.nextIndex = newLength;
+ this.length = newLength;
}
/**
@@ -423,16 +423,16 @@
@Override
public byte pop() {
final byte returnValue = peek();
- this.nextIndex --;
+ this.length --;
return returnValue;
}
@Override
public byte peek() {
- if (this.nextIndex < 1) {
+ if (this.length < 1) {
throw new EmptyStackException();
}
- return this.backingArray[this.nextIndex - 1];
+ return this.backingArray[this.length - 1];
}
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java 2021-10-22 14:40:33 UTC (rev 11914)
@@ -60,8 +60,8 @@
/** The encapsulated array. */
private char[] backingArray;
- /** Index to to the next free item in {@link #backingArray}. */
- private int nextIndex;
+ /** The number of chars in {@link #backingArray} actually used. */
+ private int length;
/**
* No-argument constructor.
@@ -81,7 +81,7 @@
this.blockSize = CharArrayBuilder.DEFAULT_BLOCK_SIZE;
}
this.backingArray = new char[this.blockSize];
- this.nextIndex = 0;
+ this.length = 0;
}
/**
@@ -89,7 +89,7 @@
* @return The number of elements used in the backing array.
*/
public int length() {
- return this.nextIndex;
+ return this.length;
}
/**
@@ -108,8 +108,8 @@
* @return The index to the beginning of the allocated elements (chars).
*/
public int alloc(final int size) {
- final int index = this.nextIndex;
- if (this.nextIndex + size >= this.backingArray.length) {
+ final int index = this.length;
+ if (this.length + size >= this.backingArray.length) {
final char[] newArray = new char[this.backingArray.length
+ this.blockSize];
System.arraycopy(this.backingArray, 0, newArray, 0,
@@ -116,7 +116,7 @@
this.backingArray.length);
this.backingArray = newArray;
}
- this.nextIndex += size;
+ this.length += size;
return index;
}
@@ -196,9 +196,9 @@
* unused capacity.
*/
public void trimToSize() {
- if (this.nextIndex < this.backingArray.length) {
- final char[] aux = new char[this.nextIndex];
- System.arraycopy(this.backingArray, 0, aux, 0, this.nextIndex);
+ if (this.length < this.backingArray.length) {
+ final char[] aux = new char[this.length];
+ System.arraycopy(this.backingArray, 0, aux, 0, this.length);
this.backingArray = aux;
}
}
@@ -217,11 +217,11 @@
}
ensureCapacityInternal(newLength);
- while (this.nextIndex < newLength) {
- this.backingArray[this.nextIndex] = 0;
- this.nextIndex ++;
+ while (this.length < newLength) {
+ this.backingArray[this.length] = 0;
+ this.length ++;
}
- this.nextIndex = newLength;
+ this.length = newLength;
}
/**
@@ -244,17 +244,17 @@
@Override
public char lastChar() {
- return this.charAt(this.nextIndex - 1);
+ return this.charAt(this.length - 1);
}
@Override
public char[] toArray() {
- return Arrays.copyOf(this.backingArray, this.nextIndex);
+ return Arrays.copyOf(this.backingArray, this.length);
}
@Override
public char charAt(final int index) {
- if ((index < 0) || (index >= this.nextIndex)) {
+ if ((index < 0) || (index >= this.length)) {
throw new IndexOutOfBoundsException("Index out of bounds: " + index);
}
return this.backingArray[index];
@@ -267,7 +267,7 @@
@Override
public void setCharAt(final int index, final char ch) {
- if ((index < 0) || (index >= this.nextIndex)) {
+ if ((index < 0) || (index >= this.length)) {
throw new StringIndexOutOfBoundsException(index);
}
this.backingArray[index] = ch;
@@ -287,16 +287,16 @@
@Override
public CharArrayBuilder append(final char charToAppend) {
- ensureCapacity(this.nextIndex + 1);
- this.backingArray[this.nextIndex] = charToAppend;
- this.nextIndex ++;
+ ensureCapacity(this.length + 1);
+ this.backingArray[this.length] = charToAppend;
+ this.length ++;
return this;
}
public CharArrayBuilder append(final char ... chars) {
ensureCapacity(length() + chars.length);
- System.arraycopy(chars, 0, this.backingArray, this.nextIndex, chars.length);
- this.nextIndex += chars.length;
+ System.arraycopy(chars, 0, this.backingArray, this.length, chars.length);
+ this.length += chars.length;
return this;
}
@@ -307,10 +307,10 @@
*/
public CharArrayBuilder append(final CharSequence newContent) {
ensureCapacity(length() + newContent.length());
- final int startingLength = this.nextIndex;
+ final int startingLength = this.length;
for (int index = 0; index < newContent.length(); index ++) {
this.backingArray[startingLength + index] = newContent.charAt(index);
- this.nextIndex ++;
+ this.length ++;
}
return this;
}
@@ -332,16 +332,16 @@
@Override
public char pop() {
final char returnValue = peek();
- this.nextIndex --;
+ this.length --;
return returnValue;
}
@Override
public char peek() {
- if (this.nextIndex < 1) {
+ if (this.length < 1) {
throw new EmptyStackException();
}
- return this.backingArray[this.nextIndex - 1];
+ return this.backingArray[this.length - 1];
}
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java 2021-10-22 14:40:33 UTC (rev 11914)
@@ -52,7 +52,7 @@
/** The internal array. */
private int[] array;
- /** The number of bytes actually used in the array. */
+ /** The number of ints in {@link #backingArray} actually used. */
private int length;
/**
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java 2021-10-22 14:40:33 UTC (rev 11914)
@@ -54,7 +54,7 @@
/** The internal array. */
private long[] array;
- /** The number of longs actually used in the array. */
+ /** The number of longs in {@link #backingArray} actually used. */
private int length;
/**
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java 2021-10-22 14:40:33 UTC (rev 11914)
@@ -52,7 +52,7 @@
/** The internal array. */
private short[] array;
- /** The number of shorts actually used in the array. */
+ /** The number of shorts in {@link #backingArray} actually used. */
private int length;
/**
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-22 14:28:21
|
Revision: 11913
http://sourceforge.net/p/foray/code/11913
Author: victormote
Date: 2021-10-22 14:28:18 +0000 (Fri, 22 Oct 2021)
Log Message:
-----------
Conform to aXSL changes adding stack-like methods.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java
trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java 2021-10-22 10:44:32 UTC (rev 11912)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ByteArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
@@ -44,6 +44,7 @@
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Arrays;
+import java.util.EmptyStackException;
/**
* A mutable array of bytes.
@@ -115,11 +116,7 @@
return this.backingArray.length;
}
- /**
- * Adds one byte.
- * @param value The byte to be added.
- * @return this.
- */
+ @Override
public ByteArrayBuilder append(final byte value) {
ensureCapacity(1);
set(this.nextIndex, value);
@@ -417,4 +414,25 @@
this.backingArray[index2] = temp;
}
+ @Override
+ public byte push(final byte newItem) {
+ append(newItem);
+ return newItem;
+ }
+
+ @Override
+ public byte pop() {
+ final byte returnValue = peek();
+ this.nextIndex --;
+ return returnValue;
+ }
+
+ @Override
+ public byte peek() {
+ if (this.nextIndex < 1) {
+ throw new EmptyStackException();
+ }
+ return this.backingArray[this.nextIndex - 1];
+ }
+
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java 2021-10-22 10:44:32 UTC (rev 11912)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/CharArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
@@ -31,6 +31,7 @@
import org.axsl.common.sequence.CharSequenceMutable;
import java.util.Arrays;
+import java.util.EmptyStackException;
/**
* A mutable array of chars that resizes itself as necessary.
@@ -284,11 +285,7 @@
this.backingArray[index2] = temp;
}
- /**
- * Append a char to this sequence.
- * @param charToAppend The char to append.
- * @return This object.
- */
+ @Override
public CharArrayBuilder append(final char charToAppend) {
ensureCapacity(this.nextIndex + 1);
this.backingArray[this.nextIndex] = charToAppend;
@@ -309,7 +306,7 @@
* @return This object.
*/
public CharArrayBuilder append(final CharSequence newContent) {
- this.ensureCapacity(length() + newContent.length());
+ ensureCapacity(length() + newContent.length());
final int startingLength = this.nextIndex;
for (int index = 0; index < newContent.length(); index ++) {
this.backingArray[startingLength + index] = newContent.charAt(index);
@@ -326,4 +323,25 @@
return new CharArray(toArray());
}
+ @Override
+ public char push(final char newItem) {
+ append(newItem);
+ return newItem;
+ }
+
+ @Override
+ public char pop() {
+ final char returnValue = peek();
+ this.nextIndex --;
+ return returnValue;
+ }
+
+ @Override
+ public char peek() {
+ if (this.nextIndex < 1) {
+ throw new EmptyStackException();
+ }
+ return this.backingArray[this.nextIndex - 1];
+ }
+
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java 2021-10-22 10:44:32 UTC (rev 11912)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/IntArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
@@ -32,6 +32,7 @@
import org.axsl.common.sequence.IntSequenceMutable;
import java.util.Arrays;
+import java.util.EmptyStackException;
/**
* <p>A resizable collection of primitive {@link Integer#TYPE}, a subset of what is available in {@link StringBuilder},
@@ -305,18 +306,13 @@
return this;
}
- /**
- * Appends an item to the end of the array (pushes an item onto the stack).
- * @param intToPush The int to be added to the end of the array.
- */
- public void push(final int intToPush) {
- this.append(intToPush);
+ @Override
+ public int push(final int intToPush) {
+ append(intToPush);
+ return intToPush;
}
- /**
- * Removes the last item in the array (the top of the stack) and returns it.
- * @return The item that was at the top of the stack before the pop.
- */
+ @Override
public int pop() {
final int index = this.length - 1;
final int returnValue = intAt(index);
@@ -324,11 +320,11 @@
return returnValue;
}
- /**
- * Returns the last item in the array (the top of the stack) without changing the array.
- * @return The last item in the array.
- */
+ @Override
public int peek() {
+ if (this.length < 1) {
+ throw new EmptyStackException();
+ }
final int index = this.length - 1;
return intAt(index);
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java 2021-10-22 10:44:32 UTC (rev 11912)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/LongArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
@@ -34,6 +34,7 @@
import org.axsl.common.sequence.LongSequencePlus;
import java.util.Arrays;
+import java.util.EmptyStackException;
/**
* <p>A resizable collection of primitive {@link Long#TYPE}, a subset of what is available in {@link StringBuilder},
@@ -275,18 +276,13 @@
return this;
}
- /**
- * Appends an item to the end of the array (pushes an item onto the stack).
- * @param longToPush The long to be added to the end of the array.
- */
- public void push(final long longToPush) {
- this.append(longToPush);
+ @Override
+ public long push(final long longToPush) {
+ append(longToPush);
+ return longToPush;
}
- /**
- * Removes the last item in the array (the top of the stack) and returns it.
- * @return The item that was at the top of the stack before the pop.
- */
+ @Override
public long pop() {
final int index = this.length - 1;
final long returnValue = longAt(index);
@@ -294,11 +290,11 @@
return returnValue;
}
- /**
- * Returns the last item in the array (the top of the stack) without changing the array.
- * @return The last item in the array.
- */
+ @Override
public long peek() {
+ if (this.length < 1) {
+ throw new EmptyStackException();
+ }
final int index = this.length - 1;
return longAt(index);
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java 2021-10-22 10:44:32 UTC (rev 11912)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/sequence/ShortArrayBuilder.java 2021-10-22 14:28:18 UTC (rev 11913)
@@ -32,6 +32,7 @@
import org.axsl.common.sequence.ShortSequenceMutable;
import java.util.Arrays;
+import java.util.EmptyStackException;
/**
* <p>A resizable collection of primitive {@link Short#TYPE}, a subset of what is available in {@link StringBuilder},
@@ -255,18 +256,13 @@
return this;
}
- /**
- * Appends an item to the end of the array (pushes an item onto the stack).
- * @param shortToPush The long to be added to the end of the array.
- */
- public void push(final short shortToPush) {
- this.append(shortToPush);
+ @Override
+ public short push(final short shortToPush) {
+ append(shortToPush);
+ return shortToPush;
}
- /**
- * Removes the last item in the array (the top of the stack) and returns it.
- * @return The item that was at the top of the stack before the pop.
- */
+ @Override
public short pop() {
final int index = this.length - 1;
final short returnValue = shortAt(index);
@@ -274,11 +270,11 @@
return returnValue;
}
- /**
- * Returns the last item in the array (the top of the stack) without changing the array.
- * @return The last item in the array.
- */
- public long peek() {
+ @Override
+ public short peek() {
+ if (this.length < 1) {
+ throw new EmptyStackException();
+ }
final int index = this.length - 1;
return shortAt(index);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-22 10:44:34
|
Revision: 11912
http://sourceforge.net/p/foray/code/11912
Author: victormote
Date: 2021-10-22 10:44:32 +0000 (Fri, 22 Oct 2021)
Log Message:
-----------
Clean up optimization code a bit. Remove unnecessary and confusing method from iterator.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java
trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java 2021-10-21 14:17:48 UTC (rev 11911)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/data/TernaryTree.java 2021-10-22 10:44:32 UTC (rev 11912)
@@ -547,17 +547,25 @@
* Balance the tree for best search performance.
*/
private void balance() {
+ /* Dump the existing tree into arrays. */
+ final String[] keysDump = new String[this.length];
+ final int[] valuesDump = new int[this.length];
+
+ final KeyValueIterator iter = new KeyValueIterator();
int i = 0;
- final int n = this.length;
- final String[] k = new String[n];
- final int[] v = new int[n];
- final KeyValueIterator iter = new KeyValueIterator();
while (iter.hasNext()) {
- v[i] = iter.getValue();
- k[i++] = iter.next().getKey();
+ final KeyValue kv = iter.next();
+ valuesDump[i] = kv.getValue();
+ keysDump[i] = kv.getKey();
+ i++;
}
+
+
+
+
+
init();
- insertBalanced(k, v, 0, n);
+ insertBalanced(keysDump, valuesDump, 0, keysDump.length);
// With uniform letter distribution sc[root] should be around 'm'
}
@@ -794,17 +802,6 @@
}
/**
- * Returns the value of the current key of the iterator.
- * @return The index value of the current key of the iterator.
- */
- public int getValue() {
- if (this.cur >= 0) {
- return TernaryTree.this.nodes.getEqual(this.cur);
- }
- return 0;
- }
-
- /**
* Move up one level in the tree??
* @return The index to the next level up in the tree??
*/
Modified: trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java
===================================================================
--- trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java 2021-10-21 14:17:48 UTC (rev 11911)
+++ trunk/foray/foray-common/src/test/java/org/foray/common/data/TernaryTreeTests.java 2021-10-22 10:44:32 UTC (rev 11912)
@@ -228,6 +228,7 @@
map.put("Hamlet", 'b');
map.put("The Tempest", 'c');
map.put("Henry V", 'd');
+ map.optimize();
final Iterator<TernaryTree.KeyValue> iterator = map.getIterator();
final List<TernaryTree.KeyValue> actual = new ArrayList<TernaryTree.KeyValue>();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-21 14:17:49
|
Revision: 11911
http://sourceforge.net/p/foray/code/11911
Author: victormote
Date: 2021-10-21 14:17:48 +0000 (Thu, 21 Oct 2021)
Log Message:
-----------
Extract method to better handle upcoming increases in complexity.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 12:56:15 UTC (rev 11910)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 14:17:48 UTC (rev 11911)
@@ -383,8 +383,7 @@
/* Treat as a misspelling. */
this.output.println("(no dictionary) " + word);
} else {
- final Word dictWord = dictionary.getWord(word);
- if (dictWord == null) {
+ if (flagThisWord(dictionary, word)) {
this.output.println("Not found: " + word);
}
}
@@ -416,6 +415,22 @@
}
+ /**
+ * Indicates whether a given word should be marked as misspelled, i.e. cannot be found in the appropriate
+ * dictionary(s) and cannot be accepted as a legitimate word any other way.
+ * @param dictionary The dictionary in which {@code word} is being sought.
+ * @param word The word being tested.
+ * @return True if and only if {@code word} is not found in {@code dict} and cannot be accepted as a legitimate
+ * word for any other reason.
+ */
+ private boolean flagThisWord(final SegmentDictionary dictionary, final CharSequence word) {
+ final Word dictWord = dictionary.getWord(word);
+ if (dictWord != null) {
+ return false;
+ }
+ return true;
+ }
+
@Override
public void characters(final char[] buffer, final int offset, final int length) {
this.charBuffer.append(buffer, offset, length);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-21 12:56:17
|
Revision: 11910
http://sourceforge.net/p/foray/code/11910
Author: victormote
Date: 2021-10-21 12:56:15 +0000 (Thu, 21 Oct 2021)
Log Message:
-----------
Use the dictionary to remove words found in it.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 12:27:22 UTC (rev 11909)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 12:56:15 UTC (rev 11910)
@@ -37,6 +37,7 @@
import org.axsl.common.i18n.Orthography;
import org.axsl.hyphen.HyphenationException;
+import org.axsl.hyphen.Word;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
@@ -373,7 +374,21 @@
final CharSequence word = words.get(index);
/* The word breaker can return empty words when the parsed text begins with non-word content. */
if (word.length() > 0) {
- this.output.println(word);
+ if (this.currentOrthographyConfig == null) {
+ /* Treat as a misspelling. */
+ this.output.println("(no config) " + word);
+ } else {
+ final SegmentDictionary dictionary = this.currentOrthographyConfig.getDictionary();
+ if (dictionary == null) {
+ /* Treat as a misspelling. */
+ this.output.println("(no dictionary) " + word);
+ } else {
+ final Word dictWord = dictionary.getWord(word);
+ if (dictWord == null) {
+ this.output.println("Not found: " + word);
+ }
+ }
+ }
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-21 12:27:23
|
Revision: 11909
http://sourceforge.net/p/foray/code/11909
Author: victormote
Date: 2021-10-21 12:27:22 +0000 (Thu, 21 Oct 2021)
Log Message:
-----------
1. Ignore empty words. 2. Ignore elements having no content.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 11:54:02 UTC (rev 11908)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 12:27:22 UTC (rev 11909)
@@ -63,6 +63,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
+import java.util.Arrays;
import java.util.List;
import java.util.Stack;
@@ -154,6 +155,9 @@
/** The Hyphenation server. */
private HyphenationServer4a server;
+ /** The list of elements having no content but that can be placed in the middle of a word. */
+ private List<String> elementIgnoreList = Arrays.asList(new String[] {"Page"});
+
/** The list of dictionaries that are currently active, i.e. that match the current orthography. */
// private List<Dictionary> currentDictionaries;
@@ -283,6 +287,12 @@
element.localName = localName;
element.qName = qName;
+ /* Some elements, having no content, can be placed in the middle of a word, making it look like that word is
+ * two words. Ignore such elements. */
+ if (this.elementIgnoreList.contains(localName)) {
+ return;
+ }
+
String languageAttr = null;
languageAttr = attributes.getValue("xml:lang");
if (languageAttr == null) {
@@ -344,6 +354,11 @@
if (this.elementStack.size() < 1) {
throw new SAXException("Element stack is empty but should not be.");
}
+ /* Some elements, having no content, can be placed in the middle of a word, making it look like that word is
+ * two words. Ignore such elements. */
+ if (this.elementIgnoreList.contains(localName)) {
+ return;
+ }
final Element element = this.elementStack.peek();
@@ -356,7 +371,10 @@
for (int index = 0; index < words.size(); index ++) {
if (index % 2 == 0) {
final CharSequence word = words.get(index);
- this.output.println(word);
+ /* The word breaker can return empty words when the parsed text begins with non-word content. */
+ if (word.length() > 0) {
+ this.output.println(word);
+ }
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-21 11:54:03
|
Revision: 11908
http://sourceforge.net/p/foray/code/11908
Author: victormote
Date: 2021-10-21 11:54:02 +0000 (Thu, 21 Oct 2021)
Log Message:
-----------
Instead of keeping a separate stack of orthography configs, recompute the current one from the element stack whenever an element is popped.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-19 02:38:04 UTC (rev 11907)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-21 11:54:02 UTC (rev 11908)
@@ -87,7 +87,10 @@
/** The qualified name, if specified. */
private String qName;
- /** The natural language specified in an xml:lang attribute, if specified. */
+ /** The orthography specified in an xml:lang attribute for this element, if any. */
+ private Orthography orthography;
+
+ /** The orthography configuration for this element, only if {@link #orthography} is not null. */
private OrthographyConfig orthographyConfig;
/**
@@ -104,6 +107,13 @@
returnValue &= ObjectUtils.safeEquals(qName, this.qName);
return returnValue;
}
+
+ @Override
+ public String toString() {
+ return String.format("Namespace: %1$s, Local Name: %2$s, QName: %3$s, Orthography: %4$s", this.namespace,
+ this.localName, this.qName, this.orthography);
+ }
+
}
/** Command-line status constant indicating that the command line itself was not properly formed. */
@@ -135,8 +145,8 @@
/** The element stack. */
private Stack<Element> elementStack = new Stack<Element>();
- /** The stack of orthography configurations. */
- private Stack<OrthographyConfig> orthographyConfigStack = new Stack<OrthographyConfig>();
+ /** The current orthography configuration. */
+ private OrthographyConfig currentOrthographyConfig;
/** The logger. */
private Logger logger = LoggerFactory.getLogger(SpellChecker.class);
@@ -280,9 +290,9 @@
}
if (languageAttr == null) {
- if (this.orthographyConfigStack.size() < 1) {
+ if (this.currentOrthographyConfig == null) {
final String message = String.format(
- "Line %1d, Column %2d: Orthography not specified.",
+ "Line %1$d, Column %2$d: Orthography not specified.",
this.locator.getLineNumber(), this.locator.getColumnNumber());
this.output.println(message);
}
@@ -302,18 +312,28 @@
* the text content. */
final Script4a script = Script4a.LATIN;
- final Orthography orthography = Orthography4a.find(language, country, script);
- final OrthographyConfig config = this.server.getOrthographyConfig(orthography);
- if (config == null) {
+ element.orthography = Orthography4a.find(language, country, script);
+ if (element.orthography == null) {
final String message = String.format(
- "Line %1d, Column %2d: Orthography not configured. Language: %3s, Country: %4s, Script: %5s",
+ "Line %1$d, Column %2$d: Orthography not found. Language: %3$s, Country: %4$s, Script: %5$s",
this.locator.getLineNumber(), this.locator.getColumnNumber(), languageString, countryString,
script.getAlphaCode());
this.output.println(message);
} else {
- element.orthographyConfig = config;
+ final OrthographyConfig config = this.server.getOrthographyConfig(element.orthography);
+ if (config == null) {
+ final String message = String.format(
+ "Line %1$d, Column %2$d: Unconfigured orthography. Language: %3$s, Country: %4$s,"
+ + " Script: %5$s",
+ this.locator.getLineNumber(), this.locator.getColumnNumber(), languageString, countryString,
+ script.getAlphaCode());
+ this.output.println(message);
+ } else {
+ element.orthographyConfig = config;
+ }
+ this.currentOrthographyConfig = config;
}
- this.orthographyConfigStack.push(config);
+
}
this.elementStack.push(element);
}
@@ -321,16 +341,15 @@
@Override
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
- if (this.orthographyConfigStack.size() < 1) {
+ if (this.elementStack.size() < 1) {
throw new SAXException("Element stack is empty but should not be.");
}
final Element element = this.elementStack.peek();
- final OrthographyConfig config = this.orthographyConfigStack.peek();
List<CharSequence> words = null;
- if (config != null) {
- words = config.getWordBreaker().breakIntoWords(this.charBuffer);
+ if (this.currentOrthographyConfig != null) {
+ words = this.currentOrthographyConfig.getWordBreaker().breakIntoWords(this.charBuffer);
}
if (words != null) {
@@ -345,13 +364,19 @@
/* Clear the character buffer. */
this.charBuffer.delete(0, this.charBuffer.length());
- /* This element should match the top of the element stack. Pop it. Also if it triggered a change in the
- * orthography config, pop that also. */
+ /* This element should match the top of the element stack. Pop it. */
if (element.matches(uri, localName, qName)) {
- if (element.orthographyConfig != null) {
- this.orthographyConfigStack.pop();
+ this.elementStack.pop();
+
+ /* Recompute the current orthography configuration. */
+ final int last = this.elementStack.size() - 1;
+ for (int index = last; index > -1; index --) {
+ final Element testElement = this.elementStack.get(index);
+ if (testElement.orthographyConfig != null) {
+ this.currentOrthographyConfig = testElement.orthographyConfig;
+ break;
+ }
}
- this.elementStack.pop();
} else {
throw new SAXException("Closing element does not match top of stack.");
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-19 02:38:07
|
Revision: 11907
http://sourceforge.net/p/foray/code/11907
Author: victormote
Date: 2021-10-19 02:38:04 +0000 (Tue, 19 Oct 2021)
Log Message:
-----------
Get orthography config and element stacks working.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-19 01:56:57 UTC (rev 11906)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-19 02:38:04 UTC (rev 11907)
@@ -64,6 +64,7 @@
import java.io.PrintStream;
import java.net.URL;
import java.util.List;
+import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
@@ -87,7 +88,7 @@
private String qName;
/** The natural language specified in an xml:lang attribute, if specified. */
-// private Orthography orthography;
+ private OrthographyConfig orthographyConfig;
/**
* Checks whether a set of element descriptor items match this instance.
@@ -122,7 +123,7 @@
/** The locator instance for identifying the document, line, and column
* number of specific elements. */
-// private Locator locator;
+ private Locator locator;
/** The entity resolver to be used for resolving Dtd catalogs and other
* entities. */
@@ -132,10 +133,10 @@
private StringBuilder charBuffer = new StringBuilder();
/** The element stack. */
-// private Stack<Element> elementStack = new Stack<Element>();
+ private Stack<Element> elementStack = new Stack<Element>();
- /** The stack of orthographies. */
-// private Stack<Orthography> orthographyStack = new Stack<Orthography>();
+ /** The stack of orthography configurations. */
+ private Stack<OrthographyConfig> orthographyConfigStack = new Stack<OrthographyConfig>();
/** The logger. */
private Logger logger = LoggerFactory.getLogger(SpellChecker.class);
@@ -143,9 +144,6 @@
/** The Hyphenation server. */
private HyphenationServer4a server;
- /** The configuration for the current orthography. */
- private OrthographyConfig orthographyConfig;
-
/** The list of dictionaries that are currently active, i.e. that match the current orthography. */
// private List<Dictionary> currentDictionaries;
@@ -264,7 +262,7 @@
@Override
public void setDocumentLocator(final Locator locator) {
-// this.locator = locator;
+ this.locator = locator;
}
@Override
@@ -274,8 +272,6 @@
element.namespace = uri;
element.localName = localName;
element.qName = qName;
- /* TODO: Remove nonsense operation below for incomplete code. */
- ObjectUtils.noOperation(element.matches(qName, localName, qName));
String languageAttr = null;
languageAttr = attributes.getValue("xml:lang");
@@ -283,7 +279,14 @@
languageAttr = attributes.getValue(XmlUtils.XML_NAMESPACE_URI, "lang");
}
- if (languageAttr != null) {
+ if (languageAttr == null) {
+ if (this.orthographyConfigStack.size() < 1) {
+ final String message = String.format(
+ "Line %1d, Column %2d: Orthography not specified.",
+ this.locator.getLineNumber(), this.locator.getColumnNumber());
+ this.output.println(message);
+ }
+ } else {
/* Is there a country code? */
final String[] languageAndCountry = languageAttr.split("-");
final String languageString = languageAndCountry[0];
@@ -300,22 +303,37 @@
final Script4a script = Script4a.LATIN;
final Orthography orthography = Orthography4a.find(language, country, script);
- this.orthographyConfig = this.server.getOrthographyConfig(orthography);
- if (this.orthographyConfig == null) {
+ final OrthographyConfig config = this.server.getOrthographyConfig(orthography);
+ if (config == null) {
final String message = String.format(
- "Orthography not configured. Language: %1s, Country: %2s, Script: %3s", languageString,
- countryString, script.getAlphaCode());
+ "Line %1d, Column %2d: Orthography not configured. Language: %3s, Country: %4s, Script: %5s",
+ this.locator.getLineNumber(), this.locator.getColumnNumber(), languageString, countryString,
+ script.getAlphaCode());
this.output.println(message);
+ } else {
+ element.orthographyConfig = config;
}
+ this.orthographyConfigStack.push(config);
}
+ this.elementStack.push(element);
}
@Override
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
- if (this.orthographyConfig != null) {
- final List<CharSequence> words = this.orthographyConfig.getWordBreaker().breakIntoWords(this.charBuffer);
- this.charBuffer.delete(0, this.charBuffer.length() - 1);
+ if (this.orthographyConfigStack.size() < 1) {
+ throw new SAXException("Element stack is empty but should not be.");
+ }
+
+ final Element element = this.elementStack.peek();
+ final OrthographyConfig config = this.orthographyConfigStack.peek();
+
+ List<CharSequence> words = null;
+ if (config != null) {
+ words = config.getWordBreaker().breakIntoWords(this.charBuffer);
+ }
+
+ if (words != null) {
for (int index = 0; index < words.size(); index ++) {
if (index % 2 == 0) {
final CharSequence word = words.get(index);
@@ -323,6 +341,20 @@
}
}
}
+
+ /* Clear the character buffer. */
+ this.charBuffer.delete(0, this.charBuffer.length());
+
+ /* This element should match the top of the element stack. Pop it. Also if it triggered a change in the
+ * orthography config, pop that also. */
+ if (element.matches(uri, localName, qName)) {
+ if (element.orthographyConfig != null) {
+ this.orthographyConfigStack.pop();
+ }
+ this.elementStack.pop();
+ } else {
+ throw new SAXException("Closing element does not match top of stack.");
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-19 01:56:59
|
Revision: 11906
http://sourceforge.net/p/foray/code/11906
Author: victormote
Date: 2021-10-19 01:56:57 +0000 (Tue, 19 Oct 2021)
Log Message:
-----------
Introduce the orthography config into the spell-checker.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/i18n/Country4a.java
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/i18n/Country4a.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/i18n/Country4a.java 2021-10-18 22:27:01 UTC (rev 11905)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/i18n/Country4a.java 2021-10-19 01:56:57 UTC (rev 11906)
@@ -406,6 +406,9 @@
* @return The instance matching {@code countryCode}, or null if the code has not been registered.
*/
public static Country4a findFromAlpha(final String countryCode) {
+ if (countryCode == null) {
+ return null;
+ }
/* Convert to uppercase, since the raw data is in that format. */
final String normalizedCountryCode = countryCode.toUpperCase().trim();
Country4a theInstance = findFrom2Char(normalizedCountryCode);
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-18 22:27:01 UTC (rev 11905)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-19 01:56:57 UTC (rev 11906)
@@ -38,6 +38,13 @@
import org.axsl.common.i18n.Orthography;
import org.axsl.hyphen.HyphenationException;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
@@ -55,6 +62,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
+import java.net.URL;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
@@ -133,22 +141,29 @@
private Logger logger = LoggerFactory.getLogger(SpellChecker.class);
/** The Hyphenation server. */
-// private HyphenationServer4a server;
+ private HyphenationServer4a server;
- /** The word-breaker instance. */
- private WordBreaker wordBreaker;
+ /** The configuration for the current orthography. */
+ private OrthographyConfig orthographyConfig;
+ /** The list of dictionaries that are currently active, i.e. that match the current orthography. */
+// private List<Dictionary> currentDictionaries;
+
/**
* Constructor.
* @param input The input source encapsulating the document to be spell-checked.
* @param output The output stream to which the spelling errors should be written.
+ * @param orthographyConfigPath The path to the orthography configuration.
* @throws HyphenationException
*/
- public SpellChecker(final InputSource input, final PrintStream output) throws HyphenationException {
+ public SpellChecker(final InputSource input, final PrintStream output, final URL orthographyConfigPath)
+ throws HyphenationException {
this.input = input;
this.output = output;
-// this.server = new HyphenationServer4a(null);
- this.wordBreaker = new WordBreakerLatin1();
+
+ final HyphenationServerConfig serverConfig = new HyphenationServerConfig();
+ serverConfig.setOrthographyConfigurationLocation(orthographyConfigPath);
+ this.server = new HyphenationServer4a(serverConfig);
}
public void start() throws SAXException, ParserConfigurationException, IOException {
@@ -179,14 +194,14 @@
this.logger.error("Parser unable to supply namespace-prefixes.");
}
- /* Turn on validation if it is available. */
- try {
- parser.setFeature("http://xml.org/sax/features/validation", true);
- } catch (final SAXNotRecognizedException e1) {
- this.logger.error("Parser does not recognize the \"validation\" feature.");
- } catch (final SAXNotSupportedException e1) {
- this.logger.error("Parser unable to validate.");
- }
+ /* Skip validation. That should be handled upstream of this process. */
+// try {
+// parser.setFeature("http://xml.org/sax/features/validation", true);
+// } catch (final SAXNotRecognizedException e1) {
+// this.logger.error("Parser does not recognize the \"validation\" feature.");
+// } catch (final SAXNotSupportedException e1) {
+// this.logger.error("Parser unable to validate.");
+// }
/* Turn on "notify-char-refs" feature.
* Sadly, this only works with Xerces.
@@ -285,10 +300,13 @@
final Script4a script = Script4a.LATIN;
final Orthography orthography = Orthography4a.find(language, country, script);
-
- /* TODO: Fix this. */
- ObjectUtils.noOperation(orthography);
-// final OrthographyConfig config = this.server.getOrthographyConfig(orthography);
+ this.orthographyConfig = this.server.getOrthographyConfig(orthography);
+ if (this.orthographyConfig == null) {
+ final String message = String.format(
+ "Orthography not configured. Language: %1s, Country: %2s, Script: %3s", languageString,
+ countryString, script.getAlphaCode());
+ this.output.println(message);
+ }
}
}
@@ -295,12 +313,14 @@
@Override
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
- final List<CharSequence> words = this.wordBreaker.breakIntoWords(this.charBuffer);
- this.charBuffer.delete(0, this.charBuffer.length() - 1);
- for (int index = 0; index < words.size(); index ++) {
- if (index % 2 == 0) {
- final CharSequence word = words.get(index);
- this.output.println(word);
+ if (this.orthographyConfig != null) {
+ final List<CharSequence> words = this.orthographyConfig.getWordBreaker().breakIntoWords(this.charBuffer);
+ this.charBuffer.delete(0, this.charBuffer.length() - 1);
+ for (int index = 0; index < words.size(); index ++) {
+ if (index % 2 == 0) {
+ final CharSequence word = words.get(index);
+ this.output.println(word);
+ }
}
}
}
@@ -311,10 +331,52 @@
this.charBuffer.append(buffer, offset, length);
}
+ /**
+ * Returns the command-line options for the {@link #main(String[])} method.
+ * @return Command-line options.
+ */
+ private static Options getCommandLineOptions() {
+ final Options clOptions = new Options();
+ final Option input = new Option("i", "input", true, "path to the input file");
+ input.setRequired(true);
+// final Option output = new Option("o", "output", true, "path to the output (pretty-printed) file");
+// output.setRequired(true);
+// output.setRequired(true);
+ final Option dictionaries = new Option("c", "config", true, "path to orthography configuration");
+ clOptions.addOption(input);
+// clOptions.addOption(output);
+ clOptions.addOption(dictionaries);
+ return clOptions;
+ }
+
+ /**
+ * Command line interface.
+ * @param args The command-line arguments. There are two:
+ * <ol>
+ * <li>--input [input file path]</li>
+ * <li>--dict [dictionary file directory]</li>
+ * </ol>
+ */
public static void main(final String[] args) {
final Logger logger = LoggerFactory.getLogger(SpellChecker.class);
- final String input = args[0];
+ final Options commandLineOptions = SpellChecker.getCommandLineOptions();
+ final CommandLineParser commandLineParser = new DefaultParser();
+ CommandLine parsedCommandLine = null;
+ try {
+ parsedCommandLine = commandLineParser.parse(commandLineOptions, args);
+ } catch (final ParseException e) {
+ logger.error(e.getMessage(), e);
+ final HelpFormatter helpFormatter = new HelpFormatter();
+ helpFormatter.printHelp("java -cp $FORAY_CLASSPATH " + SpellChecker.class.getName(), commandLineOptions,
+ true);
+ /* CheckStyle: Allow System.exit() in main method. */
+ System.exit(SpellChecker.STATUS_COMMAND_LINE_ERROR);
+ }
+
+ final String input = parsedCommandLine.getOptionValue("input");
+ final String config = parsedCommandLine.getOptionValue("config");
+
InputStream inputStream = null;
try {
FileInputStream fis = null;
@@ -328,10 +390,13 @@
final InputSource inputSource = new InputSource(inputStream);
final PrintStream output = System.out;
+
try {
- final SpellChecker checker = new SpellChecker(inputSource, output);
+ final URL orthographyConfigPath = new URL(config);
+ final SpellChecker checker = new SpellChecker(inputSource, output, orthographyConfigPath);
checker.start();
} catch (final HyphenationException | IOException | SAXException | ParserConfigurationException e) {
+ logger.error("Error configuring or running: " + input, e);
/* CheckStyle: Allow System.exit() in main method. */
System.exit(SpellChecker.STATUS_PARSING_ERROR);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-18 22:27:04
|
Revision: 11905
http://sourceforge.net/p/foray/code/11905
Author: victormote
Date: 2021-10-18 22:27:01 +0000 (Mon, 18 Oct 2021)
Log Message:
-----------
Rough-in the start of a spell-checker class.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/primitive/ObjectUtils.java
trunk/foray/foray-common/src/main/java/org/foray/common/primitive/XmlUtils.java
Added Paths:
-----------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/primitive/ObjectUtils.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/primitive/ObjectUtils.java 2021-10-15 12:21:25 UTC (rev 11904)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/primitive/ObjectUtils.java 2021-10-18 22:27:01 UTC (rev 11905)
@@ -48,7 +48,7 @@
/**
* Adds a factor to an existing hash code computation.
* @param currentHash The value of the has before adding the new factor.
- * @param hashIncrement The value to be added to the existing hash. This represents the hash or other semi-uniqe
+ * @param hashIncrement The value to be added to the existing hash. This represents the hash or other semi-unique
* value of a significant component of the item being hashed.
* @return The new hash value.
* @see "Effective Java, Item 9"
@@ -59,4 +59,37 @@
return newHash;
}
+ /**
+ * Compares two objects for equality, first checking for null.
+ * @param object1 The first object to be tested.
+ * @param object2 The second object to be tested.
+ * @return True if 1) both objects are null, or 2) neither object is null and they are equal using
+ * {@link Object#equals(Object)}.
+ */
+ public static boolean safeEquals(final Object object1, final Object object2) {
+ if (object1 == null) {
+ if (object2 == null) {
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ if (object2 == null) {
+ return false;
+ } else {
+ return object1.equals(object2);
+ }
+ }
+ }
+
+ /**
+ * A null operation.
+ * This is useful (arguable) for the case where stub or incomplete code needs to avoid compiler or style warnings
+ * because an object is not used.
+ * @param object The object to be protected from the stigma of not being used.
+ */
+ public static void noOperation(final Object object) {
+ /* Does absolutely nothing, by design. */
+ }
+
}
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/primitive/XmlUtils.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/primitive/XmlUtils.java 2021-10-15 12:21:25 UTC (rev 11904)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/primitive/XmlUtils.java 2021-10-18 22:27:01 UTC (rev 11905)
@@ -41,13 +41,17 @@
public final class XmlUtils {
/**
- * The namespace declaration to be assigned to the namespace definition
- * in XML documents.
+ * The namespace declaration to be assigned to the namespace definition in XML documents.
* @see "http://www.w3.org/2000/xmlns/"
*/
public static final String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
/**
+ * The namespace declaration assigned to the reserved prefix "xml:".
+ */
+ public static final String XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
+
+ /**
* This class should never be instantiated.
*/
private XmlUtils() { }
Added: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java (rev 0)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java 2021-10-18 22:27:01 UTC (rev 11905)
@@ -0,0 +1,341 @@
+/*
+ * Copyright 2021 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.hyphen;
+
+import org.foray.common.i18n.Country4a;
+import org.foray.common.i18n.Language4a;
+import org.foray.common.i18n.Orthography4a;
+import org.foray.common.i18n.Script4a;
+import org.foray.common.primitive.ObjectUtils;
+import org.foray.common.primitive.XmlUtils;
+
+import org.axsl.common.i18n.Orthography;
+import org.axsl.hyphen.HyphenationException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.DefaultHandler2;
+
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.List;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+/**
+ * Parses a document, looking for spelling errors.
+ * The specified natural language can change at any time, and must be tracked
+ * General plan is to capture all of the text data in one CharSequence, then parse and compare to dictionary entries.
+ */
+public class SpellChecker extends DefaultHandler2 {
+
+ private class Element {
+
+ /** The namespace URI, if specified. */
+ private String namespace;
+
+ /** The local name, if specified. */
+ private String localName;
+
+ /** The qualified name, if specified. */
+ private String qName;
+
+ /** The natural language specified in an xml:lang attribute, if specified. */
+// private Orthography orthography;
+
+ /**
+ * Checks whether a set of element descriptor items match this instance.
+ * @param namespace The namespace URI.
+ * @param localName The local name.
+ * @param qName The qualified name.
+ * @return True all element descriptor items match this instance.
+ */
+ boolean matches(final String namespace, final String localName, final String qName) {
+ boolean returnValue = true;
+ returnValue &= ObjectUtils.safeEquals(namespace, this.namespace);
+ returnValue &= ObjectUtils.safeEquals(localName, this.localName);
+ returnValue &= ObjectUtils.safeEquals(qName, this.qName);
+ return returnValue;
+ }
+ }
+
+ /** Command-line status constant indicating that the command line itself was not properly formed. */
+ public static final byte STATUS_COMMAND_LINE_ERROR = 1;
+
+ /** Command-line return status constant indicating that a file was not found. */
+ public static final byte STATUS_FILE_NOT_FOUND = 2;
+
+ /** Command-line return status constant indicating that there was an error parsing the input file. */
+ public static final byte STATUS_PARSING_ERROR = 3;
+
+ /** The input source to be pretty-printed. */
+ private InputSource input;
+
+ /** The output stream to which the pretty-printed output should be sent. */
+ private PrintStream output;
+
+ /** The locator instance for identifying the document, line, and column
+ * number of specific elements. */
+// private Locator locator;
+
+ /** The entity resolver to be used for resolving Dtd catalogs and other
+ * entities. */
+// private EntityResolver entityResolver;
+
+ /** A reusable buffer. */
+ private StringBuilder charBuffer = new StringBuilder();
+
+ /** The element stack. */
+// private Stack<Element> elementStack = new Stack<Element>();
+
+ /** The stack of orthographies. */
+// private Stack<Orthography> orthographyStack = new Stack<Orthography>();
+
+ /** The logger. */
+ private Logger logger = LoggerFactory.getLogger(SpellChecker.class);
+
+ /** The Hyphenation server. */
+// private HyphenationServer4a server;
+
+ /** The word-breaker instance. */
+ private WordBreaker wordBreaker;
+
+ /**
+ * Constructor.
+ * @param input The input source encapsulating the document to be spell-checked.
+ * @param output The output stream to which the spelling errors should be written.
+ * @throws HyphenationException
+ */
+ public SpellChecker(final InputSource input, final PrintStream output) throws HyphenationException {
+ this.input = input;
+ this.output = output;
+// this.server = new HyphenationServer4a(null);
+ this.wordBreaker = new WordBreakerLatin1();
+ }
+
+ public void start() throws SAXException, ParserConfigurationException, IOException {
+ final XMLReader parser = createParser();
+
+ /* Bind the LexicalHandler to the XMLReader if possible. */
+ try {
+ parser.setProperty("http://xml.org/sax/properties/lexical-handler", this);
+ } catch (final SAXNotSupportedException e1) {
+ this.logger.error("Parser does not support LexicalHandler.");
+ }
+
+ /* Bind the DeclHandler to the XMLReader if possible. */
+ try {
+ parser.setProperty("http://xml.org/sax/properties/declaration-handler", this);
+ } catch (final SAXNotSupportedException e) {
+ this.logger.error("Parser does not support Declaration Handler.");
+ }
+
+ /* Turn on namespace-prefixes so that we get the namespace declarations
+ * returned with other attributes and can therefore write them out
+ * along with them. */
+ try {
+ parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
+ } catch (final SAXNotRecognizedException e1) {
+ this.logger.error("Parser does not recognize the \"namespace-prefixes\" feature.");
+ } catch (final SAXNotSupportedException e1) {
+ this.logger.error("Parser unable to supply namespace-prefixes.");
+ }
+
+ /* Turn on validation if it is available. */
+ try {
+ parser.setFeature("http://xml.org/sax/features/validation", true);
+ } catch (final SAXNotRecognizedException e1) {
+ this.logger.error("Parser does not recognize the \"validation\" feature.");
+ } catch (final SAXNotSupportedException e1) {
+ this.logger.error("Parser unable to validate.");
+ }
+
+ /* Turn on "notify-char-refs" feature.
+ * Sadly, this only works with Xerces.
+ * This feature, or something like it is very important.
+ * Without it, character entities get transformed into characters
+ * without notification.
+ * When notified, we can (and do) ignore the transformed characters
+ * and use the character entities instead.
+ * We do NOT want to change the user's content. */
+ try {
+ parser.setFeature("http://apache.org/xml/features/scanner/notify-char-refs", true);
+ } catch (final SAXNotRecognizedException e) {
+ /* Make this a fatal error. */
+ this.logger.error("Parser cannot report character entities. Aborting.");
+ cleanup();
+ return;
+ } catch (final SAXNotSupportedException e) {
+ /* Make this a fatal error. */
+ this.logger.error("Parser cannot report character entities. Aborting.");
+ cleanup();
+ return;
+ }
+ parser.setContentHandler(this);
+ parser.parse(this.input);
+ cleanup();
+ }
+
+ /**
+ * Creates a SAX parser.
+ * @return The created SAX parser.
+ * @throws SAXException For error creating parser.
+ * @throws ParserConfigurationException For error configuring parser.
+ */
+ public XMLReader createParser() throws SAXException, ParserConfigurationException {
+ final SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
+ spf.setNamespaceAware(true);
+ final XMLReader xmlReader = spf.newSAXParser().getXMLReader();
+// if (this.entityResolver != null) {
+// xmlReader.setEntityResolver(this.entityResolver);
+// }
+ return xmlReader;
+ }
+
+ /**
+ * Finalize the processing.
+ */
+ private void cleanup() {
+ this.output.close();
+ }
+
+ @Override
+ public void startDocument() throws SAXException {
+ }
+
+
+ @Override
+ public void endDocument() throws SAXException {
+ }
+
+
+ @Override
+ public void setDocumentLocator(final Locator locator) {
+// this.locator = locator;
+ }
+
+ @Override
+ public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
+ throws SAXException {
+ final Element element = new Element();
+ element.namespace = uri;
+ element.localName = localName;
+ element.qName = qName;
+ /* TODO: Remove nonsense operation below for incomplete code. */
+ ObjectUtils.noOperation(element.matches(qName, localName, qName));
+
+ String languageAttr = null;
+ languageAttr = attributes.getValue("xml:lang");
+ if (languageAttr == null) {
+ languageAttr = attributes.getValue(XmlUtils.XML_NAMESPACE_URI, "lang");
+ }
+
+ if (languageAttr != null) {
+ /* Is there a country code? */
+ final String[] languageAndCountry = languageAttr.split("-");
+ final String languageString = languageAndCountry[0];
+ String countryString = null;
+ if (languageAndCountry.length > 1) {
+ countryString = languageAndCountry[1];
+ }
+
+ final Language4a language = Language4a.findFromAlpha(languageString);
+ final Country4a country = Country4a.findFromAlpha(countryString);
+ /* TODO: The following should not be hard-coded this way.
+ * Instead, either specify in the XML document (i.e. as an attribute on the element itself), or implied by
+ * the text content. */
+ final Script4a script = Script4a.LATIN;
+
+ final Orthography orthography = Orthography4a.find(language, country, script);
+
+ /* TODO: Fix this. */
+ ObjectUtils.noOperation(orthography);
+// final OrthographyConfig config = this.server.getOrthographyConfig(orthography);
+ }
+ }
+
+
+ @Override
+ public void endElement(final String uri, final String localName, final String qName) throws SAXException {
+ final List<CharSequence> words = this.wordBreaker.breakIntoWords(this.charBuffer);
+ this.charBuffer.delete(0, this.charBuffer.length() - 1);
+ for (int index = 0; index < words.size(); index ++) {
+ if (index % 2 == 0) {
+ final CharSequence word = words.get(index);
+ this.output.println(word);
+ }
+ }
+ }
+
+
+ @Override
+ public void characters(final char[] buffer, final int offset, final int length) {
+ this.charBuffer.append(buffer, offset, length);
+ }
+
+ public static void main(final String[] args) {
+ final Logger logger = LoggerFactory.getLogger(SpellChecker.class);
+
+ final String input = args[0];
+ InputStream inputStream = null;
+ try {
+ FileInputStream fis = null;
+ fis = new FileInputStream(input);
+ inputStream = new BufferedInputStream(fis);
+ } catch (final FileNotFoundException e) {
+ logger.error("File cannot be opened for input: " + input, e);
+ /* CheckStyle: Allow System.exit() in main method. */
+ System.exit(SpellChecker.STATUS_FILE_NOT_FOUND);
+ }
+ final InputSource inputSource = new InputSource(inputStream);
+
+ final PrintStream output = System.out;
+ try {
+ final SpellChecker checker = new SpellChecker(inputSource, output);
+ checker.start();
+ } catch (final HyphenationException | IOException | SAXException | ParserConfigurationException e) {
+ /* CheckStyle: Allow System.exit() in main method. */
+ System.exit(SpellChecker.STATUS_PARSING_ERROR);
+ }
+
+ }
+
+}
Property changes on: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/SpellChecker.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Rev
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-10-15 12:21:27
|
Revision: 11904
http://sourceforge.net/p/foray/code/11904
Author: victormote
Date: 2021-10-15 12:21:25 +0000 (Fri, 15 Oct 2021)
Log Message:
-----------
Add logging info about what is being parsed.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryParser.java
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryResource.java
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionarySerializer.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryParser.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryParser.java 2021-07-05 17:56:00 UTC (rev 11903)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryParser.java 2021-10-15 12:21:25 UTC (rev 11904)
@@ -63,11 +63,12 @@
/**
* Parses a given InputStream and places the parsed information into the dictionary.
* @param inputStream The input stream to parse.
+ * @param description Description of {@literal inputStream}, useful for user messages.
* @throws IOException For IO errors during parsing.
* @return The parsed dictionary.
*/
- public SegmentDictionary parse(final InputStream inputStream) throws IOException {
- logger.info("Begin dictionary word list parsing.");
+ public SegmentDictionary parse(final InputStream inputStream, final String description) throws IOException {
+ logger.info("Begin dictionary word list parsing: " + description);
final InputStreamReader isReader = new InputStreamReader(inputStream);
final BufferedReader reader = new BufferedReader(isReader);
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryResource.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryResource.java 2021-07-05 17:56:00 UTC (rev 11903)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionaryResource.java 2021-10-15 12:21:25 UTC (rev 11904)
@@ -132,7 +132,7 @@
final DictionaryParser parser = new DictionaryParser();
SegmentDictionary dictionary = null;
try {
- dictionary = parser.parse(inputStream);
+ dictionary = parser.parse(inputStream, location.getLocationDescription());
} catch (final IOException e) {
getLogger().error("IOException", e);
return null;
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionarySerializer.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionarySerializer.java 2021-07-05 17:56:00 UTC (rev 11903)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/DictionarySerializer.java 2021-10-15 12:21:25 UTC (rev 11904)
@@ -124,7 +124,7 @@
/* Parse the dictionary. */
try {
final InputStream inputStream = infile.toURI().toURL().openStream();
- dictionary = parser.parse(inputStream);
+ dictionary = parser.parse(inputStream, infile.getAbsolutePath());
} catch (final IOException e) {
this.logger.error(e.getMessage());
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-07-05 17:56:02
|
Revision: 11903
http://sourceforge.net/p/foray/code/11903
Author: victormote
Date: 2021-07-05 17:56:00 +0000 (Mon, 05 Jul 2021)
Log Message:
-----------
Handle CDATA properly.
Modified Paths:
--------------
trunk/foray/foray-xml/src/main/java/org/foray/xml/ForayPretty.java
Modified: trunk/foray/foray-xml/src/main/java/org/foray/xml/ForayPretty.java
===================================================================
--- trunk/foray/foray-xml/src/main/java/org/foray/xml/ForayPretty.java 2021-02-09 01:03:02 UTC (rev 11902)
+++ trunk/foray/foray-xml/src/main/java/org/foray/xml/ForayPretty.java 2021-07-05 17:56:00 UTC (rev 11903)
@@ -128,6 +128,9 @@
* expansion or not. */
private boolean inEntity = false;
+ /** State variable tracking whether we are currently inside a CDATA section or not. */
+ private boolean inCdata = false;
+
/** Indicates whether the item on the top of the element stack has been
* written. */
private boolean topElementWritten = true;
@@ -519,7 +522,9 @@
return;
}
ensureTopElementWritten(false);
- if (this.breakPCDATA) {
+ if (this.inCdata) {
+ writeRawPCDATA(this.charBuffer.toString());
+ } else if (this.breakPCDATA) {
/* Note: SAX input has linefeeds normalized to \n. */
XmlUtils.normalizeWhitespace(this.charBuffer);
this.queue.add(this.charBuffer.toString());
@@ -568,6 +573,24 @@
}
@Override
+ public void startCDATA() throws SAXException {
+ flushCharacters();
+ ensureTopElementWritten(false);
+ this.queue.flush();
+ writeRawPCDATA("<![CDATA[");
+ this.inCdata = true;
+ }
+
+ @Override
+ public void endCDATA() throws SAXException {
+ flushCharacters();
+ ensureTopElementWritten(false);
+// this.queue.flush();
+ writeRawPCDATA("]]>");
+ this.inCdata = false;
+ }
+
+ @Override
public void startDTD(final String name, final String publicId,
final String systemId) {
write(newLine());
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-09 01:03:10
|
Revision: 11902
http://sourceforge.net/p/foray/code/11902
Author: victormote
Date: 2021-02-09 01:03:02 +0000 (Tue, 09 Feb 2021)
Log Message:
-----------
Rename classes for consistency.
Modified Paths:
--------------
trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java
Added Paths:
-----------
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap00Ttf4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04Ttf4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04TtfEntry4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12Ttf4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12TtfEntry4a.java
Removed Paths:
-------------
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12Entry.java
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java 2021-02-08 23:30:03 UTC (rev 11901)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -31,8 +31,8 @@
import org.foray.common.sequence.IntArrayBuilder;
import org.foray.ps.encode.CharSet4a;
import org.foray.ps.encode.CharSetParser;
-import org.foray.ps.encode.Cmap4a04;
-import org.foray.ps.encode.Cmap4a12;
+import org.foray.ps.encode.Cmap04Ttf4a;
+import org.foray.ps.encode.Cmap12Ttf4a;
import org.foray.ps.encode.Encoding4aLatinExtra;
import org.foray.ps.encode.EncodingCe;
import org.foray.ps.encode.EncodingCustom;
@@ -302,12 +302,12 @@
@Override
public Cmap04Ttf makeCmap04(final String name, final int expectedEntries) {
- return new Cmap4a04(name, expectedEntries);
+ return new Cmap04Ttf4a(name, expectedEntries);
}
@Override
public Cmap12Ttf makeCmap12(final String name, final int expectedEntries) {
- return new Cmap4a12(name, expectedEntries);
+ return new Cmap12Ttf4a(name, expectedEntries);
}
}
Copied: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap00Ttf4a.java (from rev 11898, trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java)
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap00Ttf4a.java (rev 0)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap00Ttf4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2006 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.Cmap;
+
+/**
+ * Abstract superclass for all Cmap4a Tables.
+ */
+public abstract class Cmap00Ttf4a implements Cmap {
+
+ /** Constant needed for serialization. */
+ public static final long serialVersionUID = -6516855352094857929L;
+
+ @Override
+ public boolean canEncode(final int codePoint) {
+ return this.encode(codePoint) > -1;
+ }
+
+}
Copied: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04Ttf4a.java (from rev 11901, trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java)
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04Ttf4a.java (rev 0)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04Ttf4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -0,0 +1,157 @@
+/*
+ * 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.Cmap04Ttf;
+
+/**
+ * Class containing a set of 16-bit Unicode ranges that sequentially map to the same-sized range of glyph indexes.
+ * The contents are analogous to a Format 4 TrueType Font cmap table (from which this class derives its name).
+ */
+public class Cmap04Ttf4a extends Cmap00Ttf4a implements Cmap04Ttf {
+
+ /** Constant needed for serialization. */
+ public static final long serialVersionUID = 985957847129472354L;
+
+ /** The name of this encoding. */
+ private String name;
+
+ /** The array of entries. */
+ private Cmap04TtfEntry4a[] entries;
+
+ /** Counter to the last entry added. */
+ private int lastEntryAdded = -1;
+
+ /**
+ * Constructor.
+ * @param name The name of this encoding.
+ * @param expectedEntries The number of expected entries in this encoding.
+ * Setting this to the correct value will result in more efficient
+ * processing.
+ */
+ public Cmap04Ttf4a(final String name, final int expectedEntries) {
+ this.name = name;
+ this.entries = new Cmap04TtfEntry4a[expectedEntries];
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * Add a new entry to this map.
+ * @param entry The entry to be added.
+ */
+ public void addEntry(final Cmap04TtfEntry4a entry) {
+ this.lastEntryAdded++;
+ if (this.lastEntryAdded >= this.entries.length) {
+ // Fails silently.
+ return;
+ }
+ this.entries[this.lastEntryAdded] = entry;
+ }
+
+ @Override
+ public void addEntry(final int unicodeStart, final int unicodeEnd, final char glyphStartIndex) {
+ final Cmap04TtfEntry4a newEntry = new Cmap04TtfEntry4a(unicodeStart, unicodeEnd,
+ glyphStartIndex);
+ addEntry(newEntry);
+ }
+
+ @Override
+ public void addEntry(final int unicodeStart, final int unicodeEnd, final CharSequence glyphIndexes) {
+ final Cmap04TtfEntry4a newEntry = new Cmap04TtfEntry4a(unicodeStart, unicodeEnd, glyphIndexes);
+ addEntry(newEntry);
+ }
+
+ @Override
+ public int encode(final int c) {
+ final Cmap04TtfEntry4a entry = entryForChar((char) c);
+ if (entry == null) {
+ return -1;
+ }
+ return entry.encodeCharacter((char) c);
+ }
+
+ /**
+ * Find the entry for a given character.
+ * @param c The character for which an entry is sought.
+ * @return The entry, or null if none is defined for {@code c}.
+ */
+ private Cmap04TtfEntry4a entryForChar(final char c) {
+ for (int i = 0; i < this.entries.length; i++) {
+ if (this.entries[i].containsChar(c)) {
+ return this.entries[i];
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public int decode(final int glyphIndexInput) {
+ for (int i = 0; i < this.entries.length; i++) {
+ final int codePoint = this.entries[i].decodeCharacter(glyphIndexInput);
+ if (codePoint > -1) {
+ return codePoint;
+ }
+ }
+ return -1;
+ }
+
+ @Override
+ public String asPostScript(final org.axsl.ps.Encoding baseEncoding) {
+ return null;
+ }
+
+ @Override
+ public int getFirstIndex() {
+ return -1;
+ }
+
+ @Override
+ public int getLastIndex() {
+ return -1;
+ }
+
+ @Override
+ public boolean isBuiltInPs() {
+ /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
+ * prebuilt instances separate, and return different values here. */
+ return false;
+ }
+
+ @Override
+ public boolean isBuiltInPdf() {
+ /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
+ * prebuilt instances separate, and return different values here. */
+ return false;
+ }
+
+}
Copied: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04TtfEntry4a.java (from rev 11898, trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java)
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04TtfEntry4a.java (rev 0)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap04TtfEntry4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -0,0 +1,138 @@
+/*
+ * 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;
+
+/**
+ * Contains one entry from a TrueType Cmap4a subtable, format 4, which handles
+ * 16-bit Unicode codepoint ranges.
+ */
+public class Cmap04TtfEntry4a {
+
+ /** The first Unicode codepoint in this entry. */
+ private int unicodeStart;
+
+ /** The last Unicode codepoint in this entry.*/
+ private int unicodeEnd;
+
+ /** The starting glyph index for this entry. This index points to the glyph
+ * representing {@link #unicodeStart}. */
+ private char glyphStartIndex;
+
+ /** The list of non-contiguous entries, if any, for this entry.
+ * For contiguous entries, this will be null.
+ * For non-contiguous entries, this array contains the glyph Indexes for the
+ * range of Unicode characters. */
+ private CharSequence glyphIndexes;
+
+ /**
+ * Constructor for contiguous entries.
+ * @param unicodeStart The starting Unicode code point covered by this
+ * mapping.
+ * @param unicodeEnd The ending Unicode code point covered by this mapping.
+ * @param glyphStartIndex The starting glyph index mapped by this mapping.
+ * This is the encoded value for {@code unicodeStart}.
+ */
+ public Cmap04TtfEntry4a(final int unicodeStart, final int unicodeEnd, final char glyphStartIndex) {
+ this.unicodeStart = unicodeStart;
+ this.unicodeEnd = unicodeEnd;
+ this.glyphStartIndex = glyphStartIndex;
+ }
+
+ /**
+ * Constructor for non-contiguous entries.
+ * @param unicodeStart The starting Unicode code point covered by this
+ * mapping.
+ * @param unicodeEnd The ending Unicode code point covered by this mapping.
+ * @param glyphIndexes The array of encoded glyph indexes corresponding to
+ * the range {@code unicodeStart} through {@code unicodeEnd}.
+ */
+ public Cmap04TtfEntry4a(final int unicodeStart, final int unicodeEnd, final CharSequence glyphIndexes) {
+ this.unicodeStart = unicodeStart;
+ this.unicodeEnd = unicodeEnd;
+ this.glyphIndexes = glyphIndexes;
+ }
+
+ /**
+ * Compute the glyphEndIndex that is implied from the raw data.
+ * @return The last glyphIndex that is addressed by this entry, i.e. the
+ * glyph index that corresponds to unicodeEnd.
+ */
+ public char getGlyphEndIndex() {
+ return (char) (this.glyphStartIndex + this.unicodeEnd
+ - this.unicodeStart);
+ }
+
+ /**
+ * Indicates whether this entry can encode a given character.
+ * @param c The character to be tested.
+ * @return True iff this entry has encoding information for {@code c}.
+ */
+ public boolean containsChar(final int c) {
+ return (c >= this.unicodeStart) && (c <= this.unicodeEnd);
+ }
+
+ /**
+ * Encodes a given character.
+ * @param c The character to be encoded.
+ * @return The glyph index corresponding to {@code c}, or -1 if this entry does not know how to encode it.
+ */
+ public int encodeCharacter(final int c) {
+ if (! containsChar(c)) {
+ return -1;
+ }
+ if (this.glyphIndexes == null) {
+ return (char) (c - this.unicodeStart + this.glyphStartIndex);
+ }
+ return this.glyphIndexes.charAt(c - this.unicodeStart);
+ }
+
+ /**
+ * Decodes a given glyphIndex.
+ * @param glyphIndex The glyph index to be decoded.
+ * @return The Unicode code point corresponding to {@code glyphIndex}, or -1 if this entry does not know how to
+ * decode it.
+ */
+ public int decodeCharacter(final int glyphIndex) {
+ if (this.glyphIndexes == null) {
+ final char codePoint = (char) (glyphIndex - this.glyphStartIndex + this.unicodeStart);
+ if (glyphIndex >= this.glyphStartIndex
+ && codePoint <= this.unicodeStart) {
+ return codePoint;
+ }
+ return -1;
+ }
+ for (int i = 0; i < this.glyphIndexes.length(); i++) {
+ if (this.glyphIndexes.charAt(i) == glyphIndex) {
+ return this.unicodeStart + i;
+ }
+ }
+ return -1;
+ }
+
+}
Copied: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12Ttf4a.java (from rev 11901, trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java)
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12Ttf4a.java (rev 0)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12Ttf4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2006 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.Cmap12Ttf;
+
+/**
+ * Class containing a set of 32-bit Unicode ranges that sequentially map to the same-sized range of glyph indexes.
+ * The contents are analogous to a Format 12 TrueType Font cmap table (from which this class derives its name).
+ */
+public class Cmap12Ttf4a extends Cmap00Ttf4a implements Cmap12Ttf {
+
+ /** Constant needed for serialization. */
+ public static final long serialVersionUID = -606154911100219394L;
+
+ /** The name of this encoding. */
+ private String name;
+
+ /** The array of entries. */
+ private Cmap12TtfEntry4a[] entries;
+
+ /** Counter to the last entry added. */
+ private int lastEntryAdded = -1;
+
+ /**
+ * Constructor.
+ * @param name The name of this encoding.
+ * @param expectedEntries The number of expected entries in this encoding.
+ * Setting this to the correct value will result in more efficient
+ * processing.
+ */
+ public Cmap12Ttf4a(final String name, final int expectedEntries) {
+ this.name = name;
+ this.entries = new Cmap12TtfEntry4a[expectedEntries];
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * Add a new entry to this map.
+ * @param entry The entry to be added.
+ */
+ public void addEntry(final Cmap12TtfEntry4a entry) {
+ this.lastEntryAdded++;
+ if (this.lastEntryAdded >= this.entries.length) {
+ // Fails silently.
+ return;
+ }
+ this.entries[this.lastEntryAdded] = entry;
+ }
+
+ @Override
+ public void addEntry(final int unicodeStart, final int unicodeEnd,
+ final int glyphStartIndex) {
+ final Cmap12TtfEntry4a newEntry = new Cmap12TtfEntry4a(unicodeStart, unicodeEnd,
+ glyphStartIndex);
+ addEntry(newEntry);
+ }
+
+ @Override
+ public int encode(final int codePoint) {
+ final Cmap12TtfEntry4a entry = entryForChar(codePoint);
+ if (entry == null) {
+ return -1;
+ }
+ return entry.encodeCharacter(codePoint);
+ }
+
+ /**
+ * Find the entry for a given character.
+ * @param codePoint The Unicode code point for which an entry is sought.
+ * @return The entry, or null if none is defined for {@code codePoint}.
+ */
+ private Cmap12TtfEntry4a entryForChar(final int codePoint) {
+ for (int i = 0; i < this.entries.length; i++) {
+ if (this.entries[i].containsChar(codePoint)) {
+ return this.entries[i];
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public int decode(final int glyphIndexInput) {
+ for (int i = 0; i < this.entries.length; i++) {
+ final int codePoint = this.entries[i].decodeCharacter(glyphIndexInput);
+ if (codePoint != Character.MAX_VALUE) {
+ return codePoint;
+ }
+ }
+ return -1;
+ }
+
+ @Override
+ public String asPostScript(final org.axsl.ps.Encoding baseEncoding) {
+ return null;
+ }
+
+ @Override
+ public int getFirstIndex() {
+ return -1;
+ }
+
+ @Override
+ public int getLastIndex() {
+ return -1;
+ }
+
+ @Override
+ public boolean isBuiltInPs() {
+ /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
+ * prebuilt instances separate, and return different values here. */
+ return false;
+ }
+
+ @Override
+ public boolean isBuiltInPdf() {
+ /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
+ * prebuilt instances separate, and return different values here. */
+ return false;
+ }
+
+}
Copied: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12TtfEntry4a.java (from rev 11880, trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12Entry.java)
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12TtfEntry4a.java (rev 0)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap12TtfEntry4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2006 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;
+
+/**
+ * Contains one entry from a TrueType Cmap4a subtable, Format 12, which handles
+ * 32-bit Unicode codepoint ranges.
+ */
+public class Cmap12TtfEntry4a {
+
+ /** The first Unicode codepoint in this entry. */
+ private int unicodeStart;
+
+ /** The last Unicode codepoint in this entry.*/
+ private int unicodeEnd;
+
+ /** The starting glyph index for this entry. This index points to the glyph
+ * representing {@link #unicodeStart}. */
+ private int glyphStartIndex;
+
+ /**
+ * Constructor.
+ * @param unicodeStart The starting Unicode code point covered by this
+ * mapping.
+ * @param unicodeEnd The ending Unicode code point covered by this mapping.
+ * @param glyphStartIndex The starting glyph index mapped by this mapping.
+ * This is the encoded value for {@code unicodeStart}.
+ */
+ public Cmap12TtfEntry4a(final int unicodeStart, final int unicodeEnd,
+ final int glyphStartIndex) {
+ this.unicodeStart = unicodeStart;
+ this.unicodeEnd = unicodeEnd;
+ this.glyphStartIndex = glyphStartIndex;
+ }
+
+ /**
+ * Compute the glyphEndIndex that is implied from the raw data.
+ * @return The last glyphIndex that is addressed by this entry, i.e. the
+ * glyph index that corresponds to unicodeEnd.
+ */
+ public int getGlyphEndIndex() {
+ return this.glyphStartIndex + this.unicodeEnd - this.unicodeStart;
+ }
+
+ /**
+ * Indicates whether this entry can encode a given character.
+ * @param codePoint The Unicode code point to be tested.
+ * @return True iff this entry has encoding information for
+ * {@code codePoint}.
+ */
+ public boolean containsChar(final int codePoint) {
+ if (codePoint < this.unicodeStart) {
+ return false;
+ }
+ if (codePoint > this.unicodeEnd) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Encodes a given character.
+ * @param codePoint The Unicode code point to be encoded.
+ * @return The glyph index corresponding to {@code codePoint}, or zero
+ * if this entry does not know how to encode it.
+ */
+ public int encodeCharacter(final int codePoint) {
+ if (! containsChar(codePoint)) {
+ return 0;
+ }
+ return codePoint - this.unicodeStart + this.glyphStartIndex;
+ }
+
+ /**
+ * Decodes a given glyphIndex.
+ * @param glyphIndex The glyph index to be decoded.
+ * @return The Unicode code point corresponding to {@code glyphIndex},
+ * or {@link Character#MAX_VALUE} if this entry does not know how to decode
+ * it.
+ */
+ public int decodeCharacter(final int glyphIndex) {
+ final int codePoint = glyphIndex - this.glyphStartIndex
+ + this.unicodeStart;
+ if (glyphIndex >= this.glyphStartIndex
+ && codePoint <= this.unicodeStart) {
+ return codePoint;
+ }
+ return Character.MAX_VALUE;
+ }
+
+}
Deleted: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java 2021-02-08 23:30:03 UTC (rev 11901)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -1,46 +0,0 @@
-/*
- * Copyright 2006 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.Cmap;
-
-/**
- * Abstract superclass for all Cmap4a Tables.
- */
-public abstract class Cmap4a implements Cmap {
-
- /** Constant needed for serialization. */
- public static final long serialVersionUID = -6516855352094857929L;
-
- @Override
- public boolean canEncode(final int codePoint) {
- return this.encode(codePoint) > -1;
- }
-
-}
Deleted: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java 2021-02-08 23:30:03 UTC (rev 11901)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -1,157 +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.Cmap04Ttf;
-
-/**
- * Class containing a set of 16-bit Unicode ranges that sequentially map to the same-sized range of glyph indexes.
- * The contents are analogous to a Format 4 TrueType Font cmap table (from which this class derives its name).
- */
-public class Cmap4a04 extends Cmap4a implements Cmap04Ttf {
-
- /** Constant needed for serialization. */
- public static final long serialVersionUID = 985957847129472354L;
-
- /** The name of this encoding. */
- private String name;
-
- /** The array of entries. */
- private Cmap4a04Entry[] entries;
-
- /** Counter to the last entry added. */
- private int lastEntryAdded = -1;
-
- /**
- * Constructor.
- * @param name The name of this encoding.
- * @param expectedEntries The number of expected entries in this encoding.
- * Setting this to the correct value will result in more efficient
- * processing.
- */
- public Cmap4a04(final String name, final int expectedEntries) {
- this.name = name;
- this.entries = new Cmap4a04Entry[expectedEntries];
- }
-
- @Override
- public String getName() {
- return this.name;
- }
-
- /**
- * Add a new entry to this map.
- * @param entry The entry to be added.
- */
- public void addEntry(final Cmap4a04Entry entry) {
- this.lastEntryAdded++;
- if (this.lastEntryAdded >= this.entries.length) {
- // Fails silently.
- return;
- }
- this.entries[this.lastEntryAdded] = entry;
- }
-
- @Override
- public void addEntry(final int unicodeStart, final int unicodeEnd, final char glyphStartIndex) {
- final Cmap4a04Entry newEntry = new Cmap4a04Entry(unicodeStart, unicodeEnd,
- glyphStartIndex);
- addEntry(newEntry);
- }
-
- @Override
- public void addEntry(final int unicodeStart, final int unicodeEnd, final CharSequence glyphIndexes) {
- final Cmap4a04Entry newEntry = new Cmap4a04Entry(unicodeStart, unicodeEnd, glyphIndexes);
- addEntry(newEntry);
- }
-
- @Override
- public int encode(final int c) {
- final Cmap4a04Entry entry = entryForChar((char) c);
- if (entry == null) {
- return -1;
- }
- return entry.encodeCharacter((char) c);
- }
-
- /**
- * Find the entry for a given character.
- * @param c The character for which an entry is sought.
- * @return The entry, or null if none is defined for {@code c}.
- */
- private Cmap4a04Entry entryForChar(final char c) {
- for (int i = 0; i < this.entries.length; i++) {
- if (this.entries[i].containsChar(c)) {
- return this.entries[i];
- }
- }
- return null;
- }
-
- @Override
- public int decode(final int glyphIndexInput) {
- for (int i = 0; i < this.entries.length; i++) {
- final int codePoint = this.entries[i].decodeCharacter(glyphIndexInput);
- if (codePoint > -1) {
- return codePoint;
- }
- }
- return -1;
- }
-
- @Override
- public String asPostScript(final org.axsl.ps.Encoding baseEncoding) {
- return null;
- }
-
- @Override
- public int getFirstIndex() {
- return -1;
- }
-
- @Override
- public int getLastIndex() {
- return -1;
- }
-
- @Override
- public boolean isBuiltInPs() {
- /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
- * prebuilt instances separate, and return different values here. */
- return false;
- }
-
- @Override
- public boolean isBuiltInPdf() {
- /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
- * prebuilt instances separate, and return different values here. */
- return false;
- }
-
-}
Deleted: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java 2021-02-08 23:30:03 UTC (rev 11901)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -1,138 +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;
-
-/**
- * Contains one entry from a TrueType Cmap4a subtable, format 4, which handles
- * 16-bit Unicode codepoint ranges.
- */
-public class Cmap4a04Entry {
-
- /** The first Unicode codepoint in this entry. */
- private int unicodeStart;
-
- /** The last Unicode codepoint in this entry.*/
- private int unicodeEnd;
-
- /** The starting glyph index for this entry. This index points to the glyph
- * representing {@link #unicodeStart}. */
- private char glyphStartIndex;
-
- /** The list of non-contiguous entries, if any, for this entry.
- * For contiguous entries, this will be null.
- * For non-contiguous entries, this array contains the glyph Indexes for the
- * range of Unicode characters. */
- private CharSequence glyphIndexes;
-
- /**
- * Constructor for contiguous entries.
- * @param unicodeStart The starting Unicode code point covered by this
- * mapping.
- * @param unicodeEnd The ending Unicode code point covered by this mapping.
- * @param glyphStartIndex The starting glyph index mapped by this mapping.
- * This is the encoded value for {@code unicodeStart}.
- */
- public Cmap4a04Entry(final int unicodeStart, final int unicodeEnd, final char glyphStartIndex) {
- this.unicodeStart = unicodeStart;
- this.unicodeEnd = unicodeEnd;
- this.glyphStartIndex = glyphStartIndex;
- }
-
- /**
- * Constructor for non-contiguous entries.
- * @param unicodeStart The starting Unicode code point covered by this
- * mapping.
- * @param unicodeEnd The ending Unicode code point covered by this mapping.
- * @param glyphIndexes The array of encoded glyph indexes corresponding to
- * the range {@code unicodeStart} through {@code unicodeEnd}.
- */
- public Cmap4a04Entry(final int unicodeStart, final int unicodeEnd, final CharSequence glyphIndexes) {
- this.unicodeStart = unicodeStart;
- this.unicodeEnd = unicodeEnd;
- this.glyphIndexes = glyphIndexes;
- }
-
- /**
- * Compute the glyphEndIndex that is implied from the raw data.
- * @return The last glyphIndex that is addressed by this entry, i.e. the
- * glyph index that corresponds to unicodeEnd.
- */
- public char getGlyphEndIndex() {
- return (char) (this.glyphStartIndex + this.unicodeEnd
- - this.unicodeStart);
- }
-
- /**
- * Indicates whether this entry can encode a given character.
- * @param c The character to be tested.
- * @return True iff this entry has encoding information for {@code c}.
- */
- public boolean containsChar(final int c) {
- return (c >= this.unicodeStart) && (c <= this.unicodeEnd);
- }
-
- /**
- * Encodes a given character.
- * @param c The character to be encoded.
- * @return The glyph index corresponding to {@code c}, or -1 if this entry does not know how to encode it.
- */
- public int encodeCharacter(final int c) {
- if (! containsChar(c)) {
- return -1;
- }
- if (this.glyphIndexes == null) {
- return (char) (c - this.unicodeStart + this.glyphStartIndex);
- }
- return this.glyphIndexes.charAt(c - this.unicodeStart);
- }
-
- /**
- * Decodes a given glyphIndex.
- * @param glyphIndex The glyph index to be decoded.
- * @return The Unicode code point corresponding to {@code glyphIndex}, or -1 if this entry does not know how to
- * decode it.
- */
- public int decodeCharacter(final int glyphIndex) {
- if (this.glyphIndexes == null) {
- final char codePoint = (char) (glyphIndex - this.glyphStartIndex + this.unicodeStart);
- if (glyphIndex >= this.glyphStartIndex
- && codePoint <= this.unicodeStart) {
- return codePoint;
- }
- return -1;
- }
- for (int i = 0; i < this.glyphIndexes.length(); i++) {
- if (this.glyphIndexes.charAt(i) == glyphIndex) {
- return this.unicodeStart + i;
- }
- }
- return -1;
- }
-
-}
Deleted: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java 2021-02-08 23:30:03 UTC (rev 11901)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -1,152 +0,0 @@
-/*
- * Copyright 2006 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.Cmap12Ttf;
-
-/**
- * Class containing a set of 32-bit Unicode ranges that sequentially map to the same-sized range of glyph indexes.
- * The contents are analogous to a Format 12 TrueType Font cmap table (from which this class derives its name).
- */
-public class Cmap4a12 extends Cmap4a implements Cmap12Ttf {
-
- /** Constant needed for serialization. */
- public static final long serialVersionUID = -606154911100219394L;
-
- /** The name of this encoding. */
- private String name;
-
- /** The array of entries. */
- private Cmap4a12Entry[] entries;
-
- /** Counter to the last entry added. */
- private int lastEntryAdded = -1;
-
- /**
- * Constructor.
- * @param name The name of this encoding.
- * @param expectedEntries The number of expected entries in this encoding.
- * Setting this to the correct value will result in more efficient
- * processing.
- */
- public Cmap4a12(final String name, final int expectedEntries) {
- this.name = name;
- this.entries = new Cmap4a12Entry[expectedEntries];
- }
-
- @Override
- public String getName() {
- return this.name;
- }
-
- /**
- * Add a new entry to this map.
- * @param entry The entry to be added.
- */
- public void addEntry(final Cmap4a12Entry entry) {
- this.lastEntryAdded++;
- if (this.lastEntryAdded >= this.entries.length) {
- // Fails silently.
- return;
- }
- this.entries[this.lastEntryAdded] = entry;
- }
-
- @Override
- public void addEntry(final int unicodeStart, final int unicodeEnd,
- final int glyphStartIndex) {
- final Cmap4a12Entry newEntry = new Cmap4a12Entry(unicodeStart, unicodeEnd,
- glyphStartIndex);
- addEntry(newEntry);
- }
-
- @Override
- public int encode(final int codePoint) {
- final Cmap4a12Entry entry = entryForChar(codePoint);
- if (entry == null) {
- return -1;
- }
- return entry.encodeCharacter(codePoint);
- }
-
- /**
- * Find the entry for a given character.
- * @param codePoint The Unicode code point for which an entry is sought.
- * @return The entry, or null if none is defined for {@code codePoint}.
- */
- private Cmap4a12Entry entryForChar(final int codePoint) {
- for (int i = 0; i < this.entries.length; i++) {
- if (this.entries[i].containsChar(codePoint)) {
- return this.entries[i];
- }
- }
- return null;
- }
-
- @Override
- public int decode(final int glyphIndexInput) {
- for (int i = 0; i < this.entries.length; i++) {
- final int codePoint = this.entries[i].decodeCharacter(glyphIndexInput);
- if (codePoint != Character.MAX_VALUE) {
- return codePoint;
- }
- }
- return -1;
- }
-
- @Override
- public String asPostScript(final org.axsl.ps.Encoding baseEncoding) {
- return null;
- }
-
- @Override
- public int getFirstIndex() {
- return -1;
- }
-
- @Override
- public int getLastIndex() {
- return -1;
- }
-
- @Override
- public boolean isBuiltInPs() {
- /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
- * prebuilt instances separate, and return different values here. */
- return false;
- }
-
- @Override
- public boolean isBuiltInPdf() {
- /* TODO Make this class abstract and make a subclass that handled custom instances, so that we can keep the
- * prebuilt instances separate, and return different values here. */
- return false;
- }
-
-}
Deleted: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12Entry.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12Entry.java 2021-02-08 23:30:03 UTC (rev 11901)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12Entry.java 2021-02-09 01:03:02 UTC (rev 11902)
@@ -1,117 +0,0 @@
-/*
- * Copyright 2006 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;
-
-/**
- * Contains one entry from a TrueType Cmap4a subtable, Format 12, which handles
- * 32-bit Unicode codepoint ranges.
- */
-public class Cmap4a12Entry {
-
- /** The first Unicode codepoint in this entry. */
- private int unicodeStart;
-
- /** The last Unicode codepoint in this entry.*/
- private int unicodeEnd;
-
- /** The starting glyph index for this entry. This index points to the glyph
- * representing {@link #unicodeStart}. */
- private int glyphStartIndex;
-
- /**
- * Constructor.
- * @param unicodeStart The starting Unicode code point covered by this
- * mapping.
- * @param unicodeEnd The ending Unicode code point covered by this mapping.
- * @param glyphStartIndex The starting glyph index mapped by this mapping.
- * This is the encoded value for {@code unicodeStart}.
- */
- public Cmap4a12Entry(final int unicodeStart, final int unicodeEnd,
- final int glyphStartIndex) {
- this.unicodeStart = unicodeStart;
- this.unicodeEnd = unicodeEnd;
- this.glyphStartIndex = glyphStartIndex;
- }
-
- /**
- * Compute the glyphEndIndex that is implied from the raw data.
- * @return The last glyphIndex that is addressed by this entry, i.e. the
- * glyph index that corresponds to unicodeEnd.
- */
- public int getGlyphEndIndex() {
- return this.glyphStartIndex + this.unicodeEnd - this.unicodeStart;
- }
-
- /**
- * Indicates whether this entry can encode a given character.
- * @param codePoint The Unicode code point to be tested.
- * @return True iff this entry has encoding information for
- * {@code codePoint}.
- */
- public boolean containsChar(final int codePoint) {
- if (codePoint < this.unicodeStart) {
- return false;
- }
- if (codePoint > this.unicodeEnd) {
- return false;
- }
- return true;
- }
-
- /**
- * Encodes a given character.
- * @param codePoint The Unicode code point to be encoded.
- * @return The glyph index corresponding to {@code codePoint}, or zero
- * if this entry does not know how to encode it.
- */
- public int encodeCharacter(final int codePoint) {
- if (! containsChar(codePoint)) {
- return 0;
- }
- return codePoint - this.unicodeStart + this.glyphStartIndex;
- }
-
- /**
- * Decodes a given glyphIndex.
- * @param glyphIndex The glyph index to be decoded.
- * @return The Unicode code point corresponding to {@code glyphIndex},
- * or {@link Character#MAX_VALUE} if this entry does not know how to decode
- * it.
- */
- public int decodeCharacter(final int glyphIndex) {
- final int codePoint = glyphIndex - this.glyphStartIndex
- + this.unicodeStart;
- if (glyphIndex >= this.glyphStartIndex
- && codePoint <= this.unicodeStart) {
- return codePoint;
- }
- return Character.MAX_VALUE;
- }
-
-}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-08 23:30:06
|
Revision: 11901
http://sourceforge.net/p/foray/code/11901
Author: victormote
Date: 2021-02-08 23:30:03 +0000 (Mon, 08 Feb 2021)
Log Message:
-----------
Conform to aXSL changes: Rename interfaces for clarity and to simplify implementation naming.
Modified Paths:
--------------
trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 23:01:27 UTC (rev 11900)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 23:30:03 UTC (rev 11901)
@@ -116,7 +116,7 @@
}
/**
- * Get the full font glyph index (not subsetted) that corresponds to {@link subsetGlyphIndex}.
+ * Get the full font glyph index (not subsetted) that corresponds to a given subset glyph index.
* @param subsetGlyphIndex The index into the subset font glyphs.
* @return The index into the full font (not subsetted) glyphs, or -1 if the input is not valid.
*/
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java 2021-02-08 23:01:27 UTC (rev 11900)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java 2021-02-08 23:30:03 UTC (rev 11901)
@@ -31,8 +31,8 @@
import org.foray.common.io.RandomAccessInput;
import org.axsl.ps.Cmap;
-import org.axsl.ps.Cmap04;
-import org.axsl.ps.Cmap12;
+import org.axsl.ps.Cmap04Ttf;
+import org.axsl.ps.Cmap12Ttf;
import org.axsl.ps.PsServer;
import java.io.IOException;
@@ -182,7 +182,7 @@
* @return The parsed Cmap4a format 4 table.
* @throws IOException For I/O Error.
*/
- private static Cmap04 parseCMap04(final RandomAccessInput raInput, final PsServer psServer,
+ private static Cmap04Ttf parseCMap04(final RandomAccessInput raInput, final PsServer psServer,
final String fontPostScriptName) throws IOException {
/* Format and Length have already been read. */
/* Skip version */
@@ -223,7 +223,7 @@
// Parse the table entries and build a CMap4 from them.
final String encodingName = "internal-" + fontPostScriptName;
- final Cmap04 cmap04 = psServer.makeCmap04(encodingName, segCountX2 / 2);
+ final Cmap04Ttf cmap04 = psServer.makeCmap04(encodingName, segCountX2 / 2);
for (int i = 0; i < startCount.length; i++) {
if (rangeOffset[i] == 0) {
cmap04.addEntry(startCount[i],
@@ -262,7 +262,7 @@
* <a href="http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html">
* The Apple doc for the cmap table</a>
*/
- private static Cmap12 parseCMap12(final RandomAccessInput raInput, final PsServer psServer,
+ private static Cmap12Ttf parseCMap12(final RandomAccessInput raInput, final PsServer psServer,
final String fontPostScriptName) throws IOException {
/* Format and Length have already been read. */
/* Skip language. */
@@ -270,7 +270,7 @@
final int nGroups = (int) raInput.readUnsignedInt();
final String encodingName = "internal-" + fontPostScriptName;
- final Cmap12 cmap12 = psServer.makeCmap12(encodingName, nGroups);
+ final Cmap12Ttf cmap12 = psServer.makeCmap12(encodingName, nGroups);
for (int i = 0; i < nGroups; i++) {
final int startCharCode = (int) raInput.readUnsignedInt();
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java 2021-02-08 23:01:27 UTC (rev 11900)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/PsServer4a.java 2021-02-08 23:30:03 UTC (rev 11901)
@@ -71,8 +71,8 @@
import org.axsl.common.sequence.IntSequence;
import org.axsl.common.sequence.IntSequenceMutable;
import org.axsl.ps.CharSet;
-import org.axsl.ps.Cmap04;
-import org.axsl.ps.Cmap12;
+import org.axsl.ps.Cmap04Ttf;
+import org.axsl.ps.Cmap12Ttf;
import org.axsl.ps.GlyphList;
import org.axsl.ps.PsEncoding;
import org.axsl.ps.PsException;
@@ -301,12 +301,12 @@
}
@Override
- public Cmap04 makeCmap04(final String name, final int expectedEntries) {
+ public Cmap04Ttf makeCmap04(final String name, final int expectedEntries) {
return new Cmap4a04(name, expectedEntries);
}
@Override
- public Cmap12 makeCmap12(final String name, final int expectedEntries) {
+ public Cmap12Ttf makeCmap12(final String name, final int expectedEntries) {
return new Cmap4a12(name, expectedEntries);
}
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java 2021-02-08 23:01:27 UTC (rev 11900)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java 2021-02-08 23:30:03 UTC (rev 11901)
@@ -28,13 +28,13 @@
package org.foray.ps.encode;
-import org.axsl.ps.Cmap04;
+import org.axsl.ps.Cmap04Ttf;
/**
* Class containing a set of 16-bit Unicode ranges that sequentially map to the same-sized range of glyph indexes.
* The contents are analogous to a Format 4 TrueType Font cmap table (from which this class derives its name).
*/
-public class Cmap4a04 extends Cmap4a implements Cmap04 {
+public class Cmap4a04 extends Cmap4a implements Cmap04Ttf {
/** Constant needed for serialization. */
public static final long serialVersionUID = 985957847129472354L;
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java 2021-02-08 23:01:27 UTC (rev 11900)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java 2021-02-08 23:30:03 UTC (rev 11901)
@@ -28,13 +28,13 @@
package org.foray.ps.encode;
-import org.axsl.ps.Cmap12;
+import org.axsl.ps.Cmap12Ttf;
/**
* Class containing a set of 32-bit Unicode ranges that sequentially map to the same-sized range of glyph indexes.
* The contents are analogous to a Format 12 TrueType Font cmap table (from which this class derives its name).
*/
-public class Cmap4a12 extends Cmap4a implements Cmap12 {
+public class Cmap4a12 extends Cmap4a implements Cmap12Ttf {
/** Constant needed for serialization. */
public static final long serialVersionUID = -606154911100219394L;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-08 23:01:30
|
Revision: 11900
http://sourceforge.net/p/foray/code/11900
Author: victormote
Date: 2021-02-08 23:01:27 +0000 (Mon, 08 Feb 2021)
Log Message:
-----------
Add some doc to the creation of a Subset for a TrueType font.
Modified Paths:
--------------
trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 12:55:16 UTC (rev 11899)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 23:01:27 UTC (rev 11900)
@@ -79,7 +79,18 @@
this.subsetByOriginal = new IntArrayBuilder(this.arraySizeIncrement);
this.originalBySubset = new IntArrayBuilder(this.arraySizeIncrement);
if (this.consumerFont.getFOrayFont() instanceof FsTrueTypeFont) {
- // Make sure that the 3 first glyphs are included
+ /* Make sure that the 3 first glyphs are included.
+ * See "https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html", documentation of
+ * the TrueType cmap table, paragraph 2: "At this location [glyph index 0] in the font, there must be a
+ * special glyph repesenting a missing character, typically a box."
+ * Similarly, "https://developer.apple.com/fonts/TrueType-Reference-Manual/RM07/appendixB.html", the
+ * "Character Code/Glyph Mapping Requirements and Recommendations" says: "1. The first glyph (glyph index 0)
+ * must be the MISSING CHARACTER GLYPH. This glyph must have a visible appearance and non-zero advance
+ * width. 2. The second glyph (glyph index 1) must be the NULL glyph. This glyph must have no contours and
+ * zero advance width."
+ * A requirement for the third glyph is not mentioned here, but the requirements for a horizontal tab,
+ * carriage-return, space, and no-break space might indicate that (by convention at least) the third glyph
+ * would be the space. */
this.encodeSubsetIndex(0);
this.encodeSubsetIndex(1);
this.encodeSubsetIndex(2);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-08 12:55:19
|
Revision: 11899
http://sourceforge.net/p/foray/code/11899
Author: victormote
Date: 2021-02-08 12:55:16 +0000 (Mon, 08 Feb 2021)
Log Message:
-----------
Throw exception if subsetting negative glyph indexes is attempted.
Modified Paths:
--------------
trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
trunk/foray/master/config/checkstyle/checkstyle-suppressions.xml
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 12:20:11 UTC (rev 11898)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 12:55:16 UTC (rev 11899)
@@ -80,9 +80,9 @@
this.originalBySubset = new IntArrayBuilder(this.arraySizeIncrement);
if (this.consumerFont.getFOrayFont() instanceof FsTrueTypeFont) {
// Make sure that the 3 first glyphs are included
- this.encodeSubsetIndex((char) 0);
- this.encodeSubsetIndex((char) 1);
- this.encodeSubsetIndex((char) 2);
+ this.encodeSubsetIndex(0);
+ this.encodeSubsetIndex(1);
+ this.encodeSubsetIndex(2);
}
}
@@ -91,8 +91,12 @@
* necessary.
* @param fontGlyphIndex The index into the full font (not subsetted) glyphs.
* @return The index into the subsetted glyphs.
+ * @throws IllegalArgumentException If {@code fontGlyphIndex} is negative.
*/
public int encodeSubsetIndex(final int fontGlyphIndex) {
+ if (fontGlyphIndex < 0) {
+ throw new IllegalArgumentException("Cannot subset a negative glyph index: " + fontGlyphIndex);
+ }
final int index = this.originalByOriginal.binarySearch(fontGlyphIndex);
if (index < 0) {
return addMapping(fontGlyphIndex);
@@ -101,7 +105,7 @@
}
/**
- * Get the full font glyph index (not subsetted) that corresponds to subsetGlyphIndex.
+ * Get the full font glyph index (not subsetted) that corresponds to {@link subsetGlyphIndex}.
* @param subsetGlyphIndex The index into the subset font glyphs.
* @return The index into the full font (not subsetted) glyphs, or -1 if the input is not valid.
*/
@@ -118,13 +122,13 @@
* @param glyphIndex The glyph index to be added.
* @return The subset index for the new glyph.
*/
- private char addMapping(final int glyphIndex) {
+ private int addMapping(final int glyphIndex) {
final int newIndex = this.originalByOriginal.length();
this.originalByOriginal.append(glyphIndex);
this.subsetByOriginal.append(newIndex);
this.originalBySubset.append(glyphIndex);
sortByOriginal();
- return (char) newIndex;
+ return newIndex;
}
private void sortByOriginal() {
@@ -164,10 +168,9 @@
while (stillSorting) {
stillSorting = false;
for (char i = 1; i < this.numGlyphsUsed(); i++) {
- final char currentOriginalIndex = (char) this.decodeSubsetIndex(i);
+ final int currentOriginalIndex = decodeSubsetIndex(i);
final int currentCodePoint = encoding.decode(currentOriginalIndex);
- final char previousOriginalIndex
- = (char) this.decodeSubsetIndex(i - 1);
+ final int previousOriginalIndex = decodeSubsetIndex(i - 1);
final int previousCodePoint = encoding.decode(previousOriginalIndex);
if (previousCodePoint > currentCodePoint) {
/* They are out of order and should be switched. */
Modified: trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
===================================================================
--- trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java 2021-02-08 12:20:11 UTC (rev 11898)
+++ trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java 2021-02-08 12:55:16 UTC (rev 11899)
@@ -93,4 +93,22 @@
Assert.assertEquals(4, out.numGlyphsUsed());
}
+ /**
+ * Tests that an exception is thrown if a negative glyph index is subsetted.
+ */
+ @Test
+ public void subsetting_A_Negative_Glyph_Index_Should_Throw_An_Exception() {
+ final FreeStandingFont mockFsf = Mockito.mock(FreeStandingFont.class);
+ final ConsumerFont4a mockConsumerFont = Mockito.mock(ConsumerFont4a.class);
+ Mockito.when(mockConsumerFont.getFreeStandingFont()).thenReturn(mockFsf);
+
+ final Subset out = new Subset(mockConsumerFont);
+ try {
+ out.encodeSubsetIndex(-1);
+ Assert.fail("Expected IllegalArgumentException to be thrown.");
+ } catch (final IllegalArgumentException e) {
+ /* This is the expected path. */
+ }
+ }
+
}
Modified: trunk/foray/master/config/checkstyle/checkstyle-suppressions.xml
===================================================================
--- trunk/foray/master/config/checkstyle/checkstyle-suppressions.xml 2021-02-08 12:20:11 UTC (rev 11898)
+++ trunk/foray/master/config/checkstyle/checkstyle-suppressions.xml 2021-02-08 12:55:16 UTC (rev 11899)
@@ -19,13 +19,14 @@
original Pascal WEB file, and does not conform to our usual coding standards. -->
<suppress checks="DeclarationOrder"
files="src.main.java.org.foray.hyphen.zzarchive.PatGen.*"/>
-
+
+ <!-- Permanently suppress some tests in the test directories. -->
<!-- Permanently suppress Javadoc package documentation for test directories. -->
- <suppress checks="JavadocPackage"
- files="src.test.java.*"/>
+ <suppress checks="JavadocPackage" files="src.test.java.*"/>
<!-- Permanently suppress Magic Number check for test directories. -->
- <suppress checks="MagicNumber"
- files="src.test.java.*"/>
+ <suppress checks="MagicNumber" files="src.test.java.*"/>
+ <!-- Permanently suppress Method naming checks for test directories. -->
+ <suppress checks="MethodName" files="src.test.java.*"/>
<!-- Permanently suppress checks in resources, data, and archives. -->
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-08 12:20:14
|
Revision: 11898
http://sourceforge.net/p/foray/code/11898
Author: victormote
Date: 2021-02-08 12:20:11 +0000 (Mon, 08 Feb 2021)
Log Message:
-----------
Conform to aXSL changes: 1. Simplify method names. 2. Since return types are now int instead of char, return -1 to indicate "not found".
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/primitive/StringUtils.java
trunk/foray/foray-font/src/main/java/org/foray/font/FontUse4a.java
trunk/foray/foray-font/src/main/java/org/foray/font/FsTrueTypeFont.java
trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java
trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTablePost.java
trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserAfm.java
trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserPfm.java
trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
trunk/foray/foray-font/src/test/java/org/foray/font/format/ttf/OtfLookupGsubTests.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/CharSetParser.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingParser.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVector4a.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVectorInputStream.java
trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/GlyphList4a.java
trunk/foray/foray-ps/src/test/java/org/foray/ps/encode/EncodingVector4aTests.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/primitive/StringUtils.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/primitive/StringUtils.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/primitive/StringUtils.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -136,48 +136,6 @@
}
/**
- * Efficiently finds the index within {@code charArray},
- * if any, that matches {@code inputChar}. <em>Note: </em>
- * {@code charArray} <em>must be</em> already sorted. No checking
- * of this condition is performed.
- * @param charArray The array of char items which should be searched
- * to find a match for {@code inputChar}.
- * @param inputChar The char for which a match is sought.
- * @param startIndex The first index in the array to be considered.
- * @param endIndex The last index in the array to be considered.
- * @return The index (0 through stringArray.length - 1) of the matching
- * element, or -1 if no match is found.
- */
- public static int indexOfSortedCharArray(final char[] charArray,
- final char inputChar, final int startIndex, final int endIndex) {
- if (charArray == null) {
- return -1;
- }
- /*
- * Current implementation is a simple binary search.
- */
- int bottom = startIndex;
- int top = endIndex;
- while (top >= bottom) {
- final int mid = (bottom + top) / 2;
- final char testChar = charArray[mid];
- if (inputChar == testChar) {
- // This is the item we are looking for. Return it.
- return mid;
- }
- if (inputChar < testChar) {
- // inputChar is less than testChar
- top = mid - 1;
- } else {
- // inputchar is greater than testString
- bottom = mid + 1;
- }
- }
- // If not found, return an invalid index
- return -1;
- }
-
- /**
* Returns a String representing a character's hex value. For example,
* using the character "Y" (U+0059) as input should return "59", and using
* "m" (U+006D) as input should return "6D".
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/FontUse4a.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/FontUse4a.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/FontUse4a.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -126,8 +126,11 @@
/* First get the font's encoding for the character. */
int fontEncodedChar = codePoint;
if (this.encoding != null) {
- fontEncodedChar = this.encoding.encodeCharacter(codePoint);
+ fontEncodedChar = this.encoding.encode(codePoint);
}
+ if (fontEncodedChar < 0) {
+ return -1;
+ }
/* Then get the subset index. */
if (! this.consumerFont.subSetting()) {
return fontEncodedChar;
@@ -498,7 +501,7 @@
final short[] widthsByFontIndex = new short[size];
for (int i = firstIndex; i <= lastIndex; i++) {
// Decode the character for this index
- final int codePoint = encoding.decodeCharacter(i);
+ final int codePoint = encoding.decode(i);
// Find the charSet index for that character.
final int charSetIndex = charSet.getIndex(codePoint);
if (charSetIndex < 0) {
@@ -738,7 +741,7 @@
final int arraySize = ttf.getNumGlyphs();
final char[] charArray = new char[arraySize];
for (int i = 0; i < ttf.getNumGlyphs(); i++) {
- final int arrayItem = encoding.decodeCharacter((char) i);
+ final int arrayItem = encoding.decode(i);
charArray[i] = (char) arrayItem;
}
return new String(charArray);
@@ -755,8 +758,7 @@
final char[] charArray = new char[subset.numGlyphsUsed()];
for (int i = 0; i < subset.numGlyphsUsed(); i++) {
final int originalGlyphIndex = subset.decodeSubsetIndex(i);
- final int codePoint = encoding.decodeCharacter(
- (char) originalGlyphIndex);
+ final int codePoint = encoding.decode(originalGlyphIndex);
charArray[i] = (char) codePoint;
}
return new String(charArray);
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/FsTrueTypeFont.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/FsTrueTypeFont.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/FsTrueTypeFont.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -174,20 +174,18 @@
@Override
public int width(final int c, final int fontSize) {
- final int glyphIndex = this.getInternalEncoding().encodeCharacter(c);
- if (glyphIndex == 0) {
+ final int glyphIndex = this.getInternalEncoding().encode(c);
+ if (glyphIndex < 0) {
return Integer.MIN_VALUE;
}
/* Since the fontSize and width array are both in millipoints, must
* divide by 1000 to return a result in millipoints */
- return fontSize * this.getWidths()[glyphIndex]
- / WellKnownConstants.MILLIPOINTS_PER_POINT;
+ return fontSize * this.getWidths()[glyphIndex] / WellKnownConstants.MILLIPOINTS_PER_POINT;
}
@Override
public int widthUndefinedGlyph(final int fontSize) {
- return fontSize * this.getWidths()[0]
- / WellKnownConstants.MILLIPOINTS_PER_POINT;
+ return fontSize * this.getWidths()[0] / WellKnownConstants.MILLIPOINTS_PER_POINT;
}
/**
@@ -201,7 +199,7 @@
@Override
public boolean glyphAvailable(final int c) {
- final int glyphIndex = this.getInternalEncoding().encodeCharacter(c);
+ final int glyphIndex = this.getInternalEncoding().encode(c);
return glyphIndex != 0;
}
@@ -261,7 +259,7 @@
@Override
public int metricIndex(final int codePoint) {
/* For TrueType fonts, the metric index is the same as the glyph index. */
- return this.getInternalEncoding().encodeCharacter(codePoint);
+ return this.getInternalEncoding().encode(codePoint);
}
@Override
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -103,13 +103,12 @@
/**
* Get the full font glyph index (not subsetted) that corresponds to subsetGlyphIndex.
* @param subsetGlyphIndex The index into the subset font glyphs.
- * @return The index into the full font (not subsetted) glyphs, or {@link Character#MAX_VALUE} if the input is not
- * valid.
+ * @return The index into the full font (not subsetted) glyphs, or -1 if the input is not valid.
*/
public int decodeSubsetIndex(final int subsetGlyphIndex) {
if (subsetGlyphIndex < 0
|| subsetGlyphIndex > this.originalByOriginal.length()) {
- return Character.MAX_VALUE;
+ return -1;
}
return this.originalBySubset.intAt(subsetGlyphIndex);
}
@@ -166,10 +165,10 @@
stillSorting = false;
for (char i = 1; i < this.numGlyphsUsed(); i++) {
final char currentOriginalIndex = (char) this.decodeSubsetIndex(i);
- final int currentCodePoint = encoding.decodeCharacter(currentOriginalIndex);
+ final int currentCodePoint = encoding.decode(currentOriginalIndex);
final char previousOriginalIndex
= (char) this.decodeSubsetIndex(i - 1);
- final int previousCodePoint = encoding.decodeCharacter(previousOriginalIndex);
+ final int previousCodePoint = encoding.decode(previousOriginalIndex);
if (previousCodePoint > currentCodePoint) {
/* They are out of order and should be switched. */
stillSorting = true;
@@ -177,18 +176,15 @@
/* First, switch the originalBySubset array. */
this.originalBySubset.swap(i, i - 1);
- /* Second, note that the originalByOriginal array needs
- * no changes. */
+ /* Second, note that the originalByOriginal array needs no changes. */
- /* Third, change the subsetByOriginal array element for
- * the previous element. */
+ /* Third, change the subsetByOriginal array element for the previous element. */
int commonIndex = this.originalByOriginal.binarySearch(previousOriginalIndex);
- this.subsetByOriginal.setIntAt(commonIndex, i); // [commonIndex] = i;
+ this.subsetByOriginal.setIntAt(commonIndex, i);
- /* Fourth, change the subsetByOriginal array element for
- * the current element. */
+ /* Fourth, change the subsetByOriginal array element for the current element. */
commonIndex = this.originalByOriginal.binarySearch(currentOriginalIndex);
- this.subsetByOriginal.setIntAt(commonIndex, i - 1); // [commonIndex] = (char) (i - 1);
+ this.subsetByOriginal.setIntAt(commonIndex, i - 1);
}
}
}
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTableCmap.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -303,7 +303,7 @@
if (this.unicodeCMap == null) {
return -1;
}
- return this.unicodeCMap.encodeCharacter(codePoint);
+ return this.unicodeCMap.encode(codePoint);
}
/**
@@ -315,7 +315,7 @@
if (this.unicodeCMap == null) {
return -1;
}
- return this.unicodeCMap.decodeCharacter(glyphIndex);
+ return this.unicodeCMap.decode(glyphIndex);
}
}
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTablePost.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTablePost.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/format/ttf/TtfTablePost.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -31,7 +31,6 @@
import org.foray.common.io.RandomAccessInput;
import org.foray.common.primitive.CharsetUtils;
-import org.axsl.ps.Encoding;
import org.axsl.ps.PsEncoding;
import org.axsl.ps.PsServer;
@@ -218,12 +217,12 @@
public String getPostScriptName(final PsServer psServer, final char glyphIndex) {
final PsEncoding macOrdering = psServer.getPredefinedEncoding(
PsEncoding.Predefined.STANDARD_MACINTOSH_ORDERING);
- final int codePoint = macOrdering.decodeCharacter(glyphIndex);
+ final int codePoint = macOrdering.decode(glyphIndex);
switch (this.postFormat) {
case TtfTablePost.PS_FORMAT_1:
return macOrdering.mapCodePointToGlyphName(codePoint);
case TtfTablePost.PS_FORMAT_2:
- if (codePoint != Encoding.INVALID_UNICODE_CHAR) {
+ if (codePoint > -1) {
return macOrdering.mapCodePointToGlyphName(codePoint);
}
if (this.glyphNameIndex == null
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserAfm.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserAfm.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserAfm.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -1278,9 +1278,9 @@
} else {
return;
}
- final char char1 = getCodePointForGlyphName(psServer, name1);
+ final int char1 = getCodePointForGlyphName(psServer, name1);
final int glyphIndex1 = this.metrics.getCharSet().getIndex(char1);
- final char char2 = getCodePointForGlyphName(psServer, name2);
+ final int char2 = getCodePointForGlyphName(psServer, name2);
final int glyphIndex2 = this.metrics.getCharSet().getIndex(char2);
final short kernAmount = (short) Math.round(getFloat());
this.metrics.getKerning().addKerningEntry(glyphIndex1, glyphIndex2, kernAmount);
@@ -1448,7 +1448,7 @@
* @throws FontException If the char cannot be encoded.
*/
private void addIndividualChar(final PsServer psServer) throws FontException {
- final char theChar = getCodePointForGlyphName(psServer, this.glyphName);
+ final int theChar = getCodePointForGlyphName(psServer, this.glyphName);
int charIndex = -1;
if (this.metrics.getCharSet() == null) {
// We are building the charset as we go.
@@ -1483,9 +1483,9 @@
* @param glyphName The glyph name to be converted.
* @return The Unicode code point that corresponds to glyphName.
*/
- private char getCodePointForGlyphName(final PsServer psServer, final String glyphName) {
+ private int getCodePointForGlyphName(final PsServer psServer, final String glyphName) {
GlyphList gl = psServer.getPredefinedGlyphList(GlyphList.Predefined.AGL);
- char theChar = gl.mapGlyphNameToCodePoint(glyphName);
+ int theChar = gl.mapGlyphNameToCodePoint(glyphName);
if (theChar == Character.MAX_VALUE) {
gl = psServer.getPredefinedGlyphList(GlyphList.Predefined.ZAPF_DINGBATS);
theChar = gl.mapGlyphNameToCodePoint(glyphName);
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserPfm.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserPfm.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/format/type1/Type1MetricsParserPfm.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -351,8 +351,7 @@
for (int i = this.dfFirstChar; i <= this.dfLastChar; i++) {
final short width = (short) this.input.readUnsignedShortLoHi();
final char encodedIndex = (char) i;
- final int codePoint = internalEncoding.decodeCharacter(
- encodedIndex);
+ final int codePoint = internalEncoding.decode(encodedIndex);
final int charSetIndex = charSet.getIndex(codePoint);
if (charSetIndex < 0
|| charSetIndex >= extentTable.length) {
Modified: trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
===================================================================
--- trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -46,7 +46,7 @@
/* The mock encoding just returns the code point to be encoded, i.e. the glyph index is the same as the code
* point, for simplicity. */
final Encoding mockEncoding = Mockito.mock(Encoding.class);
- Mockito.when(mockEncoding.decodeCharacter(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {
+ Mockito.when(mockEncoding.decode(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {
public Integer answer(final InvocationOnMock invocation) {
final Object[] args = invocation.getArguments();
return (Integer) args[0];
Modified: trunk/foray/foray-font/src/test/java/org/foray/font/format/ttf/OtfLookupGsubTests.java
===================================================================
--- trunk/foray/foray-font/src/test/java/org/foray/font/format/ttf/OtfLookupGsubTests.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-font/src/test/java/org/foray/font/format/ttf/OtfLookupGsubTests.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -246,7 +246,7 @@
final IntArrayBuilder glyphIndexes = codePoints;
for (int index = 0; index < codePoints.length(); index ++) {
final int codePoint = codePoints.intAt(index);
- final int glyphIndex = encoding.encodeCharacter(codePoint);
+ final int glyphIndex = encoding.encode(codePoint);
glyphIndexes.replace(index, glyphIndex);
}
return codePoints;
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/CharSetParser.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/CharSetParser.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/CharSetParser.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -153,13 +153,13 @@
this.characterSet.append(Character.MIN_VALUE);
this.glyphNames.add(StringUtils.EMPTY_STRING);
final GlyphList gl = GlyphListAgl.getInstance();
- final char theChar = gl.mapGlyphNameToCodePoint(line);
+ final int theChar = gl.mapGlyphNameToCodePoint(line);
if (theChar == Character.MAX_VALUE) {
throw new PsException("Character not found in Adobe Glyph List, line "
+ this.currentLineNumber + ": " + line);
}
- this.characterSet.setCharAt(arrayIndex, theChar);
+ this.characterSet.setCharAt(arrayIndex, (char) theChar);
this.glyphNames.set(arrayIndex, line);
}
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -40,7 +40,7 @@
@Override
public boolean canEncode(final int codePoint) {
- return this.encodeCharacter(codePoint) != 0;
+ return this.encode(codePoint) > -1;
}
}
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -28,8 +28,6 @@
package org.foray.ps.encode;
-import org.foray.common.WellKnownConstants;
-
import org.axsl.ps.Cmap04;
/**
@@ -94,10 +92,10 @@
}
@Override
- public int encodeCharacter(final int c) {
+ public int encode(final int c) {
final Cmap4a04Entry entry = entryForChar((char) c);
if (entry == null) {
- return 0;
+ return -1;
}
return entry.encodeCharacter((char) c);
}
@@ -117,14 +115,14 @@
}
@Override
- public int decodeCharacter(final int glyphIndexInput) {
+ public int decode(final int glyphIndexInput) {
for (int i = 0; i < this.entries.length; i++) {
final int codePoint = this.entries[i].decodeCharacter(glyphIndexInput);
- if (codePoint != WellKnownConstants.INVALID_UNICODE_CHARACTER) {
+ if (codePoint > -1) {
return codePoint;
}
}
- return WellKnownConstants.INVALID_UNICODE_CHARACTER;
+ return -1;
}
@Override
@@ -134,12 +132,12 @@
@Override
public int getFirstIndex() {
- return INVALID_UNICODE_CHAR;
+ return -1;
}
@Override
public int getLastIndex() {
- return INVALID_UNICODE_CHAR;
+ return -1;
}
@Override
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a04Entry.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -28,8 +28,6 @@
package org.foray.ps.encode;
-import org.foray.common.WellKnownConstants;
-
/**
* Contains one entry from a TrueType Cmap4a subtable, format 4, which handles
* 16-bit Unicode codepoint ranges.
@@ -102,12 +100,11 @@
/**
* Encodes a given character.
* @param c The character to be encoded.
- * @return The glyph index corresponding to {@code c}, or zero if this
- * entry does not know how to encode it.
+ * @return The glyph index corresponding to {@code c}, or -1 if this entry does not know how to encode it.
*/
- public char encodeCharacter(final int c) {
+ public int encodeCharacter(final int c) {
if (! containsChar(c)) {
- return 0;
+ return -1;
}
if (this.glyphIndexes == null) {
return (char) (c - this.unicodeStart + this.glyphStartIndex);
@@ -118,19 +115,17 @@
/**
* Decodes a given glyphIndex.
* @param glyphIndex The glyph index to be decoded.
- * @return The Unicode code point corresponding to {@code glyphIndex},
- * or {@link Character#MAX_VALUE} if this entry does not know how to decode
- * it.
+ * @return The Unicode code point corresponding to {@code glyphIndex}, or -1 if this entry does not know how to
+ * decode it.
*/
public int decodeCharacter(final int glyphIndex) {
if (this.glyphIndexes == null) {
- final char codePoint = (char) (glyphIndex - this.glyphStartIndex
- + this.unicodeStart);
+ final char codePoint = (char) (glyphIndex - this.glyphStartIndex + this.unicodeStart);
if (glyphIndex >= this.glyphStartIndex
&& codePoint <= this.unicodeStart) {
return codePoint;
}
- return WellKnownConstants.INVALID_UNICODE_CHARACTER;
+ return -1;
}
for (int i = 0; i < this.glyphIndexes.length(); i++) {
if (this.glyphIndexes.charAt(i) == glyphIndex) {
@@ -137,7 +132,7 @@
return this.unicodeStart + i;
}
}
- return WellKnownConstants.INVALID_UNICODE_CHARACTER;
+ return -1;
}
}
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/Cmap4a12.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -87,10 +87,10 @@
}
@Override
- public int encodeCharacter(final int codePoint) {
+ public int encode(final int codePoint) {
final Cmap4a12Entry entry = entryForChar(codePoint);
if (entry == null) {
- return 0;
+ return -1;
}
return entry.encodeCharacter(codePoint);
}
@@ -110,15 +110,14 @@
}
@Override
- public int decodeCharacter(final int glyphIndexInput) {
+ public int decode(final int glyphIndexInput) {
for (int i = 0; i < this.entries.length; i++) {
- final int codePoint = this.entries[i].decodeCharacter(
- glyphIndexInput);
+ final int codePoint = this.entries[i].decodeCharacter(glyphIndexInput);
if (codePoint != Character.MAX_VALUE) {
return codePoint;
}
}
- return Character.MAX_VALUE;
+ return -1;
}
@Override
@@ -128,12 +127,12 @@
@Override
public int getFirstIndex() {
- return INVALID_UNICODE_CHAR;
+ return -1;
}
@Override
public int getLastIndex() {
- return INVALID_UNICODE_CHAR;
+ return -1;
}
@Override
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingParser.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingParser.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingParser.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -368,9 +368,8 @@
final String glyphName = this.glyphNames.get(i);
for (int j = 0; j < this.glyphListsToCheck.size(); j++) {
final GlyphList glyphList = this.glyphListsToCheck.get(j);
- final char codePoint = glyphList.mapGlyphNameToCodePoint(
- glyphName);
- if (codePoint != Character.MAX_VALUE) {
+ final int codePoint = glyphList.mapGlyphNameToCodePoint(glyphName);
+ if (codePoint > -1) {
this.codePoints.setIntAt(i, codePoint);
final char glyphIndex = this.glyphIndexes.charAt(i);
this.codePointIndexes.setIntAt(i, glyphIndex);
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVector4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVector4a.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVector4a.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -35,7 +35,6 @@
import org.axsl.common.sequence.IntSequence;
import org.axsl.common.sequence.IntSequenceMutable;
-import org.axsl.ps.Encoding;
import org.axsl.ps.GlyphList;
import org.axsl.ps.PsEncoding;
@@ -138,7 +137,7 @@
}
@Override
- public int encodeCharacter(final int codePoint) {
+ public int encode(final int codePoint) {
if (codePoint < 0) {
return -1;
}
@@ -162,7 +161,7 @@
}
@Override
- public int decodeCharacter(final int encodedIndex) {
+ public int decode(final int encodedIndex) {
/* This is not expected to be used much or at all, so no attempt at optimization has been made. */
for (int i = 0; i < this.codePointIndexes.length(); i++) {
if (this.codePointIndexes.charAt(i) == encodedIndex) {
@@ -174,7 +173,7 @@
return this.codePoints2.intAt(i);
}
}
- return INVALID_UNICODE_CHAR;
+ return -1;
}
/**
@@ -304,7 +303,7 @@
if (this.sourceGlyphLists == null) {
return null;
}
- if (encodeCharacter(codePoint) < 0) {
+ if (! canEncode(codePoint)) {
// codePoint is not in this encoding.
return null;
}
@@ -326,15 +325,15 @@
@Override
public boolean isSubsetOf(final PsEncoding otherVector) {
for (int i = 0; i < this.codePoints.length(); i++) {
- final int thisEncodedValue = this.encodeCharacter(this.codePoints.charAt(i));
- final int otherEncodedValue = otherVector.encodeCharacter(this.codePoints.charAt(i));
+ final int thisEncodedValue = this.encode(this.codePoints.charAt(i));
+ final int otherEncodedValue = otherVector.encode(this.codePoints.charAt(i));
if (thisEncodedValue != otherEncodedValue) {
return false;
}
}
for (int i = 0; i < this.codePoints2.length(); i++) {
- final int thisEncodedValue = this.encodeCharacter(this.codePoints2.intAt(i));
- final int otherEncodedValue = otherVector.encodeCharacter(this.codePoints2.intAt(i));
+ final int thisEncodedValue = this.encode(this.codePoints2.intAt(i));
+ final int otherEncodedValue = otherVector.encode(this.codePoints2.intAt(i));
if (thisEncodedValue != otherEncodedValue) {
return false;
}
@@ -400,19 +399,9 @@
this.codePointIndexes2 = (CharArray) stream.readObject();
}
-// @Override
-// public boolean isPredefinedPs() {
-// return false;
-// }
-//
-// @Override
-// public boolean isPredefinedPdf() {
-// return false;
-// }
-//
@Override
public boolean canEncode(final int codePoint) {
- return this.encodeCharacter(codePoint) > -1;
+ return this.encode(codePoint) > -1;
}
/**
@@ -437,10 +426,8 @@
if (glyphName == null) {
continue;
}
- /* Make sure it maps back. Otherwise it must have come from a
- * different list. */
- final char roundTripCodePoint = mapGlyphNameToCodePoint(glyphLists,
- glyphName);
+ /* Make sure it maps back. Otherwise it must have come from a different list. */
+ final int roundTripCodePoint = mapGlyphNameToCodePoint(glyphLists, glyphName);
if (roundTripCodePoint == codePoint) {
return glyphName;
}
@@ -449,17 +436,14 @@
}
/**
- * 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.
+ * Finds the Unicode code point 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.
+ * @return The Unicode code point which corresponds to glyphName, or -1 if there is none.
*/
- public char mapGlyphNameToCodePoint(final List<GlyphList> glyphLists, final String glyphName) {
+ public int mapGlyphNameToCodePoint(final List<GlyphList> glyphLists, final String glyphName) {
if (glyphLists == null) {
- return Encoding.INVALID_UNICODE_CHAR;
+ return -1;
}
for (int i = 0; i < glyphLists.size(); i++) {
final GlyphList list = glyphLists.get(i);
@@ -466,12 +450,12 @@
if (list == null) {
continue;
}
- final char c = list.mapGlyphNameToCodePoint(glyphName);
- if (c != Encoding.INVALID_UNICODE_CHAR) {
+ final int c = list.mapGlyphNameToCodePoint(glyphName);
+ if (c > -1) {
return c;
}
}
- return Encoding.INVALID_UNICODE_CHAR;
+ return -1;
}
@Override
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVectorInputStream.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVectorInputStream.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/EncodingVectorInputStream.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -28,9 +28,9 @@
package org.foray.ps.encode;
+import org.foray.common.WellKnownConstants;
import org.foray.ps.PsServer4a;
-import org.axsl.ps.Encoding;
import org.axsl.ps.PsEncoding;
import org.apache.commons.cli.CommandLine;
@@ -81,7 +81,7 @@
final int output;
if (input >= this.encoding.getFirstIndex()
&& input <= this.encoding.getLastIndex()) {
- output = this.encoding.decodeCharacter(input);
+ output = this.encoding.decode(input);
} else {
output = input;
}
@@ -167,7 +167,7 @@
while (true) {
final int inputByte = inputFilter.read();
if (inputByte >= 0
- && inputByte != Encoding.INVALID_UNICODE_CHAR) {
+ && inputByte != WellKnownConstants.INVALID_UNICODE_CHARACTER) {
writer.write(inputByte);
} else {
break;
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/GlyphList4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/GlyphList4a.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/encode/GlyphList4a.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -148,21 +148,18 @@
}
@Override
- public char mapGlyphNameToCodePoint(final String glyphName) {
- final char errorValue = 0xFFFF;
+ public int mapGlyphNameToCodePoint(final String glyphName) {
+ final int errorValue = -1;
if (this.sortedGlyphNames == null
|| this.codePointsForGlyphNames == null) {
return errorValue;
}
- final int sortedGlyphNameIndex = Arrays.binarySearch(
- this.sortedGlyphNames, glyphName);
+ final int sortedGlyphNameIndex = Arrays.binarySearch(this.sortedGlyphNames, glyphName);
if (sortedGlyphNameIndex < 0
- || sortedGlyphNameIndex
- >= this.codePointsForGlyphNames.length) {
+ || sortedGlyphNameIndex >= this.codePointsForGlyphNames.length) {
return errorValue;
}
- final char[] codePointArray =
- this.codePointsForGlyphNames[sortedGlyphNameIndex];
+ final char[] codePointArray = this.codePointsForGlyphNames[sortedGlyphNameIndex];
if (codePointArray == null) {
return errorValue;
}
Modified: trunk/foray/foray-ps/src/test/java/org/foray/ps/encode/EncodingVector4aTests.java
===================================================================
--- trunk/foray/foray-ps/src/test/java/org/foray/ps/encode/EncodingVector4aTests.java 2021-02-07 23:16:58 UTC (rev 11897)
+++ trunk/foray/foray-ps/src/test/java/org/foray/ps/encode/EncodingVector4aTests.java 2021-02-08 12:20:11 UTC (rev 11898)
@@ -97,20 +97,20 @@
final EncodingVector4a out = new EncodingCustom("Test Encoding", null, CODE_POINTS_1, INDEXES_1);
/* Test the ones that are in the encoding. */
- Assert.assertEquals(5, out.encodeCharacter(14_561));
- Assert.assertEquals(4, out.encodeCharacter(28));
- Assert.assertEquals(3, out.encodeCharacter(64_000));
- Assert.assertEquals(2, out.encodeCharacter(0));
- Assert.assertEquals(1, out.encodeCharacter(Character.MAX_CODE_POINT));
- Assert.assertEquals(0, out.encodeCharacter(14));
+ Assert.assertEquals(5, out.encode(14_561));
+ Assert.assertEquals(4, out.encode(28));
+ Assert.assertEquals(3, out.encode(64_000));
+ Assert.assertEquals(2, out.encode(0));
+ Assert.assertEquals(1, out.encode(Character.MAX_CODE_POINT));
+ Assert.assertEquals(0, out.encode(14));
/* Now test some that aren't. */
- Assert.assertEquals(-1, out.encodeCharacter(Integer.MIN_VALUE));
- Assert.assertEquals(-1, out.encodeCharacter(-200));
- Assert.assertEquals(-1, out.encodeCharacter(-1));
- Assert.assertEquals(-1, out.encodeCharacter(1));
- Assert.assertEquals(-1, out.encodeCharacter(200));
- Assert.assertEquals(-1, out.encodeCharacter(Integer.MAX_VALUE));
+ Assert.assertEquals(-1, out.encode(Integer.MIN_VALUE));
+ Assert.assertEquals(-1, out.encode(-200));
+ Assert.assertEquals(-1, out.encode(-1));
+ Assert.assertEquals(-1, out.encode(1));
+ Assert.assertEquals(-1, out.encode(200));
+ Assert.assertEquals(-1, out.encode(Integer.MAX_VALUE));
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-07 23:17:05
|
Revision: 11897
http://sourceforge.net/p/foray/code/11897
Author: victormote
Date: 2021-02-07 23:16:58 +0000 (Sun, 07 Feb 2021)
Log Message:
-----------
Convert subsetting logic from char[] to IntArrayBuilder.
Modified Paths:
--------------
trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-07 19:56:45 UTC (rev 11896)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-07 23:16:58 UTC (rev 11897)
@@ -29,7 +29,7 @@
package org.foray.font;
import org.foray.common.WellKnownConstants;
-import org.foray.common.primitive.StringUtils;
+import org.foray.common.sequence.IntArrayBuilder;
import org.axsl.ps.Encoding;
@@ -36,11 +36,6 @@
/**
* Keeps track of which glyphs are included in a subset, and provides methods to convert indexes back and forth between
* the original complete font and the subset font.
- *
- * <p>Design Note: Although the internals of this class will only handle
- * 16-bit font indexes, the API deliberately uses ints (31 unsigned bits)
- * instead of chars. This way the internals can be expanded in the future if
- * necessary, without changing the API.</p>
*/
public class Subset {
@@ -61,34 +56,28 @@
* The index is used as a key to retrieve the related subset index from {@link #subsetByOriginal}.
* This array is sorted in parallel with {@link #subsetByOriginal}.
*/
- private char[] originalByOriginal;
+ private IntArrayBuilder originalByOriginal;
/**
* Array whose elements are the glyph indices for the subset font, and whose index is used as a key that corresponds
* to the same index in {@link #originalByOriginal}.
*/
- private char[] subsetByOriginal;
+ private IntArrayBuilder subsetByOriginal;
/**
* Array whose elements are the actually-used glyph indices for the original full font, sorted by subset index.
*/
- private char[] originalBySubset;
+ private IntArrayBuilder originalBySubset;
/**
- * The number of subset glyphs used so far. It should be incremented after
- * creating new subset glyphs.
- */
- private int qtyGlyphs = 0;
-
- /**
* Constructor.
* @param consumerFont The parent ConsumerFont instance.
*/
public Subset(final ConsumerFont4a consumerFont) {
this.consumerFont = consumerFont;
- this.originalByOriginal = new char[this.arraySizeIncrement];
- this.subsetByOriginal = new char[this.arraySizeIncrement];
- this.originalBySubset = new char[this.arraySizeIncrement];
+ this.originalByOriginal = new IntArrayBuilder(this.arraySizeIncrement);
+ this.subsetByOriginal = new IntArrayBuilder(this.arraySizeIncrement);
+ this.originalBySubset = new IntArrayBuilder(this.arraySizeIncrement);
if (this.consumerFont.getFOrayFont() instanceof FsTrueTypeFont) {
// Make sure that the 3 first glyphs are included
this.encodeSubsetIndex((char) 0);
@@ -104,13 +93,11 @@
* @return The index into the subsetted glyphs.
*/
public int encodeSubsetIndex(final int fontGlyphIndex) {
- final int index = StringUtils.indexOfSortedCharArray(
- this.originalByOriginal, (char) fontGlyphIndex, 0,
- this.numGlyphsUsed() - 1);
+ final int index = this.originalByOriginal.binarySearch(fontGlyphIndex);
if (index < 0) {
return addMapping(fontGlyphIndex);
}
- return this.subsetByOriginal[index];
+ return this.subsetByOriginal.intAt(index);
}
/**
@@ -121,10 +108,10 @@
*/
public int decodeSubsetIndex(final int subsetGlyphIndex) {
if (subsetGlyphIndex < 0
- || subsetGlyphIndex > this.qtyGlyphs) {
+ || subsetGlyphIndex > this.originalByOriginal.length()) {
return Character.MAX_VALUE;
}
- return this.originalBySubset[subsetGlyphIndex];
+ return this.originalBySubset.intAt(subsetGlyphIndex);
}
/**
@@ -133,40 +120,29 @@
* @return The subset index for the new glyph.
*/
private char addMapping(final int glyphIndex) {
- if (this.qtyGlyphs >= this.originalByOriginal.length) {
- this.addCapacity(this.arraySizeIncrement);
- }
- final int newIndex = this.qtyGlyphs;
-
- final int insertionPoint = findInsertionPoint((char) glyphIndex);
- /* Move all past the insertion point to the next higher index. */
- for (int i = newIndex; i > insertionPoint; i--) {
- this.originalByOriginal[i] = this.originalByOriginal[i - 1];
- this.subsetByOriginal[i] = this.subsetByOriginal[i - 1];
- }
- /* Now move the new items into place. */
- this.originalByOriginal[insertionPoint] = (char) glyphIndex;
- this.subsetByOriginal[insertionPoint] = (char) newIndex;
-
- this.originalBySubset[newIndex] = (char) glyphIndex;
-
- this.qtyGlyphs ++;
+ final int newIndex = this.originalByOriginal.length();
+ this.originalByOriginal.append(glyphIndex);
+ this.subsetByOriginal.append(newIndex);
+ this.originalBySubset.append(glyphIndex);
+ sortByOriginal();
return (char) newIndex;
}
- /**
- * Finds the index in {@link #originalByOriginal} where the new item should
- * be inserted in order to keep it sorted properly.
- * @param glyphIndex The index that is being added.
- * @return The index where the new item should be inserted.
- */
- private int findInsertionPoint(final char glyphIndex) {
- for (int i = 0; i < this.numGlyphsUsed(); i++) {
- if (glyphIndex < this.originalByOriginal[i]) {
- return i;
+ private void sortByOriginal() {
+ /* Since these arrays are always sorted, and since we appended the new entries, if we start at the end and
+ * work backwards, this should require only one pass. */
+ boolean stillSorting = true;
+ while (stillSorting) {
+ stillSorting = false;
+ for (int i = this.originalByOriginal.length() - 1; i > 0; i--) {
+ if (this.originalByOriginal.intAt(i) < this.originalByOriginal.intAt(i - 1)) {
+ /* The are out of order. Swap them. */
+ this.originalByOriginal.swap(i, i - 1);
+ this.subsetByOriginal.swap(i, i - 1);
+ stillSorting = true;
+ }
}
}
- return this.numGlyphsUsed();
}
/**
@@ -199,9 +175,7 @@
stillSorting = true;
/* First, switch the originalBySubset array. */
- final char saveChar = this.originalBySubset[i];
- this.originalBySubset[i] = this.originalBySubset[i - 1];
- this.originalBySubset[i - 1] = saveChar;
+ this.originalBySubset.swap(i, i - 1);
/* Second, note that the originalByOriginal array needs
* no changes. */
@@ -208,17 +182,13 @@
/* Third, change the subsetByOriginal array element for
* the previous element. */
- int commonIndex = StringUtils.indexOfSortedCharArray(
- this.originalByOriginal, previousOriginalIndex, 0,
- this.numGlyphsUsed() - 1);
- this.subsetByOriginal[commonIndex] = i;
+ int commonIndex = this.originalByOriginal.binarySearch(previousOriginalIndex);
+ this.subsetByOriginal.setIntAt(commonIndex, i); // [commonIndex] = i;
/* Fourth, change the subsetByOriginal array element for
* the current element. */
- commonIndex = StringUtils.indexOfSortedCharArray(
- this.originalByOriginal, currentOriginalIndex, 0,
- this.numGlyphsUsed() - 1);
- this.subsetByOriginal[commonIndex] = (char) (i - 1);
+ commonIndex = this.originalByOriginal.binarySearch(currentOriginalIndex);
+ this.subsetByOriginal.setIntAt(commonIndex, i - 1); // [commonIndex] = (char) (i - 1);
}
}
}
@@ -229,51 +199,16 @@
* @return The number of glyphs in this subset.
*/
public int numGlyphsUsed() {
- return this.qtyGlyphs;
+ return this.originalByOriginal.length();
}
/**
- * Increases the size of the arrays by {@code capacityToAdd}.
- * @param capacityToAdd The number of elements to add to each of the
- * parallel arrays.
- */
- private void addCapacity(final int capacityToAdd) {
- // Can add, but not subtract.
- if (capacityToAdd < 1) {
- return;
- }
- final int newCapacity = this.originalByOriginal.length + capacityToAdd;
-
- // Increase size of originalByOriginal
- char[] newChars = new char[newCapacity];
- System.arraycopy(this.originalByOriginal, 0, newChars, 0,
- this.originalByOriginal.length);
- this.originalByOriginal = newChars;
-
- // Increase size of subsetByOriginal
- newChars = new char[newCapacity];
- System.arraycopy(this.subsetByOriginal, 0, newChars, 0,
- this.subsetByOriginal.length);
- this.subsetByOriginal = newChars;
-
- // Increase size of originalBySubset
- newChars = new char[newCapacity];
- System.arraycopy(this.originalBySubset, 0, newChars, 0,
- this.originalBySubset.length);
- this.originalBySubset = newChars;
-
- }
-
- /**
* Indicates whether a given glyph is in this subset.
- * @param originalGlyphIndex The original (full font) glyph index that is
- * being checked.
+ * @param originalGlyphIndex The original (full font) glyph index that is being checked.
* @return True iff originalGlyphIndex exists in this subset.
*/
public boolean glyphUsed(final int originalGlyphIndex) {
- final int index = StringUtils.indexOfSortedCharArray(
- this.originalByOriginal, (char) originalGlyphIndex, 0,
- this.numGlyphsUsed() - 1);
+ final int index = this.originalByOriginal.binarySearch(originalGlyphIndex);
return index > -1;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <vic...@us...> - 2021-02-07 19:56:47
|
Revision: 11896
http://sourceforge.net/p/foray/code/11896
Author: victormote
Date: 2021-02-07 19:56:45 +0000 (Sun, 07 Feb 2021)
Log Message:
-----------
1. Add test for Subset class. 2. Fix bug discovered in that test.
Modified Paths:
--------------
trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
Added Paths:
-----------
trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-07 18:44:39 UTC (rev 11895)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/Subset.java 2021-02-07 19:56:45 UTC (rev 11896)
@@ -33,12 +33,9 @@
import org.axsl.ps.Encoding;
-import org.slf4j.Logger;
-
/**
- * Keeps track of which glyphs are included in a subset, and provides methods
- * to convert indexes back and forth between the original complete font and
- * the subset font.
+ * Keeps track of which glyphs are included in a subset, and provides methods to convert indexes back and forth between
+ * the original complete font and the subset font.
*
* <p>Design Note: Although the internals of this class will only handle
* 16-bit font indexes, the API deliberately uses ints (31 unsigned bits)
@@ -59,23 +56,21 @@
private int arraySizeIncrement = WellKnownConstants.MAX_8_BIT_UNSIGNED_VALUES;
/**
- * Array whose elements are the actually-used glyph indices for the
- * original full font, sorted in that order, and whose index is used as a
- * key to retrieve the related subset index from {@link #subsetByOriginal}.
+ * Array whose elements are the actually-used glyph indices for the original full font.
+ * These are constantly kept sorted so that they can be efficiently searched.
+ * The index is used as a key to retrieve the related subset index from {@link #subsetByOriginal}.
* This array is sorted in parallel with {@link #subsetByOriginal}.
*/
private char[] originalByOriginal;
/**
- * Array whose elements are the glyph indices for the subset font, and whose
- * index is used as a key that corresponds to the same index in
- * {@link #originalByOriginal}.
+ * Array whose elements are the glyph indices for the subset font, and whose index is used as a key that corresponds
+ * to the same index in {@link #originalByOriginal}.
*/
private char[] subsetByOriginal;
/**
- * Array whose elements are the actually-used glyph indices for the
- * original full font, sorted by subset index.
+ * Array whose elements are the actually-used glyph indices for the original full font, sorted by subset index.
*/
private char[] originalBySubset;
@@ -103,10 +98,9 @@
}
/**
- * Get the subsetted font glyph index that corresponds to fontGlyphIndex,
- * creating a subset glyph index if necessary.
- * @param fontGlyphIndex The index into the full font (not subsetted)
- * glyphs.
+ * Get the subsetted font glyph index that corresponds to fontGlyphIndex, creating a subset glyph index if
+ * necessary.
+ * @param fontGlyphIndex The index into the full font (not subsetted) glyphs.
* @return The index into the subsetted glyphs.
*/
public int encodeSubsetIndex(final int fontGlyphIndex) {
@@ -120,11 +114,10 @@
}
/**
- * Get the full font glyph index (not subsetted) that corresponds to
- * subsetGlyphIndex.
+ * Get the full font glyph index (not subsetted) that corresponds to subsetGlyphIndex.
* @param subsetGlyphIndex The index into the subset font glyphs.
- * @return The index into the full font (not subsetted) glyphs, or
- * {@link Character#MAX_VALUE} if the input is not valid.
+ * @return The index into the full font (not subsetted) glyphs, or {@link Character#MAX_VALUE} if the input is not
+ * valid.
*/
public int decodeSubsetIndex(final int subsetGlyphIndex) {
if (subsetGlyphIndex < 0
@@ -177,16 +170,14 @@
}
/**
- * Sorts the subset by underlying Unicode codepoint.
+ * <p>Sorts the subset by underlying Unicode codepoint.
* This may make some output formats more compact.
- * For example, the PDF ToUnicodeCMap can be written more compactly if
- * glyph indexes are in Unicode order.
- * Since this changes the subset indexes, it is only useful if no output
- * has been written (or any other permanent meaning assigned) using the
- * pre-sorted indices.
+ * For example, the PDF ToUnicodeCMap can be written more compactly if glyph indexes are in Unicode order.</p>
+ *
+ * <p><b>Warning!</b> Since this changes the subset indexes, it is only useful if no output has been written (or any
+ * other permanent meaning assigned) using the pre-sorted indices.</p>
*/
public synchronized void sortUsedGlyphs() {
- /* TODO: This method is untested */
final FreeStandingFont fsf = this.consumerFont.getFreeStandingFont();
if (fsf == null) {
return;
@@ -198,16 +189,14 @@
while (stillSorting) {
stillSorting = false;
for (char i = 1; i < this.numGlyphsUsed(); i++) {
- final char currentOriginalIndex =
- (char) this.decodeSubsetIndex(i);
- final int currentCodePoint = encoding.decodeCharacter(
- currentOriginalIndex);
+ final char currentOriginalIndex = (char) this.decodeSubsetIndex(i);
+ final int currentCodePoint = encoding.decodeCharacter(currentOriginalIndex);
final char previousOriginalIndex
= (char) this.decodeSubsetIndex(i - 1);
- final int previousCodePoint = encoding.decodeCharacter(
- previousOriginalIndex);
+ final int previousCodePoint = encoding.decodeCharacter(previousOriginalIndex);
if (previousCodePoint > currentCodePoint) {
/* They are out of order and should be switched. */
+ stillSorting = true;
/* First, switch the originalBySubset array. */
final char saveChar = this.originalBySubset[i];
@@ -288,12 +277,4 @@
return index > -1;
}
- /**
- * Returns the logger.
- * @return The logger.
- */
- public Logger getLogger() {
- return this.consumerFont.getLogger();
- }
-
}
Added: trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
===================================================================
--- trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java (rev 0)
+++ trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java 2021-02-07 19:56:45 UTC (rev 11896)
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2021 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.font;
+
+import org.axsl.ps.Encoding;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+/**
+ * Tests of {@link Subset}.
+ */
+public class SubsetTests {
+
+ @Test
+ public void test01() {
+ /* The mock encoding just returns the code point to be encoded, i.e. the glyph index is the same as the code
+ * point, for simplicity. */
+ final Encoding mockEncoding = Mockito.mock(Encoding.class);
+ Mockito.when(mockEncoding.decodeCharacter(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {
+ public Integer answer(final InvocationOnMock invocation) {
+ final Object[] args = invocation.getArguments();
+ return (Integer) args[0];
+ }
+ });
+ final FreeStandingFont mockFsf = Mockito.mock(FreeStandingFont.class);
+ Mockito.when(mockFsf.getInternalEncoding()).thenReturn(mockEncoding);
+ final ConsumerFont4a mockConsumerFont = Mockito.mock(ConsumerFont4a.class);
+ Mockito.when(mockConsumerFont.getFreeStandingFont()).thenReturn(mockFsf);
+
+ final Subset out = new Subset(mockConsumerFont);
+ Assert.assertEquals(0, out.numGlyphsUsed());
+
+ /* Encode some glyph indexes, deliberately not in sequence. */
+ final int subsetIndex1 = out.encodeSubsetIndex(100);
+ Assert.assertEquals(0, subsetIndex1);
+ final int subsetIndex2 = out.encodeSubsetIndex(50);
+ Assert.assertEquals(1, subsetIndex2);
+ final int subsetIndex3 = out.encodeSubsetIndex(125);
+ Assert.assertEquals(2, subsetIndex3);
+ final int subsetIndex4 = out.encodeSubsetIndex(30);
+ Assert.assertEquals(3, subsetIndex4);
+ Assert.assertEquals(4, out.numGlyphsUsed());
+
+ Assert.assertEquals(100, out.decodeSubsetIndex(subsetIndex1));
+ Assert.assertEquals(50, out.decodeSubsetIndex(subsetIndex2));
+ Assert.assertEquals(125, out.decodeSubsetIndex(subsetIndex3));
+ Assert.assertEquals(30, out.decodeSubsetIndex(subsetIndex4));
+
+ /* Encode the same ones again, to ensure that no new entries are created. */
+ Assert.assertEquals(3, out.encodeSubsetIndex(30));
+ Assert.assertEquals(2, out.encodeSubsetIndex(125));
+ Assert.assertEquals(1, out.encodeSubsetIndex(50));
+ Assert.assertEquals(0, out.encodeSubsetIndex(100));
+ Assert.assertEquals(4, out.numGlyphsUsed());
+
+ /* Sort the glyphs by code point. The encoding above treats the code point and non-subsetted glyph index as
+ * equal, so we should now be sorted by the non-subsetted glyph index. */
+ out.sortUsedGlyphs();
+ Assert.assertEquals(0, out.encodeSubsetIndex(30));
+ Assert.assertEquals(1, out.encodeSubsetIndex(50));
+ Assert.assertEquals(2, out.encodeSubsetIndex(100));
+ Assert.assertEquals(3, out.encodeSubsetIndex(125));
+ Assert.assertEquals(4, out.numGlyphsUsed());
+ }
+
+}
Property changes on: trunk/foray/foray-font/src/test/java/org/foray/font/SubsetTests.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Rev
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|