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 <sal/types.h>
21 :
22 : #include <cassert>
23 :
24 : #include <basebmp/scanlineformats.hxx>
25 : #include <basegfx/polygon/b2dpolypolygon.hxx>
26 : #include <basegfx/range/b2drange.hxx>
27 : #include <basegfx/range/b2ibox.hxx>
28 : #include <rtl/instance.hxx>
29 : #include <tools/debug.hxx>
30 : #include <vcl/sysdata.hxx>
31 :
32 : #include "generic/geninst.h"
33 : #include "generic/genpspgraphics.h"
34 : #include "generic/glyphcache.hxx"
35 : #include "headless/svpbmp.hxx"
36 : #include "headless/svpgdi.hxx"
37 : #include "impfont.hxx"
38 : #include "outfont.hxx"
39 : #include "PhysicalFontFace.hxx"
40 :
41 : class PhysicalFontCollection;
42 :
43 : using namespace basegfx;
44 : using namespace basebmp;
45 :
46 612 : class SvpGlyphPeer : public GlyphCachePeer
47 : {
48 : public:
49 306 : SvpGlyphPeer() {}
50 :
51 : BitmapDeviceSharedPtr GetGlyphBmp( ServerFont&, sal_GlyphId,
52 : basebmp::Format nBmpFormat, B2IPoint& rTargetPos );
53 :
54 : protected:
55 : virtual void RemovingFont( ServerFont& ) SAL_OVERRIDE;
56 : virtual void RemovingGlyph( GlyphData& ) SAL_OVERRIDE;
57 :
58 23172 : class SvpGcpHelper
59 : {
60 : public:
61 : RawBitmap maRawBitmap;
62 : BitmapDeviceSharedPtr maBitmapDev;
63 : };
64 : };
65 :
66 306 : class SvpGlyphCache : public GlyphCache
67 : {
68 : public:
69 306 : SvpGlyphCache( SvpGlyphPeer& rPeer ) : GlyphCache( rPeer) {}
70 341169 : SvpGlyphPeer& GetPeer() { return reinterpret_cast<SvpGlyphPeer&>( mrPeer ); }
71 : static SvpGlyphCache& GetInstance();
72 : };
73 :
74 : namespace
75 : {
76 : struct GlyphCacheHolder
77 : {
78 : private:
79 : SvpGlyphPeer* m_pSvpGlyphPeer;
80 : SvpGlyphCache* m_pSvpGlyphCache;
81 : public:
82 306 : GlyphCacheHolder()
83 : {
84 306 : m_pSvpGlyphPeer = new SvpGlyphPeer();
85 306 : m_pSvpGlyphCache = new SvpGlyphCache( *m_pSvpGlyphPeer );
86 306 : }
87 306 : void release()
88 : {
89 306 : delete m_pSvpGlyphCache;
90 306 : delete m_pSvpGlyphPeer;
91 306 : m_pSvpGlyphCache = NULL;
92 306 : m_pSvpGlyphPeer = NULL;
93 306 : }
94 922222 : SvpGlyphCache& getGlyphCache()
95 : {
96 922222 : return *m_pSvpGlyphCache;
97 : }
98 306 : ~GlyphCacheHolder()
99 : {
100 306 : release();
101 306 : }
102 : };
103 :
104 : struct theGlyphCacheHolder :
105 : public rtl::Static<GlyphCacheHolder, theGlyphCacheHolder>
106 : {};
107 : }
108 :
109 922222 : SvpGlyphCache& SvpGlyphCache::GetInstance()
110 : {
111 922222 : return theGlyphCacheHolder::get().getGlyphCache();
112 : }
113 :
114 3673363 : BitmapDeviceSharedPtr SvpGlyphPeer::GetGlyphBmp( ServerFont& rServerFont,
115 : sal_GlyphId aGlyphId, basebmp::Format nBmpFormat, B2IPoint& rTargetPos )
116 : {
117 3673363 : GlyphData& rGlyphData = rServerFont.GetGlyphData( aGlyphId );
118 :
119 3673363 : if( rGlyphData.ExtDataRef().meInfo != nBmpFormat )
120 : {
121 : SvpGcpHelper* pGcpHelper = static_cast<SvpGcpHelper*>(
122 23172 : rGlyphData.ExtDataRef().mpData);
123 23172 : bool bNew = pGcpHelper == 0;
124 23172 : if( bNew )
125 23172 : pGcpHelper = new SvpGcpHelper;
126 :
127 : // get glyph bitmap in matching format
128 23172 : bool bFound = false;
129 23172 : switch( nBmpFormat )
130 : {
131 : case FORMAT_ONE_BIT_LSB_GREY:
132 0 : bFound = rServerFont.GetGlyphBitmap1( aGlyphId, pGcpHelper->maRawBitmap );
133 0 : break;
134 : case FORMAT_EIGHT_BIT_GREY:
135 23172 : bFound = rServerFont.GetGlyphBitmap8( aGlyphId, pGcpHelper->maRawBitmap );
136 23172 : break;
137 : default:
138 : OSL_FAIL( "SVP GCP::GetGlyphBmp(): illegal scanline format");
139 : // fall back to black&white mask
140 0 : nBmpFormat = FORMAT_ONE_BIT_LSB_GREY;
141 0 : bFound = false;
142 0 : break;
143 : }
144 :
145 : // return .notdef glyph if needed
146 23172 : if( !bFound && (aGlyphId != 0) )
147 : {
148 0 : if( bNew )
149 0 : delete pGcpHelper;
150 0 : return GetGlyphBmp( rServerFont, 0, nBmpFormat, rTargetPos );
151 : }
152 :
153 : // construct alpha mask from raw bitmap
154 : const B2IVector aSize(
155 : pGcpHelper->maRawBitmap.mnScanlineSize,
156 23172 : pGcpHelper->maRawBitmap.mnHeight );
157 23172 : if( aSize.getX() && aSize.getY() )
158 : {
159 22396 : static PaletteMemorySharedVector aDummyPAL;
160 22396 : pGcpHelper->maBitmapDev = createBitmapDevice( aSize, true, nBmpFormat, pGcpHelper->maRawBitmap.mpBits, aDummyPAL );
161 : }
162 :
163 23172 : rGlyphData.ExtDataRef().meInfo = nBmpFormat;
164 23172 : rGlyphData.ExtDataRef().mpData = pGcpHelper;
165 : }
166 :
167 : SvpGcpHelper* pGcpHelper = static_cast<SvpGcpHelper*>(
168 3673363 : rGlyphData.ExtDataRef().mpData);
169 : assert(pGcpHelper != 0);
170 3673363 : rTargetPos += B2IPoint( pGcpHelper->maRawBitmap.mnXOffset, pGcpHelper->maRawBitmap.mnYOffset );
171 3673363 : return pGcpHelper->maBitmapDev;
172 : }
173 :
174 9613 : void SvpGlyphPeer::RemovingFont( ServerFont& )
175 : {
176 : // nothing to do: no font resources held in SvpGlyphPeer
177 9613 : }
178 :
179 0 : void SvpGlyphPeer::RemovingGlyph( GlyphData& rGlyphData )
180 : {
181 : SvpGcpHelper* pGcpHelper = static_cast<SvpGcpHelper*>(
182 0 : rGlyphData.ExtDataRef().mpData);
183 0 : rGlyphData.ExtDataRef().meInfo = basebmp::FORMAT_NONE;
184 0 : rGlyphData.ExtDataRef().mpData = 0;
185 0 : delete pGcpHelper;
186 0 : }
187 :
188 915221 : sal_uInt16 SvpSalGraphics::SetFont( FontSelectPattern* pIFSD, int nFallbackLevel )
189 : {
190 : // release all no longer needed font resources
191 15557703 : for( int i = nFallbackLevel; i < MAX_FALLBACK; ++i )
192 : {
193 14642482 : if( m_pServerFont[i] != NULL )
194 : {
195 : // old server side font is no longer referenced
196 290298 : SvpGlyphCache::GetInstance().UncacheFont( *m_pServerFont[i] );
197 290298 : m_pServerFont[i] = NULL;
198 : }
199 : }
200 :
201 : // return early if there is no new font
202 915221 : if( !pIFSD )
203 624780 : return 0;
204 :
205 : // handle the request for a non-native X11-font => use the GlyphCache
206 290441 : ServerFont* pServerFont = SvpGlyphCache::GetInstance().CacheFont( *pIFSD );
207 290441 : if( !pServerFont )
208 0 : return SAL_SETFONT_BADFONT;
209 :
210 : // check selected font
211 290441 : if( !pServerFont->TestFont() )
212 : {
213 0 : SvpGlyphCache::GetInstance().UncacheFont( *pServerFont );
214 0 : return SAL_SETFONT_BADFONT;
215 : }
216 :
217 : // update SalGraphics font settings
218 290441 : m_pServerFont[ nFallbackLevel ] = pServerFont;
219 290441 : return SAL_SETFONT_USEDRAWTEXTARRAY;
220 : }
221 :
222 22286 : void SvpSalGraphics::GetFontMetric( ImplFontMetricData* pMetric, int nFallbackLevel )
223 : {
224 22286 : if( nFallbackLevel >= MAX_FALLBACK )
225 22286 : return;
226 :
227 22286 : if( m_pServerFont[nFallbackLevel] != NULL )
228 : {
229 : long rDummyFactor;
230 22286 : m_pServerFont[nFallbackLevel]->FetchFontMetric( *pMetric, rDummyFactor );
231 : }
232 : }
233 :
234 13910 : const FontCharMapPtr SvpSalGraphics::GetFontCharMap() const
235 : {
236 13910 : if( !m_pServerFont[0] )
237 0 : return NULL;
238 :
239 13910 : const FontCharMapPtr pFCMap = m_pServerFont[0]->GetFontCharMap();
240 13910 : return pFCMap;
241 : }
242 :
243 1712 : bool SvpSalGraphics::GetFontCapabilities(vcl::FontCapabilities &rFontCapabilities) const
244 : {
245 1712 : if (!m_pServerFont[0])
246 0 : return false;
247 :
248 1712 : return m_pServerFont[0]->GetFontCapabilities(rFontCapabilities);
249 : }
250 :
251 314 : void SvpSalGraphics::GetDevFontList( PhysicalFontCollection* pFontCollection )
252 : {
253 314 : GlyphCache& rGC = SvpGlyphCache::GetInstance();
254 :
255 314 : psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
256 314 : psp::FastPrintFontInfo aInfo;
257 628 : ::std::list< psp::fontID > aList;
258 314 : rMgr.getFontList( aList );
259 314 : ::std::list< psp::fontID >::iterator it;
260 48042 : for( it = aList.begin(); it != aList.end(); ++it )
261 : {
262 47728 : if( !rMgr.getFontFastInfo( *it, aInfo ) )
263 0 : continue;
264 :
265 : // normalize face number to the GlyphCache
266 47728 : int nFaceNum = rMgr.getFontFaceNumber( aInfo.m_nID );
267 :
268 : // inform GlyphCache about this font provided by the PsPrint subsystem
269 47728 : ImplDevFontAttributes aDFA = GenPspGraphics::Info2DevFontAttributes( aInfo );
270 47728 : aDFA.mnQuality += 4096;
271 95456 : const OString& rFileName = rMgr.getFontFileSysPath( aInfo.m_nID );
272 47728 : rGC.AddFontFile( rFileName, nFaceNum, aInfo.m_nID, aDFA );
273 47728 : }
274 :
275 : // announce glyphcache fonts
276 314 : rGC.AnnounceFonts( pFontCollection );
277 :
278 : // register platform specific font substitutions if available
279 314 : SalGenericInstance::RegisterFontSubstitutors( pFontCollection );
280 :
281 628 : ImplGetSVData()->maGDIData.mbNativeFontConfig = true;
282 314 : }
283 :
284 0 : void SvpSalGraphics::ClearDevFontCache()
285 : {
286 0 : GlyphCache& rGC = SvpGlyphCache::GetInstance();
287 0 : rGC.ClearFontCache();
288 0 : }
289 :
290 0 : bool SvpSalGraphics::AddTempDevFont( PhysicalFontCollection*,
291 : const OUString&, const OUString& )
292 : {
293 0 : return false;
294 : }
295 :
296 0 : bool SvpSalGraphics::CreateFontSubset(
297 : const OUString& rToFile,
298 : const PhysicalFontFace* pFont,
299 : sal_GlyphId* pGlyphIds,
300 : sal_uInt8* pEncoding,
301 : sal_Int32* pWidths,
302 : int nGlyphCount,
303 : FontSubsetInfo& rInfo
304 : )
305 : {
306 : // in this context the pFont->GetFontId() is a valid PSP
307 : // font since they are the only ones left after the PDF
308 : // export has filtered its list of subsettable fonts (for
309 : // which this method was created). The correct way would
310 : // be to have the GlyphCache search for the PhysicalFontFace pFont
311 0 : psp::fontID aFont = pFont->GetFontId();
312 :
313 0 : psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
314 : bool bSuccess = rMgr.createFontSubset( rInfo,
315 : aFont,
316 : rToFile,
317 : pGlyphIds,
318 : pEncoding,
319 : pWidths,
320 0 : nGlyphCount );
321 0 : return bSuccess;
322 : }
323 :
324 0 : const Ucs2SIntMap* SvpSalGraphics::GetFontEncodingVector( const PhysicalFontFace* pFont, const Ucs2OStrMap** pNonEncoded )
325 : {
326 : // in this context the pFont->GetFontId() is a valid PSP
327 : // font since they are the only ones left after the PDF
328 : // export has filtered its list of subsettable fonts (for
329 : // which this method was created). The correct way would
330 : // be to have the GlyphCache search for the PhysicalFontFace pFont
331 0 : psp::fontID aFont = pFont->GetFontId();
332 0 : return GenPspGraphics::DoGetFontEncodingVector( aFont, pNonEncoded );
333 : }
334 :
335 0 : const void* SvpSalGraphics::GetEmbedFontData(
336 : const PhysicalFontFace* pFont,
337 : const sal_Ucs* pUnicodes,
338 : sal_Int32* pWidths,
339 : FontSubsetInfo& rInfo,
340 : long* pDataLen
341 : )
342 : {
343 : // in this context the pFont->GetFontId() is a valid PSP
344 : // font since they are the only ones left after the PDF
345 : // export has filtered its list of subsettable fonts (for
346 : // which this method was created). The correct way would
347 : // be to have the GlyphCache search for the PhysicalFontFace pFont
348 0 : psp::fontID aFont = pFont->GetFontId();
349 0 : return GenPspGraphics::DoGetEmbedFontData( aFont, pUnicodes, pWidths, rInfo, pDataLen );
350 : }
351 :
352 0 : void SvpSalGraphics::FreeEmbedFontData( const void* pData, long nLen )
353 : {
354 0 : GenPspGraphics::DoFreeEmbedFontData( pData, nLen );
355 0 : }
356 :
357 0 : void SvpSalGraphics::GetGlyphWidths( const PhysicalFontFace* pFont,
358 : bool bVertical,
359 : Int32Vector& rWidths,
360 : Ucs2UIntMap& rUnicodeEnc )
361 : {
362 : // in this context the pFont->GetFontId() is a valid PSP
363 : // font since they are the only ones left after the PDF
364 : // export has filtered its list of subsettable fonts (for
365 : // which this method was created). The correct way would
366 : // be to have the GlyphCache search for the PhysicalFontFace pFont
367 0 : psp::fontID aFont = pFont->GetFontId();
368 0 : GenPspGraphics::DoGetGlyphWidths( aFont, bVertical, rWidths, rUnicodeEnc );
369 0 : }
370 :
371 683646 : bool SvpSalGraphics::GetGlyphBoundRect( sal_GlyphId aGlyphId, Rectangle& rRect )
372 : {
373 683646 : const int nLevel = aGlyphId >> GF_FONTSHIFT;
374 683646 : if( nLevel >= MAX_FALLBACK )
375 0 : return false;
376 :
377 683646 : ServerFont* pSF = m_pServerFont[ nLevel ];
378 683646 : if( !pSF )
379 0 : return false;
380 :
381 683646 : aGlyphId &= GF_IDXMASK;
382 683646 : const GlyphMetric& rGM = pSF->GetGlyphMetric( aGlyphId );
383 683646 : rRect = Rectangle( rGM.GetOffset(), rGM.GetSize() );
384 683646 : return true;
385 : }
386 :
387 2632 : bool SvpSalGraphics::GetGlyphOutline( sal_GlyphId aGlyphId, B2DPolyPolygon& rPolyPoly )
388 : {
389 2632 : const int nLevel = aGlyphId >> GF_FONTSHIFT;
390 2632 : if( nLevel >= MAX_FALLBACK )
391 0 : return false;
392 :
393 2632 : const ServerFont* pSF = m_pServerFont[ nLevel ];
394 2632 : if( !pSF )
395 0 : return false;
396 :
397 2632 : aGlyphId &= GF_IDXMASK;
398 2632 : if( pSF->GetGlyphOutline( aGlyphId, rPolyPoly ) )
399 2632 : return true;
400 :
401 0 : return false;
402 : }
403 :
404 2150300 : SalLayout* SvpSalGraphics::GetTextLayout( ImplLayoutArgs&, int nFallbackLevel )
405 : {
406 2150300 : GenericSalLayout* pLayout = NULL;
407 :
408 2150300 : if( m_pServerFont[ nFallbackLevel ] )
409 2150300 : pLayout = new ServerFontLayout( *m_pServerFont[ nFallbackLevel ] );
410 :
411 2150300 : return pLayout;
412 : }
413 :
414 341169 : void SvpSalGraphics::DrawServerFontLayout( const ServerFontLayout& rSalLayout )
415 : {
416 : // iterate over all glyphs in the layout
417 341169 : Point aPos;
418 : sal_GlyphId aGlyphId;
419 341169 : SvpGlyphPeer& rGlyphPeer = SvpGlyphCache::GetInstance().GetPeer();
420 4355701 : for( int nStart = 0; rSalLayout.GetNextGlyphs( 1, &aGlyphId, aPos, nStart ); )
421 : {
422 3673363 : int nLevel = aGlyphId >> GF_FONTSHIFT;
423 : DBG_ASSERT( nLevel < MAX_FALLBACK, "SvpGDI: invalid glyph fallback level" );
424 3673363 : ServerFont* pSF = m_pServerFont[ nLevel ];
425 3673363 : if( !pSF )
426 399150 : continue;
427 :
428 : // get the glyph's alpha mask and adjust the drawing position
429 3673363 : aGlyphId &= GF_IDXMASK;
430 3673363 : B2IPoint aDstPoint( aPos.X(), aPos.Y() );
431 : BitmapDeviceSharedPtr aAlphaMask
432 6947576 : = rGlyphPeer.GetGlyphBmp( *pSF, aGlyphId, m_eTextFmt, aDstPoint );
433 3673363 : if( !aAlphaMask ) // ignore empty glyphs
434 399150 : continue;
435 :
436 : // blend text color into target using the glyph's mask
437 3274213 : const B2IBox aSrcRect( B2ITuple(0,0), aAlphaMask->getSize() );
438 3274213 : const B2IBox aClipRect( aDstPoint, aAlphaMask->getSize() );
439 :
440 6548426 : SvpSalGraphics::ClipUndoHandle aUndo( this );
441 3274213 : if( !isClippedSetup( aClipRect, aUndo ) )
442 : m_aDevice->drawMaskedColor( m_aTextColor, aAlphaMask,
443 3274113 : aSrcRect, aDstPoint, m_aClipMap );
444 3274213 : }
445 341169 : }
446 :
447 92673 : void SvpSalGraphics::SetTextColor( SalColor nSalColor )
448 : {
449 92673 : m_aTextColor = basebmp::Color( nSalColor );
450 92673 : }
451 :
452 0 : SystemFontData SvpSalGraphics::GetSysFontData( int nFallbacklevel ) const
453 : {
454 0 : SystemFontData aSysFontData;
455 :
456 0 : if (nFallbacklevel >= MAX_FALLBACK) nFallbacklevel = MAX_FALLBACK - 1;
457 0 : if (nFallbacklevel < 0 ) nFallbacklevel = 0;
458 :
459 0 : aSysFontData.nSize = sizeof( SystemFontData );
460 0 : aSysFontData.nFontId = 0;
461 0 : aSysFontData.nFontFlags = 0;
462 0 : aSysFontData.bFakeBold = false;
463 0 : aSysFontData.bFakeItalic = false;
464 0 : aSysFontData.bAntialias = true;
465 0 : return aSysFontData;
466 1125 : }
467 :
468 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|