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 <doctok/resourceids.hxx>
22 : #include <ooxml/resourceids.hxx>
23 : #include <vector>
24 : #include <osl/file.hxx>
25 : #include <stdio.h>
26 : #include <rtl/tencinfo.h>
27 : #include <vcl/embeddedfontshelper.hxx>
28 : #include <unotools/fontdefs.hxx>
29 :
30 : #include "dmapperLoggers.hxx"
31 :
32 : namespace writerfilter {
33 : namespace dmapper
34 : {
35 :
36 274 : struct FontTable_Impl
37 : {
38 : std::vector< FontEntry::Pointer_t > aFontEntries;
39 : FontEntry::Pointer_t pCurrentEntry;
40 274 : FontTable_Impl() {}
41 : };
42 :
43 274 : FontTable::FontTable()
44 : : LoggedProperties(dmapper_logger, "FontTable")
45 : , LoggedTable(dmapper_logger, "FontTable")
46 : , LoggedStream(dmapper_logger, "FontTable")
47 274 : , m_pImpl( new FontTable_Impl )
48 : {
49 274 : }
50 :
51 822 : FontTable::~FontTable()
52 : {
53 274 : delete m_pImpl;
54 548 : }
55 :
56 2621 : void FontTable::lcl_attribute(Id Name, Value & val)
57 : {
58 : OSL_ENSURE( m_pImpl->pCurrentEntry, "current entry has to be set here");
59 2621 : if(!m_pImpl->pCurrentEntry)
60 2621 : return ;
61 2621 : int nIntValue = val.getInt();
62 2621 : OUString sValue = val.getString();
63 2621 : switch(Name)
64 : {
65 : case NS_rtf::LN_CBFFNM1:
66 0 : m_pImpl->pCurrentEntry->sFontName1 = sValue;
67 0 : break;
68 : case NS_rtf::LN_PRQ:
69 0 : m_pImpl->pCurrentEntry->nPitchRequest = static_cast<sal_Int16>( nIntValue );
70 0 : break;
71 : case NS_rtf::LN_FTRUETYPE:
72 0 : m_pImpl->pCurrentEntry->bTrueType = nIntValue == 1 ? true : false;
73 0 : break;
74 : case NS_rtf::LN_UNUSED1_3: //unused
75 : case NS_rtf::LN_FF: //unused
76 : case NS_rtf::LN_UNUSED1_7: //unused
77 0 : break;
78 : case NS_rtf::LN_WWEIGHT:
79 0 : m_pImpl->pCurrentEntry->nBaseWeight = nIntValue;
80 0 : break;
81 : case NS_rtf::LN_CHS:
82 0 : m_pImpl->pCurrentEntry->nTextEncoding = nIntValue;
83 0 : break;
84 : case NS_rtf::LN_IXCHSZALT:
85 0 : break;
86 : case NS_rtf::LN_PANOSE:
87 0 : m_pImpl->pCurrentEntry->sPanose += sValue;
88 0 : break;
89 : case NS_rtf::LN_FS:
90 0 : m_pImpl->pCurrentEntry->sFontSignature += sValue;
91 0 : break;
92 : case NS_rtf::LN_F:
93 0 : break;
94 : case NS_rtf::LN_ALTFONTNAME:
95 0 : m_pImpl->pCurrentEntry->sAlternativeFont = sValue;
96 0 : break;
97 : case NS_rtf::LN_XSZFFN:
98 : case NS_ooxml::LN_CT_Font_name:
99 1574 : m_pImpl->pCurrentEntry->sFontName = sValue;
100 1574 : break;
101 : case NS_ooxml::LN_CT_Charset_val:
102 : // w:characterSet has higher priority, set only if that one is not set
103 864 : if( m_pImpl->pCurrentEntry->nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
104 : {
105 681 : m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromWindowsCharset( nIntValue );
106 681 : if( IsStarSymbol( m_pImpl->pCurrentEntry->sFontName ))
107 3 : m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
108 : }
109 864 : break;
110 : case NS_ooxml::LN_CT_Charset_characterSet:
111 : {
112 183 : OString tmp;
113 183 : sValue.convertToString( &tmp, RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
114 183 : m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromMimeCharset( tmp.getStr() );
115 : // Older LO versions used to write incorrect character set for OpenSymbol, fix.
116 183 : if( IsStarSymbol( m_pImpl->pCurrentEntry->sFontName ))
117 0 : m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
118 183 : break;
119 : }
120 : default:
121 : {
122 : //----> debug
123 0 : int nVal = val.getInt();
124 0 : ++nVal;
125 : //<---- debug
126 : }
127 2621 : }
128 : }
129 :
130 4493 : void FontTable::lcl_sprm(Sprm& rSprm)
131 : {
132 : OSL_ENSURE( m_pImpl->pCurrentEntry, "current entry has to be set here");
133 4493 : if(!m_pImpl->pCurrentEntry)
134 4493 : return ;
135 4493 : sal_uInt32 nSprmId = rSprm.getId();
136 :
137 4493 : Value::Pointer_t pValue = rSprm.getValue();
138 4493 : sal_Int32 nIntValue = pValue->getInt();
139 : (void)nIntValue;
140 4493 : switch(nSprmId)
141 : {
142 : case NS_ooxml::LN_CT_Font_charset:
143 864 : resolveSprm( rSprm );
144 864 : break;
145 : case NS_ooxml::LN_CT_Font_embedRegular:
146 : case NS_ooxml::LN_CT_Font_embedBold:
147 : case NS_ooxml::LN_CT_Font_embedItalic:
148 : case NS_ooxml::LN_CT_Font_embedBoldItalic:
149 : {
150 0 : writerfilter::Reference< Properties >::Pointer_t pProperties = rSprm.getProps();
151 0 : if( pProperties.get( ))
152 : {
153 0 : EmbeddedFontHandler handler( m_pImpl->pCurrentEntry->sFontName,
154 : nSprmId == NS_ooxml::LN_CT_Font_embedRegular ? ""
155 : : nSprmId == NS_ooxml::LN_CT_Font_embedBold ? "b"
156 : : nSprmId == NS_ooxml::LN_CT_Font_embedItalic ? "i"
157 0 : : nSprmId == NS_ooxml::LN_CT_Font_embedBoldItalic ? "bi" : "?" );
158 0 : pProperties->resolve( handler );
159 : }
160 0 : break;
161 : }
162 4493 : }
163 : }
164 :
165 864 : void FontTable::resolveSprm(Sprm & r_Sprm)
166 : {
167 864 : writerfilter::Reference<Properties>::Pointer_t pProperties = r_Sprm.getProps();
168 864 : if( pProperties.get())
169 864 : pProperties->resolve(*this);
170 864 : }
171 :
172 1574 : void FontTable::lcl_entry(int /*pos*/, writerfilter::Reference<Properties>::Pointer_t ref)
173 : {
174 : //create a new font entry
175 : OSL_ENSURE( !m_pImpl->pCurrentEntry, "current entry has to be NULL here");
176 1574 : m_pImpl->pCurrentEntry.reset(new FontEntry);
177 1574 : ref->resolve(*this);
178 : //append it to the table
179 1574 : m_pImpl->aFontEntries.push_back( m_pImpl->pCurrentEntry );
180 1574 : m_pImpl->pCurrentEntry.reset();
181 1574 : }
182 :
183 0 : void FontTable::lcl_startSectionGroup()
184 : {
185 0 : }
186 :
187 0 : void FontTable::lcl_endSectionGroup()
188 : {
189 0 : }
190 :
191 0 : void FontTable::lcl_startParagraphGroup()
192 : {
193 0 : }
194 :
195 0 : void FontTable::lcl_endParagraphGroup()
196 : {
197 0 : }
198 :
199 0 : void FontTable::lcl_startCharacterGroup()
200 : {
201 0 : }
202 :
203 0 : void FontTable::lcl_endCharacterGroup()
204 : {
205 0 : }
206 :
207 0 : void FontTable::lcl_text(const sal_uInt8*, size_t )
208 : {
209 0 : }
210 :
211 0 : void FontTable::lcl_utext(const sal_uInt8* , size_t)
212 : {
213 0 : }
214 :
215 0 : void FontTable::lcl_props(writerfilter::Reference<Properties>::Pointer_t)
216 : {
217 0 : }
218 :
219 0 : void FontTable::lcl_table(Id, writerfilter::Reference<Table>::Pointer_t)
220 : {
221 0 : }
222 :
223 0 : void FontTable::lcl_substream(Id, ::writerfilter::Reference<Stream>::Pointer_t)
224 : {
225 0 : }
226 :
227 0 : void FontTable::lcl_info(const string& )
228 : {
229 0 : }
230 :
231 0 : void FontTable::lcl_startShape( ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > )
232 : {
233 0 : }
234 :
235 0 : void FontTable::lcl_endShape( )
236 : {
237 0 : }
238 :
239 4878 : const FontEntry::Pointer_t FontTable::getFontEntry(sal_uInt32 nIndex)
240 : {
241 4878 : return (m_pImpl->aFontEntries.size() > nIndex)
242 4878 : ? m_pImpl->aFontEntries[nIndex]
243 9756 : : FontEntry::Pointer_t();
244 : }
245 :
246 5133 : sal_uInt32 FontTable::size()
247 : {
248 5133 : return m_pImpl->aFontEntries.size();
249 : }
250 :
251 0 : EmbeddedFontHandler::EmbeddedFontHandler( const OUString& _fontName, const char* _style )
252 : : LoggedProperties(dmapper_logger, "EmbeddedFontHandler")
253 : , fontName( _fontName )
254 0 : , style( _style )
255 : {
256 0 : }
257 :
258 0 : EmbeddedFontHandler::~EmbeddedFontHandler()
259 : {
260 0 : if( !inputStream.is())
261 0 : return;
262 0 : std::vector< unsigned char > key( 32 );
263 0 : if( !fontKey.isEmpty())
264 : { // key for unobfuscating
265 : // 1 3 5 7 10 2 5 7 20 2 5 7 9 1 3 5
266 : // {62E79491-959F-41E9-B76B-6B32631DEA5C}
267 : static const int pos[ 16 ] = { 35, 33, 31, 29, 27, 25, 22, 20, 17, 15, 12, 10, 7, 5, 3, 1 };
268 0 : for( int i = 0;
269 : i < 16;
270 : ++i )
271 : {
272 0 : int v1 = fontKey[ pos[ i ]];
273 0 : int v2 = fontKey[ pos[ i ] + 1 ];
274 : assert(( v1 >= '0' && v1 <= '9' ) || ( v1 >= 'A' && v1 <= 'F' ));
275 : assert(( v2 >= '0' && v2 <= '9' ) || ( v2 >= 'A' && v2 <= 'F' ));
276 0 : int val = ( v1 - ( v1 <= '9' ? '0' : 'A' - 10 )) * 16 + v2 - ( v2 <= '9' ? '0' : 'A' - 10 );
277 0 : key[ i ] = val;
278 0 : key[ i + 16 ] = val;
279 : }
280 : }
281 0 : EmbeddedFontsHelper::addEmbeddedFont( inputStream, fontName, style, key );
282 0 : inputStream->closeInput();
283 0 : }
284 :
285 0 : void EmbeddedFontHandler::lcl_attribute( Id name, Value& val )
286 : {
287 0 : OUString sValue = val.getString();
288 0 : switch( name )
289 : {
290 : case NS_ooxml::LN_CT_FontRel_fontKey:
291 0 : fontKey = sValue;
292 0 : break;
293 : case NS_ooxml::LN_CT_Rel_id:
294 0 : id = sValue;
295 0 : break;
296 : case NS_ooxml::LN_CT_FontRel_subsetted:
297 0 : break; // TODO? Let's just ignore this for now and hope
298 : // it doesn't break anything.
299 : case NS_ooxml::LN_inputstream: // the actual font data as stream
300 0 : val.getAny() >>= inputStream;
301 0 : break;
302 : default:
303 0 : break;
304 0 : }
305 0 : }
306 :
307 0 : void EmbeddedFontHandler::lcl_sprm( Sprm& )
308 : {
309 0 : }
310 :
311 :
312 : }//namespace dmapper
313 24 : }//namespace writerfilter
314 :
315 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|