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 :
21 : #include "svgfontexport.hxx"
22 : #include "svgfilter.hxx"
23 : #include "svgwriter.hxx"
24 :
25 :
26 : #include <vcl/unohelp.hxx>
27 : #include <vcl/font.hxx>
28 : #include <vcl/outdev.hxx>
29 :
30 : static const sal_Int32 nFontEM = 2048;
31 :
32 : // -----------------
33 : // - SVGFontExport -
34 : // -----------------
35 :
36 0 : SVGFontExport::SVGFontExport( SVGExport& rExport, const ::std::vector< ObjectRepresentation >& rObjects ) :
37 : mrExport( rExport ),
38 : maObjects( rObjects ),
39 0 : mnCurFontId( 0 )
40 : {
41 0 : }
42 :
43 : // -----------------------------------------------------------------------------
44 :
45 0 : SVGFontExport::~SVGFontExport()
46 : {
47 0 : }
48 :
49 : // -----------------------------------------------------------------------------
50 :
51 0 : SVGFontExport::GlyphSet& SVGFontExport::implGetGlyphSet( const Font& rFont )
52 : {
53 0 : FontWeight eWeight( WEIGHT_NORMAL );
54 0 : FontItalic eItalic( ITALIC_NONE );
55 0 : ::rtl::OUString aFontName( rFont.GetName() );
56 0 : sal_Int32 nNextTokenPos( 0 );
57 :
58 0 : switch( rFont.GetWeight() )
59 : {
60 : case WEIGHT_BOLD:
61 : case WEIGHT_ULTRABOLD:
62 : case WEIGHT_BLACK:
63 0 : eWeight = WEIGHT_BOLD;
64 0 : break;
65 :
66 : default:
67 0 : break;
68 : }
69 :
70 0 : if( rFont.GetItalic() != ITALIC_NONE )
71 0 : eItalic = ITALIC_NORMAL;
72 :
73 0 : return( maGlyphTree[ aFontName.getToken( 0, ';', nNextTokenPos ) ][ eWeight ][ eItalic ] );
74 : }
75 :
76 : // -----------------------------------------------------------------------------
77 :
78 0 : void SVGFontExport::implCollectGlyphs()
79 : {
80 0 : VirtualDevice aVDev;
81 0 : ObjectVector::const_iterator aIter( maObjects.begin() );
82 :
83 0 : aVDev.EnableOutput( sal_False );
84 :
85 0 : while( aIter != maObjects.end() )
86 : {
87 0 : if( (*aIter).HasRepresentation() )
88 : {
89 0 : const GDIMetaFile& rMtf = (*aIter).GetRepresentation();
90 :
91 0 : aVDev.Push();
92 :
93 0 : for( size_t i = 0, nCount = rMtf.GetActionSize(); i < nCount; ++i )
94 : {
95 0 : ::rtl::OUString aText;
96 0 : MetaAction* pAction = rMtf.GetAction( i );
97 0 : const sal_uInt16 nType = pAction->GetType();
98 :
99 0 : switch( nType )
100 : {
101 : case( META_TEXT_ACTION ):
102 : {
103 0 : const MetaTextAction* pA = (const MetaTextAction*) pAction;
104 0 : aText = String( pA->GetText(), pA->GetIndex(), pA->GetLen() );
105 : }
106 0 : break;
107 :
108 : case( META_TEXTRECT_ACTION ):
109 : {
110 0 : const MetaTextRectAction* pA = (const MetaTextRectAction*) pAction;
111 0 : aText = pA->GetText();
112 : }
113 0 : break;
114 :
115 : case( META_TEXTARRAY_ACTION ):
116 : {
117 0 : const MetaTextArrayAction* pA = (const MetaTextArrayAction*) pAction;
118 0 : aText = String( pA->GetText(), pA->GetIndex(), pA->GetLen() );
119 : }
120 0 : break;
121 :
122 : case( META_STRETCHTEXT_ACTION ):
123 : {
124 0 : const MetaStretchTextAction* pA = (const MetaStretchTextAction*) pAction;
125 0 : aText = String( pA->GetText(), pA->GetIndex(), pA->GetLen() );
126 : }
127 0 : break;
128 :
129 : default:
130 0 : pAction->Execute( &aVDev );
131 0 : break;
132 : }
133 :
134 0 : if( !aText.isEmpty() )
135 : {
136 0 : GlyphSet& rGlyphSet = implGetGlyphSet( aVDev.GetFont() );
137 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > xBI(
138 0 : ::vcl::unohelper::CreateBreakIterator() );
139 :
140 0 : if( xBI.is() )
141 : {
142 0 : const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetLanguageTag().getLocale();
143 0 : sal_Int32 nCurPos = 0, nLastPos = -1;
144 :
145 0 : while( ( nCurPos < aText.getLength() ) && ( nCurPos > nLastPos ) )
146 : {
147 0 : sal_Int32 nCount2 = 1;
148 :
149 0 : nLastPos = nCurPos;
150 0 : nCurPos = xBI->nextCharacters( aText, nCurPos, rLocale,
151 : ::com::sun::star::i18n::CharacterIteratorMode::SKIPCELL,
152 0 : nCount2, nCount2 );
153 :
154 0 : rGlyphSet.insert( aText.copy( nLastPos, nCurPos - nLastPos ) );
155 : }
156 : }
157 : else
158 : {
159 0 : const sal_Unicode* pStr = aText.getStr();
160 :
161 0 : for( sal_uInt32 k = 0, nLen = aText.getLength(); k < nLen; ++k )
162 0 : rGlyphSet.insert( rtl::OUString( pStr[ k ] ) );
163 0 : }
164 : }
165 0 : }
166 :
167 0 : aVDev.Pop();
168 : }
169 :
170 0 : ++aIter;
171 0 : }
172 0 : }
173 :
174 : // -----------------------------------------------------------------------------
175 :
176 0 : void SVGFontExport::implEmbedFont( const Font& rFont )
177 : {
178 0 : if( mrExport.IsEmbedFonts() )
179 : {
180 0 : GlyphSet& rGlyphSet = implGetGlyphSet( rFont );
181 :
182 0 : if( !rGlyphSet.empty() )
183 : {
184 0 : GlyphSet::const_iterator aIter( rGlyphSet.begin() );
185 0 : const ::rtl::OUString aEmbeddedFontStr( B2UCONST( "EmbeddedFont_" ) );
186 :
187 : {
188 0 : SvXMLElementExport aExp( mrExport, XML_NAMESPACE_NONE, "defs", sal_True, sal_True );
189 0 : ::rtl::OUString aCurIdStr( aEmbeddedFontStr );
190 0 : ::rtl::OUString aUnitsPerEM( ::rtl::OUString::valueOf( nFontEM ) );
191 0 : VirtualDevice aVDev;
192 0 : Font aFont( rFont );
193 :
194 0 : aFont.SetSize( Size( 0, nFontEM ) );
195 0 : aFont.SetAlign( ALIGN_BASELINE );
196 :
197 0 : aVDev.SetMapMode( MAP_100TH_MM );
198 0 : aVDev.SetFont( aFont );
199 :
200 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "id", aCurIdStr += ::rtl::OUString::valueOf( ++mnCurFontId ) );
201 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "horiz-adv-x", aUnitsPerEM );
202 :
203 : {
204 0 : SvXMLElementExport aExp2( mrExport, XML_NAMESPACE_NONE, "font", sal_True, sal_True );
205 0 : ::rtl::OUString aFontWeight;
206 0 : ::rtl::OUString aFontStyle;
207 0 : const Size aSize( nFontEM, nFontEM );
208 :
209 : // Font Weight
210 0 : if( aFont.GetWeight() != WEIGHT_NORMAL )
211 0 : aFontWeight = B2UCONST( "bold" );
212 : else
213 0 : aFontWeight = B2UCONST( "normal" );
214 :
215 : // Font Italic
216 0 : if( aFont.GetItalic() != ITALIC_NONE )
217 0 : aFontStyle = B2UCONST( "italic" );
218 : else
219 0 : aFontStyle = B2UCONST( "normal" );
220 :
221 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "font-family", GetMappedFontName( rFont.GetName() ) );
222 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "units-per-em", aUnitsPerEM );
223 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "font-weight", aFontWeight );
224 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "font-style", aFontStyle );
225 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "ascent", ::rtl::OUString::valueOf( aVDev.GetFontMetric().GetAscent() ) );
226 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "descent", ::rtl::OUString::valueOf( aVDev.GetFontMetric().GetDescent() ) );
227 :
228 : {
229 0 : SvXMLElementExport aExp3( mrExport, XML_NAMESPACE_NONE, "font-face", sal_True, sal_True );
230 : }
231 :
232 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "horiz-adv-x", ::rtl::OUString::valueOf( aSize.Width() ) );
233 :
234 : {
235 0 : const Point aPos;
236 0 : const PolyPolygon aMissingGlyphPolyPoly( Rectangle( aPos, aSize ) );
237 :
238 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "d", SVGActionWriter::GetPathString( aMissingGlyphPolyPoly, sal_False ) );
239 :
240 : {
241 0 : SvXMLElementExport aExp4( mrExport, XML_NAMESPACE_NONE, "missing-glyph", sal_True, sal_True );
242 0 : }
243 : }
244 :
245 0 : while( aIter != rGlyphSet.end() )
246 : {
247 0 : implEmbedGlyph( aVDev, *aIter );
248 0 : ++aIter;
249 0 : }
250 0 : }
251 0 : }
252 : }
253 : }
254 0 : }
255 :
256 : // -----------------------------------------------------------------------------
257 :
258 0 : void SVGFontExport::implEmbedGlyph( OutputDevice& rOut, const ::rtl::OUString& rCellStr )
259 : {
260 0 : PolyPolygon aPolyPoly;
261 0 : const sal_Unicode nSpace = ' ';
262 :
263 0 : if( rOut.GetTextOutline( aPolyPoly, rCellStr ) )
264 : {
265 0 : Rectangle aBoundRect;
266 :
267 0 : aPolyPoly.Scale( 1.0, -1.0 );
268 :
269 0 : if( !rOut.GetTextBoundRect( aBoundRect, rCellStr ) )
270 0 : aBoundRect = Rectangle( Point( 0, 0 ), Size( rOut.GetTextWidth( rCellStr ), 0 ) );
271 :
272 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "unicode", rCellStr );
273 :
274 0 : if( rCellStr[ 0 ] == nSpace && rCellStr.getLength() == 1 )
275 0 : aBoundRect = Rectangle( Point( 0, 0 ), Size( rOut.GetTextWidth( rtl::OUString(' ') ), 0 ) );
276 :
277 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "horiz-adv-x", ::rtl::OUString::valueOf( aBoundRect.GetWidth() ) );
278 :
279 0 : const ::rtl::OUString aPathString( SVGActionWriter::GetPathString( aPolyPoly, sal_False ) );
280 0 : if( !aPathString.isEmpty() )
281 : {
282 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "d", aPathString );
283 : }
284 :
285 : {
286 0 : SvXMLElementExport aExp( mrExport, XML_NAMESPACE_NONE, "glyph", sal_True, sal_True );
287 0 : }
288 0 : }
289 0 : }
290 :
291 : // -----------------------------------------------------------------------------
292 :
293 0 : void SVGFontExport::EmbedFonts()
294 : {
295 0 : implCollectGlyphs();
296 :
297 0 : GlyphTree::const_iterator aGlyphTreeIter( maGlyphTree.begin() );
298 :
299 0 : while( aGlyphTreeIter != maGlyphTree.end() )
300 : {
301 0 : const FontWeightMap& rFontWeightMap = (*aGlyphTreeIter).second;
302 0 : FontWeightMap::const_iterator aFontWeightIter( rFontWeightMap.begin() );
303 :
304 0 : while( aFontWeightIter != rFontWeightMap.end() )
305 : {
306 0 : const FontItalicMap& rFontItalicMap = (*aFontWeightIter).second;
307 0 : FontItalicMap::const_iterator aFontItalicIter( rFontItalicMap.begin() );
308 :
309 0 : while( aFontItalicIter != rFontItalicMap.end() )
310 : {
311 0 : Font aFont;
312 :
313 0 : aFont.SetName( (*aGlyphTreeIter).first );
314 0 : aFont.SetWeight( (*aFontWeightIter).first );
315 0 : aFont.SetItalic( (*aFontItalicIter).first );
316 :
317 0 : implEmbedFont( aFont );
318 :
319 0 : ++aFontItalicIter;
320 0 : }
321 :
322 0 : ++aFontWeightIter;
323 : }
324 :
325 0 : ++aGlyphTreeIter;
326 : }
327 0 : }
328 :
329 : // -----------------------------------------------------------------------------
330 :
331 0 : ::rtl::OUString SVGFontExport::GetMappedFontName( const ::rtl::OUString& rFontName ) const
332 : {
333 0 : sal_Int32 nNextTokenPos( 0 );
334 0 : ::rtl::OUString aRet( rFontName.getToken( 0, ';', nNextTokenPos ) );
335 :
336 0 : if( mnCurFontId )
337 0 : aRet += B2UCONST( " embedded" );
338 :
339 0 : return aRet;
340 0 : }
341 :
342 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|