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.4KB

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