You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NamedCharacter.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import org.apache.xmlgraphics.fonts.Glyphs;
  20. import org.apache.fop.util.CharUtilities;
  21. /**
  22. * Represents an named character with character name (from the Adobe glyph list) and a Unicode
  23. * sequence that this character represents.
  24. */
  25. public class NamedCharacter {
  26. private String charName;
  27. private String unicodeSequence;
  28. /**
  29. * Main constructor.
  30. * @param charName the character name
  31. * @param unicodeSequence the Unicode sequence associated with this character
  32. */
  33. public NamedCharacter(String charName, String unicodeSequence) {
  34. if (charName == null) {
  35. throw new NullPointerException("charName must not be null");
  36. }
  37. this.charName = charName;
  38. if (unicodeSequence != null) {
  39. this.unicodeSequence = unicodeSequence;
  40. } else {
  41. this.unicodeSequence = Glyphs.getUnicodeSequenceForGlyphName(charName);
  42. }
  43. }
  44. /**
  45. * Simple constructor.
  46. * @param charName the character name
  47. */
  48. public NamedCharacter(String charName) {
  49. this(charName, null);
  50. }
  51. /** {@inheritDoc} */
  52. public int hashCode() {
  53. final int prime = 31;
  54. int result = 1;
  55. result = prime * result + ((charName == null) ? 0 : charName.hashCode());
  56. return result;
  57. }
  58. /** {@inheritDoc} */
  59. public boolean equals(Object obj) {
  60. if (this == obj) {
  61. return true;
  62. }
  63. if (obj == null) {
  64. return false;
  65. }
  66. if (getClass() != obj.getClass()) {
  67. return false;
  68. }
  69. final NamedCharacter other = (NamedCharacter)obj;
  70. return charName.equals(other.charName);
  71. }
  72. /**
  73. * Returns the character name (as defined by the Adobe glyph list).
  74. * @return the character name
  75. */
  76. public String getName() {
  77. return this.charName;
  78. }
  79. /**
  80. * Returns the Unicode sequence associated with this character.
  81. * @return the Unicode sequence (or null if no Unicode sequence is associated)
  82. */
  83. public String getUnicodeSequence() {
  84. return this.unicodeSequence;
  85. }
  86. /**
  87. * Indicates whether a single Unicode value is associated with this character.
  88. * @return true if exactly one Unicode value is associated with this character, false otherwise
  89. */
  90. public boolean hasSingleUnicodeValue() {
  91. return (this.unicodeSequence != null && this.unicodeSequence.length() == 1);
  92. }
  93. /**
  94. * Returns the single Unicode value associated with this named character. Check
  95. * {@link #hasSingleUnicodeValue()} before you call this method because an
  96. * IllegalStateException is thrown is a Unicode sequence with more than one character is
  97. * associated with this character.
  98. * @return the single Unicode value (or FFFF ("NOT A CHARACTER") if no Unicode value is
  99. * available)
  100. * @throws IllegalStateException if a Unicode sequence with more than one value is associated
  101. * with the named character
  102. */
  103. public char getSingleUnicodeValue() throws IllegalStateException {
  104. if (this.unicodeSequence == null) {
  105. return CharUtilities.NOT_A_CHARACTER;
  106. }
  107. if (this.unicodeSequence.length() > 1) {
  108. throw new IllegalStateException("getSingleUnicodeValue() may not be called for a"
  109. + " named character that has more than one Unicode value (a sequence)"
  110. + " associated with the named character!");
  111. }
  112. return this.unicodeSequence.charAt(0);
  113. }
  114. /** {@inheritDoc} */
  115. public String toString() {
  116. StringBuffer sb = new StringBuffer(this.unicodeSequence);
  117. sb.append(" (");
  118. if (this.unicodeSequence != null) {
  119. for (int i = 0, c = this.unicodeSequence.length(); i < c; i++) {
  120. sb.append("0x").append(Integer.toHexString(this.unicodeSequence.charAt(0)));
  121. }
  122. sb.append(", ");
  123. }
  124. sb.append(getName()).append(')');
  125. return sb.toString();
  126. }
  127. }