LCOV - code coverage report
Current view: top level - vcl/inc/generic - glyphcache.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 39 47 83.0 %
Date: 2012-08-25 Functions: 34 45 75.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 6 12 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #ifndef _SV_GLYPHCACHE_HXX
      30                 :            : #define _SV_GLYPHCACHE_HXX
      31                 :            : 
      32                 :            : #include <vcl/dllapi.h>
      33                 :            : 
      34                 :            : class GlyphCache;
      35                 :            : class GlyphMetric;
      36                 :            : class GlyphData;
      37                 :            : class ServerFont;
      38                 :            : class GlyphCachePeer;
      39                 :            : class ServerFontLayoutEngine;
      40                 :            : class ServerFontLayout;
      41                 :            : class ExtraKernInfo;
      42                 :            : struct ImplKernPairData;
      43                 :            : class ImplFontOptions;
      44                 :            : 
      45                 :            : #include <tools/gen.hxx>
      46                 :            : #include <boost/unordered_map.hpp>
      47                 :            : #include <boost/unordered_set.hpp>
      48                 :            : #include <boost/shared_ptr.hpp>
      49                 :            : 
      50                 :            : namespace basegfx { class B2DPolyPolygon; }
      51                 :            : 
      52                 :            : class RawBitmap;
      53                 :            : 
      54                 :            : #include <outfont.hxx>
      55                 :            : #include <impfont.hxx>
      56                 :            : 
      57                 :            : class ServerFontLayout;
      58                 :            : #include <sallayout.hxx>
      59                 :            : 
      60                 :            : #ifdef ENABLE_GRAPHITE
      61                 :            : class GraphiteFaceWrapper;
      62                 :            : #endif
      63                 :            : 
      64                 :            : #include <ft2build.h>
      65                 :            : #include FT_FREETYPE_H
      66                 :            : #include FT_GLYPH_H
      67                 :            : 
      68                 :            : namespace vcl
      69                 :            : {
      70                 :            :     struct FontCapabilities;
      71                 :            : }
      72                 :            : 
      73                 :            : // =======================================================================
      74                 :            : 
      75                 :            : class VCL_DLLPUBLIC GlyphCache
      76                 :            : {
      77                 :            : public:
      78                 :            :     explicit                    GlyphCache( GlyphCachePeer& );
      79                 :            :     /*virtual*/                 ~GlyphCache();
      80                 :            : 
      81                 :            :     static GlyphCache&      GetInstance();
      82                 :            : 
      83                 :            :     void                        AddFontFile( const rtl::OString& rNormalizedName,
      84                 :            :                                     int nFaceNum, sal_IntPtr nFontId, const ImplDevFontAttributes&,
      85                 :            :                                     const ExtraKernInfo* = NULL );
      86                 :            :     void                        AnnounceFonts( ImplDevFontList* ) const;
      87                 :            : 
      88                 :            :     ServerFont*                 CacheFont( const FontSelectPattern& );
      89                 :            :     void                        UncacheFont( ServerFont& );
      90                 :            :     void                        InvalidateAllGlyphs();
      91                 :            : 
      92                 :            : protected:
      93                 :            :     GlyphCachePeer&             mrPeer;
      94                 :            : 
      95                 :            : private:
      96                 :            :     friend class ServerFont;
      97                 :            :     // used by ServerFont class only
      98                 :            :     void                        AddedGlyph( ServerFont&, GlyphData& );
      99                 :            :     void                        RemovingGlyph( ServerFont&, GlyphData&, int nGlyphIndex );
     100                 :            :     void                        UsingGlyph( ServerFont&, GlyphData& );
     101                 :            :     void                        GrowNotify();
     102                 :            : 
     103                 :            : private:
     104                 :            :     void                        GarbageCollect();
     105                 :            : 
     106                 :            :     // the GlyphCache's FontList matches a font request to a serverfont instance
     107                 :            :     // the FontList key's mpFontData member is reinterpreted as integer font id
     108                 :            :     struct IFSD_Equal{  bool operator()( const FontSelectPattern&, const FontSelectPattern& ) const; };
     109                 :            :     struct IFSD_Hash{ size_t operator()( const FontSelectPattern& ) const; };
     110                 :            :     typedef ::boost::unordered_map<FontSelectPattern,ServerFont*,IFSD_Hash,IFSD_Equal > FontList;
     111                 :            :     FontList                    maFontList;
     112                 :            :     sal_uLong                       mnMaxSize;      // max overall cache size in bytes
     113                 :            :     mutable sal_uLong               mnBytesUsed;
     114                 :            :     mutable long                mnLruIndex;
     115                 :            :     mutable int                 mnGlyphCount;
     116                 :            :     ServerFont*                 mpCurrentGCFont;
     117                 :            : 
     118                 :            :     class FreetypeManager*      mpFtManager;
     119                 :            : };
     120                 :            : 
     121                 :            : // =======================================================================
     122                 :            : 
     123                 :      39108 : class GlyphMetric
     124                 :            : {
     125                 :            : public:
     126                 :     354411 :     Point                   GetOffset() const   { return maOffset; }
     127                 :            :     Point                   GetDelta() const    { return maDelta; }
     128                 :     354411 :     Size                    GetSize() const     { return maSize; }
     129                 :   66664892 :     long                    GetCharWidth() const { return mnAdvanceWidth; }
     130                 :            : 
     131                 :            : protected:
     132                 :            :     friend class GlyphData;
     133                 :      39108 :     void                    SetOffset( int nX, int nY ) { maOffset = Point( nX, nY); }
     134                 :      39108 :     void                    SetDelta( int nX, int nY )  { maDelta = Point( nX, nY); }
     135                 :      39108 :     void                    SetSize( const Size& s )    { maSize = s; }
     136                 :      39108 :     void                    SetCharWidth( long nW )     { mnAdvanceWidth = nW; }
     137                 :            : 
     138                 :            : private:
     139                 :            :     long                    mnAdvanceWidth;
     140                 :            :     Point                   maDelta;
     141                 :            :     Point                   maOffset;
     142                 :            :     Size                    maSize;
     143                 :            : };
     144                 :            : 
     145                 :            : // -----------------------------------------------------------------------
     146                 :            : 
     147                 :            : // the glyph specific data needed by a GlyphCachePeer is usually trivial,
     148                 :            : // not attaching it to the corresponding GlyphData would be overkill
     149                 :            : struct ExtGlyphData
     150                 :            : {
     151                 :            :     int     meInfo;
     152                 :            :     void*   mpData;
     153                 :            : 
     154                 :      39108 :     ExtGlyphData() : meInfo(0), mpData(NULL) {}
     155                 :            : };
     156                 :            : 
     157                 :            : // -----------------------------------------------------------------------
     158                 :            : 
     159                 :      39108 : class GlyphData
     160                 :            : {
     161                 :            : public:
     162                 :   67019303 :     const GlyphMetric&      GetMetric() const           { return maMetric; }
     163                 :            :     Size                    GetSize() const             { return maMetric.GetSize(); }
     164                 :            : 
     165                 :      39108 :     void                    SetSize( const Size& s)     { maMetric.SetSize( s ); }
     166                 :      39108 :     void                    SetOffset( int nX, int nY ) { maMetric.SetOffset( nX, nY ); }
     167                 :      39108 :     void                    SetDelta( int nX, int nY )  { maMetric.SetDelta( nX, nY ); }
     168                 :      39108 :     void                    SetCharWidth( long nW )     { maMetric.SetCharWidth( nW ); }
     169                 :            : 
     170                 :   68428520 :     void                    SetLruValue( int n ) const  { mnLruValue = n; }
     171                 :          0 :     long                    GetLruValue() const         { return mnLruValue;}
     172                 :            : 
     173                 :    5817939 :     ExtGlyphData&           ExtDataRef()                { return maExtData; }
     174                 :            :     const ExtGlyphData&     ExtDataRef() const          { return maExtData; }
     175                 :            : 
     176                 :            : private:
     177                 :            :     GlyphMetric             maMetric;
     178                 :            :     ExtGlyphData            maExtData;
     179                 :            : 
     180                 :            :     // used by GlyphCache for cache LRU algorithm
     181                 :            :     mutable long            mnLruValue;
     182                 :            : };
     183                 :            : 
     184                 :            : // =======================================================================
     185                 :            : 
     186                 :            : class FtFontInfo;
     187                 :            : 
     188                 :            : class VCL_DLLPUBLIC ServerFont
     189                 :            : {
     190                 :            : public:
     191                 :            :     ServerFont( const FontSelectPattern&, FtFontInfo* );
     192                 :            :     virtual ~ServerFont();
     193                 :            : 
     194                 :            :     const ::rtl::OString* GetFontFileName() const;
     195                 :            :     bool                TestFont() const;
     196                 :            :     FT_Face             GetFtFace() const;
     197                 :          0 :     int                 GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
     198                 :            :     void                SetFontOptions( boost::shared_ptr<ImplFontOptions> );
     199                 :            :     boost::shared_ptr<ImplFontOptions> GetFontOptions() const;
     200                 :          0 :     bool                NeedsArtificialBold() const { return mbArtBold; }
     201                 :          0 :     bool                NeedsArtificialItalic() const { return mbArtItalic; }
     202                 :            : 
     203                 :  118994997 :     const FontSelectPattern&   GetFontSelData() const      { return maFontSelData; }
     204                 :            : 
     205                 :            :     void                FetchFontMetric( ImplFontMetricData&, long& rFactor ) const;
     206                 :            :     sal_uLong           GetKernPairs( ImplKernPairData** ) const;
     207                 :            :     const unsigned char* GetTable( const char* pName, sal_uLong* pLength );
     208                 :            :     int                 GetEmUnits() const;
     209                 :          0 :     const FT_Size_Metrics& GetMetricsFT() const { return maSizeFT->metrics; }
     210                 :            :     int                 GetGlyphKernValue( int, int ) const;
     211                 :            :     const ImplFontCharMap* GetImplFontCharMap() const;
     212                 :            :     bool                GetFontCapabilities(vcl::FontCapabilities &) const;
     213                 :            : 
     214                 :            :     GlyphData&                  GetGlyphData( int nGlyphIndex );
     215                 :   67017780 :     const GlyphMetric&          GetGlyphMetric( int nGlyphIndex )
     216                 :   67017780 :                                 { return GetGlyphData( nGlyphIndex ).GetMetric(); }
     217                 :            : #ifdef ENABLE_GRAPHITE
     218                 :            :     virtual GraphiteFaceWrapper* GetGraphiteFace() const;
     219                 :            : #endif
     220                 :            : 
     221                 :            :     int                 GetGlyphIndex( sal_UCS4 ) const;
     222                 :            :     int                 GetRawGlyphIndex( sal_UCS4 ) const;
     223                 :            :     int                 FixupGlyphIndex( int nGlyphIndex, sal_UCS4 ) const;
     224                 :            :     bool                GetGlyphOutline( int nGlyphIndex, ::basegfx::B2DPolyPolygon& ) const;
     225                 :            :     bool                GetAntialiasAdvice( void ) const;
     226                 :            :     bool                GetGlyphBitmap1( int nGlyphIndex, RawBitmap& ) const;
     227                 :            :     bool                GetGlyphBitmap8( int nGlyphIndex, RawBitmap& ) const;
     228                 :            : 
     229                 :            :     void                        SetExtended( int nInfo, void* ppVoid );
     230                 :            :     int                         GetExtInfo() { return mnExtInfo; }
     231                 :            :     void*                       GetExtPointer() { return mpExtData; }
     232                 :            : 
     233                 :            : private:
     234                 :            :     friend class GlyphCache;
     235                 :            :     friend class ServerFontLayout;
     236                 :            :     friend class X11SalGraphics;
     237                 :            : 
     238                 :     192189 :     void                        AddRef() const      { ++mnRefCount; }
     239                 :          0 :     long                        GetRefCount() const { return mnRefCount; }
     240                 :            :     long                        Release() const;
     241                 :       4519 :     sal_uLong                       GetByteCount() const { return mnBytesUsed; }
     242                 :            : 
     243                 :            :     void                InitGlyphData( int nGlyphIndex, GlyphData& ) const;
     244                 :            :     void                GarbageCollect( long );
     245                 :            :     void                        ReleaseFromGarbageCollect();
     246                 :            : 
     247                 :            :     int                 ApplyGlyphTransform( int nGlyphFlags, FT_GlyphRec_*, bool ) const;
     248                 :            :     bool                ApplyGSUB( const FontSelectPattern& );
     249                 :            : 
     250                 :            :     ServerFontLayoutEngine* GetLayoutEngine();
     251                 :            : 
     252                 :            :     typedef ::boost::unordered_map<int,GlyphData> GlyphList;
     253                 :            :     mutable GlyphList           maGlyphList;
     254                 :            : 
     255                 :            :     const FontSelectPattern    maFontSelData;
     256                 :            : 
     257                 :            :     // info for GlyphcachePeer
     258                 :            :     int                         mnExtInfo;
     259                 :            :     void*                       mpExtData;
     260                 :            : 
     261                 :            :     // used by GlyphCache for cache LRU algorithm
     262                 :            :     mutable long                mnRefCount;
     263                 :            :     mutable sal_uLong               mnBytesUsed;
     264                 :            : 
     265                 :            :     ServerFont*                 mpPrevGCFont;
     266                 :            :     ServerFont*                 mpNextGCFont;
     267                 :            : 
     268                 :            :     // 16.16 fixed point values used for a rotated font
     269                 :            :     long                        mnCos;
     270                 :            :     long                        mnSin;
     271                 :            : 
     272                 :            :     int                         mnZWJ;
     273                 :            :     int                         mnZWNJ;
     274                 :            :     bool                        mbCollectedZW;
     275                 :            : 
     276                 :            :     int                         mnWidth;
     277                 :            :     int                         mnPrioEmbedded;
     278                 :            :     int                         mnPrioAntiAlias;
     279                 :            :     int                         mnPrioAutoHint;
     280                 :            :     FtFontInfo*                 mpFontInfo;
     281                 :            :     FT_Int                      mnLoadFlags;
     282                 :            :     double                      mfStretch;
     283                 :            :     FT_FaceRec_*                maFaceFT;
     284                 :            :     FT_SizeRec_*                maSizeFT;
     285                 :            : 
     286                 :            :     boost::shared_ptr<ImplFontOptions> mpFontOptions;
     287                 :            : 
     288                 :            :     bool                        mbFaceOk;
     289                 :            :     bool            mbArtItalic;
     290                 :            :     bool            mbArtBold;
     291                 :            :     bool            mbUseGamma;
     292                 :            : 
     293                 :            :     typedef ::boost::unordered_map<int,int> GlyphSubstitution;
     294                 :            :     GlyphSubstitution           maGlyphSubstitution;
     295                 :            :     rtl_UnicodeToTextConverter  maRecodeConverter;
     296                 :            : 
     297                 :            :     ServerFontLayoutEngine*     mpLayoutEngine;
     298                 :            : };
     299                 :            : 
     300                 :            : // =======================================================================
     301                 :            : 
     302                 :            : // a class for cache entries for physical font instances that are based on serverfonts
     303                 :            : class VCL_DLLPUBLIC ImplServerFontEntry : public ImplFontEntry
     304                 :            : {
     305                 :            : private:
     306                 :            :     ServerFont*    mpServerFont;
     307                 :            :     boost::shared_ptr<ImplFontOptions> mpFontOptions;
     308                 :            :     bool           mbGotFontOptions;
     309                 :            : 
     310                 :            : public:
     311                 :            :                    ImplServerFontEntry( FontSelectPattern& );
     312                 :            :     virtual        ~ImplServerFontEntry();
     313                 :       4519 :     void           SetServerFont( ServerFont* p) { mpServerFont = p; }
     314                 :            :     void           HandleFontOptions();
     315                 :            : };
     316                 :            : 
     317                 :            : // =======================================================================
     318                 :            : 
     319         [ -  + ]:    2084814 : class VCL_DLLPUBLIC ServerFontLayout : public GenericSalLayout
     320                 :            : {
     321                 :            : private:
     322                 :            :     ServerFont&     mrServerFont;
     323                 :            : 
     324                 :            :     // enforce proper copy semantic
     325                 :            :     SAL_DLLPRIVATE  ServerFontLayout( const ServerFontLayout& );
     326                 :            :     SAL_DLLPRIVATE  ServerFontLayout& operator=( const ServerFontLayout& );
     327                 :            : 
     328                 :            : public:
     329                 :            :                     ServerFontLayout( ServerFont& );
     330                 :            :     virtual bool    LayoutText( ImplLayoutArgs& );
     331                 :            :     virtual void    AdjustLayout( ImplLayoutArgs& );
     332                 :            :     virtual void    DrawText( SalGraphics& ) const;
     333                 :     993815 :     ServerFont&     GetServerFont() const   { return mrServerFont; }
     334                 :            : };
     335                 :            : 
     336                 :            : // =======================================================================
     337                 :            : 
     338                 :       2714 : class ServerFontLayoutEngine
     339                 :            : {
     340                 :            : public:
     341         [ -  + ]:       2734 :     virtual         ~ServerFontLayoutEngine() {}
     342                 :            :     virtual bool    operator()( ServerFontLayout&, ImplLayoutArgs& );
     343                 :            : };
     344                 :            : 
     345                 :            : // =======================================================================
     346                 :            : 
     347                 :            : class GlyphCachePeer
     348                 :            : {
     349                 :            : protected:
     350                 :        236 :                     GlyphCachePeer() : mnBytesUsed(0) {}
     351         [ -  + ]:        236 :     virtual         ~GlyphCachePeer() {}
     352                 :            : 
     353                 :            : public:
     354                 :     152965 :     sal_Int32       GetByteCount() const { return mnBytesUsed; }
     355                 :          0 :     virtual void    RemovingFont( ServerFont& ) {}
     356                 :          0 :     virtual void    RemovingGlyph( ServerFont&, GlyphData&, int ) {}
     357                 :            : 
     358                 :            : protected:
     359                 :            :     sal_Int32       mnBytesUsed;
     360                 :            : };
     361                 :            : 
     362                 :            : // =======================================================================
     363                 :            : 
     364                 :            : class VCL_DLLPUBLIC RawBitmap
     365                 :            : {
     366                 :            : public:
     367                 :            :                     RawBitmap();
     368                 :            :                     ~RawBitmap();
     369                 :            :     bool            Rotate( int nAngle );
     370                 :            : 
     371                 :            : public:
     372                 :            :     unsigned char*  mpBits;
     373                 :            :     sal_uLong           mnAllocated;
     374                 :            : 
     375                 :            :     sal_uLong           mnWidth;
     376                 :            :     sal_uLong           mnHeight;
     377                 :            : 
     378                 :            :     sal_uLong           mnScanlineSize;
     379                 :            :     sal_uLong           mnBitCount;
     380                 :            : 
     381                 :            :     int             mnXOffset;
     382                 :            :     int             mnYOffset;
     383                 :            : };
     384                 :            : 
     385                 :            : // =======================================================================
     386                 :            : 
     387                 :    1938794 : inline void ServerFont::SetExtended( int nInfo, void* pVoid )
     388                 :            : {
     389                 :    1938794 :     mnExtInfo = nInfo;
     390                 :    1938794 :     mpExtData = pVoid;
     391                 :    1938794 : }
     392                 :            : 
     393                 :            : // =======================================================================
     394                 :            : 
     395                 :            : // ExtraKernInfo allows an on-demand query of extra kerning info #i29881#
     396                 :            : // The kerning values have to be scaled to match the font size before use
     397                 :            : class VCL_DLLPUBLIC ExtraKernInfo
     398                 :            : {
     399                 :            : public:
     400                 :            :     ExtraKernInfo( sal_IntPtr nFontId );
     401         [ -  + ]:       8260 :     virtual ~ExtraKernInfo() {}
     402                 :            : 
     403                 :            :     int     GetUnscaledKernPairs( ImplKernPairData** ) const;
     404                 :            :     int     GetUnscaledKernValue( sal_Unicode cLeft, sal_Unicode cRight ) const;
     405                 :            : 
     406                 :            : protected:
     407                 :            :     mutable bool mbInitialized;
     408                 :            :     virtual void Initialize() const = 0;
     409                 :            : 
     410                 :            : protected:
     411                 :            :     sal_IntPtr     mnFontId;
     412                 :            : 
     413                 :            :     // container to map a unicode pair to an unscaled kerning value
     414                 :       1898 :     struct PairEqual{ int operator()(const ImplKernPairData& rA, const ImplKernPairData& rB) const
     415 [ +  - ][ +  - ]:       1898 :                           { return (rA.mnChar1 == rB.mnChar1) && (rA.mnChar2 == rB.mnChar2); } };
     416                 :      18007 :     struct PairHash{ int operator()(const ImplKernPairData& rA) const
     417                 :      18007 :                          { return (rA.mnChar1) * 256 ^ rA.mnChar2; } };
     418                 :            :     typedef boost::unordered_set< ImplKernPairData, PairHash, PairEqual > UnicodeKernPairs;
     419                 :            :     mutable UnicodeKernPairs maUnicodeKernPairs;
     420                 :            : };
     421                 :            : 
     422                 :            : // =======================================================================
     423                 :            : 
     424                 :            : #endif // _SV_GLYPHCACHE_HXX
     425                 :            : 
     426                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10