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.

SimpleSingleByteEncoding.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.xmlgraphics.fonts.Glyphs;
  25. import org.apache.fop.util.CharUtilities;
  26. /**
  27. * A simple implementation of the OneByteEncoding mostly used for encodings that are constructed
  28. * on-the-fly.
  29. */
  30. public class SimpleSingleByteEncoding implements SingleByteEncoding {
  31. private final String name;
  32. private final List<NamedCharacter> mapping = new ArrayList<NamedCharacter>();
  33. private final Map<Character, Character> charMap = new HashMap<Character, Character>();
  34. /**
  35. * Main constructor.
  36. * @param name the encoding's name
  37. */
  38. public SimpleSingleByteEncoding(String name) {
  39. this.name = name;
  40. }
  41. /** {@inheritDoc} */
  42. public String getName() {
  43. return this.name;
  44. }
  45. /** {@inheritDoc} */
  46. public char mapChar(char c) {
  47. Character nc = charMap.get(Character.valueOf(c));
  48. if (nc != null) {
  49. return nc.charValue();
  50. }
  51. return NOT_FOUND_CODE_POINT;
  52. }
  53. /** {@inheritDoc} */
  54. public String[] getCharNameMap() {
  55. String[] map = new String[getSize()];
  56. Arrays.fill(map, Glyphs.NOTDEF);
  57. for (int i = getFirstChar(); i <= getLastChar(); i++) {
  58. NamedCharacter ch = this.mapping.get(i - 1);
  59. map[i] = ch.getName();
  60. }
  61. return map;
  62. }
  63. /**
  64. * Returns the index of the first defined character.
  65. * @return the index of the first defined character (always 1 for this class)
  66. */
  67. public int getFirstChar() {
  68. return 1;
  69. }
  70. /**
  71. * Returns the index of the last defined character.
  72. * @return the index of the last defined character
  73. */
  74. public int getLastChar() {
  75. return this.mapping.size();
  76. }
  77. /**
  78. * Returns the number of characters defined by this encoding.
  79. * @return the number of characters
  80. */
  81. public int getSize() {
  82. return this.mapping.size() + 1;
  83. }
  84. /**
  85. * Indicates whether the encoding is full (with 256 code points).
  86. * @return true if the encoding is full
  87. */
  88. public boolean isFull() {
  89. return (getSize() == 256);
  90. }
  91. /**
  92. * Adds a new character to the encoding.
  93. * @param ch the named character
  94. * @return the code point assigned to the character
  95. */
  96. public char addCharacter(NamedCharacter ch) {
  97. if (!ch.hasSingleUnicodeValue()) {
  98. throw new IllegalArgumentException("Only NamedCharacters with a single Unicode value"
  99. + " are currently supported!");
  100. }
  101. if (isFull()) {
  102. throw new IllegalStateException("Encoding is full!");
  103. }
  104. char newSlot = (char)(getLastChar() + 1);
  105. this.mapping.add(ch);
  106. this.charMap.put(Character.valueOf(ch.getSingleUnicodeValue()), Character.valueOf(newSlot));
  107. return newSlot;
  108. }
  109. /**
  110. * Returns the named character at a given code point in the encoding.
  111. * @param codePoint the code point of the character
  112. * @return the NamedCharacter (or null if no character is at this position)
  113. */
  114. public NamedCharacter getCharacterForIndex(int codePoint) {
  115. if (codePoint < 0 || codePoint > 255) {
  116. throw new IllegalArgumentException("codePoint must be between 0 and 255");
  117. }
  118. if (codePoint <= getLastChar()) {
  119. return this.mapping.get(codePoint - 1);
  120. } else {
  121. return null;
  122. }
  123. }
  124. /** {@inheritDoc} */
  125. public char[] getUnicodeCharMap() {
  126. char[] map = new char[getLastChar() + 1];
  127. for (int i = 0; i < getFirstChar(); i++) {
  128. map[i] = CharUtilities.NOT_A_CHARACTER;
  129. }
  130. for (int i = getFirstChar(); i <= getLastChar(); i++) {
  131. map[i] = getCharacterForIndex(i).getSingleUnicodeValue();
  132. }
  133. return map;
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public String toString() {
  138. return getName() + " (" + getSize() + " chars)";
  139. }
  140. }