PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/hwpf/usermodel/CharacterRun.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 675 lines | 463 code | 131 blank | 81 comment | 14 complexity | eb3ee5102b1d02992e415773f3944ccc MD5 | raw file
Possible License(s): Apache-2.0
  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.usermodel;
  16. import org.apache.poi.hwpf.HWPFDocument;
  17. import org.apache.poi.hwpf.model.CHPX;
  18. import org.apache.poi.hwpf.model.FFData;
  19. import org.apache.poi.hwpf.model.Ffn;
  20. import org.apache.poi.hwpf.model.NilPICFAndBinData;
  21. import org.apache.poi.hwpf.model.StyleSheet;
  22. import org.apache.poi.hwpf.sprm.SprmBuffer;
  23. /**
  24. * This class represents a run of text that share common properties.
  25. *
  26. * @author Ryan Ackley
  27. */
  28. public final class CharacterRun
  29. extends Range
  30. implements Cloneable
  31. {
  32. public final static short SPRM_FRMARKDEL = (short)0x0800;
  33. public final static short SPRM_FRMARK = 0x0801;
  34. public final static short SPRM_FFLDVANISH = 0x0802;
  35. public final static short SPRM_PICLOCATION = 0x6A03;
  36. public final static short SPRM_IBSTRMARK = 0x4804;
  37. public final static short SPRM_DTTMRMARK = 0x6805;
  38. public final static short SPRM_FDATA = 0x0806;
  39. public final static short SPRM_SYMBOL = 0x6A09;
  40. public final static short SPRM_FOLE2 = 0x080A;
  41. public final static short SPRM_HIGHLIGHT = 0x2A0C;
  42. public final static short SPRM_OBJLOCATION = 0x680E;
  43. public final static short SPRM_ISTD = 0x4A30;
  44. public final static short SPRM_FBOLD = 0x0835;
  45. public final static short SPRM_FITALIC = 0x0836;
  46. public final static short SPRM_FSTRIKE = 0x0837;
  47. public final static short SPRM_FOUTLINE = 0x0838;
  48. public final static short SPRM_FSHADOW = 0x0839;
  49. public final static short SPRM_FSMALLCAPS = 0x083A;
  50. public final static short SPRM_FCAPS = 0x083B;
  51. public final static short SPRM_FVANISH = 0x083C;
  52. public final static short SPRM_KUL = 0x2A3E;
  53. public final static short SPRM_DXASPACE = (short)0x8840;
  54. public final static short SPRM_LID = 0x4A41;
  55. public final static short SPRM_ICO = 0x2A42;
  56. public final static short SPRM_HPS = 0x4A43;
  57. public final static short SPRM_HPSPOS = 0x4845;
  58. public final static short SPRM_ISS = 0x2A48;
  59. public final static short SPRM_HPSKERN = 0x484B;
  60. public final static short SPRM_YSRI = 0x484E;
  61. public final static short SPRM_RGFTCASCII = 0x4A4F;
  62. public final static short SPRM_RGFTCFAREAST = 0x4A50;
  63. public final static short SPRM_RGFTCNOTFAREAST = 0x4A51;
  64. public final static short SPRM_CHARSCALE = 0x4852;
  65. public final static short SPRM_FDSTRIKE = 0x2A53;
  66. public final static short SPRM_FIMPRINT = 0x0854;
  67. public final static short SPRM_FSPEC = 0x0855;
  68. public final static short SPRM_FOBJ = 0x0856;
  69. public final static short SPRM_PROPRMARK = (short)0xCA57;
  70. public final static short SPRM_FEMBOSS = 0x0858;
  71. public final static short SPRM_SFXTEXT = 0x2859;
  72. public final static short SPRM_DISPFLDRMARK = (short)0xCA62;
  73. public final static short SPRM_IBSTRMARKDEL = 0x4863;
  74. public final static short SPRM_DTTMRMARKDEL = 0x6864;
  75. public final static short SPRM_BRC = 0x6865;
  76. public final static short SPRM_SHD = 0x4866;
  77. public final static short SPRM_IDSIRMARKDEL = 0x4867;
  78. public final static short SPRM_CPG = 0x486B;
  79. public final static short SPRM_NONFELID = 0x486D;
  80. public final static short SPRM_FELID = 0x486E;
  81. public final static short SPRM_IDCTHINT = 0x286F;
  82. SprmBuffer _chpx;
  83. CharacterProperties _props;
  84. /**
  85. *
  86. * @param chpx The chpx this object is based on.
  87. * @param ss The stylesheet for the document this run belongs to.
  88. * @param istd The style index if this run's base style.
  89. * @param parent The parent range of this character run (usually a paragraph).
  90. */
  91. CharacterRun(CHPX chpx, StyleSheet ss, short istd, Range parent)
  92. {
  93. super(Math.max(parent._start, chpx.getStart()), Math.min(parent._end, chpx.getEnd()), parent);
  94. _props = chpx.getCharacterProperties(ss, istd);
  95. _chpx = chpx.getSprmBuf();
  96. }
  97. /**
  98. * Here for runtime type determination using a switch statement convenient.
  99. *
  100. * @return TYPE_CHARACTER
  101. */
  102. public int type()
  103. {
  104. return TYPE_CHARACTER;
  105. }
  106. public boolean isMarkedDeleted()
  107. {
  108. return _props.isFRMarkDel();
  109. }
  110. public void markDeleted(boolean mark)
  111. {
  112. _props.setFRMarkDel(mark);
  113. byte newVal = (byte)(mark ? 1 : 0);
  114. _chpx.updateSprm(SPRM_FRMARKDEL, newVal);
  115. }
  116. public boolean isBold()
  117. {
  118. return _props.isFBold();
  119. }
  120. public void setBold(boolean bold)
  121. {
  122. _props.setFBold(bold);
  123. byte newVal = (byte)(bold ? 1 : 0);
  124. _chpx.updateSprm(SPRM_FBOLD, newVal);
  125. }
  126. public boolean isItalic()
  127. {
  128. return _props.isFItalic();
  129. }
  130. public void setItalic(boolean italic)
  131. {
  132. _props.setFItalic(italic);
  133. byte newVal = (byte)(italic ? 1 : 0);
  134. _chpx.updateSprm(SPRM_FITALIC, newVal);
  135. }
  136. public boolean isOutlined()
  137. {
  138. return _props.isFOutline();
  139. }
  140. public void setOutline(boolean outlined)
  141. {
  142. _props.setFOutline(outlined);
  143. byte newVal = (byte)(outlined ? 1 : 0);
  144. _chpx.updateSprm(SPRM_FOUTLINE, newVal);
  145. }
  146. public boolean isFldVanished()
  147. {
  148. return _props.isFFldVanish();
  149. }
  150. public void setFldVanish(boolean fldVanish)
  151. {
  152. _props.setFFldVanish(fldVanish);
  153. byte newVal = (byte)(fldVanish ? 1 : 0);
  154. _chpx.updateSprm(SPRM_FFLDVANISH, newVal);
  155. }
  156. public boolean isSmallCaps()
  157. {
  158. return _props.isFSmallCaps();
  159. }
  160. public void setSmallCaps(boolean smallCaps)
  161. {
  162. _props.setFSmallCaps(smallCaps);
  163. byte newVal = (byte)(smallCaps ? 1 : 0);
  164. _chpx.updateSprm(SPRM_FSMALLCAPS, newVal);
  165. }
  166. public boolean isCapitalized()
  167. {
  168. return _props.isFCaps();
  169. }
  170. public void setCapitalized(boolean caps)
  171. {
  172. _props.setFCaps(caps);
  173. byte newVal = (byte)(caps ? 1 : 0);
  174. _chpx.updateSprm(SPRM_FCAPS, newVal);
  175. }
  176. public boolean isVanished()
  177. {
  178. return _props.isFVanish();
  179. }
  180. public void setVanished(boolean vanish)
  181. {
  182. _props.setFVanish(vanish);
  183. byte newVal = (byte)(vanish ? 1 : 0);
  184. _chpx.updateSprm(SPRM_FVANISH, newVal);
  185. }
  186. public boolean isMarkedInserted()
  187. {
  188. return _props.isFRMark();
  189. }
  190. public void markInserted(boolean mark)
  191. {
  192. _props.setFRMark(mark);
  193. byte newVal = (byte)(mark ? 1 : 0);
  194. _chpx.updateSprm(SPRM_FRMARK, newVal);
  195. }
  196. public boolean isStrikeThrough()
  197. {
  198. return _props.isFStrike();
  199. }
  200. public void strikeThrough(boolean strike)
  201. {
  202. _props.setFStrike(strike);
  203. byte newVal = (byte)(strike ? 1 : 0);
  204. _chpx.updateSprm(SPRM_FSTRIKE, newVal);
  205. }
  206. public boolean isShadowed()
  207. {
  208. return _props.isFShadow();
  209. }
  210. public void setShadow(boolean shadow)
  211. {
  212. _props.setFShadow(shadow);
  213. byte newVal = (byte)(shadow ? 1 : 0);
  214. _chpx.updateSprm(SPRM_FSHADOW, newVal);
  215. }
  216. public boolean isEmbossed()
  217. {
  218. return _props.isFEmboss();
  219. }
  220. public void setEmbossed(boolean emboss)
  221. {
  222. _props.setFEmboss(emboss);
  223. byte newVal = (byte)(emboss ? 1 : 0);
  224. _chpx.updateSprm(SPRM_FEMBOSS, newVal);
  225. }
  226. public boolean isImprinted()
  227. {
  228. return _props.isFImprint();
  229. }
  230. public void setImprinted(boolean imprint)
  231. {
  232. _props.setFImprint(imprint);
  233. byte newVal = (byte)(imprint ? 1 : 0);
  234. _chpx.updateSprm(SPRM_FIMPRINT, newVal);
  235. }
  236. public boolean isDoubleStrikeThrough()
  237. {
  238. return _props.isFDStrike();
  239. }
  240. public void setDoubleStrikethrough(boolean dstrike)
  241. {
  242. _props.setFDStrike(dstrike);
  243. byte newVal = (byte)(dstrike ? 1 : 0);
  244. _chpx.updateSprm(SPRM_FDSTRIKE, newVal);
  245. }
  246. public void setFtcAscii(int ftcAscii)
  247. {
  248. _props.setFtcAscii(ftcAscii);
  249. _chpx.updateSprm(SPRM_RGFTCASCII, (short)ftcAscii);
  250. }
  251. public void setFtcFE(int ftcFE)
  252. {
  253. _props.setFtcFE(ftcFE);
  254. _chpx.updateSprm(SPRM_RGFTCFAREAST, (short)ftcFE);
  255. }
  256. public void setFtcOther(int ftcOther)
  257. {
  258. _props.setFtcOther(ftcOther);
  259. _chpx.updateSprm(SPRM_RGFTCNOTFAREAST, (short)ftcOther);
  260. }
  261. public int getFontSize()
  262. {
  263. return _props.getHps();
  264. }
  265. public void setFontSize(int halfPoints)
  266. {
  267. _props.setHps(halfPoints);
  268. _chpx.updateSprm(SPRM_HPS, (short)halfPoints);
  269. }
  270. public int getCharacterSpacing()
  271. {
  272. return _props.getDxaSpace();
  273. }
  274. public void setCharacterSpacing(int twips)
  275. {
  276. _props.setDxaSpace(twips);
  277. _chpx.updateSprm(SPRM_DXASPACE, twips);
  278. }
  279. public short getSubSuperScriptIndex()
  280. {
  281. return _props.getIss();
  282. }
  283. public void setSubSuperScriptIndex(short iss)
  284. {
  285. _props.setDxaSpace(iss);
  286. _chpx.updateSprm(SPRM_DXASPACE, iss);
  287. }
  288. public int getUnderlineCode()
  289. {
  290. return _props.getKul();
  291. }
  292. public void setUnderlineCode(int kul)
  293. {
  294. _props.setKul((byte)kul);
  295. _chpx.updateSprm(SPRM_KUL, (byte)kul);
  296. }
  297. public int getColor()
  298. {
  299. return _props.getIco();
  300. }
  301. public void setColor(int color)
  302. {
  303. _props.setIco((byte)color);
  304. _chpx.updateSprm(SPRM_ICO, (byte)color);
  305. }
  306. public int getVerticalOffset()
  307. {
  308. return _props.getHpsPos();
  309. }
  310. public void setVerticalOffset(int hpsPos)
  311. {
  312. _props.setHpsPos((short) hpsPos);
  313. _chpx.updateSprm(SPRM_HPSPOS, (byte)hpsPos);
  314. }
  315. public int getKerning()
  316. {
  317. return _props.getHpsKern();
  318. }
  319. public void setKerning(int kern)
  320. {
  321. _props.setHpsKern(kern);
  322. _chpx.updateSprm(SPRM_HPSKERN, (short)kern);
  323. }
  324. public boolean isHighlighted()
  325. {
  326. return _props.isFHighlight();
  327. }
  328. public byte getHighlightedColor()
  329. {
  330. return _props.getIcoHighlight();
  331. }
  332. public void setHighlighted(byte color)
  333. {
  334. _props.setFHighlight(true);
  335. _props.setIcoHighlight(color);
  336. _chpx.updateSprm(SPRM_HIGHLIGHT, color);
  337. }
  338. public String getFontName()
  339. {
  340. if (_doc.getFontTable() == null)
  341. // old word format
  342. return null;
  343. return _doc.getFontTable().getMainFont(_props.getFtcAscii());
  344. }
  345. public boolean isSpecialCharacter()
  346. {
  347. return _props.isFSpec();
  348. }
  349. public void setSpecialCharacter(boolean spec)
  350. {
  351. _props.setFSpec(spec);
  352. byte newVal = (byte)(spec ? 1 : 0);
  353. _chpx.updateSprm(SPRM_FSPEC, newVal);
  354. }
  355. public boolean isObj()
  356. {
  357. return _props.isFObj();
  358. }
  359. public void setObj(boolean obj)
  360. {
  361. _props.setFObj(obj);
  362. byte newVal = (byte)(obj ? 1 : 0);
  363. _chpx.updateSprm(SPRM_FOBJ, newVal);
  364. }
  365. public int getPicOffset()
  366. {
  367. return _props.getFcPic();
  368. }
  369. public void setPicOffset(int offset)
  370. {
  371. _props.setFcPic(offset);
  372. _chpx.updateSprm(SPRM_PICLOCATION, offset);
  373. }
  374. /**
  375. * Does the picture offset represent picture
  376. * or binary data?
  377. * If it's set, then the picture offset refers to
  378. * a NilPICFAndBinData structure, otherwise to a
  379. * PICFAndOfficeArtData
  380. */
  381. public boolean isData()
  382. {
  383. return _props.isFData();
  384. }
  385. public void setData(boolean data)
  386. {
  387. _props.setFData(data);
  388. byte newVal = (byte)(data ? 1 : 0);
  389. _chpx.updateSprm(SPRM_FOBJ, newVal);
  390. }
  391. public boolean isOle2()
  392. {
  393. return _props.isFOle2();
  394. }
  395. public void setOle2(boolean ole)
  396. {
  397. _props.setFOle2(ole);
  398. byte newVal = (byte)(ole ? 1 : 0);
  399. _chpx.updateSprm(SPRM_FOBJ, newVal);
  400. }
  401. public int getObjOffset()
  402. {
  403. return _props.getFcObj();
  404. }
  405. public void setObjOffset(int obj)
  406. {
  407. _props.setFcObj(obj);
  408. _chpx.updateSprm(SPRM_OBJLOCATION, obj);
  409. }
  410. /**
  411. * Get the ico24 field for the CHP record.
  412. */
  413. public int getIco24()
  414. {
  415. return _props.getIco24();
  416. }
  417. /**
  418. * Set the ico24 field for the CHP record.
  419. */
  420. public void setIco24(int colour24)
  421. {
  422. _props.setIco24(colour24);
  423. }
  424. /**
  425. * clone the CharacterProperties object associated with this characterRun so
  426. * that you can apply it to another CharacterRun
  427. *
  428. * @deprecated This method shall not be public and should not be called from
  429. * high-level code
  430. */
  431. @Deprecated
  432. public CharacterProperties cloneProperties()
  433. {
  434. return _props.clone();
  435. }
  436. /**
  437. * Used to create a deep copy of this object.
  438. *
  439. * @return A deep copy.
  440. * @throws CloneNotSupportedException never
  441. */
  442. public Object clone()
  443. throws CloneNotSupportedException
  444. {
  445. CharacterRun cp = (CharacterRun)super.clone();
  446. cp._props.setDttmRMark((DateAndTime)_props.getDttmRMark().clone());
  447. cp._props.setDttmRMarkDel((DateAndTime)_props.getDttmRMarkDel().clone());
  448. cp._props.setDttmPropRMark((DateAndTime)_props.getDttmPropRMark().clone());
  449. cp._props.setDttmDispFldRMark((DateAndTime)_props.getDttmDispFldRMark().
  450. clone());
  451. cp._props.setXstDispFldRMark(_props.getXstDispFldRMark().clone());
  452. cp._props.setShd((ShadingDescriptor)_props.getShd().clone());
  453. return cp;
  454. }
  455. /**
  456. * Returns true, if the CharacterRun is a special character run containing a symbol, otherwise false.
  457. *
  458. * <p>In case of a symbol, the {@link #text()} method always returns a single character 0x0028, but word actually stores
  459. * the character in a different field. Use {@link #getSymbolCharacter()} to get that character and {@link #getSymbolFont()}
  460. * to determine its font.
  461. */
  462. public boolean isSymbol()
  463. {
  464. return isSpecialCharacter() && text().equals("\u0028");
  465. }
  466. /**
  467. * Returns the symbol character, if this is a symbol character run.
  468. *
  469. * @see #isSymbol()
  470. * @throws IllegalStateException If this is not a symbol character run: call {@link #isSymbol()} first.
  471. */
  472. public char getSymbolCharacter()
  473. {
  474. if (isSymbol()) {
  475. return (char)_props.getXchSym();
  476. } else
  477. throw new IllegalStateException("Not a symbol CharacterRun");
  478. }
  479. /**
  480. * Returns the symbol font, if this is a symbol character run. Might return null, if the font index is not found in the font table.
  481. *
  482. * @see #isSymbol()
  483. * @throws IllegalStateException If this is not a symbol character run: call {@link #isSymbol()} first.
  484. */
  485. public Ffn getSymbolFont()
  486. {
  487. if (isSymbol()) {
  488. if (_doc.getFontTable() == null)
  489. return null;
  490. // Fetch all font names
  491. Ffn[] fontNames = _doc.getFontTable().getFontNames();
  492. // Try to find the name of the font for our symbol
  493. if (fontNames.length <= _props.getFtcSym())
  494. return null;
  495. return fontNames[_props.getFtcSym()];
  496. } else
  497. throw new IllegalStateException("Not a symbol CharacterRun");
  498. }
  499. public BorderCode getBorder() {
  500. return _props.getBrc();
  501. }
  502. public int getLanguageCode() {
  503. return _props.getLidDefault();
  504. }
  505. public String toString() {
  506. String text = text();
  507. return "CharacterRun of " + text.length() + " characters - " + text;
  508. }
  509. public String[] getDropDownListValues()
  510. {
  511. if ( getDocument() instanceof HWPFDocument )
  512. {
  513. char c = _text.charAt( _start );
  514. if ( c == 0x01 )
  515. {
  516. NilPICFAndBinData data = new NilPICFAndBinData(
  517. ( (HWPFDocument) getDocument() ).getDataStream(),
  518. getPicOffset() );
  519. FFData ffData = new FFData( data.getBinData(), 0 );
  520. String[] values = ffData.getDropList();
  521. return values;
  522. }
  523. }
  524. return null;
  525. }
  526. public Integer getDropDownListDefaultItemIndex()
  527. {
  528. if ( getDocument() instanceof HWPFDocument )
  529. {
  530. char c = _text.charAt( _start );
  531. if ( c == 0x01 )
  532. {
  533. NilPICFAndBinData data = new NilPICFAndBinData(
  534. ( (HWPFDocument) getDocument() ).getDataStream(),
  535. getPicOffset() );
  536. FFData ffData = new FFData( data.getBinData(), 0 );
  537. return Integer.valueOf( ffData.getDefaultDropDownItemIndex() );
  538. }
  539. }
  540. return null;
  541. }
  542. }