Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <FontTable.hxx>
21 : #include <ooxml/resourceids.hxx>
22 : #include <vector>
23 : #include <osl/file.hxx>
24 : #include <stdio.h>
25 : #include <rtl/tencinfo.h>
26 : #include <vcl/embeddedfontshelper.hxx>
27 : #include <unotools/fontdefs.hxx>
28 : #include <com/sun/star/awt/FontPitch.hpp>
29 :
30 : #include "dmapperLoggers.hxx"
31 :
32 : namespace writerfilter {
33 : namespace dmapper
34 : {
35 :
36 1018 : struct FontTable_Impl
37 : {
38 : std::vector< FontEntry::Pointer_t > aFontEntries;
39 : FontEntry::Pointer_t pCurrentEntry;
40 1018 : FontTable_Impl() {}
41 : };
42 :
43 1018 : FontTable::FontTable()
44 : : LoggedProperties(dmapper_logger, "FontTable")
45 : , LoggedTable(dmapper_logger, "FontTable")
46 : , LoggedStream(dmapper_logger, "FontTable")
47 1018 : , m_pImpl( new FontTable_Impl )
48 : {
49 1018 : }
50 :
51 3054 : FontTable::~FontTable()
52 : {
53 1018 : delete m_pImpl;
54 2036 : }
55 :
56 19738 : void FontTable::lcl_attribute(Id Name, Value & val)
57 : {
58 : OSL_ENSURE( m_pImpl->pCurrentEntry, "current entry has to be set here");
59 19738 : if(!m_pImpl->pCurrentEntry)
60 19738 : return ;
61 19738 : int nIntValue = val.getInt();
62 19738 : OUString sValue = val.getString();
63 19738 : switch(Name)
64 : {
65 : case NS_ooxml::LN_CT_Pitch_val:
66 6247 : if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_fixed)
67 310 : m_pImpl->pCurrentEntry->nPitchRequest = awt::FontPitch::FIXED;
68 5937 : else if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_variable)
69 5720 : m_pImpl->pCurrentEntry->nPitchRequest = awt::FontPitch::VARIABLE;
70 217 : else if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_default)
71 217 : m_pImpl->pCurrentEntry->nPitchRequest = awt::FontPitch::DONTKNOW;
72 : else
73 : SAL_WARN("writerfilter", "FontTable::lcl_attribute: unhandled NS_ooxml::CT_Pitch_val: " << nIntValue);
74 6247 : break;
75 : case NS_ooxml::LN_CT_Font_name:
76 6278 : m_pImpl->pCurrentEntry->sFontName = sValue;
77 6278 : break;
78 : case NS_ooxml::LN_CT_Charset_val:
79 : // w:characterSet has higher priority, set only if that one is not set
80 5452 : if( m_pImpl->pCurrentEntry->nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
81 : {
82 3702 : m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromWindowsCharset( nIntValue );
83 3702 : if( IsStarSymbol( m_pImpl->pCurrentEntry->sFontName ))
84 4 : m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
85 : }
86 5452 : break;
87 : case NS_ooxml::LN_CT_Charset_characterSet:
88 : {
89 1761 : OString tmp;
90 1761 : sValue.convertToString( &tmp, RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
91 1761 : m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromMimeCharset( tmp.getStr() );
92 : // Older LO versions used to write incorrect character set for OpenSymbol, fix.
93 1761 : if( IsStarSymbol( m_pImpl->pCurrentEntry->sFontName ))
94 0 : m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
95 1761 : break;
96 : }
97 : default:
98 : {
99 : //----> debug
100 0 : int nVal = val.getInt();
101 0 : ++nVal;
102 : //<---- debug
103 : }
104 19738 : }
105 : }
106 :
107 24541 : void FontTable::lcl_sprm(Sprm& rSprm)
108 : {
109 : OSL_ENSURE( m_pImpl->pCurrentEntry, "current entry has to be set here");
110 24541 : if(!m_pImpl->pCurrentEntry)
111 24541 : return ;
112 24541 : sal_uInt32 nSprmId = rSprm.getId();
113 :
114 24541 : Value::Pointer_t pValue = rSprm.getValue();
115 24541 : sal_Int32 nIntValue = pValue->getInt();
116 : (void)nIntValue;
117 24541 : switch(nSprmId)
118 : {
119 : case NS_ooxml::LN_CT_Font_charset:
120 : case NS_ooxml::LN_CT_Font_pitch:
121 11710 : resolveSprm( rSprm );
122 11710 : break;
123 : case NS_ooxml::LN_CT_Font_embedRegular:
124 : case NS_ooxml::LN_CT_Font_embedBold:
125 : case NS_ooxml::LN_CT_Font_embedItalic:
126 : case NS_ooxml::LN_CT_Font_embedBoldItalic:
127 : {
128 0 : writerfilter::Reference< Properties >::Pointer_t pProperties = rSprm.getProps();
129 0 : if( pProperties.get( ))
130 : {
131 0 : EmbeddedFontHandler handler( m_pImpl->pCurrentEntry->sFontName,
132 0 : nSprmId == NS_ooxml::LN_CT_Font_embedRegular ? ""
133 0 : : nSprmId == NS_ooxml::LN_CT_Font_embedBold ? "b"
134 0 : : nSprmId == NS_ooxml::LN_CT_Font_embedItalic ? "i"
135 0 : : nSprmId == NS_ooxml::LN_CT_Font_embedBoldItalic ? "bi" : "?" );
136 0 : pProperties->resolve( handler );
137 : }
138 0 : break;
139 : }
140 : case NS_ooxml::LN_CT_Font_altName:
141 1121 : break;
142 : case NS_ooxml::LN_CT_Font_panose1:
143 2958 : break;
144 : case NS_ooxml::LN_CT_Font_family:
145 5463 : break;
146 : case NS_ooxml::LN_CT_Font_sig:
147 3000 : break;
148 : default:
149 : SAL_WARN("writerfilter", "FontTable::lcl_sprm: unhandled token: " << nSprmId);
150 289 : break;
151 24541 : }
152 : }
153 :
154 11710 : void FontTable::resolveSprm(Sprm & r_Sprm)
155 : {
156 11710 : writerfilter::Reference<Properties>::Pointer_t pProperties = r_Sprm.getProps();
157 11710 : if( pProperties.get())
158 11710 : pProperties->resolve(*this);
159 11710 : }
160 :
161 6278 : void FontTable::lcl_entry(int /*pos*/, writerfilter::Reference<Properties>::Pointer_t ref)
162 : {
163 : //create a new font entry
164 : OSL_ENSURE( !m_pImpl->pCurrentEntry, "current entry has to be NULL here");
165 6278 : m_pImpl->pCurrentEntry.reset(new FontEntry);
166 6278 : ref->resolve(*this);
167 : //append it to the table
168 6278 : m_pImpl->aFontEntries.push_back( m_pImpl->pCurrentEntry );
169 6278 : m_pImpl->pCurrentEntry.reset();
170 6278 : }
171 :
172 0 : void FontTable::lcl_startSectionGroup()
173 : {
174 0 : }
175 :
176 0 : void FontTable::lcl_endSectionGroup()
177 : {
178 0 : }
179 :
180 0 : void FontTable::lcl_startParagraphGroup()
181 : {
182 0 : }
183 :
184 0 : void FontTable::lcl_endParagraphGroup()
185 : {
186 0 : }
187 :
188 0 : void FontTable::lcl_startCharacterGroup()
189 : {
190 0 : }
191 :
192 0 : void FontTable::lcl_endCharacterGroup()
193 : {
194 0 : }
195 :
196 0 : void FontTable::lcl_text(const sal_uInt8*, size_t )
197 : {
198 0 : }
199 :
200 0 : void FontTable::lcl_utext(const sal_uInt8* , size_t)
201 : {
202 0 : }
203 :
204 0 : void FontTable::lcl_props(writerfilter::Reference<Properties>::Pointer_t)
205 : {
206 0 : }
207 :
208 0 : void FontTable::lcl_table(Id, writerfilter::Reference<Table>::Pointer_t)
209 : {
210 0 : }
211 :
212 0 : void FontTable::lcl_substream(Id, ::writerfilter::Reference<Stream>::Pointer_t)
213 : {
214 0 : }
215 :
216 0 : void FontTable::lcl_info(const string& )
217 : {
218 0 : }
219 :
220 0 : void FontTable::lcl_startShape( ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > )
221 : {
222 0 : }
223 :
224 0 : void FontTable::lcl_endShape( )
225 : {
226 0 : }
227 :
228 168 : const FontEntry::Pointer_t FontTable::getFontEntry(sal_uInt32 nIndex)
229 : {
230 168 : return (m_pImpl->aFontEntries.size() > nIndex)
231 168 : ? m_pImpl->aFontEntries[nIndex]
232 336 : : FontEntry::Pointer_t();
233 : }
234 :
235 1510 : sal_uInt32 FontTable::size()
236 : {
237 1510 : return m_pImpl->aFontEntries.size();
238 : }
239 :
240 0 : EmbeddedFontHandler::EmbeddedFontHandler( const OUString& _fontName, const char* _style )
241 : : LoggedProperties(dmapper_logger, "EmbeddedFontHandler")
242 : , fontName( _fontName )
243 0 : , style( _style )
244 : {
245 0 : }
246 :
247 0 : EmbeddedFontHandler::~EmbeddedFontHandler()
248 : {
249 0 : if( !inputStream.is())
250 0 : return;
251 0 : std::vector< unsigned char > key( 32 );
252 0 : if( !fontKey.isEmpty())
253 : { // key for unobfuscating
254 : // 1 3 5 7 10 2 5 7 20 2 5 7 9 1 3 5
255 : // {62E79491-959F-41E9-B76B-6B32631DEA5C}
256 : static const int pos[ 16 ] = { 35, 33, 31, 29, 27, 25, 22, 20, 17, 15, 12, 10, 7, 5, 3, 1 };
257 0 : for( int i = 0;
258 : i < 16;
259 : ++i )
260 : {
261 0 : int v1 = fontKey[ pos[ i ]];
262 0 : int v2 = fontKey[ pos[ i ] + 1 ];
263 : assert(( v1 >= '0' && v1 <= '9' ) || ( v1 >= 'A' && v1 <= 'F' ));
264 : assert(( v2 >= '0' && v2 <= '9' ) || ( v2 >= 'A' && v2 <= 'F' ));
265 0 : int val = ( v1 - ( v1 <= '9' ? '0' : 'A' - 10 )) * 16 + v2 - ( v2 <= '9' ? '0' : 'A' - 10 );
266 0 : key[ i ] = val;
267 0 : key[ i + 16 ] = val;
268 : }
269 : }
270 0 : EmbeddedFontsHelper::addEmbeddedFont( inputStream, fontName, style, key );
271 0 : inputStream->closeInput();
272 0 : }
273 :
274 0 : void EmbeddedFontHandler::lcl_attribute( Id name, Value& val )
275 : {
276 0 : OUString sValue = val.getString();
277 0 : switch( name )
278 : {
279 : case NS_ooxml::LN_CT_FontRel_fontKey:
280 0 : fontKey = sValue;
281 0 : break;
282 : case NS_ooxml::LN_CT_Rel_id:
283 0 : id = sValue;
284 0 : break;
285 : case NS_ooxml::LN_CT_FontRel_subsetted:
286 0 : break; // TODO? Let's just ignore this for now and hope
287 : // it doesn't break anything.
288 : case NS_ooxml::LN_inputstream: // the actual font data as stream
289 0 : val.getAny() >>= inputStream;
290 0 : break;
291 : default:
292 0 : break;
293 0 : }
294 0 : }
295 :
296 0 : void EmbeddedFontHandler::lcl_sprm( Sprm& )
297 : {
298 0 : }
299 :
300 :
301 : }//namespace dmapper
302 : }//namespace writerfilter
303 :
304 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|