fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sdext / source / pdfimport / misc / pdfihelper.cxx
blob692ba8731b0122ac1f524857934d53cd8d990c2b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "pdfihelper.hxx"
23 #include <rtl/ustrbuf.hxx>
24 #include <basegfx/numeric/ftools.hxx>
26 #include <math.h>
28 using namespace pdfi;
29 using namespace com::sun::star;
31 double pdfi::GetAverageTransformationScale(const basegfx::B2DHomMatrix& matrix)
33 double rotate, shearX;
34 basegfx::B2DTuple scale, translation;
35 matrix.decompose(scale, translation, rotate, shearX);
36 return (fabs(scale.getX()) + fabs(scale.getY())) / 2.0;
39 void pdfi::FillDashStyleProps(PropertyMap& props, const std::vector<double>& dashArray, double scale)
41 size_t pairCount = dashArray.size() / 2;
43 double distance = 0.0;
44 for (size_t i = 0; i < pairCount; i++)
45 distance += dashArray[i * 2 + 1];
46 distance /= pairCount;
48 props["draw:style"] = "rect";
49 props["draw:distance"] = convertPixelToUnitString(distance * scale);
51 int dotStage = 0;
52 int dotCounts[3] = {0, 0, 0};
53 double dotLengths[3] = {0.0, 0.0, 0.0};
55 for (size_t i = 0; i < pairCount; i++)
57 if (dotLengths[dotStage] != dashArray[i * 2])
59 dotStage++;
60 if (dotStage == 3)
61 break;
63 dotCounts[dotStage] = 1;
64 dotLengths[dotStage] = dashArray[i * 2];
66 else
68 dotCounts[dotStage]++;
72 for (int i = 1; i < 3; i++)
74 if (dotCounts[i] == 0)
75 continue;
76 props["draw:dots" + OUString::number(i)] = OUString::number(dotCounts[i]);
77 props["draw:dots" + OUString::number(i) + "-length"] =
78 convertPixelToUnitString(dotLengths[i] * scale);
82 OUString pdfi::getColorString( const rendering::ARGBColor& rCol )
84 OUStringBuffer aBuf( 7 );
85 const sal_uInt8 nRed ( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Red * 255.0 ) ) );
86 const sal_uInt8 nGreen( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Green * 255.0 ) ) );
87 const sal_uInt8 nBlue ( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Blue * 255.0 ) ) );
88 aBuf.append( '#' );
89 if( nRed < 16 )
90 aBuf.append( '0' );
91 aBuf.append( sal_Int32(nRed), 16 );
92 if( nGreen < 16 )
93 aBuf.append( '0' );
94 aBuf.append( sal_Int32(nGreen), 16 );
95 if( nBlue < 16 )
96 aBuf.append( '0' );
97 aBuf.append( sal_Int32(nBlue), 16 );
99 return aBuf.makeStringAndClear();
102 OUString pdfi::getPercentString(double value)
104 OUStringBuffer buf(32);
105 buf.append(value);
106 buf.appendAscii("%");
107 return buf.makeStringAndClear();
110 OUString pdfi::unitMMString( double fMM )
112 OUStringBuffer aBuf( 32 );
113 aBuf.append( rtl_math_round( fMM, 2, rtl_math_RoundingMode_Floor ) );
114 aBuf.appendAscii( "mm" );
116 return aBuf.makeStringAndClear();
119 OUString pdfi::convertPixelToUnitString( double fPix )
121 OUStringBuffer aBuf( 32 );
122 aBuf.append( rtl_math_round( convPx2mm( fPix ), 2, rtl_math_RoundingMode_Floor ) );
123 aBuf.appendAscii( "mm" );
125 return aBuf.makeStringAndClear();
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */