Get the style color and number just once
[LibreOffice.git] / sdext / source / pdfimport / misc / pdfihelper.cxx
blobb30624025faa0efcf2f9aab36720e556dde55d11
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>
25 #include <rtl/math.hxx>
27 #include <math.h>
29 using namespace pdfi;
30 using namespace com::sun::star;
32 double pdfi::GetAverageTransformationScale(const basegfx::B2DHomMatrix& matrix)
34 double rotate, shearX;
35 basegfx::B2DTuple scale, translation;
36 matrix.decompose(scale, translation, rotate, shearX);
37 return (fabs(scale.getX()) + fabs(scale.getY())) / 2.0;
40 void pdfi::FillDashStyleProps(PropertyMap& props, const std::vector<double>& dashArray, double scale)
42 size_t pairCount = dashArray.size() / 2;
44 double distance = 0.0;
45 for (size_t i = 0; i < pairCount; i++)
46 distance += dashArray[i * 2 + 1];
47 distance /= pairCount;
49 props[u"draw:style"_ustr] = "rect";
50 props[u"draw:distance"_ustr] = convertPixelToUnitString(distance * scale);
52 int dotStage = 0;
53 int dotCounts[3] = {0, 0, 0};
54 double dotLengths[3] = {0.0, 0.0, 0.0};
56 for (size_t i = 0; i < pairCount; i++)
58 if (!rtl::math::approxEqual(dotLengths[dotStage], dashArray[i * 2]))
60 dotStage++;
61 if (dotStage == 3)
62 break;
64 dotCounts[dotStage] = 1;
65 dotLengths[dotStage] = dashArray[i * 2];
67 else
69 dotCounts[dotStage]++;
73 for (int i = 1; i < 3; i++)
75 if (dotCounts[i] == 0)
76 continue;
77 props["draw:dots" + OUString::number(i)] = OUString::number(dotCounts[i]);
78 props["draw:dots" + OUString::number(i) + "-length"] =
79 convertPixelToUnitString(dotLengths[i] * scale);
83 OUString pdfi::getColorString( const rendering::ARGBColor& rCol )
85 OUStringBuffer aBuf( 7 );
86 const sal_uInt8 nRed ( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Red * 255.0 ) ) );
87 const sal_uInt8 nGreen( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Green * 255.0 ) ) );
88 const sal_uInt8 nBlue ( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Blue * 255.0 ) ) );
89 aBuf.append( '#' );
90 if( nRed < 16 )
91 aBuf.append( '0' );
92 aBuf.append( sal_Int32(nRed), 16 );
93 if( nGreen < 16 )
94 aBuf.append( '0' );
95 aBuf.append( sal_Int32(nGreen), 16 );
96 if( nBlue < 16 )
97 aBuf.append( '0' );
98 aBuf.append( sal_Int32(nBlue), 16 );
100 return aBuf.makeStringAndClear();
103 OUString pdfi::getPercentString(double value)
105 return OUString::number(value) + "%";
108 OUString pdfi::unitMMString( double fMM )
110 return OUString::number(rtl_math_round( fMM, 2, rtl_math_RoundingMode_Floor )) + "mm";
113 OUString pdfi::convertPixelToUnitString( double fPix )
115 return OUString::number( rtl_math_round( convPx2mm( fPix ), 2, rtl_math_RoundingMode_Floor ) ) + "mm";
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */