source: trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FontTablePart.java @ 1378

Revision 1378, 4.2 KB checked in by jharrop, 17 months ago (diff)

Tune log levels involved in opening a docx (tuning for save TODO); other clean up.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1/*
2 *  Copyright 2007-2008, Plutext Pty Ltd.
3 *   
4 *  This file is part of docx4j.
5
6    docx4j is licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18
19 */
20
21package org.docx4j.openpackaging.parts.WordprocessingML;
22
23//import java.io.IOException;
24
25//import javax.xml.bind.JAXBElement;
26import java.io.IOException;
27
28import javax.xml.bind.JAXBException;
29import javax.xml.bind.Unmarshaller;
30
31import org.apache.log4j.Logger;
32import org.docx4j.openpackaging.exceptions.InvalidFormatException;
33import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
34import org.docx4j.openpackaging.parts.JaxbXmlPart;
35import org.docx4j.openpackaging.parts.PartName;
36import org.docx4j.openpackaging.parts.relationships.Namespaces;
37import org.docx4j.wml.FontRel;
38import org.docx4j.wml.Fonts;
39
40
41public final class FontTablePart extends JaxbXmlPart<Fonts> {
42       
43        private static Logger log = Logger.getLogger(FontTablePart.class);             
44       
45        public FontTablePart(PartName partName) throws InvalidFormatException {
46                super(partName);
47                init();         
48        }
49
50        public FontTablePart() throws InvalidFormatException {
51                super(new PartName("/word/fontTable.xml"));
52                init();         
53        }
54       
55        public void init() {
56                // Used if this Part is added to [Content_Types].xml
57                setContentType(new  org.docx4j.openpackaging.contenttype.ContentType( 
58                                org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_FONTTABLE));
59
60                // Used when this Part is added to a rels
61                setRelationshipType(Namespaces.FONT_TABLE);
62        }
63   
64    /**
65     * Unmarshal a default font table, useful when creating this
66     * part from scratch.
67     *
68     * @return the newly created root object of the java content tree
69     *
70     * @throws JAXBException
71     *     If any unexpected errors occur while unmarshalling
72     */
73    public Object unmarshalDefaultFonts() throws JAXBException {
74         
75                java.io.InputStream is = null;
76                        try {
77                                // Works in Eclipse - not absence of leading '/'
78                                is = org.docx4j.utils.ResourceUtils.getResource("org/docx4j/openpackaging/parts/WordprocessingML/fontTable.xml");
79                        } catch (IOException e) {
80                                // TODO Auto-generated catch block
81                                e.printStackTrace();
82                        }               
83       
84        return unmarshal( is );         
85    }
86   
87    public void processEmbeddings() {
88       
89        Fonts fonts = (org.docx4j.wml.Fonts)this.getJaxbElement();
90                for (Fonts.Font font : fonts.getFont() ) {
91                        String fontName =  font.getName();
92       
93                        FontRel embedRegular = font.getEmbedRegular();
94                        FontRel embedBold = font.getEmbedBold();
95                        FontRel embedBoldItalic = font.getEmbedBoldItalic();
96                        FontRel embedItalic = font.getEmbedItalic();
97                       
98                        getObfuscatedFontFromRelationship(fontName, embedRegular);
99                        getObfuscatedFontFromRelationship(fontName, embedBold);
100                        getObfuscatedFontFromRelationship(fontName, embedBoldItalic);
101                        getObfuscatedFontFromRelationship(fontName, embedItalic);
102       
103                }
104    }
105   
106    private void getObfuscatedFontFromRelationship(String fontName, FontRel fontRel) {
107   
108        if (fontRel == null) {
109                //log.debug("fontRel not found for '" + fontName + "'");
110                return;
111        }
112       
113        String id = fontRel.getId();           
114        String fontKey = fontRel.getFontKey();
115                 
116        ObfuscatedFontPart obfuscatedFont = (ObfuscatedFontPart)this.getRelationshipsPart().getPart(id);
117        if (obfuscatedFont != null) {
118                obfuscatedFont.deObfuscate(fontName, fontKey);
119        } else {
120                log.error("Couldn't find ObfuscatedFontPart with id: " + id);
121        }
122    }
123
124        public static void main(String[] args) throws Exception {
125                String filepath = System.getProperty("user.dir") + "/sample-docs/FontEmbedded.docx";           
126                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filepath));
127               
128                wordMLPackage.getMainDocumentPart().getFontTablePart().processEmbeddings();
129        }
130   
131   
132}
Note: See TracBrowser for help on using the repository browser.