LCOV - code coverage report
Current view: top level - solver/unxlngi6.pro/inc/canvas - canvastools.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 31 0.0 %
Date: 2012-08-25 Functions: 0 14 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 28 0.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 INCLUDED_CANVAS_CANVASTOOLS_HXX
      30                 :            : #define INCLUDED_CANVAS_CANVASTOOLS_HXX
      31                 :            : 
      32                 :            : #include <rtl/math.hxx>
      33                 :            : #include <com/sun/star/uno/Reference.hxx>
      34                 :            : #include <com/sun/star/uno/Sequence.hxx>
      35                 :            : #include <com/sun/star/uno/RuntimeException.hpp>
      36                 :            : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      37                 :            : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
      38                 :            : #include <osl/diagnose.h>
      39                 :            : #include <rtl/ustring.hxx>
      40                 :            : 
      41                 :            : #include <string.h> // for strcmp
      42                 :            : #include <vector>
      43                 :            : #include <limits>
      44                 :            : #include <algorithm>
      45                 :            : 
      46                 :            : #include <canvas/canvastoolsdllapi.h>
      47                 :            : 
      48                 :            : namespace basegfx
      49                 :            : {
      50                 :            :     class B2DHomMatrix;
      51                 :            :     class B2DRange;
      52                 :            :     class B2IRange;
      53                 :            :     class B2IPoint;
      54                 :            :     class B2DPolyPolygon;
      55                 :            : }
      56                 :            : 
      57                 :            : namespace com { namespace sun { namespace star { namespace geometry
      58                 :            : {
      59                 :            :     struct RealSize2D;
      60                 :            :     struct IntegerSize2D;
      61                 :            :     struct AffineMatrix2D;
      62                 :            :     struct Matrix2D;
      63                 :            : } } } }
      64                 :            : 
      65                 :            : namespace com { namespace sun { namespace star { namespace rendering
      66                 :            : {
      67                 :            :     struct RenderState;
      68                 :            :     struct ViewState;
      69                 :            :     struct IntegerBitmapLayout;
      70                 :            :     class  XCanvas;
      71                 :            :     struct Texture;
      72                 :            :     class  XIntegerBitmapColorSpace;
      73                 :            :     class  XPolyPolygon2D;
      74                 :            : 
      75                 :            :     bool operator==( const RenderState& rLHS,
      76                 :            :                      const RenderState& rRHS );
      77                 :            : 
      78                 :            :     bool operator==( const ViewState& rLHS,
      79                 :            :                      const ViewState& rRHS );
      80                 :            : } } } }
      81                 :            : 
      82                 :            : namespace com { namespace sun { namespace star { namespace awt
      83                 :            : {
      84                 :            :     struct Rectangle;
      85                 :            :     class  XWindow2;
      86                 :            : } } } }
      87                 :            : 
      88                 :            : class Color;
      89                 :            : 
      90                 :            : namespace canvas
      91                 :            : {
      92                 :            :     namespace tools
      93                 :            :     {
      94                 :            :         /** Compute the next highest power of 2 of a 32-bit value
      95                 :            : 
      96                 :            :             Code devised by Sean Anderson, in good ole HAKMEM
      97                 :            :             tradition.
      98                 :            : 
      99                 :            :             @return 1 << (lg(x - 1) + 1)
     100                 :            :         */
     101                 :          0 :         inline sal_uInt32 nextPow2( sal_uInt32 x )
     102                 :            :         {
     103                 :          0 :             --x;
     104                 :          0 :             x |= x >> 1;
     105                 :          0 :             x |= x >> 2;
     106                 :          0 :             x |= x >> 4;
     107                 :          0 :             x |= x >> 8;
     108                 :          0 :             x |= x >> 16;
     109                 :            : 
     110                 :          0 :             return ++x;
     111                 :            :         }
     112                 :            : 
     113                 :            :         /**
     114                 :            :          *
     115                 :            :          * Count the number of 1-bits of an n-bit value
     116                 :            :          *
     117                 :            :          */
     118                 :            : 
     119                 :            :         // mickey's math tricks...
     120                 :            :         inline unsigned int pow2( unsigned int c ) { return 0x1 << c; }
     121                 :            :         inline unsigned int mask( unsigned int c ) { return ((unsigned int)(-1)) / (pow2(pow2(c)) + 1); }
     122                 :            :         inline unsigned int count( unsigned int x, unsigned int c ) { return ((x) & mask(c)) + (((x) >> (pow2(c))) & mask(c)); }
     123                 :            :         template<typename T>
     124                 :            :         inline unsigned int bitcount( T c ) {
     125                 :            :             unsigned int nByteIndex = 0;
     126                 :            :             unsigned int nNumBytes = sizeof(T)<<2;
     127                 :            :             do {
     128                 :            :                 c=count(c,nByteIndex++);
     129                 :            :                 nNumBytes >>= 1;
     130                 :            :             } while(nNumBytes);
     131                 :            :             return c;
     132                 :            :         }
     133                 :            :         inline sal_uInt32 bitcount32( sal_uInt32 c ) {
     134                 :            :             c=count(c,0);
     135                 :            :             c=count(c,1);
     136                 :            :             c=count(c,2);
     137                 :            :             c=count(c,3);
     138                 :            :             c=count(c,4);
     139                 :            :             return c;
     140                 :            :         }
     141                 :            : 
     142                 :            :         /** Round given floating point value down to next integer
     143                 :            :          */
     144                 :          0 :         inline sal_Int32 roundDown( const double& rVal )
     145                 :            :         {
     146                 :          0 :             return static_cast< sal_Int32 >( floor( rVal ) );
     147                 :            :         }
     148                 :            : 
     149                 :            :         /** Round given floating point value up to next integer
     150                 :            :          */
     151                 :          0 :         inline sal_Int32 roundUp( const double& rVal )
     152                 :            :         {
     153                 :          0 :             return static_cast< sal_Int32 >( ceil( rVal ) );
     154                 :            :         }
     155                 :            : 
     156                 :            :         /** Create a RealSize2D with both coordinate values set to +infinity
     157                 :            :          */
     158                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::geometry::RealSize2D createInfiniteSize2D();
     159                 :            : 
     160                 :            : 
     161                 :            :         // View- and RenderState utilities
     162                 :            :         // ===================================================================
     163                 :            : 
     164                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState&
     165                 :            :             initRenderState( ::com::sun::star::rendering::RenderState&                      renderState );
     166                 :            : 
     167                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::ViewState&
     168                 :            :             initViewState( ::com::sun::star::rendering::ViewState&                          viewState );
     169                 :            : 
     170                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix&
     171                 :            :             getViewStateTransform( ::basegfx::B2DHomMatrix&                                 transform,
     172                 :            :                                    const ::com::sun::star::rendering::ViewState&            viewState );
     173                 :            : 
     174                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::ViewState&
     175                 :            :             setViewStateTransform( ::com::sun::star::rendering::ViewState&                  viewState,
     176                 :            :                                    const ::basegfx::B2DHomMatrix&                           transform );
     177                 :            : 
     178                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix&
     179                 :            :             getRenderStateTransform( ::basegfx::B2DHomMatrix&                               transform,
     180                 :            :                                      const ::com::sun::star::rendering::RenderState&        renderState );
     181                 :            : 
     182                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState&
     183                 :            :             setRenderStateTransform( ::com::sun::star::rendering::RenderState&              renderState,
     184                 :            :                                      const ::basegfx::B2DHomMatrix&                         transform );
     185                 :            : 
     186                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState&
     187                 :            :             appendToRenderState( ::com::sun::star::rendering::RenderState&                  renderState,
     188                 :            :                                  const ::basegfx::B2DHomMatrix&                             transform );
     189                 :            : 
     190                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState&
     191                 :            :             prependToRenderState( ::com::sun::star::rendering::RenderState&                 renderState,
     192                 :            :                                   const ::basegfx::B2DHomMatrix&                            transform );
     193                 :            : 
     194                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix&
     195                 :            :             mergeViewAndRenderTransform( ::basegfx::B2DHomMatrix&                           transform,
     196                 :            :                                          const ::com::sun::star::rendering::ViewState&      viewState,
     197                 :            :                                          const ::com::sun::star::rendering::RenderState&    renderState );
     198                 :            : 
     199                 :            : 
     200                 :            :         // Matrix utilities
     201                 :            :         // ===================================================================
     202                 :            : 
     203                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::geometry::AffineMatrix2D&
     204                 :            :             setIdentityAffineMatrix2D( ::com::sun::star::geometry::AffineMatrix2D&  matrix );
     205                 :            : 
     206                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::geometry::Matrix2D&
     207                 :            :             setIdentityMatrix2D( ::com::sun::star::geometry::Matrix2D&              matrix );
     208                 :            : 
     209                 :            : 
     210                 :            :         // Special utilities
     211                 :            :         // ===================================================================
     212                 :            : 
     213                 :            :         /** Calc the bounding rectangle of a transformed rectangle.
     214                 :            : 
     215                 :            :             The method applies the given transformation to the
     216                 :            :             specified input rectangle, and returns the bounding box of
     217                 :            :             the resulting output area.
     218                 :            : 
     219                 :            :             @param o_Rect
     220                 :            :             Output rectangle
     221                 :            : 
     222                 :            :             @param i_Rect
     223                 :            :             Input rectangle
     224                 :            : 
     225                 :            :             @param i_Transformation
     226                 :            :             Transformation to apply to the input rectangle
     227                 :            : 
     228                 :            :             @return a reference to the resulting rectangle
     229                 :            :          */
     230                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2DRange& calcTransformedRectBounds( ::basegfx::B2DRange&          o_Rect,
     231                 :            :                                                         const ::basegfx::B2DRange&      i_Rect,
     232                 :            :                                                         const ::basegfx::B2DHomMatrix&  i_Transformation );
     233                 :            : 
     234                 :            :         /** Calc a transform that maps the upper, left corner of a
     235                 :            :              rectangle to the origin.
     236                 :            : 
     237                 :            :             The method is a specialized version of
     238                 :            :             calcRectToRectTransform() (Removed now), mapping the input rectangle's
     239                 :            :             the upper, left corner to the origin, and leaving the size
     240                 :            :             untouched.
     241                 :            : 
     242                 :            :             @param o_transform
     243                 :            :             Output parameter, to receive the resulting transformation
     244                 :            :             matrix.
     245                 :            : 
     246                 :            :             @param i_srcRect
     247                 :            :             Input parameter, specifies the original source
     248                 :            :             rectangle. The resulting transformation will exactly map
     249                 :            :             the source rectangle's upper, left corner to the origin.
     250                 :            : 
     251                 :            :             @param i_transformation
     252                 :            :             The original transformation matrix. This is changed with
     253                 :            :             translations (if necessary), to exactly map the source
     254                 :            :             rectangle to the origin.
     255                 :            : 
     256                 :            :             @return a reference to the resulting transformation matrix
     257                 :            : 
     258                 :            :             @see calcRectToRectTransform()
     259                 :            :             @see calcTransformedRectBounds()
     260                 :            :         */
     261                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix& calcRectToOriginTransform( ::basegfx::B2DHomMatrix&      o_transform,
     262                 :            :                                                             const ::basegfx::B2DRange&      i_srcRect,
     263                 :            :                                                             const ::basegfx::B2DHomMatrix&  i_transformation );
     264                 :            : 
     265                 :            :         /** Check whether a given rectangle is within another
     266                 :            :             transformed rectangle.
     267                 :            : 
     268                 :            :             This method checks for polygonal containedness, i.e. the
     269                 :            :             transformed rectangle is not represented as an axis-alignd
     270                 :            :             rectangle anymore (like calcTransformedRectBounds()), but
     271                 :            :             polygonal. Thus, the insideness test is based on tight
     272                 :            :             bounds.
     273                 :            : 
     274                 :            :             @param rContainedRect
     275                 :            :             This rectangle is checked, whether it is fully within the
     276                 :            :             transformed rTransformRect.
     277                 :            : 
     278                 :            :             @param rTransformRect
     279                 :            :             This rectangle is transformed, and then checked whether it
     280                 :            :             fully contains rContainedRect.
     281                 :            : 
     282                 :            :             @param rTransformation
     283                 :            :             This transformation is applied to rTransformRect
     284                 :            :          */
     285                 :            :         CANVASTOOLS_DLLPUBLIC bool isInside( const ::basegfx::B2DRange&         rContainedRect,
     286                 :            :                        const ::basegfx::B2DRange&       rTransformRect,
     287                 :            :                        const ::basegfx::B2DHomMatrix&   rTransformation );
     288                 :            : 
     289                 :            :         /** Clip a scroll to the given bound rect
     290                 :            : 
     291                 :            :             @param io_rSourceArea
     292                 :            :             Source area to scroll. The resulting clipped source area
     293                 :            :             is returned therein.
     294                 :            : 
     295                 :            :             @param io_rDestPoint
     296                 :            :             Destination point of the scroll (upper, left corner of
     297                 :            :             rSourceArea after the scroll). The new, resulting
     298                 :            :             destination point is returned therein.q
     299                 :            : 
     300                 :            :             @param o_ClippedAreas
     301                 :            :             Vector of rectangles in the <em>destination</em> area
     302                 :            :             coordinate system, which are clipped away from the source
     303                 :            :             area, and thus need extra updates (i.e. they are not
     304                 :            :             correctly copy from the scroll operation, since there was
     305                 :            :             no information about them in the source).
     306                 :            : 
     307                 :            :             @param rBounds
     308                 :            :             Bounds to clip against.
     309                 :            : 
     310                 :            :             @return false, if the resulting scroll area is empty
     311                 :            :          */
     312                 :            :         CANVASTOOLS_DLLPUBLIC bool clipScrollArea( ::basegfx::B2IRange&                  io_rSourceArea,
     313                 :            :                              ::basegfx::B2IPoint&                  io_rDestPoint,
     314                 :            :                              ::std::vector< ::basegfx::B2IRange >& o_ClippedAreas,
     315                 :            :                              const ::basegfx::B2IRange&            rBounds );
     316                 :            : 
     317                 :            :         /** Clip a blit between two differently surfaces.
     318                 :            : 
     319                 :            :             This method clips source and dest rect for a clip between
     320                 :            :             two differently clipped surfaces, such that the resulting
     321                 :            :             blit rects are fully within both clip areas.
     322                 :            : 
     323                 :            :             @param io_rSourceArea
     324                 :            :             Source area of the blit. Returned therein is the computed
     325                 :            :             clipped source area.
     326                 :            : 
     327                 :            :             @param io_rDestPoint
     328                 :            :             Dest area of the blit. Returned therein is the computed
     329                 :            :             clipped dest area.
     330                 :            : 
     331                 :            :             @param rSourceBounds
     332                 :            :             Clip bounds of the source surface
     333                 :            : 
     334                 :            :             @param rDestBounds
     335                 :            :             Clip bounds of the dest surface
     336                 :            : 
     337                 :            :             @return false, if the resulting blit is empty, i.e. fully
     338                 :            :             clipped away.
     339                 :            :          */
     340                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2IRange spritePixelAreaFromB2DRange( const ::basegfx::B2DRange& rRange );
     341                 :            : 
     342                 :            :         /** Retrieve various internal properties of the actual canvas implementation.
     343                 :            : 
     344                 :            :             This method retrieves a bunch of internal, implementation-
     345                 :            :             and platform-dependent values from the canvas
     346                 :            :             implementation. Among them are for example operating
     347                 :            :             system window handles. The actual layout and content of
     348                 :            :             the returned sequence is dependent on the component
     349                 :            :             implementation, undocumented and subject to change.
     350                 :            : 
     351                 :            :             @param i_rxCanvas
     352                 :            :             Input parameter, the canvas representation for which the device information
     353                 :            :             is to be retrieveds
     354                 :            : 
     355                 :            :             @param o_rxParams
     356                 :            :             Output parameter, the sequence of Anys that hold the device parameters. Layout is as described above
     357                 :            : 
     358                 :            :             @return A reference to the resulting sequence of parameters
     359                 :            :         */
     360                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& getDeviceInfo(
     361                 :            :             const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvas >& i_rxCanvas,
     362                 :            :             ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& o_rxParams );
     363                 :            : 
     364                 :            :         /** Return a color space for a default RGBA integer format
     365                 :            : 
     366                 :            :             Use this method for dead-simple bitmap implementations,
     367                 :            :             that map all their formats to 8888 RGBA color.
     368                 :            :          */
     369                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XIntegerBitmapColorSpace> getStdColorSpace();
     370                 :            : 
     371                 :            :         /** Return a memory layout for a default RGBA integer format
     372                 :            : 
     373                 :            :             Use this method for dead-simple bitmap implementations,
     374                 :            :             that map all their formats to 8888 RGBA color.
     375                 :            :          */
     376                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::IntegerBitmapLayout getStdMemoryLayout(
     377                 :            :             const ::com::sun::star::geometry::IntegerSize2D& rBitmapSize );
     378                 :            : 
     379                 :            :         /// Convert standard 8888 RGBA color to vcl color
     380                 :            :         CANVASTOOLS_DLLPUBLIC ::Color stdIntSequenceToColor( const ::com::sun::star::uno::Sequence<sal_Int8>& rColor );
     381                 :            : 
     382                 :            :         /// Convert standard 8888 RGBA color to vcl color
     383                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Sequence<sal_Int8> colorToStdIntSequence( const ::Color& rColor );
     384                 :            : 
     385                 :            :         // Modeled closely after boost::numeric_cast, only that we
     386                 :            :         // issue some trace output here and throw a RuntimeException
     387                 :            : 
     388                 :            :         /** Cast numeric value into another (numeric) data type
     389                 :            : 
     390                 :            :             Apart from converting the numeric value, this template
     391                 :            :             also checks if any overflow, underflow, or sign
     392                 :            :             information is lost (if yes, it throws an
     393                 :            :             uno::RuntimeException.
     394                 :            :          */
     395                 :          0 :         template< typename Target, typename Source > inline Target numeric_cast( Source arg )
     396                 :            :         {
     397                 :            :             // typedefs abbreviating respective trait classes
     398                 :            :             typedef ::std::numeric_limits< Source > SourceLimits;
     399                 :            :             typedef ::std::numeric_limits< Target > TargetLimits;
     400                 :            : 
     401                 :            : #undef min
     402                 :            : #undef max
     403                 :            : 
     404 [ #  # ][ #  # ]:          0 :             if( ( arg<0 && !TargetLimits::is_signed) ||                     // loosing the sign here
                 [ #  # ]
     405                 :            :                 ( SourceLimits::is_signed && arg<TargetLimits::min()) ||    // underflow will happen
     406                 :            :                 ( arg>TargetLimits::max() ) )                               // overflow will happen
     407                 :            :             {
     408                 :            : # if OSL_DEBUG_LEVEL > 2
     409                 :            :                 OSL_TRACE("numeric_cast detected data loss");
     410                 :            : #endif
     411                 :            :                 throw ::com::sun::star::uno::RuntimeException(
     412                 :            :                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "numeric_cast detected data loss" )),
     413 [ #  # ][ #  # ]:          0 :                     NULL );
                 [ #  # ]
     414                 :            :             }
     415                 :            : 
     416                 :          0 :             return static_cast<Target>(arg);
     417                 :            :         }
     418                 :            : 
     419                 :            :         CANVASTOOLS_DLLPUBLIC ::com::sun::star::awt::Rectangle getAbsoluteWindowRect(
     420                 :            :             const ::com::sun::star::awt::Rectangle&                                    rRect,
     421                 :            :             const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow2 >& xWin  );
     422                 :            : 
     423                 :            :         /** Retrieve for small bound marks around each corner of the given rectangle
     424                 :            :          */
     425                 :            :         CANVASTOOLS_DLLPUBLIC ::basegfx::B2DPolyPolygon getBoundMarksPolyPolygon( const ::basegfx::B2DRange& rRange );
     426                 :            : 
     427                 :            :         /** Calculate number of gradient "strips" to generate (takes
     428                 :            :            into account device resolution)
     429                 :            : 
     430                 :            :            @param nColorSteps
     431                 :            :            Maximal integer difference between all color stops, needed
     432                 :            :            for smooth gradient color differences
     433                 :            :          */
     434                 :            :         CANVASTOOLS_DLLPUBLIC int calcGradientStepCount( ::basegfx::B2DHomMatrix&                        rTotalTransform,
     435                 :            :                                    const ::com::sun::star::rendering::ViewState&   viewState,
     436                 :            :                                    const ::com::sun::star::rendering::RenderState& renderState,
     437                 :            :                                    const ::com::sun::star::rendering::Texture&     texture,
     438                 :            :                                    int                                             nColorSteps );
     439                 :            : 
     440                 :            :         /** A very simplistic map for ASCII strings and arbitrary value
     441                 :            :             types.
     442                 :            : 
     443                 :            :             This class internally references a constant, static array of
     444                 :            :             sorted MapEntries, and performs a binary search to look up
     445                 :            :             values for a given query string. Note that this map is static,
     446                 :            :             i.e. not meant to be extented at runtime.
     447                 :            : 
     448                 :            :             @tpl ValueType
     449                 :            :             The value type this map should store, associated with an ASCII
     450                 :            :             string.
     451                 :            :         */
     452                 :            :         template< typename ValueType > class ValueMap
     453                 :            :         {
     454                 :            :         public:
     455                 :          0 :             struct MapEntry
     456                 :            :             {
     457                 :            :                 const char*     maKey;
     458                 :            :                 ValueType       maValue;
     459                 :            :             };
     460                 :            : 
     461                 :            :             /** Create a ValueMap for the given array of MapEntries.
     462                 :            : 
     463                 :            :                 @param pMap
     464                 :            :                 Pointer to a <em>static</em> array of MapEntries. Must
     465                 :            :                 live longer than this object! Make absolutely sure that
     466                 :            :                 the string entries passed via pMap are ASCII-only -
     467                 :            :                 everything else might not yield correct string
     468                 :            :                 comparisons, and thus will result in undefined behaviour.
     469                 :            : 
     470                 :            :                 @param nEntries
     471                 :            :                 Number of entries for pMap
     472                 :            : 
     473                 :            :                 @param bCaseSensitive
     474                 :            :                 Whether the map query should be performed case sensitive
     475                 :            :                 or not. When bCaseSensitive is false, all MapEntry strings
     476                 :            :                 must be lowercase!
     477                 :            :             */
     478                 :          0 :             ValueMap( const MapEntry*   pMap,
     479                 :            :                       ::std::size_t     nEntries,
     480                 :            :                       bool              bCaseSensitive ) :
     481                 :            :                 mpMap( pMap ),
     482                 :            :                 mnEntries( nEntries ),
     483                 :          0 :                 mbCaseSensitive( bCaseSensitive )
     484                 :            :             {
     485                 :            : #ifdef DBG_UTIL
     486                 :            :                 // Ensure that map entries are sorted (and all lowercase, if this
     487                 :            :                 // map is case insensitive)
     488                 :            :                 const ::rtl::OString aStr( pMap->maKey );
     489                 :            :                 if( !mbCaseSensitive &&
     490                 :            :                     aStr != aStr.toAsciiLowerCase() )
     491                 :            :                 {
     492                 :            :                     OSL_TRACE("ValueMap::ValueMap(): Key %s is not lowercase",
     493                 :            :                               pMap->maKey);
     494                 :            :                     OSL_FAIL( "ValueMap::ValueMap(): Key is not lowercase" );
     495                 :            :                 }
     496                 :            : 
     497                 :            :                 if( mnEntries > 1 )
     498                 :            :                 {
     499                 :            :                     for( ::std::size_t i=0; i<mnEntries-1; ++i, ++pMap )
     500                 :            :                     {
     501                 :            :                         if( !mapComparator(pMap[0], pMap[1]) &&
     502                 :            :                             mapComparator(pMap[1], pMap[0]) )
     503                 :            :                         {
     504                 :            :                             OSL_TRACE("ValueMap::ValueMap(): Map is not sorted, keys %s and %s are wrong",
     505                 :            :                                       pMap[0].maKey,
     506                 :            :                                       pMap[1].maKey);
     507                 :            :                             OSL_FAIL( "ValueMap::ValueMap(): Map is not sorted" );
     508                 :            :                         }
     509                 :            : 
     510                 :            :                         const ::rtl::OString aStr2( pMap[1].maKey );
     511                 :            :                         if( !mbCaseSensitive &&
     512                 :            :                             aStr2 != aStr2.toAsciiLowerCase() )
     513                 :            :                         {
     514                 :            :                             OSL_TRACE("ValueMap::ValueMap(): Key %s is not lowercase",
     515                 :            :                                       pMap[1].maKey);
     516                 :            :                             OSL_FAIL( "ValueMap::ValueMap(): Key is not lowercase" );
     517                 :            :                         }
     518                 :            :                     }
     519                 :            :                 }
     520                 :            : #endif
     521                 :          0 :             }
     522                 :            : 
     523                 :            :             /** Lookup a value for the given query string
     524                 :            : 
     525                 :            :                 @param rName
     526                 :            :                 The string to lookup. If the map was created with the case
     527                 :            :                 insensitive flag, the lookup is performed
     528                 :            :                 case-insensitive, otherwise, case-sensitive.
     529                 :            : 
     530                 :            :                 @param o_rResult
     531                 :            :                 Output parameter, which receives the value associated with
     532                 :            :                 the query string. If no value was found, the referenced
     533                 :            :                 object is kept unmodified.
     534                 :            : 
     535                 :            :                 @return true, if a matching entry was found.
     536                 :            :             */
     537                 :          0 :             bool lookup( const ::rtl::OUString& rName,
     538                 :            :                          ValueType&             o_rResult ) const
     539                 :            :             {
     540                 :            :                 // rName is required to contain only ASCII characters.
     541                 :            :                 // TODO(Q1): Enforce this at upper layers
     542                 :            :                 ::rtl::OString aKey( ::rtl::OUStringToOString( mbCaseSensitive ? rName : rName.toAsciiLowerCase(),
     543 [ #  # ][ #  # ]:          0 :                                                                RTL_TEXTENCODING_ASCII_US ) );
     544                 :            :                 MapEntry aSearchKey =
     545                 :            :                     {
     546                 :            :                         aKey.getStr(),
     547                 :            :                         ValueType()
     548         [ #  # ]:          0 :                     };
     549                 :            : 
     550                 :            :                 const MapEntry* pRes;
     551                 :          0 :                 const MapEntry* pEnd = mpMap+mnEntries;
     552 [ #  # ][ #  # ]:          0 :                 if( (pRes=::std::lower_bound( mpMap,
     553                 :            :                                               pEnd,
     554                 :            :                                               aSearchKey,
     555                 :            :                                               &mapComparator )) != pEnd )
     556                 :            :                 {
     557                 :            :                     // place to _insert before_ found - is it equal to
     558                 :            :                     // the search key?
     559         [ #  # ]:          0 :                     if( strcmp( pRes->maKey, aSearchKey.maKey ) == 0 )
     560                 :            :                     {
     561                 :            :                         // yep, correct entry found
     562         [ #  # ]:          0 :                         o_rResult = pRes->maValue;
     563                 :          0 :                         return true;
     564                 :            :                     }
     565                 :            :                 }
     566                 :            : 
     567                 :            :                 // not found
     568         [ #  # ]:          0 :                 return false;
     569                 :            :             }
     570                 :            : 
     571                 :            :         private:
     572                 :          0 :             static bool mapComparator( const MapEntry& rLHS,
     573                 :            :                                        const MapEntry& rRHS )
     574                 :            :             {
     575                 :            :                 return strcmp( rLHS.maKey,
     576                 :          0 :                                rRHS.maKey ) < 0;
     577                 :            :             }
     578                 :            : 
     579                 :            :             const MapEntry*     mpMap;
     580                 :            :             ::std::size_t       mnEntries;
     581                 :            :             bool                mbCaseSensitive;
     582                 :            :         };
     583                 :            :     }
     584                 :            : }
     585                 :            : 
     586                 :            : #endif /* INCLUDED_CANVAS_CANVASTOOLS_HXX */
     587                 :            : 
     588                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10