--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.hslf.model;\r
+\r
+import org.apache.poi.hslf.record.FontEntityAtom;\r
+\r
+/**\r
+ * Represents a Font used in a presenation.\r
+ * <p>\r
+ * In PowerPoint Font is a shared resource and can be shared among text object in the presentation.\r
+ * </p>\r
+ * Some commonly used fonts are predefined in static constants. \r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class PPFont {\r
+ /**\r
+ * ANSI character set\r
+ */\r
+ public final static byte ANSI_CHARSET = 0;\r
+\r
+ /**\r
+ * Default character set.\r
+ */\r
+ public final static byte DEFAULT_CHARSET = 1;\r
+\r
+ /**\r
+ * Symbol character set\r
+ */\r
+ public final static byte SYMBOL_CHARSET = 2;\r
+\r
+\r
+ /**\r
+ * Constants for the pitch and family of the font.\r
+ * The two low-order bits specify the pitch of the font and can be one of the following values\r
+ */\r
+ public final static byte DEFAULT_PITCH = 0;\r
+ public final static byte FIXED_PITCH = 1;\r
+ public final static byte VARIABLE_PITCH = 2;\r
+\r
+ /**\r
+ * Don't care or don't know.\r
+ */\r
+ public final static byte FF_DONTCARE = 0;\r
+ /**\r
+ * Fonts with variable stroke width (proportional) and with serifs. Times New Roman is an example.\r
+ */\r
+ public final static byte FF_ROMAN = 16;\r
+ /**\r
+ * Fonts with variable stroke width (proportional) and without serifs. Arial is an example.\r
+ */\r
+ public final static byte FF_SWISS = 32;\r
+ /**\r
+ * Fonts designed to look like handwriting. Script and Cursive are examples.\r
+ */\r
+ public final static byte FF_SCRIPT = 64;\r
+ /**\r
+ * Fonts with constant stroke width (monospace), with or without serifs.\r
+ * Monospace fonts are usually modern. CourierNew is an example\r
+ */\r
+ public final static byte FF_MODERN = 48;\r
+ /**\r
+ * Novelty fonts. Old English is an example\r
+ */\r
+ public final static byte FF_DECORATIVE = 80;\r
+\r
+\r
+ protected int charset;\r
+ protected int type;\r
+ protected int flags;\r
+ protected int pitch;\r
+ protected String name;\r
+\r
+ /**\r
+ * Creates a new instance of PPFont\r
+ */\r
+ public PPFont(){\r
+\r
+ }\r
+\r
+ /**\r
+ * Creates a new instance of PPFont and initialize it from the supplied font atom\r
+ */\r
+ public PPFont(FontEntityAtom fontAtom){\r
+ name = fontAtom.getFontName();\r
+ charset = fontAtom.getCharSet();\r
+ type = fontAtom.getFontType();\r
+ flags = fontAtom.getFontFlags();\r
+ pitch = fontAtom.getPitchAndFamily();\r
+ }\r
+\r
+ /**\r
+ * set the name for the font (i.e. Arial)\r
+ *\r
+ * @param val String representing the name of the font to use\r
+ */\r
+ public void setFontName(String val){\r
+ name = val;\r
+ }\r
+\r
+ /**\r
+ * get the name for the font (i.e. Arial)\r
+ *\r
+ * @return String representing the name of the font to use\r
+ */\r
+ public String getFontName(){\r
+ return name;\r
+ }\r
+\r
+ /**\r
+ * set the character set\r
+ *\r
+ * @param val - characterset\r
+ */\r
+ public void setCharSet(int val){\r
+ charset = val;\r
+ }\r
+\r
+ /**\r
+ * get the character set\r
+ *\r
+ * @return charset - characterset\r
+ */\r
+ public int getCharSet(){\r
+ return charset;\r
+ }\r
+\r
+ /**\r
+ * set the font flags\r
+ * Bit 1: If set, font is subsetted\r
+ *\r
+ * @param val - the font flags\r
+ */\r
+ public void setFontFlags(int val){\r
+ flags = val;\r
+ }\r
+\r
+ /**\r
+ * get the character set\r
+ * Bit 1: If set, font is subsetted\r
+ *\r
+ * @return the font flags\r
+ */\r
+ public int getFontFlags(){\r
+ return flags;\r
+ }\r
+\r
+ /**\r
+ * set the font type\r
+ * <p>\r
+ * Bit 1: Raster Font\r
+ * Bit 2: Device Font\r
+ * Bit 3: TrueType Font\r
+ * </p>\r
+ *\r
+ * @param val - the font type\r
+ */\r
+ public void setFontType(int val){\r
+ type = val;\r
+ }\r
+\r
+ /**\r
+ * get the font type\r
+ * <p>\r
+ * Bit 1: Raster Font\r
+ * Bit 2: Device Font\r
+ * Bit 3: TrueType Font\r
+ * </p>\r
+ *\r
+ * @return the font type\r
+ */\r
+ public int getFontType(){\r
+ return type;\r
+ }\r
+\r
+ /**\r
+ * set lfPitchAndFamily\r
+ *\r
+ *\r
+ * @param val - Corresponds to the lfPitchAndFamily field of the Win32 API LOGFONT structure\r
+ */\r
+ public void setPitchAndFamily(int val){\r
+ pitch = val;\r
+ }\r
+\r
+ /**\r
+ * get lfPitchAndFamily\r
+ *\r
+ * @return corresponds to the lfPitchAndFamily field of the Win32 API LOGFONT structure\r
+ */\r
+ public int getPitchAndFamily(){\r
+ return pitch;\r
+ }\r
+\r
+ public static final PPFont ARIAL;\r
+ public static final PPFont TIMES_NEW_ROMAN ;\r
+ public static final PPFont COURIER_NEW;\r
+ public static final PPFont WINGDINGS;\r
+ static {\r
+ ARIAL = new PPFont();\r
+ ARIAL.setFontName("Arial");\r
+ ARIAL.setCharSet(ANSI_CHARSET);\r
+ ARIAL.setFontType(4);\r
+ ARIAL.setFontFlags(0);\r
+ ARIAL.setPitchAndFamily(VARIABLE_PITCH | FF_SWISS);\r
+\r
+ TIMES_NEW_ROMAN = new PPFont();\r
+ TIMES_NEW_ROMAN.setFontName("Times New Roman");\r
+ TIMES_NEW_ROMAN.setCharSet(ANSI_CHARSET);\r
+ TIMES_NEW_ROMAN.setFontType(4);\r
+ TIMES_NEW_ROMAN.setFontFlags(0);\r
+ TIMES_NEW_ROMAN.setPitchAndFamily(VARIABLE_PITCH | FF_ROMAN);\r
+\r
+ COURIER_NEW = new PPFont();\r
+ COURIER_NEW.setFontName("Courier New");\r
+ COURIER_NEW.setCharSet(ANSI_CHARSET);\r
+ COURIER_NEW.setFontType(4);\r
+ COURIER_NEW.setFontFlags(0);\r
+ COURIER_NEW.setPitchAndFamily(FIXED_PITCH | FF_MODERN);\r
+\r
+ WINGDINGS = new PPFont();\r
+ WINGDINGS.setFontName("Wingdings");\r
+ WINGDINGS.setCharSet(SYMBOL_CHARSET);\r
+ WINGDINGS.setFontType(4);\r
+ WINGDINGS.setFontFlags(0);\r
+ WINGDINGS.setPitchAndFamily(VARIABLE_PITCH | FF_DONTCARE);\r
+ }\r
+}\r
* @return zero based index of the font in the collection
*/
public int addFont(String name) {
- for (int i = 0; i < fonts.size(); i++) {
- if(fonts.get(i).equals(name)){
- //if the font is already present return its index
- return i;
- }
- }
+ int idx = getFontIndex(name);
+ if(idx != -1) return idx;
+ return addFont(name, 0, 0, 4, 34);
+ }
+
+ public int addFont(String name, int charset, int flags, int type, int pitch) {
FontEntityAtom fnt = new FontEntityAtom();
fnt.setFontIndex(fonts.size() << 4);
fnt.setFontName(name);
+ fnt.setCharSet(charset);
+ fnt.setFontFlags(flags);
+ fnt.setFontType(type);
+ fnt.setPitchAndFamily(pitch);
fonts.add(name);
// Append new child to the end
return fonts.size()-1; //the added font is the last in the list
}
-
- /**
+
+ /**
+ * @return zero based index of the font in the collection or -1 if not found
+ */
+ public int getFontIndex(String name) {
+ for (int i = 0; i < fonts.size(); i++) {
+ if(fonts.get(i).equals(name)){
+ //if the font is already present return its index
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public int getNumberOfFonts() {
+ return fonts.size();
+ }
+
+ /**
* Get the name of the font at the given ID, or null if there is
* no font at that ID.
* @param id
/**
* Create a new instance of <code>FontEntityAtom</code>
*/
- protected FontEntityAtom() {
+ public FontEntityAtom() {
_recdata = new byte[68];
_header = new byte[8];
}
}
- protected void setFontIndex(int idx){
+ public void setFontIndex(int idx){
LittleEndian.putShort(_header, 0, (short)idx);
}
- protected int getFontIndex(){
- return LittleEndian.getShort(_header, 0);
+ public int getFontIndex(){
+ return LittleEndian.getShort(_header, 0) >> 4;
}
- /**
+ /**
+ * set the character set
+ *
+ * @param charset - characterset
+ */
+ public void setCharSet(int charset){
+ _recdata[64] = (byte)charset;
+ }
+
+ /**
+ * get the character set
+ *
+ * @return charset - characterset
+ */
+ public int getCharSet(){
+ return _recdata[64];
+ }
+
+ /**
+ * set the font flags
+ * Bit 1: If set, font is subsetted
+ *
+ * @param flags - the font flags
+ */
+ public void setFontFlags(int flags){
+ _recdata[65] = (byte)flags;
+ }
+
+ /**
+ * get the character set
+ * Bit 1: If set, font is subsetted
+ *
+ * @return the font flags
+ */
+ public int getFontFlags(){
+ return _recdata[65];
+ }
+
+ /**
+ * set the font type
+ * <p>
+ * Bit 1: Raster Font
+ * Bit 2: Device Font
+ * Bit 3: TrueType Font
+ * </p>
+ *
+ * @param type - the font type
+ */
+ public void setFontType(int type){
+ _recdata[66] = (byte)type;
+ }
+
+ /**
+ * get the font type
+ * <p>
+ * Bit 1: Raster Font
+ * Bit 2: Device Font
+ * Bit 3: TrueType Font
+ * </p>
+ *
+ * @return the font type
+ */
+ public int getFontType(){
+ return _recdata[66];
+ }
+
+ /**
+ * set lfPitchAndFamily
+ *
+ *
+ * @param val - Corresponds to the lfPitchAndFamily field of the Win32 API LOGFONT structure
+ */
+ public void setPitchAndFamily(int val){
+ _recdata[67] = (byte)val;
+ }
+
+ /**
+ * get lfPitchAndFamily
+ *
+ * @return corresponds to the lfPitchAndFamily field of the Win32 API LOGFONT structure
+ */
+ public int getPitchAndFamily(){
+ return _recdata[67];
+ }
+
+ /**
* Write the contents of the record back, so it can be written to disk
*/
public void writeOut(OutputStream out) throws IOException {
}
return addPicture(data, format);
}
+
+ /**
+ * Add a font in this presentation
+ *
+ * @param font the font to add
+ * @return 0-based index of the font
+ */
+ public int addFont(PPFont font) {
+ FontCollection fonts = getDocumentRecord().getEnvironment().getFontCollection();
+ int idx = fonts.getFontIndex(font.getFontName());
+ if(idx == -1){
+ idx = fonts.addFont(font.getFontName(), font.getCharSet(), font.getFontFlags(), font.getFontType(), font.getPitchAndFamily());
+ }
+ return idx;
+ }
+
+ /**
+ * Get a font by index
+ *
+ * @param idx 0-based index of the font
+ * @return of an instance of <code>PPFont</code> or <code>null</code> if not found
+ */
+ public PPFont getFont(int idx) {
+ PPFont font = null;
+ FontCollection fonts = getDocumentRecord().getEnvironment().getFontCollection();
+ Record[] ch = fonts.getChildRecords();
+ for (int i = 0; i < ch.length; i++) {
+ if(ch[i] instanceof FontEntityAtom) {
+ FontEntityAtom atom = (FontEntityAtom)ch[i];
+ if(atom.getFontIndex() == idx){
+ font = new PPFont(atom);
+ break;
+ }
+ }
+ }
+ return font;
+ }
+
+ /**
+ * get the number of fonts in the presentation
+ *
+ * @return number of fonts
+ */
+ public int getNumberOfFonts() {
+ return getDocumentRecord().getEnvironment().getFontCollection().getNumberOfFonts();
+ }
}
--- /dev/null
+\r
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.hslf.model;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Test adding fonts to the presenataion resources\r
+ * \r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestPPFont extends TestCase{\r
+\r
+ public void testCreate() throws IOException {\r
+ SlideShow ppt = new SlideShow();\r
+ assertEquals(1, ppt.getNumberOfFonts());\r
+ assertEquals("Arial", ppt.getFont(0).getFontName());\r
+\r
+ //adding the same font twice\r
+ assertEquals(0, ppt.addFont(PPFont.ARIAL));\r
+ assertEquals(1, ppt.getNumberOfFonts());\r
+\r
+ assertEquals(1, ppt.addFont(PPFont.TIMES_NEW_ROMAN));\r
+ assertEquals(2, ppt.addFont(PPFont.COURIER_NEW));\r
+ assertEquals(3, ppt.addFont(PPFont.WINGDINGS));\r
+\r
+ assertEquals(4, ppt.getNumberOfFonts());\r
+\r
+ assertEquals(PPFont.TIMES_NEW_ROMAN.getFontName(), ppt.getFont(1).getFontName());\r
+ assertEquals(PPFont.COURIER_NEW.getFontName(), ppt.getFont(2).getFontName());\r
+\r
+ PPFont font3 = ppt.getFont(3);\r
+ assertEquals(PPFont.WINGDINGS.getFontName(), font3.getFontName());\r
+ assertEquals(PPFont.SYMBOL_CHARSET, font3.getCharSet());\r
+ assertEquals(PPFont.VARIABLE_PITCH, font3.getPitchAndFamily());\r
+ }\r
+}\r