Bump version to 4.1-6
[LibreOffice.git] / writerfilter / source / dmapper / FontTable.cxx
blob47fdc012549b726f172a874ceec8bbd8e0175747
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 <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>
29 #include "dmapperLoggers.hxx"
31 namespace writerfilter {
32 namespace dmapper
35 struct FontTable_Impl
37 std::vector< FontEntry::Pointer_t > aFontEntries;
38 FontEntry::Pointer_t pCurrentEntry;
39 FontTable_Impl() {}
42 FontTable::FontTable()
43 : LoggedProperties(dmapper_logger, "FontTable")
44 , LoggedTable(dmapper_logger, "FontTable")
45 , LoggedStream(dmapper_logger, "FontTable")
46 , m_pImpl( new FontTable_Impl )
50 FontTable::~FontTable()
52 delete m_pImpl;
55 void FontTable::lcl_attribute(Id Name, Value & val)
57 OSL_ENSURE( m_pImpl->pCurrentEntry, "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_rtf::LN_CBFFNM1:
65 m_pImpl->pCurrentEntry->sFontName1 = sValue;
66 break;
67 case NS_rtf::LN_PRQ:
68 m_pImpl->pCurrentEntry->nPitchRequest = static_cast<sal_Int16>( nIntValue );
69 break;
70 case NS_rtf::LN_FTRUETYPE:
71 m_pImpl->pCurrentEntry->bTrueType = nIntValue == 1 ? true : false;
72 break;
73 case NS_rtf::LN_UNUSED1_3: //unused
74 case NS_rtf::LN_FF: //unused
75 case NS_rtf::LN_UNUSED1_7: //unused
76 break;
77 case NS_rtf::LN_WWEIGHT:
78 m_pImpl->pCurrentEntry->nBaseWeight = nIntValue;
79 break;
80 case NS_rtf::LN_CHS:
81 m_pImpl->pCurrentEntry->nTextEncoding = nIntValue;
82 break;
83 case NS_rtf::LN_IXCHSZALT:
84 break;
85 case NS_rtf::LN_PANOSE:
86 m_pImpl->pCurrentEntry->sPanose += sValue;
87 break;
88 case NS_rtf::LN_FS:
89 m_pImpl->pCurrentEntry->sFontSignature += sValue;
90 break;
91 case NS_rtf::LN_F:
92 break;
93 case NS_rtf::LN_ALTFONTNAME:
94 m_pImpl->pCurrentEntry->sAlternativeFont = sValue;
95 break;
96 case NS_rtf::LN_XSZFFN:
97 case NS_ooxml::LN_CT_Font_name:
98 m_pImpl->pCurrentEntry->sFontName = sValue;
99 break;
100 case NS_ooxml::LN_CT_Charset_val:
101 // w:characterSet has higher priority, set only if that one is not set
102 if( m_pImpl->pCurrentEntry->nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
103 m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromWindowsCharset( nIntValue );
104 break;
105 case NS_ooxml::LN_CT_Charset_characterSet:
107 OString tmp;
108 sValue.convertToString( &tmp, RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
109 m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromMimeCharset( tmp.getStr() );
110 break;
112 default:
114 //----> debug
115 int nVal = val.getInt();
116 ++nVal;
117 //<---- debug
122 void FontTable::lcl_sprm(Sprm& rSprm)
124 OSL_ENSURE( m_pImpl->pCurrentEntry, "current entry has to be set here");
125 if(!m_pImpl->pCurrentEntry)
126 return ;
127 sal_uInt32 nSprmId = rSprm.getId();
129 Value::Pointer_t pValue = rSprm.getValue();
130 sal_Int32 nIntValue = pValue->getInt();
131 (void)nIntValue;
132 switch(nSprmId)
134 case NS_ooxml::LN_CT_Font_charset:
135 resolveSprm( rSprm );
136 break;
137 case NS_ooxml::LN_CT_Font_embedRegular:
138 case NS_ooxml::LN_CT_Font_embedBold:
139 case NS_ooxml::LN_CT_Font_embedItalic:
140 case NS_ooxml::LN_CT_Font_embedBoldItalic:
142 writerfilter::Reference< Properties >::Pointer_t pProperties = rSprm.getProps();
143 if( pProperties.get( ))
145 EmbeddedFontHandler handler( m_pImpl->pCurrentEntry->sFontName,
146 nSprmId == NS_ooxml::LN_CT_Font_embedRegular ? ""
147 : nSprmId == NS_ooxml::LN_CT_Font_embedBold ? "b"
148 : nSprmId == NS_ooxml::LN_CT_Font_embedItalic ? "i"
149 : nSprmId == NS_ooxml::LN_CT_Font_embedBoldItalic ? "bi" : "?" );
150 pProperties->resolve( handler );
152 break;
157 void FontTable::resolveSprm(Sprm & r_Sprm)
159 writerfilter::Reference<Properties>::Pointer_t pProperties = r_Sprm.getProps();
160 if( pProperties.get())
161 pProperties->resolve(*this);
164 void FontTable::lcl_entry(int /*pos*/, writerfilter::Reference<Properties>::Pointer_t ref)
166 //create a new font entry
167 OSL_ENSURE( !m_pImpl->pCurrentEntry, "current entry has to be NULL here");
168 m_pImpl->pCurrentEntry.reset(new FontEntry);
169 ref->resolve(*this);
170 //append it to the table
171 m_pImpl->aFontEntries.push_back( m_pImpl->pCurrentEntry );
172 m_pImpl->pCurrentEntry.reset();
175 void FontTable::lcl_startSectionGroup()
179 void FontTable::lcl_endSectionGroup()
183 void FontTable::lcl_startParagraphGroup()
187 void FontTable::lcl_endParagraphGroup()
191 void FontTable::lcl_startCharacterGroup()
195 void FontTable::lcl_endCharacterGroup()
199 void FontTable::lcl_text(const sal_uInt8*, size_t )
203 void FontTable::lcl_utext(const sal_uInt8* , size_t)
207 void FontTable::lcl_props(writerfilter::Reference<Properties>::Pointer_t)
211 void FontTable::lcl_table(Id, writerfilter::Reference<Table>::Pointer_t)
215 void FontTable::lcl_substream(Id, ::writerfilter::Reference<Stream>::Pointer_t)
219 void FontTable::lcl_info(const string& )
223 void FontTable::lcl_startShape( ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > )
227 void FontTable::lcl_endShape( )
231 const FontEntry::Pointer_t FontTable::getFontEntry(sal_uInt32 nIndex)
233 return (m_pImpl->aFontEntries.size() > nIndex)
234 ? m_pImpl->aFontEntries[nIndex]
235 : FontEntry::Pointer_t();
238 sal_uInt32 FontTable::size()
240 return m_pImpl->aFontEntries.size();
243 EmbeddedFontHandler::EmbeddedFontHandler( const OUString& _fontName, const char* _style )
244 : LoggedProperties(dmapper_logger, "EmbeddedFontHandler")
245 , fontName( _fontName )
246 , style( _style )
250 EmbeddedFontHandler::~EmbeddedFontHandler()
252 if( !inputStream.is())
253 return;
254 std::vector< unsigned char > key( 32 );
255 if( !fontKey.isEmpty())
256 { // key for unobfuscating
257 // 1 3 5 7 10 2 5 7 20 2 5 7 9 1 3 5
258 // {62E79491-959F-41E9-B76B-6B32631DEA5C}
259 static const int pos[ 16 ] = { 35, 33, 31, 29, 27, 25, 22, 20, 17, 15, 12, 10, 7, 5, 3, 1 };
260 for( int i = 0;
261 i < 16;
262 ++i )
264 int v1 = fontKey[ pos[ i ]];
265 int v2 = fontKey[ pos[ i ] + 1 ];
266 assert(( v1 >= '0' && v1 <= '9' ) || ( v1 >= 'A' && v1 <= 'F' ));
267 assert(( v2 >= '0' && v2 <= '9' ) || ( v2 >= 'A' && v2 <= 'F' ));
268 int val = ( v1 - ( v1 <= '9' ? '0' : 'A' - 10 )) * 16 + v2 - ( v2 <= '9' ? '0' : 'A' - 10 );
269 key[ i ] = val;
270 key[ i + 16 ] = val;
273 EmbeddedFontsHelper::addEmbeddedFont( inputStream, fontName, style, key );
274 inputStream->closeInput();
277 void EmbeddedFontHandler::lcl_attribute( Id name, Value& val )
279 OUString sValue = val.getString();
280 switch( name )
282 case NS_ooxml::LN_CT_FontRel_fontKey:
283 fontKey = sValue;
284 break;
285 case NS_ooxml::LN_CT_Rel_id:
286 id = sValue;
287 break;
288 case NS_ooxml::LN_CT_FontRel_subsetted:
289 break; // TODO? Let's just ignore this for now and hope
290 // it doesn't break anything.
291 case NS_ooxml::LN_inputstream: // the actual font data as stream
292 val.getAny() >>= inputStream;
293 break;
294 default:
295 break;
299 void EmbeddedFontHandler::lcl_sprm( Sprm& )
304 }//namespace dmapper
305 }//namespace writerfilter
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */