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 4.9KB

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