LibreOffice Module sdext (master)  1
pdfihelper.cxx
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 
21 #include <pdfihelper.hxx>
22 
23 #include <rtl/ustrbuf.hxx>
25 
26 #include <math.h>
27 
28 using namespace pdfi;
29 using namespace com::sun::star;
30 
32 {
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;
37 }
38 
39 void pdfi::FillDashStyleProps(PropertyMap& props, const std::vector<double>& dashArray, double scale)
40 {
41  size_t pairCount = dashArray.size() / 2;
42 
43  double distance = 0.0;
44  for (size_t i = 0; i < pairCount; i++)
45  distance += dashArray[i * 2 + 1];
46  distance /= pairCount;
47 
48  props["draw:style"] = "rect";
49  props["draw:distance"] = convertPixelToUnitString(distance * scale);
50 
51  int dotStage = 0;
52  int dotCounts[3] = {0, 0, 0};
53  double dotLengths[3] = {0.0, 0.0, 0.0};
54 
55  for (size_t i = 0; i < pairCount; i++)
56  {
57  if (!rtl::math::approxEqual(dotLengths[dotStage], dashArray[i * 2]))
58  {
59  dotStage++;
60  if (dotStage == 3)
61  break;
62 
63  dotCounts[dotStage] = 1;
64  dotLengths[dotStage] = dashArray[i * 2];
65  }
66  else
67  {
68  dotCounts[dotStage]++;
69  }
70  }
71 
72  for (int i = 1; i < 3; i++)
73  {
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);
79  }
80 }
81 
82 OUString pdfi::getColorString( const rendering::ARGBColor& rCol )
83 {
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 );
98 
99  return aBuf.makeStringAndClear();
100 }
101 
102 OUString pdfi::getPercentString(double value)
103 {
104  return OUString::number(value) + "%";
105 }
106 
107 OUString pdfi::unitMMString( double fMM )
108 {
109  OUStringBuffer aBuf( 32 );
110  aBuf.append( rtl_math_round( fMM, 2, rtl_math_RoundingMode_Floor ) );
111  aBuf.append( "mm" );
112 
113  return aBuf.makeStringAndClear();
114 }
115 
116 OUString pdfi::convertPixelToUnitString( double fPix )
117 {
118  OUStringBuffer aBuf( 32 );
119  aBuf.append( rtl_math_round( convPx2mm( fPix ), 2, rtl_math_RoundingMode_Floor ) );
120  aBuf.append( "mm" );
121 
122  return aBuf.makeStringAndClear();
123 }
124 
125 
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< OUString, OUString > PropertyMap
Definition: pdfihelper.hxx:45
OUString unitMMString(double fMM)
Definition: pdfihelper.cxx:107
OUString getPercentString(double value)
Definition: pdfihelper.cxx:102
double getX() const
OUString convertPixelToUnitString(double fPix)
Definition: pdfihelper.cxx:116
aBuf
double getY() const
def rotate(shapename, deg)
double convPx2mm(double fPix)
Definition: pdfihelper.hxx:54
B2IRange fround(const B2DRange &rRange)
double distance
bool decompose(B2DTuple &rScale, B2DTuple &rTranslate, double &rRotate, double &rShearX) const
double GetAverageTransformationScale(const basegfx::B2DHomMatrix &matrix)
Definition: pdfihelper.cxx:31
int i
sal_Int32 scale
OUString getColorString(const css::rendering::ARGBColor &)
Convert color to "#FEFEFE" color notation.
void FillDashStyleProps(PropertyMap &props, const std::vector< double > &dashArray, double scale)
Definition: pdfihelper.cxx:39
unsigned char sal_uInt8