CMapFormat6.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * $Id: CMapFormat6.java,v 1.1 2009/02/16 00:26:24 tomoke Exp $
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5. * Santa Clara, California 95054, U.S.A. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. package com.sun.pdfview.font.ttf;
  22. import net.sf.andpdf.pdfviewer.ByteBuffer;
  23. import java.util.*;
  24. /**
  25. *
  26. * @author jkaplan
  27. */
  28. public class CMapFormat6 extends CMap {
  29. /** First character code of subrange. */
  30. private short firstCode;
  31. /** Number of character codes in subrange. */
  32. private short entryCount;
  33. /** Array of glyph index values for character codes in the range. */
  34. private short [] glyphIndexArray;
  35. /** a reverse lookup from glyph id to index. */
  36. private HashMap<Short,Short> glyphLookup = new HashMap<Short,Short>();
  37. /** Creates a new instance of CMapFormat0 */
  38. protected CMapFormat6(short language) {
  39. super((short) 6, language);
  40. }
  41. /**
  42. * Get the length of this table
  43. */
  44. public short getLength() {
  45. // start with the size of the fixed header
  46. short size = 5 * 2;
  47. // add the size of each segment header
  48. size += entryCount * 2;
  49. return size;
  50. }
  51. /**
  52. * Cannot map from a byte
  53. */
  54. public byte map(byte src) {
  55. char c = map((char) src);
  56. if (c < Byte.MIN_VALUE || c > Byte.MAX_VALUE) {
  57. // out of range
  58. return 0;
  59. }
  60. return (byte) c;
  61. }
  62. /**
  63. * Map from char
  64. */
  65. public char map(char src) {
  66. // find first segment with endcode > src
  67. if (src < firstCode || src > (firstCode + entryCount)) {
  68. // Codes outside of the range are assumed to be missing and are
  69. // mapped to the glyph with index 0
  70. return '\000';
  71. }
  72. return (char) glyphIndexArray[src - firstCode];
  73. }
  74. /**
  75. * Get the src code which maps to the given glyphID
  76. */
  77. public char reverseMap(short glyphID) {
  78. Short result = glyphLookup.get(new Short(glyphID));
  79. if (result == null) {
  80. return '\000';
  81. }
  82. return (char) result.shortValue();
  83. }
  84. /**
  85. * Get the data in this map as a ByteBuffer
  86. */
  87. public void setData(int length, ByteBuffer data) {
  88. // read the table size values
  89. firstCode = data.getShort();
  90. entryCount = data.getShort();
  91. glyphIndexArray = new short [entryCount];
  92. for (int i = 0; i < glyphIndexArray.length; i++) {
  93. glyphIndexArray[i] = data.getShort();
  94. glyphLookup.put(new Short(glyphIndexArray[i]),
  95. new Short((short) (i + firstCode)));
  96. }
  97. }
  98. /**
  99. * Get the data in the map as a byte buffer
  100. */
  101. public ByteBuffer getData() {
  102. ByteBuffer buf = ByteBuffer.allocate(getLength());
  103. // write the header
  104. buf.putShort(getFormat());
  105. buf.putShort((short) getLength());
  106. buf.putShort(getLanguage());
  107. // write the various values
  108. buf.putShort(firstCode);
  109. buf.putShort(entryCount);
  110. // write the endCodes
  111. for (int i = 0; i < glyphIndexArray.length; i++) {
  112. buf.putShort(glyphIndexArray[i]);
  113. }
  114. // reset the data pointer
  115. buf.flip();
  116. return buf;
  117. }
  118. }