/src/libreoffice/oox/source/drawingml/textbodyproperties.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 | | #include <com/sun/star/text/WritingMode.hpp> |
21 | | #include <com/sun/star/drawing/TextHorizontalAdjust.hpp> |
22 | | #include <drawingml/textbodyproperties.hxx> |
23 | | #include <oox/token/properties.hxx> |
24 | | #include <oox/token/tokens.hxx> |
25 | | #include <tools/gen.hxx> |
26 | | #include <svx/svdobj.hxx> |
27 | | #include <svx/svdotext.hxx> |
28 | | #include <svx/svdoashp.hxx> |
29 | | #include <svx/sdtditm.hxx> |
30 | | |
31 | | #include <array> |
32 | | |
33 | | using namespace ::com::sun::star::drawing; |
34 | | using namespace ::com::sun::star::text; |
35 | | using namespace css; |
36 | | |
37 | | namespace oox::drawingml { |
38 | | |
39 | | TextBodyProperties::TextBodyProperties() |
40 | 94.9k | : mbAnchorCtr(false) |
41 | 94.9k | , meVA( TextVerticalAdjust_TOP ) |
42 | 94.9k | { |
43 | 94.9k | } |
44 | | |
45 | | /* For Legacy purposes: TODO: Check if it is required at all! */ |
46 | | void TextBodyProperties::pushVertSimulation() |
47 | 39.8k | { |
48 | 39.8k | sal_Int32 tVert = moVert.value_or( XML_horz ); |
49 | 39.8k | if( !(tVert == XML_vert || tVert == XML_eaVert || tVert == XML_vert270 || tVert == XML_mongolianVert) ) |
50 | 38.5k | return; |
51 | | |
52 | | // #160799# fake different vertical text modes by top-bottom writing mode |
53 | 1.28k | maPropertyMap.setProperty( PROP_TextWritingMode, WritingMode_TB_RL); |
54 | | |
55 | | // workaround for TB_LR as using WritingMode2 doesn't work |
56 | 1.28k | if( meVA != TextVerticalAdjust_CENTER ) |
57 | 1.03k | maPropertyMap.setProperty( PROP_TextHorizontalAdjust, |
58 | 1.03k | (tVert == XML_vert270) ? TextHorizontalAdjust_RIGHT : TextHorizontalAdjust_LEFT); |
59 | 1.28k | if( tVert == XML_vert270 ) |
60 | 0 | maPropertyMap.setProperty( PROP_TextVerticalAdjust, TextVerticalAdjust_BOTTOM); |
61 | 1.28k | if( ( tVert == XML_vert && meVA == TextVerticalAdjust_TOP ) || |
62 | 1.28k | ( tVert == XML_vert270 && meVA == TextVerticalAdjust_BOTTOM ) ) |
63 | 0 | maPropertyMap.setProperty( PROP_TextHorizontalAdjust, TextHorizontalAdjust_RIGHT); |
64 | 1.28k | else if( meVA == TextVerticalAdjust_CENTER ) |
65 | 250 | maPropertyMap.setProperty( PROP_TextHorizontalAdjust, TextHorizontalAdjust_CENTER); |
66 | 1.28k | } |
67 | | |
68 | | /* Push text distances / insets, taking into consideration text rotation */ |
69 | | void TextBodyProperties::pushTextDistances(Size const& rTextAreaSize) |
70 | 61.3k | { |
71 | 61.3k | for (auto & rValue : maTextDistanceValues) |
72 | 245k | rValue.reset(); |
73 | | |
74 | 61.3k | sal_Int32 nOff = 0; |
75 | 61.3k | static constexpr auto aProps = std::to_array<sal_Int32>({ |
76 | 61.3k | PROP_TextLeftDistance, |
77 | 61.3k | PROP_TextUpperDistance, |
78 | 61.3k | PROP_TextRightDistance, |
79 | 61.3k | PROP_TextLowerDistance |
80 | 61.3k | }); |
81 | | |
82 | 61.3k | switch (moTextPreRotation.value_or(0)) |
83 | 61.3k | { |
84 | 0 | case 90*1*60000: nOff = 3; break; |
85 | 0 | case 90*2*60000: nOff = 2; break; |
86 | 0 | case 90*3*60000: nOff = 1; break; |
87 | 61.3k | default: break; |
88 | 61.3k | } |
89 | | |
90 | 61.3k | if (moVert && (moVert.value() == XML_eaVert || moVert.value() == XML_vert)) |
91 | 1.30k | nOff = (nOff + 3) % aProps.size(); |
92 | 60.0k | else if (moVert && moVert.value() == XML_vert270) |
93 | 40 | nOff = (nOff + 1) % aProps.size(); |
94 | | |
95 | 306k | for (size_t i = 0; i < aProps.size(); i++) |
96 | 245k | { |
97 | 245k | sal_Int32 nVal = 0; |
98 | | |
99 | | // Hack for n#760986 |
100 | | // TODO: Preferred method would be to have a textbox on top |
101 | | // of the shape and the place it according to the (off,ext) |
102 | 245k | if (nOff == 0 && moTextOffLeft) |
103 | 284 | nVal = *moTextOffLeft; |
104 | | |
105 | 245k | if (nOff == 1 && moTextOffUpper) |
106 | 312 | nVal = *moTextOffUpper; |
107 | | |
108 | 245k | if (nOff == 2 && moTextOffRight) |
109 | 300 | nVal = *moTextOffRight; |
110 | | |
111 | 245k | if (nOff == 3 && moTextOffLower) |
112 | 312 | nVal = *moTextOffLower; |
113 | | |
114 | 245k | sal_Int32 nTextOffsetValue = nVal; |
115 | | |
116 | 245k | if (moInsets[i]) |
117 | 177k | { |
118 | 177k | nTextOffsetValue = *moInsets[i] + nVal; |
119 | 177k | } |
120 | | |
121 | | // if inset is set, then always set the value |
122 | | // this prevents the default to be set (0 is a valid value) |
123 | 245k | if (moInsets[i] || nTextOffsetValue) |
124 | 177k | { |
125 | 177k | maTextDistanceValues[nOff] = nTextOffsetValue; |
126 | 177k | } |
127 | | |
128 | 245k | nOff = (nOff + 1) % aProps.size(); |
129 | 245k | } |
130 | | |
131 | | // Check if left and right are set |
132 | 61.3k | if (maTextDistanceValues[0] && maTextDistanceValues[2]) |
133 | 44.3k | { |
134 | 44.3k | double nWidth = rTextAreaSize.getWidth(); |
135 | | |
136 | 44.3k | double nLeft = *maTextDistanceValues[0]; |
137 | 44.3k | double nRight = *maTextDistanceValues[2]; |
138 | | |
139 | | // Check if left + right is more than text area width. |
140 | | // If yes, we need to adjust the values as defined in OOXML. |
141 | | // (Overload zero width to mean don't adjust) |
142 | 44.3k | if (nWidth > 0 && nLeft + nRight >= nWidth) |
143 | 1.11k | { |
144 | 1.11k | double diffFactor = (nLeft + nRight - nWidth) / 2.0; |
145 | | |
146 | 1.11k | maTextDistanceValues[0] = nLeft - diffFactor; |
147 | 1.11k | maTextDistanceValues[2] = nRight - diffFactor; |
148 | 1.11k | } |
149 | 44.3k | } |
150 | | |
151 | | // Check if bottom and top are set |
152 | 61.3k | if (maTextDistanceValues[1] && maTextDistanceValues[3]) |
153 | 44.3k | { |
154 | 44.3k | double nHeight = rTextAreaSize.getHeight(); |
155 | | |
156 | 44.3k | double nTop = *maTextDistanceValues[1]; |
157 | 44.3k | double nBottom = *maTextDistanceValues[3]; |
158 | | |
159 | | // Check if top + bottom is more than text area height. |
160 | | // If yes, we need to adjust the values as defined in OOXML. |
161 | | // (Overload zero height to mean don't adjust) |
162 | 44.3k | if (nHeight > 0 && nTop + nBottom >= nHeight) |
163 | 19 | { |
164 | 19 | double diffFactor = (nTop + nBottom - nHeight) / 2.0; |
165 | | |
166 | 19 | maTextDistanceValues[1] = nTop - diffFactor; |
167 | 19 | maTextDistanceValues[3] = nBottom - diffFactor; |
168 | 19 | } |
169 | 44.3k | } |
170 | | |
171 | 306k | for (size_t i = 0; i < aProps.size(); i++) |
172 | 245k | { |
173 | 245k | if (maTextDistanceValues[i]) |
174 | 177k | maPropertyMap.setProperty(aProps[i], *maTextDistanceValues[i]); |
175 | 245k | } |
176 | 61.3k | } |
177 | | |
178 | | /* Readjust the text distances / insets if necessary to take |
179 | | the text area into account, not just the shape area*/ |
180 | | void TextBodyProperties::readjustTextDistances(uno::Reference<drawing::XShape> const& xShape) |
181 | 50.5k | { |
182 | | // Only for custom shapes (for now) |
183 | 50.5k | auto* pCustomShape = dynamic_cast<SdrObjCustomShape*>(SdrObject::getSdrObjectFromXShape(xShape)); |
184 | 50.5k | if (pCustomShape) |
185 | 10.7k | { |
186 | 10.7k | sal_Int32 nLower = pCustomShape->GetTextLowerDistance(); |
187 | 10.7k | sal_Int32 nUpper = pCustomShape->GetTextUpperDistance(); |
188 | | |
189 | 10.7k | pCustomShape->SetMergedItem(makeSdrTextUpperDistItem(0)); |
190 | 10.7k | pCustomShape->SetMergedItem(makeSdrTextLowerDistItem(0)); |
191 | | |
192 | 10.7k | tools::Rectangle aAnchorRect; |
193 | 10.7k | pCustomShape->TakeTextAnchorRect(aAnchorRect); |
194 | 10.7k | Size aAnchorSize = aAnchorRect.GetSize(); |
195 | | |
196 | 10.7k | pushTextDistances(aAnchorSize); |
197 | 10.7k | if (maTextDistanceValues[1] && maTextDistanceValues[3]) |
198 | 7.51k | { |
199 | 7.51k | nLower = *maTextDistanceValues[3]; |
200 | 7.51k | nUpper = *maTextDistanceValues[1]; |
201 | 7.51k | } |
202 | | |
203 | 10.7k | pCustomShape->SetMergedItem(makeSdrTextLowerDistItem(nLower)); |
204 | 10.7k | pCustomShape->SetMergedItem(makeSdrTextUpperDistItem(nUpper)); |
205 | 10.7k | } |
206 | 50.5k | } |
207 | | |
208 | | |
209 | | } // namespace oox::drawingml |
210 | | |
211 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |