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 : ScopedVclPtrInstance< VirtualDevice > pVDev;
82 0 : ObjectVector::const_iterator aIter( maObjects.begin() );
83 :
84 0 : pVDev->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 : pVDev->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 MetaActionType nType = pAction->GetType();
99 :
100 0 : switch( nType )
101 : {
102 : case( MetaActionType::TEXT ):
103 : {
104 0 : const MetaTextAction* pA = static_cast<const MetaTextAction*>(pAction);
105 0 : sal_Int32 aLength=std::min( pA->GetText().getLength(), pA->GetLen() );
106 0 : aText = pA->GetText().copy( pA->GetIndex(), aLength );
107 : }
108 0 : break;
109 :
110 : case( MetaActionType::TEXTRECT ):
111 : {
112 0 : const MetaTextRectAction* pA = static_cast<const MetaTextRectAction*>(pAction);
113 0 : aText = pA->GetText();
114 : }
115 0 : break;
116 :
117 : case( MetaActionType::TEXTARRAY ):
118 : {
119 0 : const MetaTextArrayAction* pA = static_cast<const MetaTextArrayAction*>(pAction);
120 0 : sal_Int32 aLength=std::min( pA->GetText().getLength(), pA->GetLen() );
121 0 : aText = pA->GetText().copy( pA->GetIndex(), aLength );
122 : }
123 0 : break;
124 :
125 : case( MetaActionType::STRETCHTEXT ):
126 : {
127 0 : const MetaStretchTextAction* pA = static_cast<const MetaStretchTextAction*>(pAction);
128 0 : sal_Int32 aLength=std::min( pA->GetText().getLength(), pA->GetLen() );
129 0 : aText = pA->GetText().copy( pA->GetIndex(), aLength );
130 : }
131 0 : break;
132 :
133 : default:
134 0 : pAction->Execute( pVDev );
135 0 : break;
136 : }
137 :
138 0 : if( !aText.isEmpty() )
139 : {
140 0 : GlyphSet& rGlyphSet = implGetGlyphSet( pVDev->GetFont() );
141 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > xBI(
142 0 : vcl::unohelper::CreateBreakIterator() );
143 :
144 0 : if( xBI.is() )
145 : {
146 0 : const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetLanguageTag().getLocale();
147 0 : sal_Int32 nCurPos = 0, nLastPos = -1;
148 :
149 0 : while( ( nCurPos < aText.getLength() ) && ( nCurPos > nLastPos ) )
150 : {
151 0 : sal_Int32 nCount2 = 1;
152 :
153 0 : nLastPos = nCurPos;
154 0 : nCurPos = xBI->nextCharacters( aText, nCurPos, rLocale,
155 : ::com::sun::star::i18n::CharacterIteratorMode::SKIPCELL,
156 0 : nCount2, nCount2 );
157 :
158 0 : rGlyphSet.insert( aText.copy( nLastPos, nCurPos - nLastPos ) );
159 : }
160 : }
161 : else
162 : {
163 0 : const sal_Unicode* pStr = aText.getStr();
164 :
165 0 : for( sal_uInt32 k = 0, nLen = aText.getLength(); k < nLen; ++k )
166 0 : rGlyphSet.insert( OUString( pStr[ k ] ) );
167 0 : }
168 : }
169 0 : }
170 :
171 0 : pVDev->Pop();
172 : }
173 :
174 0 : ++aIter;
175 0 : }
176 0 : }
177 :
178 :
179 :
180 0 : void SVGFontExport::implEmbedFont( const vcl::Font& rFont )
181 : {
182 0 : if( mrExport.IsEmbedFonts() )
183 : {
184 0 : GlyphSet& rGlyphSet = implGetGlyphSet( rFont );
185 :
186 0 : if( !rGlyphSet.empty() )
187 : {
188 0 : GlyphSet::const_iterator aIter( rGlyphSet.begin() );
189 0 : const OUString aEmbeddedFontStr( "EmbeddedFont_" );
190 :
191 : {
192 0 : SvXMLElementExport aExp( mrExport, XML_NAMESPACE_NONE, "defs", true, true );
193 0 : OUString aCurIdStr( aEmbeddedFontStr );
194 0 : OUString aUnitsPerEM( OUString::number( nFontEM ) );
195 0 : ScopedVclPtrInstance< VirtualDevice > pVDev;
196 0 : vcl::Font aFont( rFont );
197 :
198 0 : aFont.SetSize( Size( 0, nFontEM ) );
199 0 : aFont.SetAlign( ALIGN_BASELINE );
200 :
201 0 : pVDev->SetMapMode( MAP_100TH_MM );
202 0 : pVDev->SetFont( aFont );
203 :
204 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "id", aCurIdStr += OUString::number( ++mnCurFontId ) );
205 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "horiz-adv-x", aUnitsPerEM );
206 :
207 : {
208 0 : SvXMLElementExport aExp2( mrExport, XML_NAMESPACE_NONE, "font", true, true );
209 0 : OUString aFontWeight;
210 0 : OUString aFontStyle;
211 0 : const Size aSize( nFontEM, nFontEM );
212 :
213 : // Font Weight
214 0 : if( aFont.GetWeight() != WEIGHT_NORMAL )
215 0 : aFontWeight = "bold";
216 : else
217 0 : aFontWeight = "normal";
218 :
219 : // Font Italic
220 0 : if( aFont.GetItalic() != ITALIC_NONE )
221 0 : aFontStyle = "italic";
222 : else
223 0 : aFontStyle = "normal";
224 :
225 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "font-family", GetMappedFontName( rFont.GetName() ) );
226 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "units-per-em", aUnitsPerEM );
227 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "font-weight", aFontWeight );
228 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "font-style", aFontStyle );
229 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "ascent", OUString::number( pVDev->GetFontMetric().GetAscent() ) );
230 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "descent", OUString::number( pVDev->GetFontMetric().GetDescent() ) );
231 :
232 : {
233 0 : SvXMLElementExport aExp3( mrExport, XML_NAMESPACE_NONE, "font-face", true, true );
234 : }
235 :
236 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "horiz-adv-x", OUString::number( aSize.Width() ) );
237 :
238 : {
239 0 : const Point aPos;
240 0 : const tools::PolyPolygon aMissingGlyphPolyPoly( Rectangle( aPos, aSize ) );
241 :
242 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "d", SVGActionWriter::GetPathString( aMissingGlyphPolyPoly, false ) );
243 :
244 : {
245 0 : SvXMLElementExport aExp4( mrExport, XML_NAMESPACE_NONE, "missing-glyph", true, true );
246 0 : }
247 : }
248 :
249 0 : while( aIter != rGlyphSet.end() )
250 : {
251 0 : implEmbedGlyph( *pVDev.get(), *aIter );
252 0 : ++aIter;
253 0 : }
254 0 : }
255 0 : }
256 : }
257 : }
258 0 : }
259 :
260 :
261 :
262 0 : void SVGFontExport::implEmbedGlyph( OutputDevice& rOut, const OUString& rCellStr )
263 : {
264 0 : tools::PolyPolygon aPolyPoly;
265 0 : const sal_Unicode nSpace = ' ';
266 :
267 0 : if( rOut.GetTextOutline( aPolyPoly, rCellStr ) )
268 : {
269 0 : Rectangle aBoundRect;
270 :
271 0 : aPolyPoly.Scale( 1.0, -1.0 );
272 :
273 0 : if( !rOut.GetTextBoundRect( aBoundRect, rCellStr ) )
274 0 : aBoundRect = Rectangle( Point( 0, 0 ), Size( rOut.GetTextWidth( rCellStr ), 0 ) );
275 :
276 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "unicode", rCellStr );
277 :
278 0 : if( rCellStr[ 0 ] == nSpace && rCellStr.getLength() == 1 )
279 0 : aBoundRect = Rectangle( Point( 0, 0 ), Size( rOut.GetTextWidth( OUString(' ') ), 0 ) );
280 :
281 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "horiz-adv-x", OUString::number( aBoundRect.GetWidth() ) );
282 :
283 0 : const OUString aPathString( SVGActionWriter::GetPathString( aPolyPoly, false ) );
284 0 : if( !aPathString.isEmpty() )
285 : {
286 0 : mrExport.AddAttribute( XML_NAMESPACE_NONE, "d", aPathString );
287 : }
288 :
289 : {
290 0 : SvXMLElementExport aExp( mrExport, XML_NAMESPACE_NONE, "glyph", true, true );
291 0 : }
292 0 : }
293 0 : }
294 :
295 :
296 :
297 0 : void SVGFontExport::EmbedFonts()
298 : {
299 0 : implCollectGlyphs();
300 :
301 0 : GlyphTree::const_iterator aGlyphTreeIter( maGlyphTree.begin() );
302 :
303 0 : while( aGlyphTreeIter != maGlyphTree.end() )
304 : {
305 0 : const FontWeightMap& rFontWeightMap = (*aGlyphTreeIter).second;
306 0 : FontWeightMap::const_iterator aFontWeightIter( rFontWeightMap.begin() );
307 :
308 0 : while( aFontWeightIter != rFontWeightMap.end() )
309 : {
310 0 : const FontItalicMap& rFontItalicMap = (*aFontWeightIter).second;
311 0 : FontItalicMap::const_iterator aFontItalicIter( rFontItalicMap.begin() );
312 :
313 0 : while( aFontItalicIter != rFontItalicMap.end() )
314 : {
315 0 : vcl::Font aFont;
316 :
317 0 : aFont.SetName( (*aGlyphTreeIter).first );
318 0 : aFont.SetWeight( (*aFontWeightIter).first );
319 0 : aFont.SetItalic( (*aFontItalicIter).first );
320 :
321 0 : implEmbedFont( aFont );
322 :
323 0 : ++aFontItalicIter;
324 0 : }
325 :
326 0 : ++aFontWeightIter;
327 : }
328 :
329 0 : ++aGlyphTreeIter;
330 : }
331 0 : }
332 :
333 :
334 :
335 0 : OUString SVGFontExport::GetMappedFontName( const OUString& rFontName ) const
336 : {
337 0 : sal_Int32 nNextTokenPos( 0 );
338 0 : OUString aRet( rFontName.getToken( 0, ';', nNextTokenPos ) );
339 :
340 0 : if( mnCurFontId )
341 0 : aRet += " embedded";
342 :
343 0 : return aRet;
344 9 : }
345 :
346 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|