📄 stylesheet.java
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.poi.hdf.model.hdftypes;import java.util.*;import org.apache.poi.util.LittleEndian;import org.apache.poi.hdf.model.hdftypes.definitions.TCAbstractType;/** * Represents a document's stylesheet. A word documents formatting is stored as * compressed styles that are based on styles contained in the stylesheet. This * class also contains static utility functions to uncompress different * formatting properties. * * @author Ryan Ackley */public class StyleSheet implements HDFType{ private static final int NIL_STYLE = 4095; private static final int PAP_TYPE = 1; private static final int CHP_TYPE = 2; private static final int SEP_TYPE = 4; private static final int TAP_TYPE = 5; //Vector _styleDescriptions; StyleDescription _nilStyle = new StyleDescription(); StyleDescription[] _styleDescriptions; /** * StyleSheet constructor. Loads a document's stylesheet information, * * @param styleSheet A byte array containing a document's raw stylesheet * info. Found by using FileInformationBlock.getFcStshf() and * FileInformationBLock.getLcbStshf() */ public StyleSheet(byte[] styleSheet) { int stshiLength = LittleEndian.getShort(styleSheet, 0); int stdCount = LittleEndian.getShort(styleSheet, 2); int baseLength = LittleEndian.getShort(styleSheet, 4); int[] rgftc = new int[3]; rgftc[0] = LittleEndian.getInt(styleSheet, 14); rgftc[1] = LittleEndian.getInt(styleSheet, 18); rgftc[2] = LittleEndian.getInt(styleSheet, 22); int offset = 0; _styleDescriptions = new StyleDescription[stdCount]; for(int x = 0; x < stdCount; x++) { int stdOffset = (2 + stshiLength) + offset; int stdSize = LittleEndian.getShort(styleSheet, stdOffset); if(stdSize > 0) { byte[] std = new byte[stdSize]; //get past the size stdOffset += 2; System.arraycopy(styleSheet, stdOffset, std, 0, stdSize); StyleDescription aStyle = new StyleDescription(std, baseLength, true); _styleDescriptions[x] = aStyle; } offset += stdSize + 2; } for(int x = 0; x < _styleDescriptions.length; x++) { if(_styleDescriptions[x] != null) { createPap(x); createChp(x); } } } /** * Creates a PartagraphProperties object from a papx stored in the * StyleDescription at the index istd in the StyleDescription array. The PAP * is placed in the StyleDescription at istd after its been created. Not * every StyleDescription will contain a papx. In these cases this function * does nothing * * @param istd The index of the StyleDescription to create the * ParagraphProperties from (and also place the finished PAP in) */ private void createPap(int istd) { StyleDescription sd = _styleDescriptions[istd]; ParagraphProperties pap = sd.getPAP(); byte[] papx = sd.getPAPX(); int baseIndex = sd.getBaseStyle(); if(pap == null && papx != null) { ParagraphProperties parentPAP = _nilStyle.getPAP(); if(baseIndex != NIL_STYLE) { parentPAP = _styleDescriptions[baseIndex].getPAP(); if(parentPAP == null) { createPap(baseIndex); parentPAP = _styleDescriptions[baseIndex].getPAP(); } } pap = (ParagraphProperties)uncompressProperty(papx, parentPAP, this); sd.setPAP(pap); } } /** * Creates a CharacterProperties object from a chpx stored in the * StyleDescription at the index istd in the StyleDescription array. The * CharacterProperties object is placed in the StyleDescription at istd after * its been created. Not every StyleDescription will contain a chpx. In these * cases this function does nothing. * * @param istd The index of the StyleDescription to create the * CharacterProperties object from. */ private void createChp(int istd) { StyleDescription sd = _styleDescriptions[istd]; CharacterProperties chp = sd.getCHP(); byte[] chpx = sd.getCHPX(); int baseIndex = sd.getBaseStyle(); if(chp == null && chpx != null) { CharacterProperties parentCHP = _nilStyle.getCHP(); if(baseIndex != NIL_STYLE) { parentCHP = _styleDescriptions[baseIndex].getCHP(); if(parentCHP == null) { createChp(baseIndex); parentCHP = _styleDescriptions[baseIndex].getCHP(); } } chp = (CharacterProperties)uncompressProperty(chpx, parentCHP, this); sd.setCHP(chp); } } /** * Gets the StyleDescription at index x. * * @param x the index of the desired StyleDescription. */ public StyleDescription getStyleDescription(int x) { return _styleDescriptions[x]; } /** * Used in decompression of a chpx. This performs an operation defined by * a single sprm. * * @param oldCHP The base CharacterProperties. * @param newCHP The current CharacterProperties. * @param operand The operand defined by the sprm (See Word file format spec) * @param param The parameter defined by the sprm (See Word file format spec) * @param varParam The variable length parameter defined by the sprm. (See * Word file format spec) * @param grpprl The entire chpx that this operation is a part of. * @param offset The offset in the grpprl of the next sprm * @param styleSheet The StyleSheet for this document. */ static void doCHPOperation(CharacterProperties oldCHP, CharacterProperties newCHP, int operand, int param, byte[] varParam, byte[] grpprl, int offset, StyleSheet styleSheet) { switch(operand) { case 0: newCHP.setFRMarkDel(getFlag(param)); break; case 0x1: newCHP.setFRMark(getFlag(param)); break; case 0x2: break; case 0x3: newCHP.setFcPic(param); newCHP.setFSpec(true); break; case 0x4: newCHP.setIbstRMark((short)param); break; case 0x5: short[] dttmRMark = new short[2]; dttmRMark[0] = LittleEndian.getShort(grpprl, (offset - 4)); dttmRMark[1] = LittleEndian.getShort(grpprl, (offset - 2)); newCHP.setDttmRMark(dttmRMark); break; case 0x6: newCHP.setFData(getFlag(param)); break; case 0x7: //don't care about this break; case 0x8: short chsDiff = (short)((param & 0xff0000) >>> 8); newCHP.setFChsDiff(getFlag(chsDiff)); newCHP.setChse((short)(param & 0xffff)); break; case 0x9: newCHP.setFSpec(true); newCHP.setFtcSym((short)LittleEndian.getShort(varParam, 0)); newCHP.setXchSym((short)LittleEndian.getShort(varParam, 2)); break; case 0xa: newCHP.setFOle2(getFlag(param)); break; case 0xb: //? break; case 0xc: newCHP.setIcoHighlight((byte)param); newCHP.setFHighlight(getFlag(param)); break; case 0xd: break; case 0xe: newCHP.setFcObj(param); break; case 0xf: break; case 0x10: //? break; case 0x11: break; case 0x12: break; case 0x13: break; case 0x14: break; case 0x15: break; case 0x16: break; case 0x17: break; case 0x18: break; case 0x19: break; case 0x1a: break; case 0x1b: break; case 0x1c: break; case 0x1d: break; case 0x1e: break; case 0x1f: break; case 0x20: break; case 0x21: break; case 0x22: break; case 0x23: break; case 0x24: break; case 0x25: break; case 0x26: break; case 0x27: break; case 0x28: break; case 0x29: break; case 0x2a: break; case 0x2b: break; case 0x2c: break; case 0x2d: break; case 0x2e: break; case 0x2f: break; case 0x30: newCHP.setIstd(param); break; case 0x31: //permutation vector for fast saves, who cares! break; case 0x32: newCHP.setFBold(false); newCHP.setFItalic(false); newCHP.setFOutline(false); newCHP.setFStrike(false); newCHP.setFShadow(false); newCHP.setFSmallCaps(false); newCHP.setFCaps(false); newCHP.setFVanish(false); newCHP.setKul((byte)0); newCHP.setIco((byte)0); break; case 0x33: try { newCHP = (CharacterProperties)oldCHP.clone(); } catch(CloneNotSupportedException e) { //do nothing }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -