lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / writerfilter / source / dmapper / FontTable.cxx
blob1d3de2c1779e1f5f603fb577ee5e76185823a8f8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "FontTable.hxx"
21 #include <ooxml/resourceids.hxx>
22 #include <vector>
23 #include <sal/log.hxx>
24 #include <osl/file.hxx>
25 #include <rtl/tencinfo.h>
26 #include <vcl/embeddedfontshelper.hxx>
27 #include <unotools/fontdefs.hxx>
28 #include <com/sun/star/awt/FontPitch.hpp>
30 using namespace com::sun::star;
32 namespace writerfilter {
33 namespace dmapper
36 struct FontTable_Impl
38 std::vector< FontEntry::Pointer_t > aFontEntries;
39 FontEntry::Pointer_t pCurrentEntry;
40 FontTable_Impl() {}
43 FontTable::FontTable()
44 : LoggedProperties("FontTable")
45 , LoggedTable("FontTable")
46 , LoggedStream("FontTable")
47 , m_pImpl( new FontTable_Impl )
51 FontTable::~FontTable()
55 void FontTable::lcl_attribute(Id Name, Value & val)
57 SAL_WARN_IF( !m_pImpl->pCurrentEntry, "writerfilter.dmapper", "current entry has to be set here" );
58 if(!m_pImpl->pCurrentEntry)
59 return ;
60 int nIntValue = val.getInt();
61 OUString sValue = val.getString();
62 switch(Name)
64 case NS_ooxml::LN_CT_Pitch_val:
65 if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_fixed)
67 else if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_variable)
69 else if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_default)
71 else
72 SAL_WARN("writerfilter.dmapper", "FontTable::lcl_attribute: unhandled NS_ooxml::CT_Pitch_val: " << nIntValue);
73 break;
74 case NS_ooxml::LN_CT_Font_name:
75 m_pImpl->pCurrentEntry->sFontName = sValue;
76 break;
77 case NS_ooxml::LN_CT_Charset_val:
78 // w:characterSet has higher priority, set only if that one is not set
79 if( m_pImpl->pCurrentEntry->nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
81 m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromWindowsCharset( nIntValue );
82 if( IsStarSymbol( m_pImpl->pCurrentEntry->sFontName ))
83 m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
85 break;
86 case NS_ooxml::LN_CT_Charset_characterSet:
88 OString tmp;
89 sValue.convertToString( &tmp, RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
90 m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromMimeCharset( tmp.getStr() );
91 // Older LO versions used to write incorrect character set for OpenSymbol, fix.
92 if( IsStarSymbol( m_pImpl->pCurrentEntry->sFontName ))
93 m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
94 break;
96 default:
98 //----> debug
99 int nVal = val.getInt();
100 ++nVal;
101 //<---- debug
106 void FontTable::lcl_sprm(Sprm& rSprm)
108 SAL_WARN_IF( !m_pImpl->pCurrentEntry, "writerfilter.dmapper", "current entry has to be set here" );
109 if(!m_pImpl->pCurrentEntry)
110 return ;
111 sal_uInt32 nSprmId = rSprm.getId();
113 switch(nSprmId)
115 case NS_ooxml::LN_CT_Font_charset:
116 case NS_ooxml::LN_CT_Font_pitch:
117 resolveSprm( rSprm );
118 break;
119 case NS_ooxml::LN_CT_Font_embedRegular:
120 case NS_ooxml::LN_CT_Font_embedBold:
121 case NS_ooxml::LN_CT_Font_embedItalic:
122 case NS_ooxml::LN_CT_Font_embedBoldItalic:
124 writerfilter::Reference< Properties >::Pointer_t pProperties = rSprm.getProps();
125 if( pProperties.get( ))
127 EmbeddedFontHandler handler( m_pImpl->pCurrentEntry->sFontName,
128 nSprmId == NS_ooxml::LN_CT_Font_embedRegular ? ""
129 : nSprmId == NS_ooxml::LN_CT_Font_embedBold ? "b"
130 : nSprmId == NS_ooxml::LN_CT_Font_embedItalic ? "i"
131 : /*NS_ooxml::LN_CT_Font_embedBoldItalic*/ "bi" );
132 pProperties->resolve( handler );
134 break;
136 case NS_ooxml::LN_CT_Font_altName:
137 break;
138 case NS_ooxml::LN_CT_Font_panose1:
139 break;
140 case NS_ooxml::LN_CT_Font_family:
141 break;
142 case NS_ooxml::LN_CT_Font_sig:
143 break;
144 case NS_ooxml::LN_CT_Font_notTrueType:
145 break;
146 default:
147 SAL_WARN("writerfilter.dmapper", "FontTable::lcl_sprm: unhandled token: " << nSprmId);
148 break;
152 void FontTable::resolveSprm(Sprm & r_Sprm)
154 writerfilter::Reference<Properties>::Pointer_t pProperties = r_Sprm.getProps();
155 if( pProperties.get())
156 pProperties->resolve(*this);
159 void FontTable::lcl_entry(int /*pos*/, writerfilter::Reference<Properties>::Pointer_t ref)
161 //create a new font entry
162 SAL_WARN_IF( m_pImpl->pCurrentEntry, "writerfilter.dmapper", "current entry has to be NULL here" );
163 m_pImpl->pCurrentEntry = new FontEntry;
164 ref->resolve(*this);
165 //append it to the table
166 m_pImpl->aFontEntries.push_back( m_pImpl->pCurrentEntry );
167 m_pImpl->pCurrentEntry.clear();
170 void FontTable::lcl_startSectionGroup()
174 void FontTable::lcl_endSectionGroup()
178 void FontTable::lcl_startParagraphGroup()
182 void FontTable::lcl_endParagraphGroup()
186 void FontTable::lcl_startCharacterGroup()
190 void FontTable::lcl_endCharacterGroup()
194 void FontTable::lcl_text(const sal_uInt8*, size_t )
198 void FontTable::lcl_utext(const sal_uInt8* , size_t)
202 void FontTable::lcl_props(writerfilter::Reference<Properties>::Pointer_t)
206 void FontTable::lcl_table(Id, writerfilter::Reference<Table>::Pointer_t)
210 void FontTable::lcl_substream(Id, ::writerfilter::Reference<Stream>::Pointer_t)
214 void FontTable::lcl_info(const std::string& )
218 void FontTable::lcl_startShape(uno::Reference<drawing::XShape> const&)
222 void FontTable::lcl_endShape( )
226 const FontEntry::Pointer_t FontTable::getFontEntry(sal_uInt32 nIndex)
228 return (m_pImpl->aFontEntries.size() > nIndex)
229 ? m_pImpl->aFontEntries[nIndex]
230 : FontEntry::Pointer_t();
233 sal_uInt32 FontTable::size()
235 return m_pImpl->aFontEntries.size();
238 EmbeddedFontHandler::EmbeddedFontHandler( const OUString& _fontName, const char* _style )
239 : LoggedProperties("EmbeddedFontHandler")
240 , fontName( _fontName )
241 , style( _style )
245 EmbeddedFontHandler::~EmbeddedFontHandler()
247 if( !inputStream.is())
248 return;
249 std::vector< unsigned char > key( 32 );
250 if( !fontKey.isEmpty())
251 { // key for unobfuscating
252 // 1 3 5 7 10 2 5 7 20 2 5 7 9 1 3 5
253 // {62E79491-959F-41E9-B76B-6B32631DEA5C}
254 static const int pos[ 16 ] = { 35, 33, 31, 29, 27, 25, 22, 20, 17, 15, 12, 10, 7, 5, 3, 1 };
255 for( int i = 0;
256 i < 16;
257 ++i )
259 int v1 = fontKey[ pos[ i ]];
260 int v2 = fontKey[ pos[ i ] + 1 ];
261 assert(( v1 >= '0' && v1 <= '9' ) || ( v1 >= 'A' && v1 <= 'F' ));
262 assert(( v2 >= '0' && v2 <= '9' ) || ( v2 >= 'A' && v2 <= 'F' ));
263 int val = ( v1 - ( v1 <= '9' ? '0' : 'A' - 10 )) * 16 + v2 - ( v2 <= '9' ? '0' : 'A' - 10 );
264 key[ i ] = val;
265 key[ i + 16 ] = val;
268 EmbeddedFontsHelper::addEmbeddedFont( inputStream, fontName, style, key );
269 inputStream->closeInput();
272 void EmbeddedFontHandler::lcl_attribute( Id name, Value& val )
274 OUString sValue = val.getString();
275 switch( name )
277 case NS_ooxml::LN_CT_FontRel_fontKey:
278 fontKey = sValue;
279 break;
280 case NS_ooxml::LN_CT_Rel_id:
281 id = sValue;
282 break;
283 case NS_ooxml::LN_CT_FontRel_subsetted:
284 break; // TODO? Let's just ignore this for now and hope
285 // it doesn't break anything.
286 case NS_ooxml::LN_inputstream: // the actual font data as stream
287 val.getAny() >>= inputStream;
288 break;
289 default:
290 break;
294 void EmbeddedFontHandler::lcl_sprm( Sprm& )
299 }//namespace dmapper
300 }//namespace writerfilter
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */