LibreOffice Module canvas (master)  1
canvastools.hxx
Go to the documentation of this file.
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 #ifndef INCLUDED_CANVAS_CANVASTOOLS_HXX
21 #define INCLUDED_CANVAS_CANVASTOOLS_HXX
22 
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/uno/RuntimeException.hpp>
26 #include <osl/diagnose.h>
27 #include <rtl/ustring.hxx>
28 #include <sal/log.hxx>
29 
30 #include <math.h>
31 #include <string.h>
32 #include <vector>
33 #include <limits>
34 
36 
37 namespace basegfx
38 {
39  class B2DHomMatrix;
40  class B2DRange;
41  class B2IRange;
42  class B2IPoint;
43  class B2DPolyPolygon;
44 }
45 
46 namespace com { namespace sun { namespace star { namespace geometry
47 {
48  struct RealSize2D;
49  struct IntegerSize2D;
50  struct AffineMatrix2D;
51  struct Matrix2D;
52 } } } }
53 
54 namespace com { namespace sun { namespace star { namespace rendering
55 {
56  struct RenderState;
57  struct ViewState;
58  struct IntegerBitmapLayout;
59  class XCanvas;
60  struct Texture;
61  class XIntegerBitmapColorSpace;
62 } } } }
63 
64 namespace com { namespace sun { namespace star { namespace awt
65 {
66  struct Rectangle;
67  class XWindow2;
68 } } } }
69 
70 namespace com { namespace sun { namespace star { namespace beans {
71  struct PropertyValue;
72 } } } }
73 
74 class Color;
75 class OutputDevice;
76 
77 namespace canvas
78 {
79  namespace tools
80  {
88  inline sal_uInt32 nextPow2( sal_uInt32 x )
89  {
90  --x;
91  x |= x >> 1;
92  x |= x >> 2;
93  x |= x >> 4;
94  x |= x >> 8;
95  x |= x >> 16;
96 
97  return ++x;
98  }
99 
108  inline sal_Int32 roundDown( const double& rVal )
109  {
110  return static_cast< sal_Int32 >( floor( rVal ) );
111  }
112 
115  inline sal_Int32 roundUp( const double& rVal )
116  {
117  return static_cast< sal_Int32 >( ceil( rVal ) );
118  }
119 
122  CANVASTOOLS_DLLPUBLIC css::geometry::RealSize2D createInfiniteSize2D();
123 
124 
125  // View- and RenderState utilities
126 
127 
128  CANVASTOOLS_DLLPUBLIC css::rendering::RenderState&
129  initRenderState( css::rendering::RenderState& renderState );
130 
131  CANVASTOOLS_DLLPUBLIC css::rendering::ViewState&
132  initViewState( css::rendering::ViewState& viewState );
133 
134  CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix&
136  const css::rendering::ViewState& viewState );
137 
138  CANVASTOOLS_DLLPUBLIC css::rendering::ViewState&
139  setViewStateTransform( css::rendering::ViewState& viewState,
140  const ::basegfx::B2DHomMatrix& transform );
141 
142  CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix&
144  const css::rendering::RenderState& renderState );
145 
146  CANVASTOOLS_DLLPUBLIC css::rendering::RenderState&
147  setRenderStateTransform( css::rendering::RenderState& renderState,
148  const ::basegfx::B2DHomMatrix& transform );
149 
150  CANVASTOOLS_DLLPUBLIC css::rendering::RenderState&
151  appendToRenderState( css::rendering::RenderState& renderState,
152  const ::basegfx::B2DHomMatrix& transform );
153 
154  CANVASTOOLS_DLLPUBLIC css::rendering::RenderState&
155  prependToRenderState( css::rendering::RenderState& renderState,
156  const ::basegfx::B2DHomMatrix& transform );
157 
158  CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix&
160  const css::rendering::ViewState& viewState,
161  const css::rendering::RenderState& renderState );
162 
163 
164  // Matrix utilities
165 
166 
167  CANVASTOOLS_DLLPUBLIC css::geometry::AffineMatrix2D&
168  setIdentityAffineMatrix2D( css::geometry::AffineMatrix2D& matrix );
169 
170  CANVASTOOLS_DLLPUBLIC css::geometry::Matrix2D&
171  setIdentityMatrix2D( css::geometry::Matrix2D& matrix );
172 
173 
174  // Special utilities
175 
176 
194  CANVASTOOLS_DLLPUBLIC ::basegfx::B2DRange& calcTransformedRectBounds( ::basegfx::B2DRange& o_Rect,
195  const ::basegfx::B2DRange& i_Rect,
196  const ::basegfx::B2DHomMatrix& i_Transformation );
197 
225  CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix& calcRectToOriginTransform( ::basegfx::B2DHomMatrix& o_transform,
226  const ::basegfx::B2DRange& i_srcRect,
227  const ::basegfx::B2DHomMatrix& i_transformation );
228 
249  CANVASTOOLS_DLLPUBLIC bool isInside( const ::basegfx::B2DRange& rContainedRect,
250  const ::basegfx::B2DRange& rTransformRect,
251  const ::basegfx::B2DHomMatrix& rTransformation );
252 
277  ::basegfx::B2IPoint& io_rDestPoint,
278  ::std::vector< ::basegfx::B2IRange >& o_ClippedAreas,
279  const ::basegfx::B2IRange& rBounds );
280 
304  CANVASTOOLS_DLLPUBLIC ::basegfx::B2IRange spritePixelAreaFromB2DRange( const ::basegfx::B2DRange& rRange );
305 
324  CANVASTOOLS_DLLPUBLIC css::uno::Sequence< css::uno::Any >& getDeviceInfo(
325  const css::uno::Reference< css::rendering::XCanvas >& i_rxCanvas,
326  css::uno::Sequence< css::uno::Any >& o_rxParams );
327 
333  CANVASTOOLS_DLLPUBLIC css::uno::Reference< css::rendering::XIntegerBitmapColorSpace> const & getStdColorSpace();
334 
341  CANVASTOOLS_DLLPUBLIC css::uno::Reference< css::rendering::XIntegerBitmapColorSpace> const & getStdColorSpaceWithoutAlpha();
342 
348  CANVASTOOLS_DLLPUBLIC css::rendering::IntegerBitmapLayout getStdMemoryLayout(
349  const css::geometry::IntegerSize2D& rBitmapSize );
350 
352  CANVASTOOLS_DLLPUBLIC css::uno::Sequence<sal_Int8> colorToStdIntSequence( const ::Color& rColor );
353 
354  // Modelled closely after boost::numeric_cast, only that we
355  // issue some trace output here and throw a RuntimeException
356 
364  template< typename Target, typename Source > inline Target numeric_cast( Source arg )
365  {
366  // typedefs abbreviating respective trait classes
367  typedef ::std::numeric_limits< Source > SourceLimits;
368  typedef ::std::numeric_limits< Target > TargetLimits;
369 
370 #undef min
371 #undef max
372 
373  if( ( arg<0 && !TargetLimits::is_signed) || // losing the sign here
374  ( SourceLimits::is_signed && arg<TargetLimits::min()) || // underflow will happen
375  ( arg>TargetLimits::max() ) ) // overflow will happen
376  {
377 # if OSL_DEBUG_LEVEL > 2
378  SAL_WARN("canvas", "numeric_cast detected data loss");
379 #endif
380  throw css::uno::RuntimeException(
381  "numeric_cast detected data loss",
382  nullptr );
383  }
384 
385  return static_cast<Target>(arg);
386  }
387 
388  CANVASTOOLS_DLLPUBLIC css::awt::Rectangle getAbsoluteWindowRect(
389  const css::awt::Rectangle& rRect,
390  const css::uno::Reference< css::awt::XWindow2 >& xWin );
391 
394  CANVASTOOLS_DLLPUBLIC ::basegfx::B2DPolyPolygon getBoundMarksPolyPolygon( const ::basegfx::B2DRange& rRange );
395 
404  const css::rendering::ViewState& viewState,
405  const css::rendering::RenderState& renderState,
406  const css::rendering::Texture& texture,
407  int nColorSteps );
408 
421  template< typename ValueType > class ValueMap
422  {
423  public:
424  struct MapEntry
425  {
426  const char* maKey;
428  };
429 
447  ValueMap( const MapEntry* pMap,
448  ::std::size_t nEntries,
449  bool bCaseSensitive ) :
450  mpMap( pMap ),
451  mnEntries( nEntries ),
452  mbCaseSensitive( bCaseSensitive )
453  {
454 #ifdef DBG_UTIL
455  // Ensure that map entries are sorted (and all lowercase, if this
456  // map is case insensitive)
457  const OString aStr( pMap->maKey );
458  if( !mbCaseSensitive &&
459  aStr != aStr.toAsciiLowerCase() )
460  {
461  SAL_WARN("canvas", "ValueMap::ValueMap(): Key is not lowercase " << pMap->maKey);
462  }
463 
464  if( mnEntries > 1 )
465  {
466  for( ::std::size_t i=0; i<mnEntries-1; ++i, ++pMap )
467  {
468  if( !mapComparator(pMap[0], pMap[1]) &&
469  mapComparator(pMap[1], pMap[0]) )
470  {
471  SAL_WARN("canvas", "ValueMap::ValueMap(): Map is not sorted, keys are wrong, "
472  << pMap[0].maKey << " and " << pMap[1].maKey);
473  OSL_FAIL( "ValueMap::ValueMap(): Map is not sorted" );
474  }
475 
476  const OString aStr2( pMap[1].maKey );
477  if( !mbCaseSensitive &&
478  aStr2 != aStr2.toAsciiLowerCase() )
479  {
480  SAL_WARN("canvas", "ValueMap::ValueMap(): Key is not lowercase" << pMap[1].maKey);
481  }
482  }
483  }
484 #endif
485  }
486 
501  bool lookup( const OUString& rName,
502  ValueType& o_rResult ) const
503  {
504  // rName is required to contain only ASCII characters.
505  // TODO(Q1): Enforce this at upper layers
506  OString aKey( OUStringToOString( mbCaseSensitive ? rName : rName.toAsciiLowerCase(),
507  RTL_TEXTENCODING_ASCII_US ) );
508  MapEntry aSearchKey =
509  {
510  aKey.getStr(),
511  ValueType()
512  };
513 
514  const MapEntry* pRes;
515  const MapEntry* pEnd = mpMap+mnEntries;
516  if( (pRes=::std::lower_bound( mpMap,
517  pEnd,
518  aSearchKey,
519  &mapComparator )) != pEnd )
520  {
521  // place to _insert before_ found - is it equal to
522  // the search key?
523  if( strcmp( pRes->maKey, aSearchKey.maKey ) == 0 )
524  {
525  // yep, correct entry found
526  o_rResult = pRes->maValue;
527  return true;
528  }
529  }
530 
531  // not found
532  return false;
533  }
534 
535  private:
536  static bool mapComparator( const MapEntry& rLHS,
537  const MapEntry& rRHS )
538  {
539  return strcmp( rLHS.maKey,
540  rRHS.maKey ) < 0;
541  }
542 
543  const MapEntry* mpMap;
544  ::std::size_t mnEntries;
546  };
547 
548  CANVASTOOLS_DLLPUBLIC void clipOutDev(const css::rendering::ViewState& viewState,
549  const css::rendering::RenderState& renderState,
550  OutputDevice& rOutDev,
551  OutputDevice* p2ndOutDev=nullptr);
552 
553  CANVASTOOLS_DLLPUBLIC void extractExtraFontProperties(const css::uno::Sequence<css::beans::PropertyValue>& rExtraFontProperties,
554  sal_uInt32& rEmphasisMark);
555  }
556 }
557 
558 #endif /* INCLUDED_CANVAS_CANVASTOOLS_HXX */
559 
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Reference< rendering::XIntegerBitmapColorSpace > const & getStdColorSpaceWithoutAlpha()
Return a color space for a default RGB integer format.
rendering::ViewState & setViewStateTransform(rendering::ViewState &viewState, const ::basegfx::B2DHomMatrix &transform)
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
awt::Rectangle getAbsoluteWindowRect(const awt::Rectangle &rRect, const uno::Reference< awt::XWindow2 > &xWin)
const MapEntry * mpMap
#define CANVASTOOLS_DLLPUBLIC
#define max(a, b)
Definition: dx_winstuff.hxx:46
uno::Sequence< sal_Int8 > colorToStdIntSequence(const ::Color &rColor)
Convert standard 8888 RGBA color to vcl color.
::basegfx::B2DRange & calcTransformedRectBounds(::basegfx::B2DRange &outRect, const ::basegfx::B2DRange &inRect, const ::basegfx::B2DHomMatrix &transformation)
Calc the bounding rectangle of a transformed rectangle.
rendering::RenderState & setRenderStateTransform(rendering::RenderState &renderState, const ::basegfx::B2DHomMatrix &transform)
A very simplistic map for ASCII strings and arbitrary value types.
float x
Definition: dx_9rm.cxx:189
const CacheKey maKey
ValueMap(const MapEntry *pMap,::std::size_t nEntries, bool bCaseSensitive)
Create a ValueMap for the given array of MapEntries.
sal_Int32 roundDown(const double &rVal)
Count the number of 1-bits of a n-bit value.
#define min(a, b)
Definition: dx_winstuff.hxx:49
bool isInside(const ::basegfx::B2DRange &rContainedRect, const ::basegfx::B2DRange &rTransformRect, const ::basegfx::B2DHomMatrix &rTransformation)
Check whether a given rectangle is within another transformed rectangle.
bool lookup(const OUString &rName, ValueType &o_rResult) const
Lookup a value for the given query string.
::basegfx::B2DPolyPolygon getBoundMarksPolyPolygon(const ::basegfx::B2DRange &rRange)
Retrieve for small bound marks around each corner of the given rectangle.
::basegfx::B2IRange spritePixelAreaFromB2DRange(const ::basegfx::B2DRange &rRange)
Clip a blit between two differently surfaces.
rendering::IntegerBitmapLayout getStdMemoryLayout(const geometry::IntegerSize2D &rBmpSize)
void extractExtraFontProperties(const uno::Sequence< beans::PropertyValue > &rExtraFontProperties, sal_uInt32 &rEmphasisMark)
void clipOutDev(const rendering::ViewState &viewState, const rendering::RenderState &renderState, OutputDevice &rOutDev, OutputDevice *p2ndOutDev)
::basegfx::B2DHomMatrix & getRenderStateTransform(::basegfx::B2DHomMatrix &transform, const rendering::RenderState &renderState)
int i
geometry::AffineMatrix2D & setIdentityAffineMatrix2D(geometry::AffineMatrix2D &matrix)
Target numeric_cast(Source arg)
Cast numeric value into another (numeric) data type.
sal_uInt32 nextPow2(sal_uInt32 x)
Compute the next highest power of 2 of a 32-bit value.
Definition: canvastools.hxx:88
rendering::RenderState & prependToRenderState(rendering::RenderState &renderState, const ::basegfx::B2DHomMatrix &rTransform)
int calcGradientStepCount(::basegfx::B2DHomMatrix &rTotalTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState, const rendering::Texture &texture, int nColorSteps)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
::basegfx::B2DHomMatrix & calcRectToOriginTransform(::basegfx::B2DHomMatrix &o_transform, const ::basegfx::B2DRange &i_srcRect, const ::basegfx::B2DHomMatrix &i_transformation)
Calc a transform that maps the upper, left corner of a rectangle to the origin.
::basegfx::B2DHomMatrix & getViewStateTransform(::basegfx::B2DHomMatrix &transform, const rendering::ViewState &viewState)
Definition: canvastools.cxx:94
uno::Sequence< uno::Any > & getDeviceInfo(const uno::Reference< rendering::XCanvas > &i_rxCanvas, uno::Sequence< uno::Any > &o_rxParams)
sal_Int32 roundUp(const double &rVal)
Round given floating point value up to next integer.
ValueType
static bool mapComparator(const MapEntry &rLHS, const MapEntry &rRHS)
geometry::RealSize2D createInfiniteSize2D()
Create a RealSize2D with both coordinate values set to +infinity.
Definition: canvastools.cxx:67
bool clipScrollArea(::basegfx::B2IRange &io_rSourceArea,::basegfx::B2IPoint &io_rDestPoint, std::vector< ::basegfx::B2IRange > &o_ClippedAreas, const ::basegfx::B2IRange &rBounds)
ViewState
rendering::ViewState & initViewState(rendering::ViewState &viewState)
Definition: canvastools.cxx:85
rendering::RenderState & initRenderState(rendering::RenderState &renderState)
Definition: canvastools.cxx:74
#define SAL_WARN(area, stream)
rendering::RenderState & appendToRenderState(rendering::RenderState &renderState, const ::basegfx::B2DHomMatrix &rTransform)
aStr
uno::Reference< rendering::XIntegerBitmapColorSpace > const & getStdColorSpace()
Return a color space for a default RGBA integer format.
geometry::Matrix2D & setIdentityMatrix2D(geometry::Matrix2D &matrix)