LibreOffice Module oox (master) 1
textbodyproperties.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#include <com/sun/star/text/WritingMode.hpp>
21#include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
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
33using namespace ::com::sun::star::drawing;
34using namespace ::com::sun::star::text;
35using namespace css;
36
37namespace oox::drawingml {
38
40 : mbAnchorCtr(false)
41 , meVA( TextVerticalAdjust_TOP )
42{
43}
44
45/* For Legacy purposes: TODO: Check if it is required at all! */
47{
48 sal_Int32 tVert = moVert.value_or( XML_horz );
49 if( !(tVert == XML_vert || tVert == XML_eaVert || tVert == XML_vert270 || tVert == XML_mongolianVert) )
50 return;
51
52 // #160799# fake different vertical text modes by top-bottom writing mode
53 maPropertyMap.setProperty( PROP_TextWritingMode, WritingMode_TB_RL);
54
55 // workaround for TB_LR as using WritingMode2 doesn't work
56 if( meVA != TextVerticalAdjust_CENTER )
57 maPropertyMap.setProperty( PROP_TextHorizontalAdjust,
58 (tVert == XML_vert270) ? TextHorizontalAdjust_RIGHT : TextHorizontalAdjust_LEFT);
59 if( tVert == XML_vert270 )
60 maPropertyMap.setProperty( PROP_TextVerticalAdjust, TextVerticalAdjust_BOTTOM);
61 if( ( tVert == XML_vert && meVA == TextVerticalAdjust_TOP ) ||
62 ( tVert == XML_vert270 && meVA == TextVerticalAdjust_BOTTOM ) )
63 maPropertyMap.setProperty( PROP_TextHorizontalAdjust, TextHorizontalAdjust_RIGHT);
64 else if( meVA == TextVerticalAdjust_CENTER )
65 maPropertyMap.setProperty( PROP_TextHorizontalAdjust, TextHorizontalAdjust_CENTER);
66}
67
68/* Push text distances / insets, taking into consideration Shape Rotation */
70{
71 for (auto & rValue : maTextDistanceValues)
72 rValue.reset();
73
74 sal_Int32 nOff = 0;
75 static constexpr const std::array<sal_Int32, 4> aProps {
76 PROP_TextLeftDistance,
77 PROP_TextUpperDistance,
78 PROP_TextRightDistance,
79 PROP_TextLowerDistance
80 };
81
82 switch (moRotation.value_or(0))
83 {
84 case 90*1*60000: nOff = 3; break;
85 case 90*2*60000: nOff = 2; break;
86 case 90*3*60000: nOff = 1; break;
87 default: break;
88 }
89
90 for (size_t i = 0; i < aProps.size(); i++)
91 {
92 sal_Int32 nVal = 0;
93
94 // Hack for n#760986
95 // TODO: Preferred method would be to have a textbox on top
96 // of the shape and the place it according to the (off,ext)
97 if (nOff == 0 && moTextOffLeft)
98 nVal = *moTextOffLeft;
99
100 if (nOff == 1 && moTextOffUpper)
101 nVal = *moTextOffUpper;
102
103
104 if (nOff == 2 && moTextOffRight)
105 nVal = *moTextOffRight;
106
107 if (nOff == 3 && moTextOffLower)
108 nVal = *moTextOffLower;
109
110
111 if( nVal < 0 )
112 nVal = 0;
113
114 sal_Int32 nTextOffsetValue = nVal;
115
116 if (moInsets[i])
117 {
118 nTextOffsetValue = *moInsets[i] + nVal;
119 }
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 if (moInsets[i] || nTextOffsetValue)
124 {
125 maTextDistanceValues[nOff] = nTextOffsetValue;
126 }
127
128 nOff = (nOff + 1) % aProps.size();
129 }
130
131 // Check if bottom and top are set
133 {
134 double nHeight = rTextAreaSize.getHeight();
135
136 double nTop = *maTextDistanceValues[1];
137 double nBottom = *maTextDistanceValues[3];
138
139 // Check if top + bottom is more than text area height.
140 // If yes, we need to adjust the values as defined in OOXML.
141 if (nTop + nBottom >= nHeight)
142 {
143 double diffFactor = (nTop + nBottom - nHeight) / 2.0;
144
145 maTextDistanceValues[1] = nTop - diffFactor;
146 maTextDistanceValues[3] = nBottom - diffFactor;
147 }
148 }
149
150 for (size_t i = 0; i < aProps.size(); i++)
151 {
154 }
155}
156
157/* Readjust the text distances / insets if necessary to take
158 the text area into account, not just the shape area*/
159void TextBodyProperties::readjustTextDistances(uno::Reference<drawing::XShape> const& xShape)
160{
161 // Only for custom shapes (for now)
162 auto* pCustomShape = dynamic_cast<SdrObjCustomShape*>(SdrObject::getSdrObjectFromXShape(xShape));
163 if (pCustomShape)
164 {
165 sal_Int32 nLower = pCustomShape->GetTextLowerDistance();
166 sal_Int32 nUpper = pCustomShape->GetTextUpperDistance();
167
168 pCustomShape->SetMergedItem(makeSdrTextUpperDistItem(0));
169 pCustomShape->SetMergedItem(makeSdrTextLowerDistItem(0));
170
171 tools::Rectangle aAnchorRect;
172 pCustomShape->TakeTextAnchorRect(aAnchorRect);
173 Size aAnchorSize = aAnchorRect.GetSize();
174
175 pushTextDistances(aAnchorSize);
177 {
178 nLower = *maTextDistanceValues[3];
179 nUpper = *maTextDistanceValues[1];
180 }
181
182 pCustomShape->SetMergedItem(makeSdrTextLowerDistItem(nLower));
183 pCustomShape->SetMergedItem(makeSdrTextUpperDistItem(nUpper));
184 }
185}
186
187
188} // namespace oox::drawingml
189
190/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SdrObject * getSdrObjectFromXShape(const css::uno::Reference< css::uno::XInterface > &xInt)
tools::Long GetTextLowerDistance() const
constexpr tools::Long getHeight() const
bool setProperty(sal_Int32 nPropId, Type &&rValue)
Sets the specified property to the passed value.
Definition: propertymap.hxx:72
constexpr Size GetSize() const
SdrMetricItem makeSdrTextUpperDistItem(tools::Long mnHeight)
SdrMetricItem makeSdrTextLowerDistItem(tools::Long mnHeight)
std::optional< sal_Int32 > moTextOffLower
std::optional< sal_Int32 > moVert
css::drawing::TextVerticalAdjust meVA
std::array< std::optional< sal_Int32 >, 4 > moInsets
std::optional< sal_Int32 > moTextOffUpper
void readjustTextDistances(css::uno::Reference< css::drawing::XShape > const &xShape)
void pushTextDistances(Size const &rShapeSize)
std::optional< sal_Int32 > moTextOffLeft
std::array< std::optional< sal_Int32 >, 4 > maTextDistanceValues
std::optional< sal_Int32 > moRotation
std::optional< sal_Int32 > moTextOffRight