/src/libreoffice/sdext/source/pdfimport/misc/pdfihelper.cxx
Line | Count | Source |
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> |
24 | | #include <basegfx/numeric/ftools.hxx> |
25 | | #include <rtl/math.hxx> |
26 | | |
27 | | #include <math.h> |
28 | | |
29 | | using namespace pdfi; |
30 | | using namespace com::sun::star; |
31 | | |
32 | | double pdfi::GetAverageTransformationScale(const basegfx::B2DHomMatrix& matrix) |
33 | 0 | { |
34 | 0 | double rotate, shearX; |
35 | 0 | basegfx::B2DTuple scale, translation; |
36 | 0 | matrix.decompose(scale, translation, rotate, shearX); |
37 | 0 | return (fabs(scale.getX()) + fabs(scale.getY())) / 2.0; |
38 | 0 | } |
39 | | |
40 | | void pdfi::FillDashStyleProps(PropertyMap& props, const std::vector<double>& dashArray, double scale) |
41 | 0 | { |
42 | 0 | size_t pairCount = dashArray.size() / 2; |
43 | |
|
44 | 0 | double distance = 0.0; |
45 | 0 | for (size_t i = 0; i < pairCount; i++) |
46 | 0 | distance += dashArray[i * 2 + 1]; |
47 | 0 | distance /= pairCount; |
48 | |
|
49 | 0 | props[u"draw:style"_ustr] = "rect"; |
50 | 0 | props[u"draw:distance"_ustr] = convertPixelToUnitString(distance * scale); |
51 | |
|
52 | 0 | int dotStage = 0; |
53 | 0 | int dotCounts[3] = {0, 0, 0}; |
54 | 0 | double dotLengths[3] = {0.0, 0.0, 0.0}; |
55 | |
|
56 | 0 | for (size_t i = 0; i < pairCount; i++) |
57 | 0 | { |
58 | 0 | if (!rtl::math::approxEqual(dotLengths[dotStage], dashArray[i * 2])) |
59 | 0 | { |
60 | 0 | dotStage++; |
61 | 0 | if (dotStage == 3) |
62 | 0 | break; |
63 | | |
64 | 0 | dotCounts[dotStage] = 1; |
65 | 0 | dotLengths[dotStage] = dashArray[i * 2]; |
66 | 0 | } |
67 | 0 | else |
68 | 0 | { |
69 | 0 | dotCounts[dotStage]++; |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | 0 | for (int i = 1; i < 3; i++) |
74 | 0 | { |
75 | 0 | if (dotCounts[i] == 0) |
76 | 0 | continue; |
77 | 0 | props["draw:dots" + OUString::number(i)] = OUString::number(dotCounts[i]); |
78 | 0 | props["draw:dots" + OUString::number(i) + "-length"] = |
79 | 0 | convertPixelToUnitString(dotLengths[i] * scale); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | OUString pdfi::getColorString( const rendering::ARGBColor& rCol ) |
84 | 0 | { |
85 | 0 | OUStringBuffer aBuf( 7 ); |
86 | 0 | const sal_uInt8 nRed ( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Red * 255.0 ) ) ); |
87 | 0 | const sal_uInt8 nGreen( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Green * 255.0 ) ) ); |
88 | 0 | const sal_uInt8 nBlue ( sal::static_int_cast<sal_Int8>( basegfx::fround( rCol.Blue * 255.0 ) ) ); |
89 | 0 | aBuf.append( '#' ); |
90 | 0 | if( nRed < 16 ) |
91 | 0 | aBuf.append( '0' ); |
92 | 0 | aBuf.append( sal_Int32(nRed), 16 ); |
93 | 0 | if( nGreen < 16 ) |
94 | 0 | aBuf.append( '0' ); |
95 | 0 | aBuf.append( sal_Int32(nGreen), 16 ); |
96 | 0 | if( nBlue < 16 ) |
97 | 0 | aBuf.append( '0' ); |
98 | 0 | aBuf.append( sal_Int32(nBlue), 16 ); |
99 | |
|
100 | 0 | return aBuf.makeStringAndClear(); |
101 | 0 | } |
102 | | |
103 | | OUString pdfi::getPercentString(double value) |
104 | 0 | { |
105 | 0 | return OUString::number(value) + "%"; |
106 | 0 | } |
107 | | |
108 | | OUString pdfi::unitMMString( double fMM ) |
109 | 0 | { |
110 | 0 | return OUString::number(rtl_math_round( fMM, 2, rtl_math_RoundingMode_Floor )) + "mm"; |
111 | 0 | } |
112 | | |
113 | | OUString pdfi::convertPixelToUnitString( double fPix ) |
114 | 0 | { |
115 | 0 | return OUString::number( rtl_math_round( convPx2mm( fPix ), 2, rtl_math_RoundingMode_Floor ) ) + "mm"; |
116 | 0 | } |
117 | | |
118 | | |
119 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |