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 <o3tl/sorted_vector.hxx>
21 : #include <tools/fontenum.hxx>
22 : #include <xmloff/xmlnmspe.hxx>
23 : #include <xmloff/xmltoken.hxx>
24 : #include <xmloff/xmluconv.hxx>
25 : #include "fonthdl.hxx"
26 : #include <xmloff/xmlexp.hxx>
27 : #include <xmloff/XMLFontAutoStylePool.hxx>
28 : #include <vcl/embeddedfontshelper.hxx>
29 : #include <osl/file.hxx>
30 :
31 : #include <com/sun/star/embed/ElementModes.hpp>
32 : #include <com/sun/star/embed/XTransactedObject.hpp>
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 :
35 :
36 : using namespace ::com::sun::star;
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::xmloff::token;
39 :
40 18198 : class XMLFontAutoStylePoolEntry_Impl
41 : {
42 : OUString sName;
43 : OUString sFamilyName;
44 : OUString sStyleName;
45 : FontFamily nFamily;
46 : FontPitch nPitch;
47 : rtl_TextEncoding eEnc;
48 :
49 : public:
50 :
51 : inline XMLFontAutoStylePoolEntry_Impl(
52 : const OUString& rName,
53 : const OUString& rFamilyName,
54 : const OUString& rStyleName,
55 : FontFamily nFamily,
56 : FontPitch nPitch,
57 : rtl_TextEncoding eEnc );
58 :
59 : inline XMLFontAutoStylePoolEntry_Impl(
60 : const OUString& rFamilyName,
61 : const OUString& rStyleName,
62 : FontFamily nFamily,
63 : FontPitch nPitch,
64 : rtl_TextEncoding eEnc );
65 :
66 10660 : const OUString& GetName() const { return sName; }
67 51134 : const OUString& GetFamilyName() const { return sFamilyName; }
68 39142 : const OUString& GetStyleName() const { return sStyleName; }
69 98702 : FontFamily GetFamily() const { return nFamily; }
70 86950 : FontPitch GetPitch() const { return nPitch; }
71 81134 : rtl_TextEncoding GetEncoding() const { return eEnc; }
72 : };
73 :
74 :
75 1160 : inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
76 : const OUString& rName,
77 : const OUString& rFamilyName,
78 : const OUString& rStyleName,
79 : FontFamily nFam,
80 : FontPitch nP,
81 : rtl_TextEncoding eE ) :
82 : sName( rName ),
83 : sFamilyName( rFamilyName ),
84 : sStyleName( rStyleName ),
85 : nFamily( nFam ),
86 : nPitch( nP ),
87 1160 : eEnc( eE )
88 : {
89 1160 : }
90 :
91 17038 : inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
92 : const OUString& rFamilyName,
93 : const OUString& rStyleName,
94 : FontFamily nFam,
95 : FontPitch nP,
96 : rtl_TextEncoding eE ) :
97 : sFamilyName( rFamilyName ),
98 : sStyleName( rStyleName ),
99 : nFamily( nFam ),
100 : nPitch( nP ),
101 17038 : eEnc( eE )
102 : {
103 17038 : }
104 :
105 : struct XMLFontAutoStylePoolEntryCmp_Impl {
106 39984 : bool operator()(
107 : XMLFontAutoStylePoolEntry_Impl* const& r1,
108 : XMLFontAutoStylePoolEntry_Impl* const& r2 ) const
109 : {
110 39984 : bool nEnc1(r1->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
111 39984 : bool nEnc2(r2->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
112 39984 : if( nEnc1 != nEnc2 )
113 100 : return nEnc1 < nEnc2;
114 39884 : else if( r1->GetPitch() != r2->GetPitch() )
115 3008 : return r1->GetPitch() < r2->GetPitch();
116 36876 : else if( r1->GetFamily() != r2->GetFamily() )
117 11892 : return r1->GetFamily() < r2->GetFamily();
118 : else
119 : {
120 24984 : sal_Int32 nCmp = r1->GetFamilyName().compareTo( r2->GetFamilyName() );
121 24984 : if( 0 == nCmp )
122 18988 : return r1->GetStyleName().compareTo( r2->GetStyleName() ) < 0;
123 : else
124 5996 : return nCmp < 0;
125 : }
126 : }
127 : };
128 :
129 954 : class XMLFontAutoStylePool_Impl : public o3tl::sorted_vector<XMLFontAutoStylePoolEntry_Impl*, XMLFontAutoStylePoolEntryCmp_Impl>
130 : {
131 : public:
132 954 : ~XMLFontAutoStylePool_Impl() { DeleteAndDestroyAll(); }
133 : };
134 :
135 954 : XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp, bool _tryToEmbedFonts ) :
136 : rExport( rExp ),
137 954 : pPool( new XMLFontAutoStylePool_Impl ),
138 1908 : tryToEmbedFonts( _tryToEmbedFonts )
139 : {
140 954 : }
141 :
142 2646 : XMLFontAutoStylePool::~XMLFontAutoStylePool()
143 : {
144 954 : delete pPool;
145 1692 : }
146 :
147 9204 : OUString XMLFontAutoStylePool::Add(
148 : const OUString& rFamilyName,
149 : const OUString& rStyleName,
150 : FontFamily nFamily,
151 : FontPitch nPitch,
152 : rtl_TextEncoding eEnc )
153 : {
154 9204 : OUString sPoolName;
155 : XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
156 18408 : nPitch, eEnc );
157 9204 : XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
158 9204 : if( it != pPool->end() )
159 : {
160 8044 : sPoolName = (*it)->GetName();
161 : }
162 : else
163 : {
164 1160 : OUString sName;
165 1160 : sal_Int32 nLen = rFamilyName.indexOf( ';', 0 );
166 1160 : if( -1 == nLen )
167 : {
168 1160 : sName = rFamilyName;
169 : }
170 0 : else if( nLen > 0 )
171 : {
172 0 : sName = rFamilyName.copy( 0, nLen );
173 0 : sName = sName.trim();
174 : }
175 :
176 1160 : if( sName.isEmpty() )
177 10 : sName = OUString( 'F' );
178 :
179 1160 : if( m_aNames.find(sName) != m_aNames.end() )
180 : {
181 184 : sal_Int32 nCount = 1;
182 184 : OUString sPrefix( sName );
183 184 : sName += OUString::number( nCount );
184 384 : while( m_aNames.find(sName) != m_aNames.end() )
185 : {
186 16 : sName = sPrefix;
187 16 : sName += OUString::number( ++nCount );
188 184 : }
189 : }
190 :
191 : XMLFontAutoStylePoolEntry_Impl *pEntry =
192 : new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
193 1160 : nFamily, nPitch, eEnc );
194 1160 : pPool->insert( pEntry );
195 1160 : m_aNames.insert(sName);
196 : }
197 :
198 18408 : return sPoolName;
199 : }
200 :
201 7834 : OUString XMLFontAutoStylePool::Find(
202 : const OUString& rFamilyName,
203 : const OUString& rStyleName,
204 : FontFamily nFamily,
205 : FontPitch nPitch,
206 : rtl_TextEncoding eEnc ) const
207 : {
208 7834 : OUString sName;
209 : XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
210 15668 : nPitch, eEnc );
211 7834 : XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
212 7834 : if( it != pPool->end() )
213 : {
214 1450 : sName = (*it)->GetName();
215 : }
216 :
217 15668 : return sName;
218 : }
219 :
220 :
221 440 : void XMLFontAutoStylePool::exportXML()
222 : {
223 440 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
224 : XML_FONT_FACE_DECLS,
225 440 : true, true );
226 880 : Any aAny;
227 880 : OUString sTmp;
228 880 : XMLFontFamilyNamePropHdl aFamilyNameHdl;
229 880 : XMLFontFamilyPropHdl aFamilyHdl;
230 880 : XMLFontPitchPropHdl aPitchHdl;
231 880 : XMLFontEncodingPropHdl aEncHdl;
232 440 : const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
233 :
234 880 : std::map< OUString, OUString > fontFilesMap; // our url to document url
235 440 : sal_uInt32 nCount = pPool->size();
236 1606 : for( sal_uInt32 i=0; i<nCount; i++ )
237 : {
238 1166 : const XMLFontAutoStylePoolEntry_Impl *pEntry = (*pPool)[ i ];
239 :
240 1166 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
241 2332 : XML_NAME, pEntry->GetName() );
242 :
243 1166 : aAny <<= pEntry->GetFamilyName();
244 1166 : if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
245 1166 : GetExport().AddAttribute( XML_NAMESPACE_SVG,
246 1166 : XML_FONT_FAMILY, sTmp );
247 :
248 1166 : const OUString& rStyleName = pEntry->GetStyleName();
249 1166 : if( !rStyleName.isEmpty() )
250 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
251 : XML_FONT_ADORNMENTS,
252 0 : rStyleName );
253 :
254 1166 : aAny <<= (sal_Int16)pEntry->GetFamily();
255 1166 : if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv ) )
256 1112 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
257 1112 : XML_FONT_FAMILY_GENERIC, sTmp );
258 :
259 1166 : aAny <<= (sal_Int16)pEntry->GetPitch();
260 1166 : if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv ) )
261 1060 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
262 1060 : XML_FONT_PITCH, sTmp );
263 :
264 1166 : aAny <<= (sal_Int16)pEntry->GetEncoding();
265 1166 : if( aEncHdl.exportXML( sTmp, aAny, rUnitConv ) )
266 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
267 0 : XML_FONT_CHARSET, sTmp );
268 :
269 1166 : SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
270 : XML_FONT_FACE,
271 1166 : true, true );
272 :
273 1166 : if( tryToEmbedFonts )
274 : {
275 0 : std::vector< OUString > fileUrls;
276 : static const FontWeight weight[] = { WEIGHT_NORMAL, WEIGHT_BOLD, WEIGHT_NORMAL, WEIGHT_BOLD };
277 : static const FontItalic italic[] = { ITALIC_NONE, ITALIC_NONE, ITALIC_NORMAL, ITALIC_NORMAL };
278 : assert( SAL_N_ELEMENTS( weight ) == SAL_N_ELEMENTS( italic ));
279 0 : for( unsigned int j = 0;
280 0 : j < SAL_N_ELEMENTS( weight );
281 : ++j )
282 : {
283 : // Embed font if at least viewing is allowed (in which case the opening app must check
284 : // the font license rights too and open either read-only or not use the font for editing).
285 0 : OUString fileUrl = EmbeddedFontsHelper::fontFileUrl( pEntry->GetFamilyName(), pEntry->GetFamily(),
286 0 : italic[ j ], weight[ j ], pEntry->GetPitch(), pEntry->GetEncoding(),
287 0 : EmbeddedFontsHelper::ViewingAllowed );
288 0 : if( fileUrl.isEmpty())
289 0 : continue;
290 0 : if( !fontFilesMap.count( fileUrl ))
291 : {
292 0 : OUString docUrl = embedFontFile( fileUrl );
293 0 : if( !docUrl.isEmpty())
294 0 : fontFilesMap[ fileUrl ] = docUrl;
295 : else
296 0 : continue; // --> failed to embed
297 : }
298 0 : fileUrls.push_back( fileUrl );
299 0 : }
300 0 : if( !fileUrls.empty())
301 : {
302 0 : SvXMLElementExport fontFaceSrc( GetExport(), XML_NAMESPACE_SVG,
303 0 : XML_FONT_FACE_SRC, true, true );
304 0 : for( std::vector< OUString >::const_iterator it = fileUrls.begin();
305 0 : it != fileUrls.end();
306 : ++it )
307 : {
308 0 : if( fontFilesMap.count( *it ))
309 : {
310 0 : GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, fontFilesMap[ *it ] );
311 0 : GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, "simple" );
312 0 : SvXMLElementExport fontFaceUri( GetExport(), XML_NAMESPACE_SVG,
313 0 : XML_FONT_FACE_URI, true, true );
314 : }
315 0 : }
316 0 : }
317 : }
318 1606 : }
319 440 : }
320 :
321 0 : OUString XMLFontAutoStylePool::embedFontFile( const OUString& fileUrl )
322 : {
323 : try
324 : {
325 0 : osl::File file( fileUrl );
326 0 : if( file.open( osl_File_OpenFlag_Read ) != osl::File::E_None )
327 0 : return OUString();
328 :
329 0 : if ( !GetExport().GetTargetStorage().is() )
330 0 : return OUString();
331 :
332 0 : uno::Reference< embed::XStorage > storage;
333 0 : storage.set( GetExport().GetTargetStorage()->openStorageElement( OUString( "Fonts" ),
334 0 : ::embed::ElementModes::WRITE ), uno::UNO_QUERY_THROW );
335 0 : int index = 0;
336 0 : OUString name;
337 0 : do
338 : {
339 0 : name = "font" + OUString::number( ++index ) + ".ttf";
340 0 : } while( storage->hasByName( name ) );
341 0 : uno::Reference< io::XOutputStream > outputStream;
342 0 : outputStream.set( storage->openStreamElement( name, ::embed::ElementModes::WRITE ), UNO_QUERY_THROW );
343 0 : uno::Reference < beans::XPropertySet > propertySet( outputStream, uno::UNO_QUERY );
344 : assert( propertySet.is());
345 0 : propertySet->setPropertyValue( "MediaType", uno::makeAny( OUString( "application/x-font-ttf" ))); // TODO
346 : for(;;)
347 : {
348 : char buffer[ 4096 ];
349 : sal_uInt64 readSize;
350 : sal_Bool eof;
351 0 : if( file.isEndOfFile( &eof ) != osl::File::E_None )
352 : {
353 : SAL_WARN( "xmloff", "Error reading font file " << fileUrl );
354 0 : outputStream->closeOutput();
355 0 : return OUString();
356 : }
357 0 : if( eof )
358 0 : break;
359 0 : if( file.read( buffer, 4096, readSize ) != osl::File::E_None )
360 : {
361 : SAL_WARN( "xmloff", "Error reading font file " << fileUrl );
362 0 : outputStream->closeOutput();
363 0 : return OUString();
364 : }
365 0 : if( readSize == 0 )
366 0 : break;
367 0 : outputStream->writeBytes( uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( buffer ), readSize ));
368 0 : }
369 0 : outputStream->closeOutput();
370 0 : if( storage.is() )
371 : {
372 0 : Reference< embed::XTransactedObject > transaction( storage, UNO_QUERY );
373 0 : if( transaction.is())
374 : {
375 0 : transaction->commit();
376 0 : return "Fonts/" + name;
377 0 : }
378 0 : }
379 0 : } catch( const Exception& e )
380 : {
381 : SAL_WARN( "xmloff", "Exception when embedding a font file:" << e.Message );
382 : }
383 0 : return OUString();
384 : }
385 :
386 :
387 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|