From b0033606e4a2296f615f8dfb15ab86c1595768cc Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 24 Sep 2008 16:56:12 +0000 Subject: [PATCH] XSSFFont improvements from patch #45874 git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@698651 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xssf/dev/XSSFDump.java | 2 +- .../apache/poi/xssf/model/StylesTable.java | 130 +++++----- .../poi/xssf/usermodel/FontCharset.java | 112 ++++++++ .../apache/poi/xssf/usermodel/FontFamily.java | 74 ++++++ .../apache/poi/xssf/usermodel/FontScheme.java | 68 +++++ .../poi/xssf/usermodel/FontUnderline.java | 75 ++++++ .../poi/xssf/usermodel/IndexedColors.java | 17 ++ .../apache/poi/xssf/usermodel/XSSFFont.java | 239 ++++++++---------- .../org/apache/poi/xssf/util/Charset.java | 27 -- .../poi/xssf/usermodel/TestXSSFFont.java | 78 +++--- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 6 +- 11 files changed, 559 insertions(+), 269 deletions(-) create mode 100755 src/ooxml/java/org/apache/poi/xssf/usermodel/FontCharset.java create mode 100755 src/ooxml/java/org/apache/poi/xssf/usermodel/FontFamily.java create mode 100755 src/ooxml/java/org/apache/poi/xssf/usermodel/FontScheme.java create mode 100755 src/ooxml/java/org/apache/poi/xssf/usermodel/FontUnderline.java diff --git a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java index 9cd50813f5..6c3d2dcbdc 100755 --- a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java +++ b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java @@ -69,7 +69,7 @@ public class XSSFDump { File f = new File(root, entry.getName()); FileOutputStream out = new FileOutputStream(f); - if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml")){ + if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){ //pass the xml through the Xerces serializer to produce nicely formatted output Document doc = builder.parse(zip.getInputStream(entry)); diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 7326943784..985d92be4b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -29,6 +29,8 @@ import java.util.Map.Entry; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.StylesSource; +import org.apache.poi.xssf.usermodel.FontFamily; +import org.apache.poi.xssf.usermodel.FontScheme; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; @@ -103,38 +105,38 @@ public class StylesTable implements StylesSource, XSSFModel { */ public void readFrom(InputStream is) throws IOException { try { - doc = StyleSheetDocument.Factory.parse(is); - // Grab all the different bits we care about - if(doc.getStyleSheet().getNumFmts() != null) + doc = StyleSheetDocument.Factory.parse(is); + // Grab all the different bits we care about + if(doc.getStyleSheet().getNumFmts() != null) for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) { - numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode()); + numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode()); } - if(doc.getStyleSheet().getFonts() != null) + if(doc.getStyleSheet().getFonts() != null) for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) { - fonts.add(font); + fonts.add(font); } - if(doc.getStyleSheet().getFills() != null) + if(doc.getStyleSheet().getFills() != null) for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) { - fills.add(fill); + fills.add(fill); } - if(doc.getStyleSheet().getBorders() != null) + if(doc.getStyleSheet().getBorders() != null) for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) { - borders.add(border); + borders.add(border); } - if(doc.getStyleSheet().getCellXfs() != null) + if(doc.getStyleSheet().getCellXfs() != null) for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) { - xfs.add(xf); + xfs.add(xf); } - if(doc.getStyleSheet().getCellStyleXfs() != null) + if(doc.getStyleSheet().getCellStyleXfs() != null) for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) { - styleXfs.add(xf); + styleXfs.add(xf); } - // dxf - if(doc.getStyleSheet().getDxfs() != null) - for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) { - dxfs.add(dxf); - } - + // dxf + if(doc.getStyleSheet().getDxfs() != null) + for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) { + dxfs.add(dxf); + } + } catch (XmlException e) { throw new IOException(e.getLocalizedMessage()); } @@ -350,7 +352,7 @@ public class StylesTable implements StylesSource, XSSFModel { doc.getStyleSheet().setCellStyleXfs(ctSXfs); } - // Style dxf + // Style dxfs if(dxfs.size() > 0) { CTDxfs ctDxfs = CTDxfs.Factory.newInstance(); ctDxfs.setCount(dxfs.size()); @@ -359,8 +361,6 @@ public class StylesTable implements StylesSource, XSSFModel { doc.getStyleSheet().setDxfs(ctDxfs); } - - // Save doc.save(out, options); } @@ -375,47 +375,51 @@ public class StylesTable implements StylesSource, XSSFModel { private long putFont(XSSFFont font, ArrayList fonts) { return font.putFont(fonts); - } + } + private void initialize() { //CTFont ctFont = createDefaultFont(); - XSSFFont xssfFont = createDefaultFont(); - fonts.add(xssfFont.getCTFont()); - - CTFill ctFill = createDefaultFill(); - fills.add(ctFill); - - CTBorder ctBorder = createDefaultBorder(); - borders.add(ctBorder); - - CTXf styleXf = createDefaultXf(); - styleXfs.add(styleXf); - CTXf xf = createDefaultXf(); - xf.setXfId(0); - xfs.add(xf); + XSSFFont xssfFont = createDefaultFont(); + fonts.add(xssfFont.getCTFont()); + + CTFill[] ctFill = createDefaultFills(); + fills.add(ctFill[0]); +/* + fills.add(ctFill[1]); +*/ + CTBorder ctBorder = createDefaultBorder(); + borders.add(ctBorder); + + CTXf styleXf = createDefaultXf(); + styleXfs.add(styleXf); + CTXf xf = createDefaultXf(); + xf.setXfId(0); + xfs.add(xf); } - + private CTXf createDefaultXf() { - CTXf ctXf = CTXf.Factory.newInstance(); - ctXf.setNumFmtId(0); - ctXf.setFontId(0); - ctXf.setFillId(0); - ctXf.setBorderId(0); - return ctXf; + CTXf ctXf = CTXf.Factory.newInstance(); + ctXf.setNumFmtId(0); + ctXf.setFontId(0); + ctXf.setFillId(0); + ctXf.setBorderId(0); + return ctXf; } private CTBorder createDefaultBorder() { - CTBorder ctBorder = CTBorder.Factory.newInstance(); - ctBorder.addNewBottom(); - ctBorder.addNewTop(); - ctBorder.addNewLeft(); - ctBorder.addNewRight(); - ctBorder.addNewDiagonal(); - return ctBorder; + CTBorder ctBorder = CTBorder.Factory.newInstance(); + ctBorder.addNewBottom(); + ctBorder.addNewTop(); + ctBorder.addNewLeft(); + ctBorder.addNewRight(); + ctBorder.addNewDiagonal(); + return ctBorder; } - private CTFill createDefaultFill() { - CTFill ctFill = CTFill.Factory.newInstance(); - ctFill.addNewPatternFill().setPatternType(STPatternType.NONE); - return ctFill; + private CTFill[] createDefaultFills() { + CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()}; + ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE); + ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY); + return ctFill; } private XSSFFont createDefaultFont() { @@ -434,19 +438,17 @@ public class StylesTable implements StylesSource, XSSFModel { xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE); xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME); - xssfFont.setFamily(XSSFFont.FONT_FAMILY_SWISS); - xssfFont.setScheme(XSSFFont.SCHEME_MINOR); + xssfFont.setFamily(FontFamily.SWISS); + xssfFont.setScheme(FontScheme.MINOR); return xssfFont; } - - public CTDxf getDxf(long idx) { - if(dxfs.size()==0) - return CTDxf.Factory.newInstance(); - else - return dxfs.get((int) idx); + if(dxfs.size()==0) + return CTDxf.Factory.newInstance(); + else + return dxfs.get((int) idx); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/FontCharset.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontCharset.java new file mode 100755 index 0000000000..4ad63f06ae --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontCharset.java @@ -0,0 +1,112 @@ +/* ==================================================================== + 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.xssf.usermodel; + + +/** + * Charset represents the basic set of characters associated with a font (that it can display), and + * corresponds to the ANSI codepage (8-bit or DBCS) of that character set used by a given language. + * + * @author Gisella Bronzetti + * + */ +public enum FontCharset { + + ANSI(0), + DEFAULT(1), + SYMBOL(2), + MAC(77), + SHIFTJIS(128), + HANGEUL(129), + HANGUL(129), + JOHAB(130), + GB2312(134), + CHINESEBIG5(136), + GREEK(161), + TURKISH(162), + VIETNAMESE(163), + HEBREW(177), + ARABIC(178), + BALTIC(186), + RUSSIAN(204), + THAI(222), + EASTEUROPE(238), + OEM(255); + + + private int charset; + + + FontCharset(int value){ + charset = value; + } + + /** + * Returns index of this charset + * + * @return index of this charset + */ + public byte getValue(){ + return (byte)charset; + } + + public static FontCharset valueOf(int value){ + switch (value) { + case 0: + return ANSI; + case 1: + return DEFAULT; + case 2: + return SYMBOL; + case 77: + return MAC; + case 128: + return SHIFTJIS; + case 129: + return HANGEUL; + case 130: + return JOHAB; + case 134: + return GB2312; + case 136: + return CHINESEBIG5; + case 161: + return GREEK; + case 162: + return TURKISH; + case 163: + return VIETNAMESE; + case 177: + return HEBREW; + case 178: + return ARABIC; + case 186: + return BALTIC; + case 204: + return RUSSIAN; + case 222: + return THAI; + case 238: + return EASTEUROPE; + case 255: + return OEM; + } + throw new RuntimeException("Charset value ["+ value +"] not supported"); + } + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/FontFamily.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontFamily.java new file mode 100755 index 0000000000..2457336bd3 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontFamily.java @@ -0,0 +1,74 @@ +/* ==================================================================== + 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.xssf.usermodel; + + +/** + * + * The font family this font belongs to. A font family is a set of fonts having common stroke width and serif + * characteristics. The font name overrides when there are conflicting values. + * + * @author Gisella Bronzetti + * + */ +public enum FontFamily { + + NOT_APPLICABLE(0), + ROMAN(1), + SWISS(2), + MODERN(3), + SCRIPT(4), + DECORATIVE(5); + + private int family; + + + FontFamily(int value){ + family = value; + } + + /** + * Returns index of this font family + * + * @return index of this font family + */ + public int getValue(){ + return family; + } + + + public static FontFamily valueOf(int family){ + switch (family) { + case 0: + return NOT_APPLICABLE; + case 1: + return ROMAN; + case 2: + return SWISS; + case 3: + return MODERN; + case 4: + return SCRIPT; + case 5: + return DECORATIVE; + } + throw new RuntimeException("Family value ["+ family +"] not supported"); + } + + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/FontScheme.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontScheme.java new file mode 100755 index 0000000000..c7c9cf0f12 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontScheme.java @@ -0,0 +1,68 @@ +/* ==================================================================== + 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.xssf.usermodel; + +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme; + + +/** + * Defines the font scheme to which this font belongs. + * When a font definition is part of a theme definition, then the font is categorized as either a major or minor font scheme component. + * When a new theme is chosen, every font that is part of a theme definition is updated to use the new major or minor font definition for that + * theme. + * Usually major fonts are used for styles like headings, and minor fonts are used for body & paragraph text. + * + * @author Gisella Bronzetti + * + */ +public enum FontScheme { + + + MAJOR(STFontScheme.MAJOR), + MINOR(STFontScheme.MINOR), + NONE(STFontScheme.NONE); + + private STFontScheme.Enum scheme; + + + FontScheme(STFontScheme.Enum value){ + scheme = value; + } + + /** + * Returns STFontScheme.Enum value of this font scheme + * + * @return STFontScheme.Enum value of this font scheme + */ + public STFontScheme.Enum getValue(){ + return scheme; + } + + + public static FontScheme valueOf(STFontScheme.Enum scheme){ + switch (scheme.intValue()) { + case STFontScheme.INT_MAJOR: + return MAJOR; + case STFontScheme.INT_MINOR: + return MINOR; + case STFontScheme.INT_NONE: + return NONE; + } + throw new RuntimeException("Schema value ["+ scheme +"] not supported"); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/FontUnderline.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontUnderline.java new file mode 100755 index 0000000000..34b52bbef4 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/FontUnderline.java @@ -0,0 +1,75 @@ +/* ==================================================================== + 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.xssf.usermodel; + +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues; + + +/** + * the different types of possible underline formatting + * + * @author Gisella Bronzetti + * + */ +public enum FontUnderline { + +/** + * Double-line underlining under each character in the + * cell. underlines are drawn through the descenders of + * characters such as g and p. + */ + DOUBLE(STUnderlineValues.DOUBLE), + DOUBLE_ACCOUNTING(STUnderlineValues.DOUBLE_ACCOUNTING), + NONE(STUnderlineValues.NONE), + SINGLE(STUnderlineValues.SINGLE), + SINGLE_ACCOUNTING(STUnderlineValues.SINGLE_ACCOUNTING); + + private STUnderlineValues.Enum underline; + + + FontUnderline(STUnderlineValues.Enum value){ + underline = value; + } + + /** + * Returns index of this font family + * + * @return index of this font family + */ + public STUnderlineValues.Enum getValue(){ + return underline; + } + + public static FontUnderline valueOf(STUnderlineValues.Enum underline){ + switch (underline.intValue()) { + case STUnderlineValues.INT_DOUBLE: + return DOUBLE; + case STUnderlineValues.INT_DOUBLE_ACCOUNTING: + return DOUBLE_ACCOUNTING; + case STUnderlineValues.INT_SINGLE: + return SINGLE; + case STUnderlineValues.INT_SINGLE_ACCOUNTING: + return SINGLE_ACCOUNTING; + case STUnderlineValues.INT_NONE: + return NONE; + } + throw new RuntimeException("Underline value ["+ underline +"] not supported"); + } + + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java index fbce6fa36c..a390a3646a 100755 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java @@ -1,3 +1,20 @@ +/* ==================================================================== + 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.xssf.usermodel; /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index e052519451..a46444d99f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -16,13 +16,12 @@ ==================================================================== */ package org.apache.poi.xssf.usermodel; -import org.apache.poi.ss.usermodel.Font; +import java.util.ArrayList; -import org.apache.poi.xssf.util.Charset; +import org.apache.poi.ss.usermodel.Font; import org.apache.poi.xssf.usermodel.extensions.XSSFColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; - import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme; @@ -30,27 +29,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun; -import java.util.ArrayList; - public class XSSFFont implements Font { - - public static final int SCHEME_MAJOR=2; - public static final int SCHEME_MINOR=3; - public static final int SCHEME_NONE=0; - - public static final int FONT_FAMILY_NOT_APPLICABLE=0; - public static final int FONT_FAMILY_ROMAN=1; - public static final int FONT_FAMILY_SWISS=2; - public static final int FONT_FAMILY_MODERN=3; - public static final int FONT_FAMILY_SCRIPT=4; - public static final int FONT_FAMILY_DECORATIVE=5; - - public static final String DEFAULT_FONT_NAME="Calibri"; public static final short DEFAULT_FONT_SIZE=11; public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex(); @@ -70,48 +53,27 @@ public class XSSFFont implements Font { return ctFont; } - /** - * - */ + public boolean getBold() { CTBooleanProperty bold=ctFont.sizeOfBArray() == 0 ? null : ctFont.getBArray(0); return (bold!=null && bold.getVal()); } - - public byte getCharSet() { CTIntProperty charset= ctFont.sizeOfCharsetArray() == 0?null:ctFont.getCharsetArray(0); - if(charset!=null){ - //this value must be set -- can't be null - switch (charset.getVal()) { - case Charset.ANSI_CHARSET: - return Font.ANSI_CHARSET; - - case Charset.DEFAULT_CHARSET: - return Font.DEFAULT_CHARSET; - - case Charset.SYMBOL_CHARSET: - return Font.SYMBOL_CHARSET; - - default://maight be correct to return this byte value??? - return Byte.parseByte(Integer.toString(charset.getVal())); - } - } - else - return Font.ANSI_CHARSET; + return charset == null ? FontCharset.ANSI.getValue() : FontCharset.valueOf(charset.getVal()).getValue(); } public short getColor() { CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); - if(color == null) return Font.COLOR_NORMAL; + if(color == null) return IndexedColors.BLACK.getIndex(); long index=color.getIndexed(); if (index==XSSFFont.DEFAULT_FONT_COLOR){ - return Font.COLOR_NORMAL; + return IndexedColors.BLACK.getIndex(); } else if(index == IndexedColors.RED.getIndex()){ - return Font.COLOR_RED; + return IndexedColors.RED.getIndex(); } else{ return Short.parseShort(new Long(index).toString()); @@ -119,9 +81,10 @@ public class XSSFFont implements Font { } - public byte[] getRgbColor() { - CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); - return color.getRgb(); + public XSSFColor getRgbColor() { + CTColor ctColor=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); + XSSFColor color=new XSSFColor(ctColor); + return color; } public short getThemeColor() { @@ -134,13 +97,14 @@ public class XSSFFont implements Font { public short getFontHeight() { CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0); if(size!=null){ - double fontHeight= size.getVal()/20; + double fontHeight= size.getVal(); return (short)fontHeight; } else - return DEFAULT_FONT_SIZE/20; + return DEFAULT_FONT_SIZE; } + public short getFontHeightInPoints() { CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0); if(size!=null){ @@ -152,9 +116,9 @@ public class XSSFFont implements Font { } - public String getFontName() { - CTFontName name=ctFont.sizeOfNameArray()==0?null:ctFont.getNameArray(0); - return name==null? null:name.getVal(); + public String getFontName() { + CTFontName name = ctFont.sizeOfNameArray() == 0 ? null : ctFont.getNameArray(0); + return name == null ? null : name.getVal(); } @@ -187,26 +151,31 @@ public class XSSFFont implements Font { } public byte getUnderline() { - CTUnderlineProperty underline=ctFont.sizeOfUArray()==0?null:ctFont.getUArray(0); - if(underline!=null){ - switch (underline.getVal().intValue()) { - case STUnderlineValues.INT_DOUBLE: - return Font.U_DOUBLE; - case STUnderlineValues.INT_DOUBLE_ACCOUNTING: - return Font.U_DOUBLE_ACCOUNTING; - - case STUnderlineValues.INT_SINGLE_ACCOUNTING: - return Font.U_SINGLE_ACCOUNTING; - - case STUnderlineValues.INT_NONE: - return Font.U_NONE; - - case STUnderlineValues.INT_SINGLE: - default: - return Font.U_SINGLE; - } - } - return Font.U_NONE; + /* + CTUnderlineProperty underline = ctFont.sizeOfUArray() == 0 ? null : ctFont.getUArray(0); + return underline == null ? (byte)FontUnderline.NONE.getValue().intValue() : (byte)FontUnderline.valueOf(underline.getVal()).getValue().intValue(); + */ + CTUnderlineProperty underline=ctFont.sizeOfUArray()==0?null:ctFont.getUArray(0); + if(underline!=null){ + FontUnderline fontUnderline=FontUnderline.valueOf(underline.getVal()); + switch (fontUnderline.getValue().intValue()) { + case STUnderlineValues.INT_DOUBLE: + return Font.U_DOUBLE; + case STUnderlineValues.INT_DOUBLE_ACCOUNTING: + return Font.U_DOUBLE_ACCOUNTING; + + case STUnderlineValues.INT_SINGLE_ACCOUNTING: + return Font.U_SINGLE_ACCOUNTING; + + case STUnderlineValues.INT_NONE: + return Font.U_NONE; + + case STUnderlineValues.INT_SINGLE: + default: + return Font.U_SINGLE; + } + } + return Font.U_NONE; } /** @@ -218,26 +187,28 @@ public class XSSFFont implements Font { ctBold.setVal(true); } - /** - * - */ + public void setCharSet(byte charset) { CTIntProperty charsetProperty=ctFont.sizeOfCharsetArray()==0?ctFont.addNewCharset():ctFont.getCharsetArray(0); switch (charset) { case Font.ANSI_CHARSET: - charsetProperty.setVal(Charset.ANSI_CHARSET); + charsetProperty.setVal(FontCharset.ANSI.getValue()); break; case Font.SYMBOL_CHARSET: - charsetProperty.setVal(Charset.SYMBOL_CHARSET); + charsetProperty.setVal(FontCharset.SYMBOL.getValue()); break; case Font.DEFAULT_CHARSET: - charsetProperty.setVal(Charset.DEFAULT_CHARSET); + charsetProperty.setVal(FontCharset.DEFAULT.getValue()); break; default: throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset"); } } + + public void setCharSet(FontCharset charSet) { + setCharSet(charSet.getValue()); + } public void setColor(short color) { CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); @@ -257,24 +228,24 @@ public class XSSFFont implements Font { } - public void setFontHeight(short height) { - CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0); - fontSize.setVal(height*20); + setFontHeight((double)height); } + public void setFontHeight(double height) { + CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0); + fontSize.setVal(height); + } public void setFontHeightInPoints(short height) { - CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0); - fontSize.setVal(height); + setFontHeight((double)height); } - - public void setRgbColor(XSSFColor color) { - CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); - ctColor.setRgb(color.getRgb()); - } + public void setRgbColor(XSSFColor color) { + CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); + ctColor.setRgb(color.getRgb()); + } public void setThemeColor(short theme) { CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); @@ -282,8 +253,8 @@ public class XSSFFont implements Font { } public void setFontName(String name) { - CTFontName fontName=ctFont.sizeOfNameArray()==0?ctFont.addNewName():ctFont.getNameArray(0); - fontName.setVal(name); + CTFontName fontName=ctFont.sizeOfNameArray()==0?ctFont.addNewName():ctFont.getNameArray(0); + fontName.setVal(name); } public void setItalic(boolean italic) { @@ -297,7 +268,7 @@ public class XSSFFont implements Font { } public void setTypeOffset(short offset) { - CTVerticalAlignFontProperty offsetProperty=ctFont.sizeOfVertAlignArray()==0?ctFont.addNewVertAlign(): ctFont.getVertAlignArray(0); + CTVerticalAlignFontProperty offsetProperty=ctFont.sizeOfVertAlignArray()==0?ctFont.addNewVertAlign(): ctFont.getVertAlignArray(0); switch (offset) { case Font.SS_NONE: offsetProperty.setVal(STVerticalAlignRun.BASELINE); @@ -315,25 +286,36 @@ public class XSSFFont implements Font { CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0); switch (underline) { case Font.U_DOUBLE: - ctUnderline.setVal(STUnderlineValues.DOUBLE); + ctUnderline.setVal(FontUnderline.DOUBLE.getValue()); break; case Font.U_DOUBLE_ACCOUNTING: - ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING); + ctUnderline.setVal(FontUnderline.DOUBLE_ACCOUNTING.getValue()); break; case Font.U_SINGLE_ACCOUNTING: - ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING); + ctUnderline.setVal(FontUnderline.SINGLE_ACCOUNTING.getValue()); break; case Font.U_NONE: - ctUnderline.setVal(STUnderlineValues.NONE); + ctUnderline.setVal(FontUnderline.NONE.getValue()); break; - case Font.U_SINGLE: default: - ctUnderline.setVal(STUnderlineValues.SINGLE); + ctUnderline.setVal(FontUnderline.SINGLE.getValue()); break; } } - + + public void setUnderline(FontUnderline underline) { + CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0); + ctUnderline.setVal(underline.getValue()); + } + + + public String toString(){ + return "org.apache.poi.xssf.usermodel.XSSFFont{" + + ctFont + + "}"; + } + public long putFont(ArrayList fonts) { //TODO @@ -349,58 +331,37 @@ public class XSSFFont implements Font { return fonts.size() - 1; } - // solo di xssfFont - non di Font- - //sono utilizzati nel metodo createDefaultFont in StylesTable insta. +/* + this methds are used only for XSSFFont and aren't in Font interface + are used in method SthlesTable.createDefaultfont + */ - public int getScheme(){ + public FontScheme getScheme(){ CTFontScheme scheme=ctFont.sizeOfSchemeArray()==0?null:ctFont.getSchemeArray(0); - if(scheme!=null){ - int fontScheme = scheme.getVal().intValue(); - switch (fontScheme) { - case STFontScheme.INT_MAJOR: - return XSSFFont.SCHEME_MAJOR; - case STFontScheme.INT_MINOR: - return XSSFFont.SCHEME_MINOR; - case STFontScheme.INT_NONE: - return XSSFFont.SCHEME_NONE; - - default: - return fontScheme; - } - } - return 0; + return scheme == null ? FontScheme.NONE : FontScheme.valueOf(scheme.getVal()); } - - public void setScheme(int scheme){ + public void setScheme(FontScheme scheme){ CTFontScheme ctFontScheme=ctFont.sizeOfSchemeArray()==0?ctFont.addNewScheme():ctFont.getSchemeArray(0); - switch (scheme) { - case XSSFFont.SCHEME_MAJOR: - ctFontScheme.setVal(STFontScheme.MAJOR); - break; - case XSSFFont.SCHEME_MINOR: - ctFontScheme.setVal(STFontScheme.MINOR); - break; - case XSSFFont.SCHEME_NONE: - ctFontScheme.setVal(STFontScheme.NONE); - break; - default: - throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont"); - } + ctFontScheme.setVal(scheme.getValue()); } public int getFamily(){ - CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0); - if(family!=null) - return family.getVal(); - else - return XSSFFont.FONT_FAMILY_SWISS; + CTIntProperty family = ctFont.sizeOfFamilyArray() == 0 ? ctFont.addNewFamily() : ctFont.getFamilyArray(0); + return family == null ? FontFamily.NOT_APPLICABLE.getValue() : FontFamily.valueOf(family.getVal()).getValue(); } - public void setFamily(int value){ - CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0); + + public void setFamily(int value){ + CTIntProperty family = ctFont.sizeOfFamilyArray() == 0 ? ctFont.addNewFamily() : ctFont.getFamilyArray(0); family.setVal(value); } + + public void setFamily(FontFamily family){ + setFamily(family.getValue()); + } + + } diff --git a/src/ooxml/java/org/apache/poi/xssf/util/Charset.java b/src/ooxml/java/org/apache/poi/xssf/util/Charset.java index e9bdaa3cd1..e69de29bb2 100755 --- a/src/ooxml/java/org/apache/poi/xssf/util/Charset.java +++ b/src/ooxml/java/org/apache/poi/xssf/util/Charset.java @@ -1,27 +0,0 @@ -package org.apache.poi.xssf.util; - -public class Charset { - - public static final int ANSI_CHARSET=0; - public static final int DEFAULT_CHARSET=1; - public static final int SYMBOL_CHARSET=2; - public static final int MAC_CHARSET=77; - public static final int SHIFTJIS_CHARSET=128; - public static final int HANGEUL_CHARSET=129; - public static final int HANGUL_CHARSET=129; - public static final int JOHAB_CHARSET=130; - public static final int GB2312_CHARSET=134; - public static final int CHINESEBIG5_CHARSET=136; - public static final int GREEK_CHARSET=161; - public static final int TURKISH_CHARSET=162; - public static final int VIETNAMESE_CHARSET=163; - public static final int HEBREW_CHARSET=177; - public static final int ARABIC_CHARSET=178; - public static final int BALTIC_CHARSET=186; - public static final int RUSSIAN_CHARSET=204; - public static final int THAI_CHARSET=222; - public static final int EASTEUROPE_CHARSET=238; - public static final int OEM_CHARSET=255; - - -} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java index ced39c2b71..c17ab4af62 100755 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java @@ -13,7 +13,7 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.xssf.util.Charset; +import org.apache.poi.xssf.usermodel.extensions.XSSFColor; import org.openxml4j.opc.Package; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; @@ -54,14 +54,14 @@ public class TestXSSFFont extends TestCase{ public void testCharSet(){ CTFont ctFont=CTFont.Factory.newInstance(); CTIntProperty prop=ctFont.addNewCharset(); - prop.setVal(Charset.ANSI_CHARSET); + prop.setVal(FontCharset.ANSI.getValue()); ctFont.setCharsetArray(0,prop); XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet()); - xssfFont.setCharSet(Font.DEFAULT_CHARSET); - assertEquals(Charset.DEFAULT_CHARSET,ctFont.getCharsetArray(0).getVal()); + xssfFont.setCharSet(FontCharset.DEFAULT); + assertEquals(FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal()); } @@ -118,10 +118,10 @@ public class TestXSSFFont extends TestCase{ ctFont.setSzArray(0,size); XSSFFont xssfFont=new XSSFFont(ctFont); - assertEquals(11/20,xssfFont.getFontHeight()); + assertEquals(11,xssfFont.getFontHeight()); xssfFont.setFontHeight((short)20); - assertEquals(new Double(20*20).doubleValue(),ctFont.getSzArray(0).getVal()); + assertEquals(new Double(20).doubleValue(),ctFont.getSzArray(0).getVal()); } @@ -151,7 +151,11 @@ public class TestXSSFFont extends TestCase{ xssfFont.setUnderline(Font.U_DOUBLE); assertEquals(ctFont.getUArray().length,1); assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal()); - } + + xssfFont.setUnderline(FontUnderline.DOUBLE_ACCOUNTING); + assertEquals(ctFont.getUArray().length,1); + assertEquals(STUnderlineValues.DOUBLE_ACCOUNTING,ctFont.getUArray(0).getVal()); + } public void testColor(){ CTFont ctFont=CTFont.Factory.newInstance(); @@ -160,47 +164,52 @@ public class TestXSSFFont extends TestCase{ ctFont.setColorArray(0,color); XSSFFont xssfFont=new XSSFFont(ctFont); - assertEquals(Font.COLOR_NORMAL,xssfFont.getColor()); + assertEquals(IndexedColors.BLACK.getIndex(),xssfFont.getColor()); xssfFont.setColor(IndexedColors.RED.getIndex()); assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed()); } -/* - public void testRgbColor(){ - CTFont ctFont=CTFont.Factory.newInstance(); - CTColor color=ctFont.addNewColor(); - color.setRgb(new byte[]{}); - ctFont.setColorArray(0,color); - XSSFFont xssfFont=new XSSFFont(ctFont); - assertEquals(,xssfFont.getRgbColor()); - - xssfFont.setRgbColor(new XSSFColor(new java.awt.Color(10,19,10))); - //assertEquals(,ctFont.getColorArray(0).getRgb()); + public void testRgbColor(){ + CTFont ctFont=CTFont.Factory.newInstance(); + CTColor color=ctFont.addNewColor(); + + color.setRgb(Integer.toHexString(0xFFFFFF).getBytes()); + ctFont.setColorArray(0,color); + + XSSFFont xssfFont=new XSSFFont(ctFont); + assertEquals(ctFont.getColorArray(0).getRgb()[0],xssfFont.getRgbColor().getRgb()[0]); + assertEquals(ctFont.getColorArray(0).getRgb()[1],xssfFont.getRgbColor().getRgb()[1]); + assertEquals(ctFont.getColorArray(0).getRgb()[2],xssfFont.getRgbColor().getRgb()[2]); + assertEquals(ctFont.getColorArray(0).getRgb()[3],xssfFont.getRgbColor().getRgb()[3]); + + color.setRgb(Integer.toHexString(0xF1F1F1).getBytes()); + XSSFColor newColor=new XSSFColor(color); + xssfFont.setRgbColor(newColor); + assertEquals(ctFont.getColorArray(0).getRgb()[2],newColor.getRgb()[2]); } public void testThemeColor(){ - CTFont ctFont=CTFont.Factory.newInstance(); - CTColor color=ctFont.addNewColor(); - color.setTheme(); - ctFont.setColorArray(0,color); + CTFont ctFont=CTFont.Factory.newInstance(); + CTColor color=ctFont.addNewColor(); + color.setTheme((long)1); + ctFont.setColorArray(0,color); - XSSFFont xssfFont=new XSSFFont(ctFont); - assertEquals(,xssfFont.getThemeColor()); + XSSFFont xssfFont=new XSSFFont(ctFont); + assertEquals(ctFont.getColorArray(0).getTheme(),xssfFont.getThemeColor()); - xssfFont.setThemeColor(Font.COLOR_RED); - assertEquals(,ctFont.getColorArray(0).getTheme()); - assertEquals(,ctFont.getColorArray(0).getTint()); + xssfFont.setThemeColor(IndexedColors.RED.getIndex()); + assertEquals(IndexedColors.RED.getIndex(),ctFont.getColorArray(0).getTheme()); } -*/ + public void testFamily(){ CTFont ctFont=CTFont.Factory.newInstance(); CTIntProperty family=ctFont.addNewFamily(); - family.setVal(XSSFFont.FONT_FAMILY_MODERN); + family.setVal(FontFamily.MODERN.getValue()); ctFont.setFamilyArray(0,family); XSSFFont xssfFont=new XSSFFont(ctFont); - assertEquals(XSSFFont.FONT_FAMILY_MODERN,xssfFont.getFamily()); + assertEquals(FontFamily.MODERN.getValue(),xssfFont.getFamily()); } @@ -211,9 +220,9 @@ public class TestXSSFFont extends TestCase{ ctFont.setSchemeArray(0,scheme); XSSFFont font=new XSSFFont(ctFont); - assertEquals(XSSFFont.SCHEME_MAJOR,font.getScheme()); + assertEquals(FontScheme.MAJOR,font.getScheme()); - font.setScheme(XSSFFont.SCHEME_NONE); + font.setScheme(FontScheme.NONE); assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).getVal()); } @@ -332,7 +341,7 @@ public class TestXSSFFont extends TestCase{ Font font2=new XSSFFont(); font2.setFontHeight((short)1); font2.setFontName("Courier"); - font2.setColor(Font.COLOR_NORMAL); + font2.setColor(IndexedColors.BLACK.getIndex()); font2.setUnderline(Font.U_DOUBLE); CellStyle cellStyle2=workbook.createCellStyle(); cellStyle2.setFont(font2); @@ -362,5 +371,4 @@ public class TestXSSFFont extends TestCase{ out.close(); } - } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 65a934304b..087d200115 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -242,7 +242,7 @@ public class TestXSSFWorkbook extends TestCase { public void testFindFont(){ //get dafault font and check against default value XSSFWorkbook workbook = new XSSFWorkbook(); - Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, Font.COLOR_NORMAL, (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE); + Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, IndexedColors.BLACK.getIndex(), (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE); assertNotNull(fontFind); //get default font, then change 2 values and check against different values (height changes) @@ -251,9 +251,8 @@ public class TestXSSFWorkbook extends TestCase { font.setUnderline(Font.U_DOUBLE); StylesSource styleSource=new StylesTable(); long index=styleSource.putFont(font); - System.out.println("index="+index); workbook.setStylesSource(styleSource); - fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, Font.COLOR_NORMAL, (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE); + fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE); assertNull(fontFind); } @@ -291,6 +290,7 @@ public class TestXSSFWorkbook extends TestCase { assertNotNull(fontAt); } + public void testGetNumberOfFonts(){ XSSFWorkbook wb = new XSSFWorkbook(); -- 2.39.5