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