LibreOffice Module writerfilter (master) 1
FontTable.cxx
Go to the documentation of this file.
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 <o3tl/deleter.hxx>
22#include <ooxml/resourceids.hxx>
23#include <utility>
24#include <vector>
25#include <sal/log.hxx>
26#include <rtl/tencinfo.h>
28#include <unotools/fontdefs.hxx>
29
30using namespace com::sun::star;
31
33{
34
36{
37 std::unique_ptr<EmbeddedFontsHelper, o3tl::default_delete<EmbeddedFontsHelper>> xEmbeddedFontHelper;
38 std::vector< FontEntry::Pointer_t > aFontEntries;
41};
42
44: LoggedProperties("FontTable")
45, LoggedTable("FontTable")
46, LoggedStream("FontTable")
48{
49}
50
52{
53}
54
56{
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)
63 {
64 case NS_ooxml::LN_CT_Pitch_val:
65 if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_fixed)
66 ;
67 else if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_variable)
68 ;
69 else if (static_cast<Id>(nIntValue) == NS_ooxml::LN_Value_ST_Pitch_default)
70 ;
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 )
80 {
81 m_pImpl->pCurrentEntry->nTextEncoding = rtl_getTextEncodingFromWindowsCharset( nIntValue );
82 if( IsOpenSymbol( m_pImpl->pCurrentEntry->sFontName ))
83 m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
84 }
85 break;
86 case NS_ooxml::LN_CT_Charset_characterSet:
87 {
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( IsOpenSymbol( m_pImpl->pCurrentEntry->sFontName ))
93 m_pImpl->pCurrentEntry->nTextEncoding = RTL_TEXTENCODING_SYMBOL;
94 break;
95 }
96 default: ;
97 }
98}
99
101{
102 SAL_WARN_IF( !m_pImpl->pCurrentEntry, "writerfilter.dmapper", "current entry has to be set here" );
103 if(!m_pImpl->pCurrentEntry)
104 return ;
105 sal_uInt32 nSprmId = rSprm.getId();
106
107 switch(nSprmId)
108 {
109 case NS_ooxml::LN_CT_Font_charset:
110 case NS_ooxml::LN_CT_Font_pitch:
111 resolveSprm( rSprm );
112 break;
113 case NS_ooxml::LN_CT_Font_embedRegular:
114 case NS_ooxml::LN_CT_Font_embedBold:
115 case NS_ooxml::LN_CT_Font_embedItalic:
116 case NS_ooxml::LN_CT_Font_embedBoldItalic:
117 {
119 if( pProperties )
120 {
121 EmbeddedFontHandler handler(*this, m_pImpl->pCurrentEntry->sFontName,
122 nSprmId == NS_ooxml::LN_CT_Font_embedRegular ? u""
123 : nSprmId == NS_ooxml::LN_CT_Font_embedBold ? u"b"
124 : nSprmId == NS_ooxml::LN_CT_Font_embedItalic ? u"i"
125 : /*NS_ooxml::LN_CT_Font_embedBoldItalic*/ u"bi" );
126 pProperties->resolve( handler );
127 }
128 break;
129 }
130 case NS_ooxml::LN_CT_Font_altName:
131 break;
132 case NS_ooxml::LN_CT_Font_panose1:
133 break;
134 case NS_ooxml::LN_CT_Font_family:
135 break;
136 case NS_ooxml::LN_CT_Font_sig:
137 break;
138 case NS_ooxml::LN_CT_Font_notTrueType:
139 break;
140 default:
141 SAL_WARN("writerfilter.dmapper", "FontTable::lcl_sprm: unhandled token: " << nSprmId);
142 break;
143 }
144}
145
147{
149 if( pProperties )
150 pProperties->resolve(*this);
151}
152
154{
155 //create a new font entry
156 SAL_WARN_IF( m_pImpl->pCurrentEntry, "writerfilter.dmapper", "current entry has to be NULL here" );
157 m_pImpl->pCurrentEntry = new FontEntry;
158 ref->resolve(*this);
159 //append it to the table
160 m_pImpl->aFontEntries.push_back( m_pImpl->pCurrentEntry );
161 m_pImpl->pCurrentEntry.clear();
162}
163
165{
166}
167
169{
170}
171
173{
174}
175
177{
178}
179
181{
182}
183
185{
186}
187
188void FontTable::lcl_text(const sal_uInt8*, size_t )
189{
190}
191
192void FontTable::lcl_utext(const sal_uInt8* , size_t)
193{
194}
195
197{
198}
199
201{
202}
203
205{
206}
207
209{
210}
211
213{
214}
215
217{
218 return (m_pImpl->aFontEntries.size() > nIndex)
219 ? m_pImpl->aFontEntries[nIndex]
221}
222
223sal_uInt32 FontTable::size()
224{
225 return m_pImpl->aFontEntries.size();
226}
227
228void FontTable::addEmbeddedFont(const css::uno::Reference<css::io::XInputStream>& stream,
229 const OUString& fontName, std::u16string_view extra,
230 std::vector<unsigned char> const & key)
231{
232 if (!m_pImpl->xEmbeddedFontHelper)
233 m_pImpl->xEmbeddedFontHelper.reset(new EmbeddedFontsHelper);
234 m_pImpl->xEmbeddedFontHelper->addEmbeddedFont(stream, fontName, extra, key);
235}
236
237EmbeddedFontHandler::EmbeddedFontHandler(FontTable& rFontTable, OUString _fontName, std::u16string_view style )
238: LoggedProperties("EmbeddedFontHandler")
239, m_fontTable( rFontTable )
240, m_fontName(std::move( _fontName ))
241, m_style( style )
242{
243}
244
246{
247 if( !m_inputStream.is())
248 return;
249 std::vector< unsigned char > key( 32 );
250 if( !m_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 )
258 {
259 int v1 = m_fontKey[ pos[ i ]];
260 int v2 = m_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;
266 }
267 }
269 m_inputStream->closeInput();
270}
271
273{
274 OUString sValue = val.getString();
275 switch( name )
276 {
277 case NS_ooxml::LN_CT_FontRel_fontKey:
278 m_fontKey = sValue;
279 break;
280 case NS_ooxml::LN_CT_Rel_id:
281 break;
282 case NS_ooxml::LN_CT_FontRel_subsetted:
283 break; // TODO? Let's just ignore this for now and hope
284 // it doesn't break anything.
285 case NS_ooxml::LN_inputstream: // the actual font data as stream
286 val.getAny() >>= m_inputStream;
287 break;
288 default:
289 break;
290 }
291}
292
294{
295}
296
297
298}//namespace writerfilter::dmapper
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
An SPRM: Section, Paragraph and Run Modifier.
virtual sal_uInt32 getId() const =0
Returns id of the SPRM.
virtual writerfilter::Reference< Properties >::Pointer_t getProps()=0
Returns reference to properties contained in the SPRM.
virtual int getInt() const =0
Returns integer representation of the value.
virtual OUString getString() const =0
Returns string representation of the value.
virtual css::uno::Any getAny() const =0
Returns representation of the value as uno::Any.
EmbeddedFontHandler(FontTable &rFontTable, OUString fontName, std::u16string_view style)
Definition: FontTable.cxx:237
virtual void lcl_sprm(Sprm &rSprm) override
Definition: FontTable.cxx:293
css::uno::Reference< css::io::XInputStream > m_inputStream
Definition: FontTable.hxx:100
virtual void lcl_attribute(Id name, Value &val) override
Definition: FontTable.cxx:272
virtual void lcl_startCharacterGroup() override
Definition: FontTable.cxx:180
virtual ~FontTable() override
Definition: FontTable.cxx:51
virtual void lcl_sprm(Sprm &sprm) override
Definition: FontTable.cxx:100
virtual void lcl_attribute(Id Name, Value &val) override
Definition: FontTable.cxx:55
void addEmbeddedFont(const css::uno::Reference< css::io::XInputStream > &stream, const OUString &fontName, std::u16string_view extra, std::vector< unsigned char > const &key)
Definition: FontTable.cxx:228
virtual void lcl_startParagraphGroup() override
Definition: FontTable.cxx:172
FontEntry::Pointer_t getFontEntry(sal_uInt32 nIndex)
Definition: FontTable.cxx:216
virtual void lcl_startShape(css::uno::Reference< css::drawing::XShape > const &xShape) override
Definition: FontTable.cxx:208
virtual void lcl_props(writerfilter::Reference< Properties >::Pointer_t ref) override
Definition: FontTable.cxx:196
void resolveSprm(Sprm &r_sprm)
Definition: FontTable.cxx:146
std::unique_ptr< FontTable_Impl > m_pImpl
Definition: FontTable.hxx:45
virtual void lcl_text(const sal_uInt8 *data, size_t len) override
Definition: FontTable.cxx:188
virtual void lcl_endSectionGroup() override
Definition: FontTable.cxx:168
virtual void lcl_endParagraphGroup() override
Definition: FontTable.cxx:176
virtual void lcl_endShape() override
Definition: FontTable.cxx:212
virtual void lcl_endCharacterGroup() override
Definition: FontTable.cxx:184
virtual void lcl_table(Id name, writerfilter::Reference< Table >::Pointer_t ref) override
Definition: FontTable.cxx:200
virtual void lcl_entry(writerfilter::Reference< Properties >::Pointer_t ref) override
Definition: FontTable.cxx:153
virtual void lcl_startSectionGroup() override
Definition: FontTable.cxx:164
virtual void lcl_utext(const sal_uInt8 *data, size_t len) override
Definition: FontTable.cxx:192
virtual void lcl_substream(Id name, ::writerfilter::Reference< Stream >::Pointer_t ref) override
Definition: FontTable.cxx:204
Reference< XOutputStream > stream
float u
UNOTOOLS_DLLPUBLIC bool IsOpenSymbol(std::u16string_view rFontName)
const char * name
sal_Int32 nIndex
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
int i
sal_uInt32 Id
tools::SvRef< FontEntry > Pointer_t
Definition: FontTable.hxx:33
std::vector< FontEntry::Pointer_t > aFontEntries
Definition: FontTable.cxx:38
FontEntry::Pointer_t pCurrentEntry
Definition: FontTable.cxx:39
std::unique_ptr< EmbeddedFontsHelper, o3tl::default_delete< EmbeddedFontsHelper > > xEmbeddedFontHelper
Definition: FontTable.cxx:37
OUString Name
unsigned char sal_uInt8
size_t pos