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.

Ffn.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.util.Arrays;
  17. import org.apache.poi.hwpf.HWPFDocument;
  18. import org.apache.poi.util.BitField;
  19. import org.apache.poi.util.BitFieldFactory;
  20. import org.apache.poi.util.IOUtils;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.util.LittleEndian;
  23. import org.apache.poi.util.LittleEndianConsts;
  24. /**
  25. * FFN - Font Family Name. FFN is a data structure that stores the names of the Main
  26. * Font and that of Alternate font as an array of characters. It has also a header
  27. * that stores info about the whole structure and the fonts
  28. */
  29. @Internal
  30. public final class Ffn {
  31. private int _cbFfnM1;//total length of FFN - 1.
  32. private byte _info;
  33. private static BitField _prq = BitFieldFactory.getInstance(0x0003);// pitch request
  34. private static BitField _fTrueType = BitFieldFactory.getInstance(0x0004);// when 1, font is a TrueType font
  35. private static BitField _ff = BitFieldFactory.getInstance(0x0070);
  36. private short _wWeight;// base weight of font
  37. private byte _chs;// character set identifier
  38. private byte _ixchSzAlt; // index into ffn.szFfn to the name of
  39. // the alternate font
  40. private byte[] _panose = new byte[10];//????
  41. private byte[] _fontSig = new byte[24];//????
  42. // zero terminated string that records name of font, cuurently not
  43. // supporting Extended chars
  44. private char[] _xszFfn;
  45. // extra facilitator members
  46. private int _xszFfnLength;
  47. public Ffn(byte[] buf, int offset) {
  48. int offsetTmp = offset;
  49. _cbFfnM1 = LittleEndian.getUByte(buf, offset);
  50. offset += LittleEndianConsts.BYTE_SIZE;
  51. _info = buf[offset];
  52. offset += LittleEndianConsts.BYTE_SIZE;
  53. _wWeight = LittleEndian.getShort(buf, offset);
  54. offset += LittleEndianConsts.SHORT_SIZE;
  55. _chs = buf[offset];
  56. offset += LittleEndianConsts.BYTE_SIZE;
  57. _ixchSzAlt = buf[offset];
  58. offset += LittleEndianConsts.BYTE_SIZE;
  59. // read panose and fs so we can write them back out.
  60. System.arraycopy(buf, offset, _panose, 0, _panose.length);
  61. offset += _panose.length;
  62. System.arraycopy(buf, offset, _fontSig, 0, _fontSig.length);
  63. offset += _fontSig.length;
  64. offsetTmp = offset - offsetTmp;
  65. _xszFfnLength = (this.getSize() - offsetTmp) / 2;
  66. if (_xszFfnLength < 0) {
  67. throw new IllegalArgumentException("Had invalid computed size: " + _xszFfnLength + " with size " + getSize() + " and offsetTmp: " + offsetTmp);
  68. }
  69. _xszFfn = new char[_xszFfnLength];
  70. for (int i = 0; i < _xszFfnLength; i++) {
  71. _xszFfn[i] = (char) LittleEndian.getShort(buf, offset);
  72. offset += LittleEndianConsts.SHORT_SIZE;
  73. }
  74. }
  75. public int get_cbFfnM1() {
  76. return _cbFfnM1;
  77. }
  78. public short getWeight() {
  79. return _wWeight;
  80. }
  81. public byte getChs() {
  82. return _chs;
  83. }
  84. public byte[] getPanose() {
  85. return _panose;
  86. }
  87. public byte[] getFontSig() {
  88. return _fontSig;
  89. }
  90. public int getSize() {
  91. return (_cbFfnM1 + 1);
  92. }
  93. public String getMainFontName() {
  94. int index = 0;
  95. for (; index < _xszFfnLength; index++) {
  96. if (_xszFfn[index] == '\0') {
  97. break;
  98. }
  99. }
  100. return new String(_xszFfn, 0, index);
  101. }
  102. public String getAltFontName() {
  103. int index = _ixchSzAlt;
  104. for (; index < _xszFfnLength; index++) {
  105. if (_xszFfn[index] == '\0') {
  106. break;
  107. }
  108. }
  109. return new String(_xszFfn, _ixchSzAlt, index);
  110. }
  111. public void set_cbFfnM1(int _cbFfnM1) {
  112. this._cbFfnM1 = _cbFfnM1;
  113. }
  114. // changed protected to public
  115. public byte[] toByteArray() {
  116. int offset = 0;
  117. byte[] buf = IOUtils.safelyAllocate(this.getSize(), HWPFDocument.getMaxRecordLength());
  118. buf[offset] = (byte) _cbFfnM1;
  119. offset += LittleEndianConsts.BYTE_SIZE;
  120. buf[offset] = _info;
  121. offset += LittleEndianConsts.BYTE_SIZE;
  122. LittleEndian.putShort(buf, offset, _wWeight);
  123. offset += LittleEndianConsts.SHORT_SIZE;
  124. buf[offset] = _chs;
  125. offset += LittleEndianConsts.BYTE_SIZE;
  126. buf[offset] = _ixchSzAlt;
  127. offset += LittleEndianConsts.BYTE_SIZE;
  128. System.arraycopy(_panose, 0, buf, offset, _panose.length);
  129. offset += _panose.length;
  130. System.arraycopy(_fontSig, 0, buf, offset, _fontSig.length);
  131. offset += _fontSig.length;
  132. for (int i = 0; i < _xszFfn.length; i++) {
  133. LittleEndian.putShort(buf, offset, (short) _xszFfn[i]);
  134. offset += LittleEndianConsts.SHORT_SIZE;
  135. }
  136. return buf;
  137. }
  138. @Override
  139. public boolean equals(Object other) {
  140. if (!(other instanceof Ffn)) return false;
  141. Ffn o = (Ffn) other;
  142. return (
  143. o._cbFfnM1 == this._cbFfnM1
  144. && o._info == this._info
  145. && o._wWeight == _wWeight
  146. && o._chs == _chs
  147. && o._ixchSzAlt == _ixchSzAlt
  148. && Arrays.equals(o._panose, _panose)
  149. && Arrays.equals(o._fontSig, _fontSig)
  150. && Arrays.equals(o._xszFfn, _xszFfn)
  151. );
  152. }
  153. @Override
  154. public int hashCode() {
  155. assert false : "hashCode not designed";
  156. return 42; // any arbitrary constant will do
  157. }
  158. }