LibreOffice Module oox (master) 1
vmltextbox.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
21
22#include <rtl/ustrbuf.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>
33#include <utility>
34
35namespace oox::vml {
36
37using namespace com::sun::star;
38
40{
41}
42
44 maParagraph(std::move( aParagraph )),
45 maFont(std::move( aFont )),
46 maText(std::move( aText ))
47{
48}
49
51 : mrTypeModel(rTypeModel)
52 , borderDistanceSet( false )
53 , borderDistanceLeft(0)
54 , borderDistanceTop(0)
55 , borderDistanceRight(0)
56 , borderDistanceBottom(0)
57{
58}
59
60void TextBox::appendPortion( const TextParagraphModel& rParagraph, const TextFontModel& rFont, const OUString& rText )
61{
62 maPortions.emplace_back( rParagraph, rFont, rText );
63}
64
66{
67 return maPortions.empty() ? nullptr : &maPortions.front().maFont;
68}
69
70OUString TextBox::getText() const
71{
72 OUStringBuffer aBuffer;
73 for (auto const& portion : maPortions)
74 aBuffer.append( portion.maText );
75 return aBuffer.makeStringAndClear();
76}
77
78void TextBox::convert(const uno::Reference<drawing::XShape>& xShape) const
79{
80 uno::Reference<text::XTextAppend> xTextAppend(xShape, uno::UNO_QUERY);
81 OUString sParaStyle;
82 bool bAmbiguousStyle = true;
83
84 for (auto const& portion : maPortions)
85 {
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())
91 {
92 aPropertyValue.Name = "CharFontName";
93 aPropertyValue.Value <<= rFont.moName.value();
94 aPropVec.push_back(aPropertyValue);
95
96 aPropertyValue.Name = "CharFontNameAsian";
97 aPropertyValue.Value <<= rFont.moNameAsian.value_or("");
98 aPropVec.push_back(aPropertyValue);
99
100 aPropertyValue.Name = "CharFontNameComplex";
101 aPropertyValue.Value <<= rFont.moNameComplex.value_or("");
102 aPropVec.push_back(aPropertyValue);
103 }
104 if (rFont.mobBold.has_value())
105 {
106 aPropertyValue.Name = "CharWeight";
107 aPropertyValue.Value <<= rFont.mobBold.value() ? awt::FontWeight::BOLD : awt::FontWeight::NORMAL;
108 aPropVec.push_back(aPropertyValue);
109 }
110 if (rFont.monSize.has_value())
111 {
112 aPropertyValue.Name = "CharHeight";
113 aPropertyValue.Value <<= double(rFont.monSize.value()) / 2.;
114 aPropVec.push_back(aPropertyValue);
115 }
116 if (rFont.monSpacing.has_value())
117 {
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
121 // be done there.
122 aPropertyValue.Value <<= sal_Int16(rFont.monSpacing.value());
123 aPropVec.push_back(aPropertyValue);
124 }
125 if (rParagraph.moParaAdjust.has_value())
126 {
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;
132
133 aPropertyValue.Name = "ParaAdjust";
134 aPropertyValue.Value <<= eAdjust;
135 aPropVec.push_back(aPropertyValue);
136 }
137
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() )
141 {
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
146 else
147 bAmbiguousStyle = rParagraph.moParaStyleName.has_value(); // ambiguous if both default and specified style used.
148 }
149 else if ( !bAmbiguousStyle )
150 {
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.
155 }
156 if (rFont.moColor.has_value())
157 {
158 aPropertyValue.Name = "CharColor";
159 aPropertyValue.Value <<= rFont.moColor.value().toUInt32(16);
160 aPropVec.push_back(aPropertyValue);
161 }
162 xTextAppend->appendTextPortion(portion.maText, comphelper::containerToSequence(aPropVec));
163 }
164
165 try
166 {
167 // Track the style in a grabBag for use later when style details are known.
169 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY_THROW);
170 aGrabBag.update( xPropertySet->getPropertyValue("CharInteropGrabBag") );
171 aGrabBag["mso-pStyle"] <<= sParaStyle;
172 xPropertySet->setPropertyValue("CharInteropGrabBag", uno::Any(aGrabBag.getAsConstPropertyValueList()));
173 }
174 catch (const uno::Exception&)
175 {
176 TOOLS_WARN_EXCEPTION( "oox.vml","convert() grabbag exception" );
177 }
178
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("");
185
186 if ( maLayoutFlow != "vertical" )
187 return;
188
189 uno::Reference<beans::XPropertySet> xProperties(xShape, uno::UNO_QUERY);
190
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("TextHorizontalAdjust", uno::Any(drawing::TextHorizontalAdjust_RIGHT));
199
200 xProperties->setPropertyValue( "TextWritingMode", uno::Any( text::WritingMode_TB_RL ) );
201}
202
203} // namespace oox::vml
204
205/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Text maText
css::uno::Sequence< css::beans::PropertyValue > getAsConstPropertyValueList() const
void update(const SequenceAsHashMap &rSource)
void convert(const css::uno::Reference< css::drawing::XShape > &xShape) const
Definition: vmltextbox.cxx:78
OUString maLayoutFlow
Definition: vmltextbox.hxx:99
PortionVector maPortions
Definition: vmltextbox.hxx:105
void appendPortion(const TextParagraphModel &rParagraph, const TextFontModel &rFont, const OUString &rText)
Appends a new text portion to the textbox.
Definition: vmltextbox.cxx:60
const TextFontModel * getFirstFont() const
Returns the font settings of the first text portion.
Definition: vmltextbox.cxx:65
OUString getText() const
Returns the entire text of all text portions.
Definition: vmltextbox.cxx:70
TextBox(ShapeTypeModel &rTypeModel)
Definition: vmltextbox.cxx:50
#define TOOLS_WARN_EXCEPTION(area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
The shape model structure contains all properties shared by all types of shapes.
Definition: vmlshape.hxx:61
Font settings for a text portion in a textbox.
Definition: vmltextbox.hxx:50
std::optional< sal_Int32 > monSize
Font size in twips.
Definition: vmltextbox.hxx:55
std::optional< OUString > moNameAsian
Asian font name.
Definition: vmltextbox.hxx:52
std::optional< OUString > moNameComplex
Complex font name.
Definition: vmltextbox.hxx:53
std::optional< bool > mobBold
Definition: vmltextbox.hxx:58
std::optional< OUString > moName
Font name.
Definition: vmltextbox.hxx:51
std::optional< sal_Int32 > monSpacing
Definition: vmltextbox.hxx:61
std::optional< OUString > moColor
Font color, HTML encoded, sort of.
Definition: vmltextbox.hxx:54
A text paragraph in a textbox.
Definition: vmltextbox.hxx:43
std::optional< OUString > moParaStyleName
Definition: vmltextbox.hxx:45
std::optional< OUString > moParaAdjust
Paragraph adjust (left, center, right, etc.)
Definition: vmltextbox.hxx:44
TextPortionModel(TextParagraphModel aParagraph, TextFontModel aFont, OUString aText)
Definition: vmltextbox.cxx:43
std::unique_ptr< char[]> aBuffer