LibreOffice Module sw (master) 1
xmlithlp.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 <limits.h>
21
22#include "xmlithlp.hxx"
25#include <editeng/brushitem.hxx>
26
27#include <xmloff/xmluconv.hxx>
28#include <osl/diagnose.h>
29#include <o3tl/safeint.hxx>
30
31#include <com/sun/star/table/BorderLineStyle.hpp>
32#include <com/sun/star/text/HoriOrientation.hpp>
33#include <com/sun/star/text/VertOrientation.hpp>
34
35using ::editeng::SvxBorderLine;
36using namespace ::xmloff::token;
37using namespace ::com::sun::star;
38
39#define SVX_XML_BORDER_WIDTH_THIN 0
40#define SVX_XML_BORDER_WIDTH_MIDDLE 1
41#define SVX_XML_BORDER_WIDTH_THICK 2
42
43const struct SvXMLEnumMapEntry<sal_uInt16> psXML_BorderStyles[] =
44{
47 { XML_SOLID, table::BorderLineStyle::SOLID },
48 { XML_DOUBLE, table::BorderLineStyle::DOUBLE },
49 { XML_DOUBLE_THIN, table::BorderLineStyle::DOUBLE_THIN },
50 { XML_DOTTED, table::BorderLineStyle::DOTTED },
51 { XML_DASHED, table::BorderLineStyle::DASHED },
52 { XML_FINE_DASHED, table::BorderLineStyle::FINE_DASHED },
53 { XML_DASH_DOT, table::BorderLineStyle::DASH_DOT },
54 { XML_DASH_DOT_DOT, table::BorderLineStyle::DASH_DOT_DOT },
55 { XML_GROOVE, table::BorderLineStyle::ENGRAVED },
56 { XML_RIDGE, table::BorderLineStyle::EMBOSSED },
57 { XML_INSET, table::BorderLineStyle::INSET },
58 { XML_OUTSET, table::BorderLineStyle::OUTSET },
60};
61
62const struct SvXMLEnumMapEntry<sal_uInt16> psXML_NamedBorderWidths[] =
63{
68};
69// mapping tables to map external xml input to internal box line widths
70
71const sal_uInt16 aBorderWidths[] = {
75};
76
77bool sw_frmitems_parseXMLBorder( std::u16string_view rValue,
78 const SvXMLUnitConverter& rUnitConverter,
79 bool& rHasStyle, sal_uInt16& rStyle,
80 bool& rHasWidth, sal_uInt16& rWidth,
81 sal_uInt16& rNamedWidth,
82 bool& rHasColor, Color& rColor )
83{
84 std::u16string_view aToken;
85 SvXMLTokenEnumerator aTokens( rValue );
86
87 rHasStyle = false;
88 rHasWidth = false;
89 rHasColor = false;
90
91 rStyle = USHRT_MAX;
92 rWidth = 0;
93 rNamedWidth = USHRT_MAX;
94
95 sal_Int32 nTemp;
96 while( aTokens.getNextToken( aToken ) && !aToken.empty() )
97 {
98 if( !rHasWidth &&
99 SvXMLUnitConverter::convertEnum( rNamedWidth, aToken,
101 {
102 rHasWidth = true;
103 }
104 else if( !rHasStyle &&
105 SvXMLUnitConverter::convertEnum( rStyle, aToken,
107 {
108 rHasStyle = true;
109 }
110 else if (!rHasColor && ::sax::Converter::convertColor(rColor, aToken))
111 {
112 rHasColor = true;
113 }
114 else if( !rHasWidth &&
115 rUnitConverter.convertMeasureToCore(nTemp, aToken, 0, USHRT_MAX))
116 {
117 rWidth = o3tl::narrowing<sal_uInt16>(nTemp);
118 rHasWidth = true;
119 }
120 else
121 {
122 // misformed
123 return false;
124 }
125 }
126
127 return rHasStyle || rHasWidth || rHasColor;
128}
129
130static void sw_frmitems_setXMLBorderStyle( SvxBorderLine& rLine, sal_uInt16 nStyle )
131{
132 SvxBorderLineStyle eStyle = SvxBorderLineStyle::NONE;
133 if ( nStyle != table::BorderLineStyle::NONE )
134 eStyle = SvxBorderLineStyle( nStyle );
135 rLine.SetBorderLineStyle(eStyle);
136}
137
138bool sw_frmitems_setXMLBorder( std::unique_ptr<SvxBorderLine>& rpLine,
139 bool bHasStyle, sal_uInt16 nStyle,
140 bool bHasWidth, sal_uInt16 nWidth,
141 sal_uInt16 nNamedWidth,
142 bool bHasColor, const Color& rColor )
143{
144 // first of all, delete an empty line
145 if( (bHasStyle && table::BorderLineStyle::NONE == nStyle) ||
146 (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
147 {
148 bool bRet = nullptr != rpLine;
149 rpLine.reset();
150 return bRet;
151 }
152
153 // if there is no line and no style and no with, there will never be a line
154 if( !rpLine && !(bHasStyle && bHasWidth) )
155 return false;
156
157 // We now do know that there will be a line
158 if( !rpLine )
159 rpLine.reset(new SvxBorderLine);
160
161 if( ( bHasWidth &&
162 (USHRT_MAX != nNamedWidth || (nWidth != rpLine->GetWidth() ) ) ) ||
163 ( bHasStyle &&
164 ((table::BorderLineStyle::SOLID == nStyle && rpLine->GetDistance()) ||
165 (table::BorderLineStyle::DOUBLE == nStyle && !rpLine->GetDistance())) ) )
166 {
167 bool bDouble = (bHasWidth && table::BorderLineStyle::DOUBLE == nStyle ) ||
168 rpLine->GetDistance();
169
170 // fdo#38542: for double borders, do not override the width
171 // set via style:border-line-width{,-left,-right,-top,-bottom}
172 if (!bDouble || !rpLine->GetWidth())
173 {
174 // The width has to be changed
175 if (bHasWidth && USHRT_MAX != nNamedWidth)
176 {
177 if (bDouble)
178 {
179 rpLine->SetBorderLineStyle( SvxBorderLineStyle::DOUBLE );
180 }
181 rpLine->SetWidth( aBorderWidths[nNamedWidth] );
182 }
183 else
184 {
185 if (!bHasWidth)
186 nWidth = rpLine->GetScaledWidth();
187
188 rpLine->SetWidth( nWidth );
189 }
190 }
191 sw_frmitems_setXMLBorderStyle( *rpLine, nStyle );
192 }
193
194 // set color
195 if( bHasColor )
196 rpLine->SetColor( rColor );
197
198 return true;
199}
200
201void sw_frmitems_setXMLBorder( std::unique_ptr<SvxBorderLine>& rpLine,
202 sal_uInt16 nWidth, sal_uInt16 nOutWidth,
203 sal_uInt16 nInWidth, sal_uInt16 nDistance )
204{
205 if( !rpLine )
206 rpLine.reset(new SvxBorderLine);
207
208 if( nWidth > 0 )
209 rpLine->SetWidth( nWidth );
210 else
211 rpLine->GuessLinesWidths(SvxBorderLineStyle::DOUBLE,
212 nOutWidth, nInWidth, nDistance);
213}
214
216{
221};
222
224{
225 { XML_LEFT, GPOS_LM },
226 { XML_RIGHT, GPOS_RM },
228};
229
231{
232 { XML_TOP, GPOS_MT },
233 { XML_BOTTOM, GPOS_MB },
235};
236
238 SvxGraphicPosition eHori )
239{
240 OSL_ENSURE( GPOS_LM==eHori || GPOS_MM==eHori || GPOS_RM==eHori,
241 "sw_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
242
243 switch( ePos )
244 {
245 case GPOS_LT:
246 case GPOS_MT:
247 case GPOS_RT:
248 ePos = GPOS_LM==eHori ? GPOS_LT : (GPOS_MM==eHori ? GPOS_MT : GPOS_RT);
249 break;
250
251 case GPOS_LM:
252 case GPOS_MM:
253 case GPOS_RM:
254 ePos = eHori;
255 break;
256
257 case GPOS_LB:
258 case GPOS_MB:
259 case GPOS_RB:
260 ePos = GPOS_LM==eHori ? GPOS_LB : (GPOS_MM==eHori ? GPOS_MB : GPOS_RB);
261 break;
262 default:
263 ;
264 }
265}
266
268 SvxGraphicPosition eVert )
269{
270 OSL_ENSURE( GPOS_MT==eVert || GPOS_MM==eVert || GPOS_MB==eVert,
271 "sw_frmitems_MergeXMLVertPos: horizontal pos must be middle" );
272
273 switch( ePos )
274 {
275 case GPOS_LT:
276 case GPOS_LM:
277 case GPOS_LB:
278 ePos = GPOS_MT==eVert ? GPOS_LT : (GPOS_MM==eVert ? GPOS_LM : GPOS_LB);
279 break;
280
281 case GPOS_MT:
282 case GPOS_MM:
283 case GPOS_MB:
284 ePos = eVert;
285 break;
286
287 case GPOS_RT:
288 case GPOS_RM:
289 case GPOS_RB:
290 ePos = GPOS_MT==eVert ? GPOS_RT : (GPOS_MM==eVert ? GPOS_RM : GPOS_RB);
291 break;
292 default:
293 ;
294 }
295}
296
297const struct SvXMLEnumMapEntry<sal_uInt16> psXML_BreakType[] =
298{
299 { XML_AUTO, 0 },
300 { XML_COLUMN, 1 },
301 { XML_PAGE, 2 },
302 { XML_EVEN_PAGE, 2 },
303 { XML_ODD_PAGE, 2 },
305};
306
307const struct SvXMLEnumMapEntry<sal_Int16> aXMLTableAlignMap[] =
308{
309 { XML_LEFT, text::HoriOrientation::LEFT },
310 { XML_LEFT, text::HoriOrientation::LEFT_AND_WIDTH },
311 { XML_CENTER, text::HoriOrientation::CENTER },
312 { XML_RIGHT, text::HoriOrientation::RIGHT },
313 { XML_MARGINS, text::HoriOrientation::FULL },
315 { XML_TOKEN_INVALID, 0 }
316};
317
318const struct SvXMLEnumMapEntry<sal_Int16> aXMLTableVAlignMap[] =
319{
320 { XML_TOP, text::VertOrientation::TOP },
321 { XML_MIDDLE, text::VertOrientation::CENTER },
322 { XML_BOTTOM, text::VertOrientation::BOTTOM },
323 { XML_TOKEN_INVALID, 0 }
324};
325
326const struct SvXMLEnumMapEntry<sal_uInt16> aXML_KeepTogetherType[] =
327{
328 { XML_ALWAYS, 0 },
329 { XML_AUTO, 1 },
331};
332
333/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::chart::ChartAxisLabelPosition ePos
SvxBorderLineStyle
SvxGraphicPosition
GPOS_MT
GPOS_LT
GPOS_RT
GPOS_MM
GPOS_TILED
GPOS_AREA
GPOS_MB
GPOS_LB
GPOS_LM
GPOS_RM
GPOS_RB
bool getNextToken(std::u16string_view &rToken)
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
bool convertMeasureToCore(sal_Int32 &rValue, std::u16string_view rString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32) const
static const sal_Int16 VeryThin
static const sal_Int16 Thin
static const sal_Int16 Hairline
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
XML_ALWAYS
XML_SOLID
XML_RIDGE
XML_REPEAT
XML_AUTO
XML_PAGE
XML_COLUMN
XML_STRETCH
XML_TOKEN_INVALID
XML_NONE
XML_OUTSET
XML_DASH_DOT
XML_DASHED
XML_DASH_DOT_DOT
XML_HIDDEN
XML_GROOVE
XML_MARGINS
XML_TOP
XML_DOTTED
XML_MIDDLE
XML_DOUBLE_THIN
XML_THICK
XML_FINE_DASHED
XML_LEFT
XML_CENTER
XML_THIN
XML_INSET
XML_BACKGROUND_NO_REPEAT
XML_DOUBLE
XML_RIGHT
XML_ODD_PAGE
XML_BOTTOM
XML_EVEN_PAGE
const struct SvXMLEnumMapEntry< sal_Int16 > aXMLTableVAlignMap[]
Definition: xmlithlp.cxx:318
#define SVX_XML_BORDER_WIDTH_MIDDLE
Definition: xmlithlp.cxx:40
const struct SvXMLEnumMapEntry< sal_Int16 > aXMLTableAlignMap[]
Definition: xmlithlp.cxx:307
const struct SvXMLEnumMapEntry< SvxGraphicPosition > psXML_BrushHoriPos[]
Definition: xmlithlp.cxx:223
void sw_frmitems_MergeXMLVertPos(SvxGraphicPosition &ePos, SvxGraphicPosition eVert)
Definition: xmlithlp.cxx:267
const struct SvXMLEnumMapEntry< SvxGraphicPosition > psXML_BrushVertPos[]
Definition: xmlithlp.cxx:230
#define SVX_XML_BORDER_WIDTH_THIN
Definition: xmlithlp.cxx:39
const sal_uInt16 aBorderWidths[]
Definition: xmlithlp.cxx:71
const struct SvXMLEnumMapEntry< sal_uInt16 > psXML_BorderStyles[]
Definition: xmlithlp.cxx:43
const struct SvXMLEnumMapEntry< sal_uInt16 > aXML_KeepTogetherType[]
Definition: xmlithlp.cxx:326
static void sw_frmitems_setXMLBorderStyle(SvxBorderLine &rLine, sal_uInt16 nStyle)
Definition: xmlithlp.cxx:130
bool sw_frmitems_parseXMLBorder(std::u16string_view rValue, const SvXMLUnitConverter &rUnitConverter, bool &rHasStyle, sal_uInt16 &rStyle, bool &rHasWidth, sal_uInt16 &rWidth, sal_uInt16 &rNamedWidth, bool &rHasColor, Color &rColor)
Define various helper variables and functions for xmlimpit.cxx and xmlexpit.cxx.
Definition: xmlithlp.cxx:77
const struct SvXMLEnumMapEntry< sal_uInt16 > psXML_BreakType[]
Definition: xmlithlp.cxx:297
bool sw_frmitems_setXMLBorder(std::unique_ptr< SvxBorderLine > &rpLine, bool bHasStyle, sal_uInt16 nStyle, bool bHasWidth, sal_uInt16 nWidth, sal_uInt16 nNamedWidth, bool bHasColor, const Color &rColor)
Definition: xmlithlp.cxx:138
const struct SvXMLEnumMapEntry< SvxGraphicPosition > psXML_BrushRepeat[]
Definition: xmlithlp.cxx:215
#define SVX_XML_BORDER_WIDTH_THICK
Definition: xmlithlp.cxx:41
void sw_frmitems_MergeXMLHoriPos(SvxGraphicPosition &ePos, SvxGraphicPosition eHori)
Definition: xmlithlp.cxx:237
const struct SvXMLEnumMapEntry< sal_uInt16 > psXML_NamedBorderWidths[]
Definition: xmlithlp.cxx:62