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
54
55 // workaround for TB_LR as using WritingMode2 doesn't work
56 if( meVA != TextVerticalAdjust_CENTER )
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 text 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 {
80 };
81
82 switch (moTextPreRotation.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 if (moVert && (moVert.value() == XML_eaVert || moVert.value() == XML_vert))
91 nOff = (nOff + 3) % aProps.size();
92 else if (moVert && moVert.value() == XML_vert270)
93 nOff = (nOff + 1) % aProps.size();
94
95 for (size_t i = 0; i < aProps.size(); i++)
96 {
97 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 if (nOff == 0 && moTextOffLeft)
103 nVal = *moTextOffLeft;
104
105 if (nOff == 1 && moTextOffUpper)
106 nVal = *moTextOffUpper;
107
108 if (nOff == 2 && moTextOffRight)
109 nVal = *moTextOffRight;
110
111 if (nOff == 3 && moTextOffLower)
112 nVal = *moTextOffLower;
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 > moTextOffRight
std::optional< sal_Int32 > moTextPreRotation
constexpr OUStringLiteral PROP_TextLeftDistance
constexpr OUStringLiteral PROP_TextHorizontalAdjust
constexpr OUStringLiteral PROP_TextUpperDistance
constexpr OUStringLiteral PROP_TextLowerDistance
constexpr OUStringLiteral PROP_TextRightDistance
constexpr OUStringLiteral PROP_TextWritingMode
constexpr OUStringLiteral PROP_TextVerticalAdjust