1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/vml/vmltextbox.hxx>
22 #include <rtl/ustrbuf.hxx>
23 #include <comphelper/diagnose_ex.hxx>
24 #include <com/sun/star/awt/FontWeight.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
27 #include <com/sun/star/drawing/XShape.hpp>
28 #include <com/sun/star/text/XTextAppend.hpp>
29 #include <com/sun/star/text/WritingMode.hpp>
30 #include <com/sun/star/style/ParagraphAdjust.hpp>
31 #include <comphelper/sequence.hxx>
32 #include <comphelper/sequenceashashmap.hxx>
37 using namespace com::sun::star
;
39 TextFontModel::TextFontModel()
43 TextPortionModel::TextPortionModel( TextParagraphModel aParagraph
, TextFontModel aFont
, OUString aText
) :
44 maParagraph(std::move( aParagraph
)),
45 maFont(std::move( aFont
)),
46 maText(std::move( aText
))
50 TextBox::TextBox(ShapeTypeModel
& rTypeModel
)
51 : mrTypeModel(rTypeModel
)
52 , borderDistanceSet( false )
53 , borderDistanceLeft(0)
54 , borderDistanceTop(0)
55 , borderDistanceRight(0)
56 , borderDistanceBottom(0)
60 void TextBox::appendPortion( const TextParagraphModel
& rParagraph
, const TextFontModel
& rFont
, const OUString
& rText
)
62 maPortions
.emplace_back( rParagraph
, rFont
, rText
);
65 const TextFontModel
* TextBox::getFirstFont() const
67 return maPortions
.empty() ? nullptr : &maPortions
.front().maFont
;
70 OUString
TextBox::getText() const
72 OUStringBuffer aBuffer
;
73 for (auto const& portion
: maPortions
)
74 aBuffer
.append( portion
.maText
);
75 return aBuffer
.makeStringAndClear();
78 void TextBox::convert(const uno::Reference
<drawing::XShape
>& xShape
) const
80 uno::Reference
<text::XTextAppend
> xTextAppend(xShape
, uno::UNO_QUERY
);
82 bool bAmbiguousStyle
= true;
84 for (auto const& portion
: maPortions
)
86 beans::PropertyValue aPropertyValue
;
87 std::vector
<beans::PropertyValue
> aPropVec
;
88 const TextParagraphModel
& rParagraph
= portion
.maParagraph
;
89 const TextFontModel
& rFont
= portion
.maFont
;
90 if (rFont
.moName
.has_value())
92 aPropertyValue
.Name
= "CharFontName";
93 aPropertyValue
.Value
<<= rFont
.moName
.value();
94 aPropVec
.push_back(aPropertyValue
);
96 aPropertyValue
.Name
= "CharFontNameAsian";
97 aPropertyValue
.Value
<<= rFont
.moNameAsian
.value_or("");
98 aPropVec
.push_back(aPropertyValue
);
100 aPropertyValue
.Name
= "CharFontNameComplex";
101 aPropertyValue
.Value
<<= rFont
.moNameComplex
.value_or("");
102 aPropVec
.push_back(aPropertyValue
);
104 if (rFont
.mobBold
.has_value())
106 aPropertyValue
.Name
= "CharWeight";
107 aPropertyValue
.Value
<<= rFont
.mobBold
.value() ? awt::FontWeight::BOLD
: awt::FontWeight::NORMAL
;
108 aPropVec
.push_back(aPropertyValue
);
110 if (rFont
.monSize
.has_value())
112 aPropertyValue
.Name
= "CharHeight";
113 aPropertyValue
.Value
<<= double(rFont
.monSize
.value()) / 2.;
114 aPropVec
.push_back(aPropertyValue
);
116 if (rFont
.monSpacing
.has_value())
118 aPropertyValue
.Name
= "CharKerning";
119 // Value is not converted to mm100: SvxKerningItem::PutValue() gets
120 // called with nMemberId = 0, so no mm100 -> twips conversion will
122 aPropertyValue
.Value
<<= sal_Int16(rFont
.monSpacing
.value());
123 aPropVec
.push_back(aPropertyValue
);
125 if (rParagraph
.moParaAdjust
.has_value())
127 style::ParagraphAdjust eAdjust
= style::ParagraphAdjust_LEFT
;
128 if (rParagraph
.moParaAdjust
.value() == "center")
129 eAdjust
= style::ParagraphAdjust_CENTER
;
130 else if (rParagraph
.moParaAdjust
.value() == "right")
131 eAdjust
= style::ParagraphAdjust_RIGHT
;
133 aPropertyValue
.Name
= "ParaAdjust";
134 aPropertyValue
.Value
<<= eAdjust
;
135 aPropVec
.push_back(aPropertyValue
);
138 // All paragraphs should be either undefined (default) or the same style,
139 // because it will only be applied to the entire shape, and not per-paragraph.
140 if (sParaStyle
.isEmpty() )
142 if ( rParagraph
.moParaStyleName
.has_value() )
143 sParaStyle
= rParagraph
.moParaStyleName
.value();
144 if ( bAmbiguousStyle
)
145 bAmbiguousStyle
= false; // both empty parastyle and ambiguous can only be true at the first paragraph
147 bAmbiguousStyle
= rParagraph
.moParaStyleName
.has_value(); // ambiguous if both default and specified style used.
149 else if ( !bAmbiguousStyle
)
151 if ( !rParagraph
.moParaStyleName
.has_value() )
152 bAmbiguousStyle
= true; // ambiguous if both specified and default style used.
153 else if ( rParagraph
.moParaStyleName
.value() != sParaStyle
)
154 bAmbiguousStyle
= true; // ambiguous if two different styles specified.
156 if (rFont
.moColor
.has_value())
158 aPropertyValue
.Name
= "CharColor";
159 aPropertyValue
.Value
<<= rFont
.moColor
.value().toUInt32(16);
160 aPropVec
.push_back(aPropertyValue
);
162 xTextAppend
->appendTextPortion(portion
.maText
, comphelper::containerToSequence(aPropVec
));
167 // Track the style in a grabBag for use later when style details are known.
168 comphelper::SequenceAsHashMap aGrabBag
;
169 uno::Reference
<beans::XPropertySet
> xPropertySet(xShape
, uno::UNO_QUERY_THROW
);
170 aGrabBag
.update( xPropertySet
->getPropertyValue(u
"CharInteropGrabBag"_ustr
) );
171 aGrabBag
[u
"mso-pStyle"_ustr
] <<= sParaStyle
;
172 xPropertySet
->setPropertyValue(u
"CharInteropGrabBag"_ustr
, uno::Any(aGrabBag
.getAsConstPropertyValueList()));
174 catch (const uno::Exception
&)
176 TOOLS_WARN_EXCEPTION( "oox.vml","convert() grabbag exception" );
179 // Remove the last character of the shape text, if it would be a newline.
180 uno::Reference
< text::XTextCursor
> xCursor
= xTextAppend
->createTextCursor();
181 xCursor
->gotoEnd(false);
182 xCursor
->goLeft(1, true);
183 if (xCursor
->getString() == "\n")
184 xCursor
->setString(u
""_ustr
);
186 if ( maLayoutFlow
!= "vertical" )
189 uno::Reference
<beans::XPropertySet
> xProperties(xShape
, uno::UNO_QUERY
);
191 // VML has the text horizontally aligned to left (all the time),
192 // v-text-anchor for vertical alignment, and vertical mode to swap the
193 // two. drawinglayer supports both horizontal and vertical alignment,
194 // but no vertical mode: we use T->B, R->L instead.
195 // As a result, we need to set horizontal adjustment here to 'right',
196 // that will result in vertical 'top' after writing mode is applied,
197 // which matches the VML behavior.
198 xProperties
->setPropertyValue(u
"TextHorizontalAdjust"_ustr
, uno::Any(drawing::TextHorizontalAdjust_RIGHT
));
200 xProperties
->setPropertyValue( u
"TextWritingMode"_ustr
, uno::Any( text::WritingMode_TB_RL
) );
203 } // namespace oox::vml
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */