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
37namespace basegfx
38{
39 class B2DHomMatrix;
40 class B2DRange;
41 class B2IRange;
42 class B2IPoint;
43 class B2DPolyPolygon;
44}
45
47{
48 struct RealSize2D;
49 struct IntegerSize2D;
50 struct AffineMatrix2D;
51 struct Matrix2D;
52}
53
55{
56 struct RenderState;
57 struct ViewState;
58 struct IntegerBitmapLayout;
59 class XCanvas;
60 struct Texture;
61 class XIntegerBitmapColorSpace;
62}
63
64namespace com::sun::star::awt
65{
66 struct Rectangle;
67 class XWindow2;
68}
69
70namespace com::sun::star::beans {
71 struct PropertyValue;
72}
73
74class Color;
75class OutputDevice;
76
77namespace 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
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 return;
466
467 for( ::std::size_t i=0; i<mnEntries-1; ++i, ++pMap )
468 {
469 if( !mapComparator(pMap[0], pMap[1]) &&
470 mapComparator(pMap[1], pMap[0]) )
471 {
472 SAL_WARN("canvas", "ValueMap::ValueMap(): Map is not sorted, keys are wrong, "
473 << pMap[0].maKey << " and " << pMap[1].maKey);
474 OSL_FAIL( "ValueMap::ValueMap(): Map is not sorted" );
475 }
476
477 const OString aStr2( pMap[1].maKey );
478 if( !mbCaseSensitive &&
479 aStr2 != aStr2.toAsciiLowerCase() )
480 {
481 SAL_WARN("canvas", "ValueMap::ValueMap(): Key is not lowercase" << pMap[1].maKey);
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* pEnd = mpMap+mnEntries;
515 const MapEntry* pRes = ::std::lower_bound( mpMap,
516 pEnd,
517 aSearchKey,
518 &mapComparator );
519 if( pRes != 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
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: */
const CacheKey maKey
#define CANVASTOOLS_DLLPUBLIC
A very simplistic map for ASCII strings and arbitrary value types.
ValueMap(const MapEntry *pMap, ::std::size_t nEntries, bool bCaseSensitive)
Create a ValueMap for the given array of MapEntries.
static bool mapComparator(const MapEntry &rLHS, const MapEntry &rRHS)
const MapEntry * mpMap
bool lookup(const OUString &rName, ValueType &o_rResult) const
Lookup a value for the given query string.
float x
Definition: dx_9rm.cxx:190
#define max(a, b)
Definition: dx_winstuff.hxx:43
#define SAL_WARN(area, stream)
aStr
uno::Reference< rendering::XIntegerBitmapColorSpace > const & getStdColorSpaceWithoutAlpha()
Return a color space for a default RGB integer format.
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
bool isInside(const ::basegfx::B2DRange &rContainedRect, const ::basegfx::B2DRange &rTransformRect, const ::basegfx::B2DHomMatrix &rTransformation)
Check whether a given rectangle is within another transformed rectangle.
::basegfx::B2DPolyPolygon getBoundMarksPolyPolygon(const ::basegfx::B2DRange &rRange)
Retrieve for small bound marks around each corner of the given rectangle.
rendering::IntegerBitmapLayout getStdMemoryLayout(const geometry::IntegerSize2D &rBmpSize)
rendering::ViewState & setViewStateTransform(rendering::ViewState &viewState, const ::basegfx::B2DHomMatrix &transform)
rendering::RenderState & setRenderStateTransform(rendering::RenderState &renderState, const ::basegfx::B2DHomMatrix &transform)
rendering::RenderState & appendToRenderState(rendering::RenderState &renderState, const ::basegfx::B2DHomMatrix &rTransform)
uno::Sequence< sal_Int8 > colorToStdIntSequence(const ::Color &rColor)
Convert standard 8888 RGBA color to vcl color.
geometry::Matrix2D & setIdentityMatrix2D(geometry::Matrix2D &matrix)
::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.
rendering::RenderState & prependToRenderState(rendering::RenderState &renderState, const ::basegfx::B2DHomMatrix &rTransform)
sal_Int32 roundDown(const double &rVal)
Count the number of 1-bits of a n-bit value.
Target numeric_cast(Source arg)
Cast numeric value into another (numeric) data type.
::basegfx::B2DRange & calcTransformedRectBounds(::basegfx::B2DRange &outRect, const ::basegfx::B2DRange &inRect, const ::basegfx::B2DHomMatrix &transformation)
Calc the bounding rectangle of a transformed rectangle.
::basegfx::B2IRange spritePixelAreaFromB2DRange(const ::basegfx::B2DRange &rRange)
Clip a blit between two differently surfaces.
geometry::AffineMatrix2D & setIdentityAffineMatrix2D(geometry::AffineMatrix2D &matrix)
geometry::RealSize2D createInfiniteSize2D()
Create a RealSize2D with both coordinate values set to +infinity.
Definition: canvastools.cxx:67
sal_uInt32 nextPow2(sal_uInt32 x)
Compute the next highest power of 2 of a 32-bit value.
Definition: canvastools.hxx:88
awt::Rectangle getAbsoluteWindowRect(const awt::Rectangle &rRect, const uno::Reference< awt::XWindow2 > &xWin)
bool clipScrollArea(::basegfx::B2IRange &io_rSourceArea, ::basegfx::B2IPoint &io_rDestPoint, std::vector< ::basegfx::B2IRange > &o_ClippedAreas, const ::basegfx::B2IRange &rBounds)
uno::Reference< rendering::XIntegerBitmapColorSpace > const & getStdColorSpace()
Return a color space for a default RGBA integer format.
sal_Int32 roundUp(const double &rVal)
Round given floating point value up to next integer.
rendering::RenderState & initRenderState(rendering::RenderState &renderState)
Definition: canvastools.cxx:74
::basegfx::B2DHomMatrix & getViewStateTransform(::basegfx::B2DHomMatrix &transform, const rendering::ViewState &viewState)
Definition: canvastools.cxx:94
rendering::ViewState & initViewState(rendering::ViewState &viewState)
Definition: canvastools.cxx:85
::basegfx::B2DHomMatrix & getRenderStateTransform(::basegfx::B2DHomMatrix &transform, const rendering::RenderState &renderState)
int calcGradientStepCount(::basegfx::B2DHomMatrix &rTotalTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState, const rendering::Texture &texture, int nColorSteps)
void clipOutDev(const rendering::ViewState &viewState, const rendering::RenderState &renderState, OutputDevice &rOutDev, OutputDevice *p2ndOutDev)
uno::Sequence< uno::Any > & getDeviceInfo(const uno::Reference< rendering::XCanvas > &i_rxCanvas, uno::Sequence< uno::Any > &o_rxParams)
void extractExtraFontProperties(const uno::Sequence< beans::PropertyValue > &rExtraFontProperties, sal_uInt32 &rEmphasisMark)
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
ValueType
ViewState