add Colorref structure and use it to store color information in CHP(X)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1150606 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-25 09:39:07 +00:00
parent 62d7a21e94
commit 55ed7e8d77
6 changed files with 803 additions and 564 deletions

View File

@ -0,0 +1,108 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hwpf.model;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
/**
* 24-bit color structure
*
* @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
*/
@Internal
public class Colorref implements Cloneable
{
private int value;
public Colorref()
{
this.value = -1;
}
public Colorref( byte[] data, int offset )
{
this.value = LittleEndian.getInt( data, offset );
}
public Colorref( int value )
{
this.value = value;
}
@Override
public Colorref clone() throws CloneNotSupportedException
{
return new Colorref( this.value );
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
Colorref other = (Colorref) obj;
if ( value != other.value )
return false;
return true;
}
public int getValue()
{
return value;
}
@Override
public int hashCode()
{
return value;
}
public boolean isEmpty()
{
return value == -1;
}
public void setValue( int value )
{
this.value = value;
}
public byte[] toByteArray()
{
if ( isEmpty() )
throw new IllegalStateException(
"Structure state (EMPTY) is not good for serialization" );
byte[] bs = new byte[4];
LittleEndian.putInt( bs, 0, this.value );
return bs;
}
@Override
public String toString()
{
if ( isEmpty() )
return "[COLORREF] EMPTY";
return "[COLORREF] 0x" + Integer.toHexString( value );
}
}

View File

@ -275,11 +275,13 @@ public final class CharacterSprmCompressor
{ {
size += SprmUtils.addSprm((short)0x2859, newCHP.getSfxtText(), null, sprmList); size += SprmUtils.addSprm((short)0x2859, newCHP.getSfxtText(), null, sprmList);
} }
if (newCHP.getIco24() != oldCHP.getIco24()) if ( !newCHP.getCv().equals( oldCHP.getCv() ) )
{ {
if(newCHP.getIco24() != -1) // don't add a sprm if we're looking at an ico = Auto // don't add a sprm if we're looking at an ico = Auto
size += SprmUtils.addSprm((short)0x6870, newCHP.getIco24(), null, sprmList); if ( !newCHP.getCv().isEmpty() )
} size += SprmUtils.addSprm( CharacterProperties.SPRM_CCV, newCHP
.getCv().getValue(), null, sprmList );
}
return SprmUtils.getGrpprl(sprmList, size); return SprmUtils.getGrpprl(sprmList, size);
} }

View File

@ -17,6 +17,7 @@
package org.apache.poi.hwpf.sprm; package org.apache.poi.hwpf.sprm;
import org.apache.poi.hwpf.model.Colorref;
import org.apache.poi.hwpf.model.Hyphenation; import org.apache.poi.hwpf.model.Hyphenation;
import org.apache.poi.hwpf.usermodel.BorderCode; import org.apache.poi.hwpf.usermodel.BorderCode;
import org.apache.poi.hwpf.usermodel.CharacterProperties; import org.apache.poi.hwpf.usermodel.CharacterProperties;
@ -601,9 +602,10 @@ public final class CharacterSprmUncompressor extends SprmUncompressor
case 0x6f: case 0x6f:
newCHP.setIdctHint ((byte) sprm.getOperand()); newCHP.setIdctHint ((byte) sprm.getOperand());
break; break;
case 0x70: case 0x70:
newCHP.setIco24 (sprm.getOperand()); // sprmCCv -- 0x6870
break; newCHP.setCv( new Colorref( sprm.getOperand() ) );
break;
case 0x71: case 0x71:
// sprmCShd // sprmCShd
break; break;

View File

@ -17,6 +17,7 @@
package org.apache.poi.hwpf.usermodel; package org.apache.poi.hwpf.usermodel;
import org.apache.poi.hwpf.model.Colorref;
import org.apache.poi.hwpf.model.types.CHPAbstractType; import org.apache.poi.hwpf.model.types.CHPAbstractType;
/** /**
@ -65,6 +66,10 @@ public final class CharacterProperties
public final static short SPRM_PROPRMARK = (short)0xCA57; public final static short SPRM_PROPRMARK = (short)0xCA57;
public final static short SPRM_FEMBOSS = 0x0858; public final static short SPRM_FEMBOSS = 0x0858;
public final static short SPRM_SFXTEXT = 0x2859; public final static short SPRM_SFXTEXT = 0x2859;
/*
* Microsoft Office Word 97-2007 Binary File Format (.doc) Specification;
* Page 60 of 210
*/
public final static short SPRM_DISPFLDRMARK = (short)0xCA62; public final static short SPRM_DISPFLDRMARK = (short)0xCA62;
public final static short SPRM_IBSTRMARKDEL = 0x4863; public final static short SPRM_IBSTRMARKDEL = 0x4863;
public final static short SPRM_DTTMRMARKDEL = 0x6864; public final static short SPRM_DTTMRMARKDEL = 0x6864;
@ -75,8 +80,10 @@ public final class CharacterProperties
public final static short SPRM_NONFELID = 0x486D; public final static short SPRM_NONFELID = 0x486D;
public final static short SPRM_FELID = 0x486E; public final static short SPRM_FELID = 0x486E;
public final static short SPRM_IDCTHINT = 0x286F; public final static short SPRM_IDCTHINT = 0x286F;
/**
int _ico24 = -1; // default to -1 so we can ignore it for word 97 files * change chp.cv
*/
public final static short SPRM_CCV = 0x6870;
public CharacterProperties() public CharacterProperties()
{ {
@ -305,66 +312,69 @@ public final class CharacterProperties
super.setIcoHighlight(color); super.setIcoHighlight(color);
} }
/** /**
* Get the ico24 field for the CHP record. * Get the ico24 field for the CHP record.
*/ */
public int getIco24() public int getIco24()
{
if ( _ico24 == -1 )
{ {
switch(getIco()) // convert word 97 colour numbers to 0xBBGGRR value if ( !getCv().isEmpty() )
{ return getCv().getValue();
case 0: // auto
return -1;
case 1: // black
return 0x000000;
case 2: // blue
return 0xFF0000;
case 3: // cyan
return 0xFFFF00;
case 4: // green
return 0x00FF00;
case 5: // magenta
return 0xFF00FF;
case 6: // red
return 0x0000FF;
case 7: // yellow
return 0x00FFFF;
case 8: // white
return 0x0FFFFFF;
case 9: // dark blue
return 0x800000;
case 10: // dark cyan
return 0x808000;
case 11: // dark green
return 0x008000;
case 12: // dark magenta
return 0x800080;
case 13: // dark red
return 0x000080;
case 14: // dark yellow
return 0x008080;
case 15: // dark grey
return 0x808080;
case 16: // light grey
return 0xC0C0C0;
}
}
return _ico24;
}
/** // convert word 97 colour numbers to 0xBBRRGGRR value
* Set the ico24 field for the CHP record. switch ( getIco() )
*/ {
public void setIco24(int colour24) case 0: // auto
{ return -1;
_ico24 = colour24 & 0xFFFFFF; // only keep the 24bit 0xBBGGRR colour case 1: // black
} return 0x00000000;
case 2: // blue
return 0x00FF0000;
case 3: // cyan
return 0x00FFFF00;
case 4: // green
return 0x0000FF00;
case 5: // magenta
return 0x00FF00FF;
case 6: // red
return 0x000000FF;
case 7: // yellow
return 0x0000FFFF;
case 8: // white
return 0x00FFFFFF;
case 9: // dark blue
return 0x00800000;
case 10: // dark cyan
return 0x00808000;
case 11: // dark green
return 0x00008000;
case 12: // dark magenta
return 0x00800080;
case 13: // dark red
return 0x00000080;
case 14: // dark yellow
return 0x00008080;
case 15: // dark grey
return 0x00808080;
case 16: // light grey
return 0x00C0C0C0;
}
return -1;
}
/**
* Set the ico24 field for the CHP record.
*/
public void setIco24( int colour24 )
{
setCv( new Colorref( colour24 & 0xFFFFFF ) );
}
public Object clone() throws CloneNotSupportedException public Object clone() throws CloneNotSupportedException
{ {
CharacterProperties cp = (CharacterProperties) super.clone(); CharacterProperties cp = (CharacterProperties) super.clone();
cp.setCv( getCv().clone() );
cp.setDttmRMark( (DateAndTime) getDttmRMark().clone() ); cp.setDttmRMark( (DateAndTime) getDttmRMark().clone() );
cp.setDttmRMarkDel( (DateAndTime) getDttmRMarkDel().clone() ); cp.setDttmRMarkDel( (DateAndTime) getDttmRMarkDel().clone() );
cp.setDttmPropRMark( (DateAndTime) getDttmPropRMark().clone() ); cp.setDttmPropRMark( (DateAndTime) getDttmPropRMark().clone() );
@ -373,8 +383,6 @@ public final class CharacterProperties
cp.setShd( (ShadingDescriptor) getShd().clone() ); cp.setShd( (ShadingDescriptor) getShd().clone() );
cp.setBrc( (BorderCode) getBrc().clone() ); cp.setBrc( (BorderCode) getBrc().clone() );
cp._ico24 = _ico24;
return cp; return cp;
} }
} }

View File

@ -79,7 +79,8 @@
<!-- rgftc[iftcCompositeMax] --> <!-- rgftc[iftcCompositeMax] -->
<field type="int" size="4" name="dxaSpace" <field type="int" size="4" name="dxaSpace"
description="Space following each character in the run expressed in twip units."/> description="Space following each character in the run expressed in twip units."/>
<!-- cv -->
<field type="Colorref" size="4" name="cv" description="24-bit color"/>
<!-- Microsoft Office Word 97-2007 Binary File Format (.doc) Specification --> <!-- Microsoft Office Word 97-2007 Binary File Format (.doc) Specification -->
<!-- Page 104 of 210 --> <!-- Page 104 of 210 -->