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.

GlyphCoverageTable.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.complexscripts.fonts;
  19. import java.util.Arrays;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. // CSOFF: LineLengthCheck
  25. // CSOFF: InnerAssignmentCheck
  26. // CSOFF: NoWhitespaceAfterCheck
  27. /**
  28. * <p>.Base class implementation of glyph coverage table.</p>
  29. *
  30. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  31. */
  32. public final class GlyphCoverageTable extends GlyphMappingTable implements GlyphCoverageMapping {
  33. /* logging instance */
  34. private static final Log log = LogFactory.getLog(GlyphCoverageTable.class); // CSOK: ConstantNameCheck
  35. /** empty mapping table */
  36. public static final int GLYPH_COVERAGE_TYPE_EMPTY = GLYPH_MAPPING_TYPE_EMPTY;
  37. /** mapped mapping table */
  38. public static final int GLYPH_COVERAGE_TYPE_MAPPED = GLYPH_MAPPING_TYPE_MAPPED;
  39. /** range based mapping table */
  40. public static final int GLYPH_COVERAGE_TYPE_RANGE = GLYPH_MAPPING_TYPE_RANGE;
  41. private GlyphCoverageMapping cm;
  42. private GlyphCoverageTable ( GlyphCoverageMapping cm ) {
  43. assert cm != null;
  44. assert cm instanceof GlyphMappingTable;
  45. this.cm = cm;
  46. }
  47. /** {@inheritDoc} */
  48. public int getType() {
  49. return ( (GlyphMappingTable) cm ) .getType();
  50. }
  51. /** {@inheritDoc} */
  52. public List getEntries() {
  53. return ( (GlyphMappingTable) cm ) .getEntries();
  54. }
  55. /** {@inheritDoc} */
  56. public int getCoverageSize() {
  57. return cm.getCoverageSize();
  58. }
  59. /** {@inheritDoc} */
  60. public int getCoverageIndex ( int gid ) {
  61. return cm.getCoverageIndex ( gid );
  62. }
  63. /**
  64. * Create glyph coverage table.
  65. * @param entries list of mapped or ranged coverage entries, or null or empty list
  66. * @return a new covera table instance
  67. */
  68. public static GlyphCoverageTable createCoverageTable ( List entries ) {
  69. GlyphCoverageMapping cm;
  70. if ( ( entries == null ) || ( entries.size() == 0 ) ) {
  71. cm = new EmptyCoverageTable ( entries );
  72. } else if ( isMappedCoverage ( entries ) ) {
  73. cm = new MappedCoverageTable ( entries );
  74. } else if ( isRangeCoverage ( entries ) ) {
  75. cm = new RangeCoverageTable ( entries );
  76. } else {
  77. cm = null;
  78. }
  79. assert cm != null : "unknown coverage type";
  80. return new GlyphCoverageTable ( cm );
  81. }
  82. private static boolean isMappedCoverage ( List entries ) {
  83. if ( ( entries == null ) || ( entries.size() == 0 ) ) {
  84. return false;
  85. } else {
  86. for ( Iterator it = entries.iterator(); it.hasNext();) {
  87. Object o = it.next();
  88. if ( ! ( o instanceof Integer ) ) {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. }
  95. private static boolean isRangeCoverage ( List entries ) {
  96. if ( ( entries == null ) || ( entries.size() == 0 ) ) {
  97. return false;
  98. } else {
  99. for ( Iterator it = entries.iterator(); it.hasNext();) {
  100. Object o = it.next();
  101. if ( ! ( o instanceof MappingRange ) ) {
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. }
  108. private static class EmptyCoverageTable extends GlyphMappingTable.EmptyMappingTable implements GlyphCoverageMapping {
  109. public EmptyCoverageTable ( List entries ) {
  110. super ( entries );
  111. }
  112. /** {@inheritDoc} */
  113. public int getCoverageSize() {
  114. return 0;
  115. }
  116. /** {@inheritDoc} */
  117. public int getCoverageIndex ( int gid ) {
  118. return -1;
  119. }
  120. }
  121. private static class MappedCoverageTable extends GlyphMappingTable.MappedMappingTable implements GlyphCoverageMapping {
  122. private int[] map;
  123. public MappedCoverageTable ( List entries ) {
  124. populate ( entries );
  125. }
  126. /** {@inheritDoc} */
  127. public List getEntries() {
  128. List entries = new java.util.ArrayList();
  129. if ( map != null ) {
  130. for ( int i = 0, n = map.length; i < n; i++ ) {
  131. entries.add ( Integer.valueOf ( map [ i ] ) );
  132. }
  133. }
  134. return entries;
  135. }
  136. /** {@inheritDoc} */
  137. public int getMappingSize() {
  138. return ( map != null ) ? map.length : 0;
  139. }
  140. public int getMappedIndex ( int gid ) {
  141. int i;
  142. if ( ( i = Arrays.binarySearch ( map, gid ) ) >= 0 ) {
  143. return i;
  144. } else {
  145. return -1;
  146. }
  147. }
  148. /** {@inheritDoc} */
  149. public int getCoverageSize() {
  150. return getMappingSize();
  151. }
  152. /** {@inheritDoc} */
  153. public int getCoverageIndex ( int gid ) {
  154. return getMappedIndex ( gid );
  155. }
  156. private void populate ( List entries ) {
  157. int i = 0;
  158. int skipped = 0;
  159. int n = entries.size();
  160. int gidMax = -1;
  161. int[] map = new int [ n ];
  162. for ( Iterator it = entries.iterator(); it.hasNext();) {
  163. Object o = it.next();
  164. if ( o instanceof Integer ) {
  165. int gid = ( (Integer) o ) . intValue();
  166. if ( ( gid >= 0 ) && ( gid < 65536 ) ) {
  167. if ( gid > gidMax ) {
  168. map [ i++ ] = gidMax = gid;
  169. } else {
  170. log.info ( "ignoring out of order or duplicate glyph index: " + gid );
  171. skipped++;
  172. }
  173. } else {
  174. throw new AdvancedTypographicTableFormatException ( "illegal glyph index: " + gid );
  175. }
  176. } else {
  177. throw new AdvancedTypographicTableFormatException ( "illegal coverage entry, must be Integer: " + o );
  178. }
  179. }
  180. assert ( i + skipped ) == n;
  181. assert this.map == null;
  182. this.map = map;
  183. }
  184. /** {@inheritDoc} */
  185. public String toString() {
  186. StringBuffer sb = new StringBuffer();
  187. sb.append('{');
  188. for ( int i = 0, n = map.length; i < n; i++ ) {
  189. if ( i > 0 ) {
  190. sb.append(',');
  191. }
  192. sb.append ( Integer.toString ( map [ i ] ) );
  193. }
  194. sb.append('}');
  195. return sb.toString();
  196. }
  197. }
  198. private static class RangeCoverageTable extends GlyphMappingTable.RangeMappingTable implements GlyphCoverageMapping {
  199. public RangeCoverageTable ( List entries ) {
  200. super ( entries );
  201. }
  202. /** {@inheritDoc} */
  203. public int getMappedIndex ( int gid, int s, int m ) {
  204. return m + gid - s;
  205. }
  206. /** {@inheritDoc} */
  207. public int getCoverageSize() {
  208. return getMappingSize();
  209. }
  210. /** {@inheritDoc} */
  211. public int getCoverageIndex ( int gid ) {
  212. return getMappedIndex ( gid );
  213. }
  214. }
  215. }