]> source.dussan.org Git - poi.git/commitdiff
Support for cloning one font record onto another, plus tests
authorNick Burch <nick@apache.org>
Sat, 12 Jul 2008 16:56:55 +0000 (16:56 +0000)
committerNick Burch <nick@apache.org>
Sat, 12 Jul 2008 16:56:55 +0000 (16:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@676201 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/FontRecord.java
src/testcases/org/apache/poi/hssf/record/AllRecordTests.java
src/testcases/org/apache/poi/hssf/record/TestFontRecord.java [new file with mode: 0644]

index 42e058f4724147f2faaaf15bc6dfdb6aa2e7c4f6..d6a5ce859fa721e6a1a258ffd915474bad1c34a1 100644 (file)
@@ -531,6 +531,8 @@ public class FontRecord
 
     public int getRecordSize()
     {
+       // Note - no matter the original, we always
+       //  re-serialise the font name as unicode
         return (getFontNameLength() * 2) + 20;
     }
 
@@ -538,6 +540,25 @@ public class FontRecord
     {
         return sid;
     }
+    
+    /**
+     * Clones all the font style information from another
+     *  FontRecord, onto this one. This 
+     *  will then hold all the same font style options.
+     */
+    public void cloneStyleFrom(FontRecord source) {
+        field_1_font_height         = source.field_1_font_height; 
+        field_2_attributes          = source.field_2_attributes;
+        field_3_color_palette_index = source.field_3_color_palette_index;
+        field_4_bold_weight         = source.field_4_bold_weight;
+        field_5_super_sub_script    = source.field_5_super_sub_script;
+        field_6_underline           = source.field_6_underline;
+        field_7_family              = source.field_7_family;
+        field_8_charset             = source.field_8_charset;
+        field_9_zero                = source.field_9_zero;
+        field_10_font_name_len      = source.field_10_font_name_len;
+        field_11_font_name          = source.field_11_font_name;
+    }
 
        public int hashCode() {
                final int prime = 31;
index 988be1dac5ef7303ff03314764819ba6c4910a68..e8d1cdf4430beff37a27c5b7b8ce73763e70bb47 100755 (executable)
@@ -62,6 +62,7 @@ public final class AllRecordTests {
                result.addTestSuite(TestEndSubRecord.class);
                result.addTestSuite(TestEscherAggregate.class);
                result.addTestSuite(TestExternalNameRecord.class);
+               result.addTestSuite(TestFontRecord.class);
                result.addTestSuite(TestFontBasisRecord.class);
                result.addTestSuite(TestFontIndexRecord.class);
                result.addTestSuite(TestFormulaRecord.class);
diff --git a/src/testcases/org/apache/poi/hssf/record/TestFontRecord.java b/src/testcases/org/apache/poi/hssf/record/TestFontRecord.java
new file mode 100644 (file)
index 0000000..6485f58
--- /dev/null
@@ -0,0 +1,124 @@
+
+/* ====================================================================
+   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.hssf.record;
+
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the serialization and deserialization of the FontRecord
+ * class works correctly.  Test data taken directly from a real
+ * Excel file.
+ */
+public class TestFontRecord
+        extends TestCase
+{
+       byte[] header = new byte[] {
+               0x31, 00, 0x1a, 00, // sid=31, 26 bytes long
+       };
+    byte[] data = new byte[] {
+               0xC8-256, 00,       // font height = xc8
+               00, 00,             // attrs = 0 
+               0xFF-256, 0x7F,     // colour palette = x7fff 
+               0x90-256, 0x01,     // bold weight = x190
+               00, 00,  // supersubscript
+               00, 00,  // underline, family
+               00, 00,  // charset, padding
+               05, 01,  // name length, unicode flag
+               0x41, 0x00, 0x72, 0x00, 0x69, // Arial, as unicode 
+               0x00, 0x61, 0x00, 0x6C, 0x00
+    };
+
+    public TestFontRecord(String name)
+    {
+        super(name);
+    }
+
+    public void testLoad()
+            throws Exception
+    {
+
+        FontRecord record = new FontRecord(new TestcaseRecordInputStream((short)0x31, (short)data.length, data));
+        assertEquals( 0xc8, record.getFontHeight());
+        assertEquals( 0x00, record.getAttributes());
+        assertFalse( record.isItalic());
+        assertFalse( record.isStruckout());
+        assertFalse( record.isMacoutlined());
+        assertFalse( record.isMacshadowed());
+        assertEquals( 0x7fff, record.getColorPaletteIndex());
+        assertEquals( 0x190, record.getBoldWeight());
+        assertEquals( 0x00, record.getSuperSubScript());
+        assertEquals( 0x00, record.getUnderline());
+        assertEquals( 0x00, record.getFamily());
+        assertEquals( 0x00, record.getCharset());
+        assertEquals( 0x05, record.getFontNameLength());
+        assertEquals( "Arial", record.getFontName());
+
+
+        assertEquals( 26 + 4, record.getRecordSize() );
+        record.validateSid((short)0x31);
+    }
+
+    public void testStore()
+    {
+//      .fontheight      = c8
+//      .attributes      = 0
+//           .italic     = false
+//           .strikout   = false
+//           .macoutlined= false
+//           .macshadowed= false
+//      .colorpalette    = 7fff
+//      .boldweight      = 190
+//      .supersubscript  = 0
+//      .underline       = 0
+//      .family          = 0
+//      .charset         = 0
+//      .namelength      = 5
+//      .fontname        = Arial
+
+        FontRecord record = new FontRecord();
+        record.setFontHeight((short)0xc8);
+        record.setAttributes((short)0);
+        record.setColorPaletteIndex((short)0x7fff);
+        record.setBoldWeight((short)0x190);
+        record.setSuperSubScript((short)0);
+        record.setUnderline((byte)0);
+        record.setFamily((byte)0);
+        record.setCharset((byte)0);
+        record.setFontNameLength((byte)5);
+        record.setFontName("Arial");
+
+        byte [] recordBytes = record.serialize();
+        assertEquals(recordBytes.length - 4, data.length);
+        for (int i = 0; i < data.length; i++)
+            assertEquals("At offset " + i, data[i], recordBytes[i+4]);
+    }
+    
+    public void testCloneOnto() throws Exception {
+        FontRecord base = new FontRecord(new TestcaseRecordInputStream((short)0x31, (short)data.length, data));
+       
+        FontRecord other = new FontRecord();
+        other.cloneStyleFrom(base);
+
+        byte [] recordBytes = other.serialize();
+        assertEquals(recordBytes.length - 4, data.length);
+        for (int i = 0; i < data.length; i++)
+            assertEquals("At offset " + i, data[i], recordBytes[i+4]);
+    }
+}