Revision: 11865
http://sourceforge.net/p/foray/code/11865
Author: victormote
Date: 2021-01-26 13:20:09 +0000 (Tue, 26 Jan 2021)
Log Message:
-----------
Tie in bidi char type enumeration with Java Character directionality notion.
Modified Paths:
--------------
trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiCharType.java
Added Paths:
-----------
trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiUtils.java
Modified: trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiCharType.java
===================================================================
--- trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiCharType.java 2021-01-26 12:03:38 UTC (rev 11864)
+++ trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiCharType.java 2021-01-26 13:20:09 UTC (rev 11865)
@@ -36,72 +36,144 @@
public enum BidiCharType {
/** The "Left-to-Right" Bidi character type. */
- L,
+ L(Character.DIRECTIONALITY_LEFT_TO_RIGHT),
/** The "Right-to-Left" Bidi character type. */
- R,
+ R(Character.DIRECTIONALITY_RIGHT_TO_LEFT),
/** The "Right-to-Left Arabic" Bidi character type. */
- AL,
+ AL(Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC),
/** The "European Number" Bidi character type. */
- EN,
+ EN(Character.DIRECTIONALITY_EUROPEAN_NUMBER),
/** The "European Number Separator" Bidi character type. */
- ES,
+ ES(Character.DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR),
/** The "European Number Terminator" Bidi character type. */
- ET,
+ ET(Character.DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR),
/** The "Arabic Number" Bidi character type. */
- AN,
+ AN(Character.DIRECTIONALITY_ARABIC_NUMBER),
/** The "Common Number Separator" Bidi character type. */
- CS,
+ CS(Character.DIRECTIONALITY_COMMON_NUMBER_SEPARATOR),
/** The "Nonspacing Mark" Bidi character type. */
- NSM,
+ NSM(Character.DIRECTIONALITY_NONSPACING_MARK),
/** The "Boundary Neutral" Bidi character type. */
- BN,
+ BN(Character.DIRECTIONALITY_BOUNDARY_NEUTRAL),
/** The "Paragraph Separator" Bidi character type. */
- B,
+ B(Character.DIRECTIONALITY_PARAGRAPH_SEPARATOR),
/** The "Segment Separator" Bidi character type. */
- S,
+ S(Character.DIRECTIONALITY_SEGMENT_SEPARATOR),
/** The "Whitespace" Bidi character type. */
- WS,
+ WS(Character.DIRECTIONALITY_WHITESPACE),
/** The "Other Neutrals" Bidi character type. */
- ON,
+ ON(Character.DIRECTIONALITY_OTHER_NEUTRALS),
/** The "Left-to-Right Embedding" Bidi character type. */
- LRE,
+ LRE(Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING),
/** The "Left-to-Right Override" Bidi character type. */
- LRO,
+ LRO(Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE),
/** The "Right-to-Left Embedding" Bidi character type. */
- RLE,
+ RLE(Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING),
/** The "Right-to-Left Override" Bidi character type. */
- RLO,
+ RLO(Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE),
/** The "Pop Directional Format" Bidi character type. */
- PDF,
+ PDF(Character.DIRECTIONALITY_POP_DIRECTIONAL_FORMAT),
/** The "Left-to-Right Isolate" Bidi character type. */
- LRI,
+ LRI(BidiUtils.DIRECTIONALITY_LEFT_TO_RIGHT_ISOLATE),
/** The "Right-to-Left Isolate" Bidi character type. */
- RLI,
+ RLI(BidiUtils.DIRECTIONALITY_RIGHT_TO_LEFT_ISOLATE),
/** The "First Strong Isolate" Bidi character type. */
- FSI,
+ FSI(BidiUtils.DIRECTIONALITY_FIRST_STRONG_ISOLATE),
/** The "Pop Directional Isolate" Bidi character type. */
- PDI
+ PDI(BidiUtils.DIRECTIONALITY_POP_DIRECTIONAL_ISOLATE);
+
+
+ /**
+ * Array whose index is the bidi character types index within {@link Character}.
+ * This should correspond to {@link #values()}, but we use this instead to avoid possible performance problems.
+ * @see #directionality
+ */
+ private static final BidiCharType[] VALUES = {
+ L, R, AL, EN, ES, ET, AN, CS, NSM, BN, B, S, WS, ON, LRE, LRO, RLE, RLO, PDF, LRI, RLI, FSI, PDI
+ };
+
+
+ /**
+ * The numeric index assigned to the bidi character type within {@link Character}.
+ * @see Character#getDirectionality(int)
+ */
+ private byte directionality;
+
+ /**
+ * Private constructor.
+ * @param index The numeric index assigned to the character type at {@link Character#getDirectionality(int)}.
+ */
+ BidiCharType(final byte index) {
+ this.directionality = index;
+ }
+
+ /**
+ * Returns the corresponding directionality index assigned to the character type within {@link Character}.
+ * @return The corresponding directionality index assigned to the character type within {@link Character}.
+ * @see Character#getDirectionality(int)
+ */
+ public byte getDirectionality() {
+ return this.directionality;
+ }
+
+ /**
+ * Returns the instance of this enumeration for a given bidi character directionality index.
+ * @param directionality The directionality index for which the enumeration is needed.
+ * @return The instance of this enumeration matching {@code directionality}, or null if {@code directionality} is
+ * out-of-bounds.
+ * @see Character#getDirectionality(int)
+ */
+ public static BidiCharType fromDirectionality(final int directionality) {
+ if (directionality < 0
+ || directionality >= VALUES.length) {
+ return null;
+ }
+ return VALUES[directionality];
+ }
+
+ /**
+ * Returns the instance of this enumeration for a given character.
+ * @param ch The character for which the bidi character type is needed.
+ * @return The instance of this enumeration for {@code ch}, or null if it is unknown or undefined.
+ * @see Character#getDirectionality(char)
+ */
+ public static BidiCharType forChar(final char ch) {
+ final int directionality = Character.getDirectionality(ch);
+ return fromDirectionality(directionality);
+ }
+
+ /**
+ * Returns the instance of this enumeration for a given Unicode code point.
+ * @param cp The code point for which the bidi character type is needed.
+ * @return The instance of this enumeration for {@code cp}, or null if it is unknown or undefined.
+ * @see Character#getDirectionality(int)
+ */
+ public static BidiCharType forCodePoint(final int cp) {
+ final int directionality = Character.getDirectionality(cp);
+ return fromDirectionality(directionality);
+ }
+
}
Added: trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiUtils.java
===================================================================
--- trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiUtils.java (rev 0)
+++ trunk/foray/foray-unicode/src/main/java/org/foray/unicode/bidi/BidiUtils.java 2021-01-26 13:20:09 UTC (rev 11865)
@@ -0,0 +1,71 @@
+/*
+ * 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.unicode.bidi;
+
+/**
+ * Utility code supporting the Unicode Bidirectional logic.
+ */
+public final class BidiUtils {
+
+
+ /**
+ * Weak bidirectional character type "LRI" in the Unicode specification.
+ * Substitute for same-named and same-values constant in {@link java.lang.Character} starting in Java 9.
+ * TODO: After FOray upgrades to Java 9, replace this constant with the one in {@link java.lang.Character}.
+ */
+ public static final byte DIRECTIONALITY_LEFT_TO_RIGHT_ISOLATE = 19;
+
+ /**
+ * Weak bidirectional character type "RLI" in the Unicode specification.
+ * Substitute for same-named and same-values constant in {@link java.lang.Character} starting in Java 9.
+ * TODO: After FOray upgrades to Java 9, replace this constant with the one in {@link java.lang.Character}.
+ */
+ public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_ISOLATE = 20;
+
+ /**
+ * Weak bidirectional character type "FSI" in the Unicode specification.
+ * Substitute for same-named and same-values constant in {@link java.lang.Character} starting in Java 9.
+ * TODO: After FOray upgrades to Java 9, replace this constant with the one in {@link java.lang.Character}.
+ */
+ public static final byte DIRECTIONALITY_FIRST_STRONG_ISOLATE = 21;
+
+ /**
+ * Weak bidirectional character type "PDI" in the Unicode specification.
+ * Substitute for same-named and same-values constant in {@link java.lang.Character} starting in Java 9.
+ * TODO: After FOray upgrades to Java 9, replace this constant with the one in {@link java.lang.Character}.
+ */
+ public static final byte DIRECTIONALITY_POP_DIRECTIONAL_ISOLATE = 22;
+
+
+ /**
+ * Private constructor. This is a utility class and should never be instantiated.
+ */
+ private BidiUtils() { }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|