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.

FontTable.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwpf.model;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import org.apache.poi.hwpf.model.io.HWPFFileSystem;
  19. import org.apache.poi.util.Internal;
  20. import org.apache.poi.util.LittleEndian;
  21. import org.apache.poi.util.LittleEndianConsts;
  22. import org.apache.poi.util.POILogFactory;
  23. import org.apache.poi.util.POILogger;
  24. /**
  25. * FontTable or in MS terminology sttbfffn is a common data structure written in all
  26. * Word files. The sttbfffn is an sttbf where each string is an FFN structure instead
  27. * of pascal-style strings. An sttbf is a string Table stored in file. Thus sttbffn
  28. * is like an Sttbf with an array of FFN structures that stores the font name strings
  29. *
  30. * @author Praveen Mathew
  31. */
  32. @Internal
  33. public final class FontTable
  34. {
  35. private final static POILogger _logger = POILogFactory.getLogger(FontTable.class);
  36. private short _stringCount;// how many strings are included in the string table
  37. private short _extraDataSz;// size in bytes of the extra data
  38. // added extra facilitator members
  39. private int lcbSttbfffn;// count of bytes in sttbfffn
  40. private int fcSttbfffn;// table stream offset for sttbfffn
  41. // FFN structure containing strings of font names
  42. private Ffn[] _fontNames;
  43. public FontTable(byte[] buf, int offset, int lcbSttbfffn)
  44. {
  45. this.lcbSttbfffn = lcbSttbfffn;
  46. this.fcSttbfffn = offset;
  47. _stringCount = LittleEndian.getShort(buf, offset);
  48. offset += LittleEndianConsts.SHORT_SIZE;
  49. _extraDataSz = LittleEndian.getShort(buf, offset);
  50. offset += LittleEndianConsts.SHORT_SIZE;
  51. _fontNames = new Ffn[_stringCount]; //Ffn corresponds to a Pascal style String in STTBF.
  52. for(int i = 0;i<_stringCount; i++)
  53. {
  54. _fontNames[i] = new Ffn(buf,offset);
  55. offset += _fontNames[i].getSize();
  56. }
  57. }
  58. public short getStringCount()
  59. {
  60. return _stringCount;
  61. }
  62. public short getExtraDataSz()
  63. {
  64. return _extraDataSz;
  65. }
  66. public Ffn[] getFontNames()
  67. {
  68. return _fontNames;
  69. }
  70. public int getSize()
  71. {
  72. return lcbSttbfffn;
  73. }
  74. public String getMainFont(int chpFtc )
  75. {
  76. if(chpFtc >= _stringCount)
  77. {
  78. _logger.log(POILogger.INFO, "Mismatch in chpFtc with stringCount");
  79. return null;
  80. }
  81. return _fontNames[chpFtc].getMainFontName();
  82. }
  83. public String getAltFont(int chpFtc )
  84. {
  85. if(chpFtc >= _stringCount)
  86. {
  87. _logger.log(POILogger.INFO, "Mismatch in chpFtc with stringCount");
  88. return null;
  89. }
  90. return _fontNames[chpFtc].getAltFontName();
  91. }
  92. public void setStringCount(short stringCount)
  93. {
  94. this._stringCount = stringCount;
  95. }
  96. @Deprecated
  97. public void writeTo( HWPFFileSystem sys ) throws IOException
  98. {
  99. ByteArrayOutputStream tableStream = sys.getStream( "1Table" );
  100. writeTo( tableStream );
  101. }
  102. public void writeTo( ByteArrayOutputStream tableStream ) throws IOException
  103. {
  104. byte[] buf = new byte[LittleEndianConsts.SHORT_SIZE];
  105. LittleEndian.putShort(buf, 0, _stringCount);
  106. tableStream.write(buf);
  107. LittleEndian.putShort(buf, 0, _extraDataSz);
  108. tableStream.write(buf);
  109. for(int i = 0; i < _fontNames.length; i++)
  110. {
  111. tableStream.write(_fontNames[i].toByteArray());
  112. }
  113. }
  114. @Override
  115. public boolean equals(Object other) {
  116. if (!(other instanceof FontTable)) return false;
  117. FontTable o = (FontTable)other;
  118. if (o._stringCount != this._stringCount
  119. || o._extraDataSz != this._extraDataSz
  120. || o._fontNames.length != this._fontNames.length
  121. ) return false;
  122. for (int i=0; i<o._fontNames.length; i++) {
  123. if (!o._fontNames[i].equals(this._fontNames[i])) return false;
  124. }
  125. return true;
  126. }
  127. @Override
  128. public int hashCode() {
  129. assert false : "hashCode not designed";
  130. return 42; // any arbitrary constant will do
  131. }
  132. }