1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_CANVAS_CANVASTOOLS_HXX
21 #define INCLUDED_CANVAS_CANVASTOOLS_HXX
23 #include <rtl/math.hxx>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/uno/RuntimeException.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
29 #include <osl/diagnose.h>
30 #include <rtl/ustring.hxx>
37 #include <canvas/canvastoolsdllapi.h>
48 namespace com
{ namespace sun
{ namespace star
{ namespace geometry
52 struct AffineMatrix2D
;
56 namespace com
{ namespace sun
{ namespace star
{ namespace rendering
60 struct IntegerBitmapLayout
;
63 class XIntegerBitmapColorSpace
;
66 bool operator==( const RenderState
& rLHS
,
67 const RenderState
& rRHS
);
69 bool operator==( const ViewState
& rLHS
,
70 const ViewState
& rRHS
);
73 namespace com
{ namespace sun
{ namespace star
{ namespace awt
86 /** Compute the next highest power of 2 of a 32-bit value
88 Code devised by Sean Anderson, in good ole HAKMEM
91 @return 1 << (lg(x - 1) + 1)
93 inline sal_uInt32
nextPow2( sal_uInt32 x
)
107 * Count the number of 1-bits of an n-bit value
111 // mickey's math tricks...
112 inline unsigned int pow2( unsigned int c
) { return 0x1 << c
; }
113 inline unsigned int mask( unsigned int c
) { return ((unsigned int)(-1)) / (pow2(pow2(c
)) + 1); }
114 inline unsigned int count( unsigned int x
, unsigned int c
) { return ((x
) & mask(c
)) + (((x
) >> (pow2(c
))) & mask(c
)); }
116 inline unsigned int bitcount( T c
) {
117 unsigned int nByteIndex
= 0;
118 unsigned int nNumBytes
= sizeof(T
)<<2;
120 c
=count(c
,nByteIndex
++);
125 inline sal_uInt32
bitcount32( sal_uInt32 c
) {
134 /** Round given floating point value down to next integer
136 inline sal_Int32
roundDown( const double& rVal
)
138 return static_cast< sal_Int32
>( floor( rVal
) );
141 /** Round given floating point value up to next integer
143 inline sal_Int32
roundUp( const double& rVal
)
145 return static_cast< sal_Int32
>( ceil( rVal
) );
148 /** Create a RealSize2D with both coordinate values set to +infinity
150 CANVASTOOLS_DLLPUBLIC ::com::sun::star::geometry::RealSize2D
createInfiniteSize2D();
153 // View- and RenderState utilities
156 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState
&
157 initRenderState( ::com::sun::star::rendering::RenderState
& renderState
);
159 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::ViewState
&
160 initViewState( ::com::sun::star::rendering::ViewState
& viewState
);
162 CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix
&
163 getViewStateTransform( ::basegfx::B2DHomMatrix
& transform
,
164 const ::com::sun::star::rendering::ViewState
& viewState
);
166 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::ViewState
&
167 setViewStateTransform( ::com::sun::star::rendering::ViewState
& viewState
,
168 const ::basegfx::B2DHomMatrix
& transform
);
170 CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix
&
171 getRenderStateTransform( ::basegfx::B2DHomMatrix
& transform
,
172 const ::com::sun::star::rendering::RenderState
& renderState
);
174 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState
&
175 setRenderStateTransform( ::com::sun::star::rendering::RenderState
& renderState
,
176 const ::basegfx::B2DHomMatrix
& transform
);
178 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState
&
179 appendToRenderState( ::com::sun::star::rendering::RenderState
& renderState
,
180 const ::basegfx::B2DHomMatrix
& transform
);
182 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::RenderState
&
183 prependToRenderState( ::com::sun::star::rendering::RenderState
& renderState
,
184 const ::basegfx::B2DHomMatrix
& transform
);
186 CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix
&
187 mergeViewAndRenderTransform( ::basegfx::B2DHomMatrix
& transform
,
188 const ::com::sun::star::rendering::ViewState
& viewState
,
189 const ::com::sun::star::rendering::RenderState
& renderState
);
195 CANVASTOOLS_DLLPUBLIC ::com::sun::star::geometry::AffineMatrix2D
&
196 setIdentityAffineMatrix2D( ::com::sun::star::geometry::AffineMatrix2D
& matrix
);
198 CANVASTOOLS_DLLPUBLIC ::com::sun::star::geometry::Matrix2D
&
199 setIdentityMatrix2D( ::com::sun::star::geometry::Matrix2D
& matrix
);
205 /** Calc the bounding rectangle of a transformed rectangle.
207 The method applies the given transformation to the
208 specified input rectangle, and returns the bounding box of
209 the resulting output area.
217 @param i_Transformation
218 Transformation to apply to the input rectangle
220 @return a reference to the resulting rectangle
222 CANVASTOOLS_DLLPUBLIC ::basegfx::B2DRange
& calcTransformedRectBounds( ::basegfx::B2DRange
& o_Rect
,
223 const ::basegfx::B2DRange
& i_Rect
,
224 const ::basegfx::B2DHomMatrix
& i_Transformation
);
226 /** Calc a transform that maps the upper, left corner of a
227 rectangle to the origin.
229 The method is a specialized version of
230 calcRectToRectTransform() (Removed now), mapping the input rectangle's
231 the upper, left corner to the origin, and leaving the size
235 Output parameter, to receive the resulting transformation
239 Input parameter, specifies the original source
240 rectangle. The resulting transformation will exactly map
241 the source rectangle's upper, left corner to the origin.
243 @param i_transformation
244 The original transformation matrix. This is changed with
245 translations (if necessary), to exactly map the source
246 rectangle to the origin.
248 @return a reference to the resulting transformation matrix
250 @see calcRectToRectTransform()
251 @see calcTransformedRectBounds()
253 CANVASTOOLS_DLLPUBLIC ::basegfx::B2DHomMatrix
& calcRectToOriginTransform( ::basegfx::B2DHomMatrix
& o_transform
,
254 const ::basegfx::B2DRange
& i_srcRect
,
255 const ::basegfx::B2DHomMatrix
& i_transformation
);
257 /** Check whether a given rectangle is within another
258 transformed rectangle.
260 This method checks for polygonal containedness, i.e. the
261 transformed rectangle is not represented as an axis-alignd
262 rectangle anymore (like calcTransformedRectBounds()), but
263 polygonal. Thus, the insideness test is based on tight
266 @param rContainedRect
267 This rectangle is checked, whether it is fully within the
268 transformed rTransformRect.
270 @param rTransformRect
271 This rectangle is transformed, and then checked whether it
272 fully contains rContainedRect.
274 @param rTransformation
275 This transformation is applied to rTransformRect
277 CANVASTOOLS_DLLPUBLIC
bool isInside( const ::basegfx::B2DRange
& rContainedRect
,
278 const ::basegfx::B2DRange
& rTransformRect
,
279 const ::basegfx::B2DHomMatrix
& rTransformation
);
281 /** Clip a scroll to the given bound rect
283 @param io_rSourceArea
284 Source area to scroll. The resulting clipped source area
288 Destination point of the scroll (upper, left corner of
289 rSourceArea after the scroll). The new, resulting
290 destination point is returned therein.q
292 @param o_ClippedAreas
293 Vector of rectangles in the <em>destination</em> area
294 coordinate system, which are clipped away from the source
295 area, and thus need extra updates (i.e. they are not
296 correctly copy from the scroll operation, since there was
297 no information about them in the source).
300 Bounds to clip against.
302 @return false, if the resulting scroll area is empty
304 CANVASTOOLS_DLLPUBLIC
bool clipScrollArea( ::basegfx::B2IRange
& io_rSourceArea
,
305 ::basegfx::B2IPoint
& io_rDestPoint
,
306 ::std::vector
< ::basegfx::B2IRange
>& o_ClippedAreas
,
307 const ::basegfx::B2IRange
& rBounds
);
309 /** Clip a blit between two differently surfaces.
311 This method clips source and dest rect for a clip between
312 two differently clipped surfaces, such that the resulting
313 blit rects are fully within both clip areas.
315 @param io_rSourceArea
316 Source area of the blit. Returned therein is the computed
320 Dest area of the blit. Returned therein is the computed
324 Clip bounds of the source surface
327 Clip bounds of the dest surface
329 @return false, if the resulting blit is empty, i.e. fully
332 CANVASTOOLS_DLLPUBLIC ::basegfx::B2IRange
spritePixelAreaFromB2DRange( const ::basegfx::B2DRange
& rRange
);
334 /** Retrieve various internal properties of the actual canvas implementation.
336 This method retrieves a bunch of internal, implementation-
337 and platform-dependent values from the canvas
338 implementation. Among them are for example operating
339 system window handles. The actual layout and content of
340 the returned sequence is dependent on the component
341 implementation, undocumented and subject to change.
344 Input parameter, the canvas representation for which the device information
348 Output parameter, the sequence of Anys that hold the device parameters. Layout is as described above
350 @return A reference to the resulting sequence of parameters
352 CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& getDeviceInfo(
353 const ::com::sun::star::uno::Reference
< ::com::sun::star::rendering::XCanvas
>& i_rxCanvas
,
354 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& o_rxParams
);
356 /** Return a color space for a default RGBA integer format
358 Use this method for dead-simple bitmap implementations,
359 that map all their formats to 8888 RGBA color.
361 CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Reference
< ::com::sun::star::rendering::XIntegerBitmapColorSpace
> getStdColorSpace();
363 /** Return a color space for a default RGB integer format
365 Use this method for dead-simple bitmap implementations,
366 that map all their formats to 8888 RGB color (the last byte
369 CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Reference
< ::com::sun::star::rendering::XIntegerBitmapColorSpace
> getStdColorSpaceWithoutAlpha();
371 /** Return a memory layout for a default RGBA integer format
373 Use this method for dead-simple bitmap implementations,
374 that map all their formats to 8888 RGBA color.
376 CANVASTOOLS_DLLPUBLIC ::com::sun::star::rendering::IntegerBitmapLayout
getStdMemoryLayout(
377 const ::com::sun::star::geometry::IntegerSize2D
& rBitmapSize
);
379 /// Convert standard 8888 RGBA color to vcl color
380 CANVASTOOLS_DLLPUBLIC ::Color
stdIntSequenceToColor( const ::com::sun::star::uno::Sequence
<sal_Int8
>& rColor
);
382 /// Convert standard 8888 RGBA color to vcl color
383 CANVASTOOLS_DLLPUBLIC ::com::sun::star::uno::Sequence
<sal_Int8
> colorToStdIntSequence( const ::Color
& rColor
);
385 // Modelled closely after boost::numeric_cast, only that we
386 // issue some trace output here and throw a RuntimeException
388 /** Cast numeric value into another (numeric) data type
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.
395 template< typename Target
, typename Source
> inline Target
numeric_cast( Source arg
)
397 // typedefs abbreviating respective trait classes
398 typedef ::std::numeric_limits
< Source
> SourceLimits
;
399 typedef ::std::numeric_limits
< Target
> TargetLimits
;
404 if( ( arg
<0 && !TargetLimits::is_signed
) || // losing the sign here
405 ( SourceLimits::is_signed
&& arg
<TargetLimits::min()) || // underflow will happen
406 ( arg
>TargetLimits::max() ) ) // overflow will happen
408 # if OSL_DEBUG_LEVEL > 2
409 OSL_TRACE("numeric_cast detected data loss");
411 throw ::com::sun::star::uno::RuntimeException(
412 "numeric_cast detected data loss",
416 return static_cast<Target
>(arg
);
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
);
423 /** Retrieve for small bound marks around each corner of the given rectangle
425 CANVASTOOLS_DLLPUBLIC ::basegfx::B2DPolyPolygon
getBoundMarksPolyPolygon( const ::basegfx::B2DRange
& rRange
);
427 /** Calculate number of gradient "strips" to generate (takes
428 into account device resolution)
431 Maximal integer difference between all color stops, needed
432 for smooth gradient color differences
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
,
440 /** A very simplistic map for ASCII strings and arbitrary value
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.
449 The value type this map should store, associated with an ASCII
452 template< typename ValueType
> class ValueMap
461 /** Create a ValueMap for the given array of MapEntries.
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.
471 Number of entries for pMap
473 @param bCaseSensitive
474 Whether the map query should be performed case sensitive
475 or not. When bCaseSensitive is false, all MapEntry strings
478 ValueMap( const MapEntry
* pMap
,
479 ::std::size_t nEntries
,
480 bool bCaseSensitive
) :
482 mnEntries( nEntries
),
483 mbCaseSensitive( bCaseSensitive
)
486 // Ensure that map entries are sorted (and all lowercase, if this
487 // map is case insensitive)
488 const OString
aStr( pMap
->maKey
);
489 if( !mbCaseSensitive
&&
490 aStr
!= aStr
.toAsciiLowerCase() )
492 OSL_TRACE("ValueMap::ValueMap(): Key %s is not lowercase",
494 OSL_FAIL( "ValueMap::ValueMap(): Key is not lowercase" );
499 for( ::std::size_t i
=0; i
<mnEntries
-1; ++i
, ++pMap
)
501 if( !mapComparator(pMap
[0], pMap
[1]) &&
502 mapComparator(pMap
[1], pMap
[0]) )
504 OSL_TRACE("ValueMap::ValueMap(): Map is not sorted, keys %s and %s are wrong",
507 OSL_FAIL( "ValueMap::ValueMap(): Map is not sorted" );
510 const OString
aStr2( pMap
[1].maKey
);
511 if( !mbCaseSensitive
&&
512 aStr2
!= aStr2
.toAsciiLowerCase() )
514 OSL_TRACE("ValueMap::ValueMap(): Key %s is not lowercase",
516 OSL_FAIL( "ValueMap::ValueMap(): Key is not lowercase" );
523 /** Lookup a value for the given query string
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.
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.
535 @return true, if a matching entry was found.
537 bool lookup( const OUString
& rName
,
538 ValueType
& o_rResult
) const
540 // rName is required to contain only ASCII characters.
541 // TODO(Q1): Enforce this at upper layers
542 OString
aKey( OUStringToOString( mbCaseSensitive
? rName
: rName
.toAsciiLowerCase(),
543 RTL_TEXTENCODING_ASCII_US
) );
544 MapEntry aSearchKey
=
550 const MapEntry
* pRes
;
551 const MapEntry
* pEnd
= mpMap
+mnEntries
;
552 if( (pRes
=::std::lower_bound( mpMap
,
555 &mapComparator
)) != pEnd
)
557 // place to _insert before_ found - is it equal to
559 if( strcmp( pRes
->maKey
, aSearchKey
.maKey
) == 0 )
561 // yep, correct entry found
562 o_rResult
= pRes
->maValue
;
572 static bool mapComparator( const MapEntry
& rLHS
,
573 const MapEntry
& rRHS
)
575 return strcmp( rLHS
.maKey
,
579 const MapEntry
* mpMap
;
580 ::std::size_t mnEntries
;
581 bool mbCaseSensitive
;
584 CANVASTOOLS_DLLPUBLIC
void clipOutDev(const css::rendering::ViewState
& viewState
,
585 const css::rendering::RenderState
& renderState
,
586 OutputDevice
& rOutDev
,
587 OutputDevice
* p2ndOutDev
=NULL
);
591 #endif /* INCLUDED_CANVAS_CANVASTOOLS_HXX */
593 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */