Debian Codegraph @

src/lib/WPSParagraph.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
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 * Major Contributor(s):
10 * Copyright (C) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25 #include <iomanip>
26 #include <sstream>
27
28 #include <librevenge/librevenge.h>
29
30 #include "libwps_internal.h"
31
32 #include "WPSContentListener.h"
33 #include "WPSList.h"
34
35 #include "WPSParagraph.h"
36
37 void WPSTabStop::addTo(librevenge::RVNGPropertyListVector &propList, double decalX) const
38 {
39 librevenge::RVNGPropertyList tab;
40
41 // type
42 switch (m_alignment)
43 {
44 case RIGHT:
45 tab.insert("style:type", "right");
46 break;
47 case CENTER:
48 tab.insert("style:type", "center");
49 break;
50 case DECIMAL:
51 tab.insert("style:type", "char");
52 tab.insert("style:char", "."); // Assume a decimal point for now
53 break;
54 case BAR: // BAR is not handled in OOo
55 case LEFT:
56 default:
57 break;
58 }
59
60 // leader character
61 if (m_leaderCharacter != 0x0000)
62 {
63 librevenge::RVNGString sLeader;
64 sLeader.sprintf("%c", m_leaderCharacter);
65 tab.insert("style:leader-text", sLeader);
66 tab.insert("style:leader-style", "solid");
67 }
68
69 // position
70 double position = m_position+decalX;
71 if (position < 0.00005 && position > -0.00005)
72 position = 0.0;
73 tab.insert("style:position", position);
74
75 propList.append(tab);
76 }
77
78 //! operator<<
79 std::ostream &operator<<(std::ostream &o, WPSTabStop const &tab)
80 {
81 o << tab.m_position;
82
83 switch (tab.m_alignment)
84 {
85 case WPSTabStop::LEFT:
86 o << "L";
87 break;
88 case WPSTabStop::CENTER:
89 o << "C";
90 break;
91 case WPSTabStop::RIGHT:
92 o << "R";
93 break;
94 case WPSTabStop::DECIMAL:
95 o << ":decimal";
96 break;
97 case WPSTabStop::BAR:
98 o << ":bar";
99 break;
100 default:
101 o << ":#type=" << int(tab.m_alignment);
102 break;
103 }
104 if (tab.m_leaderCharacter != '\0')
105 o << ":sep='"<< char(tab.m_leaderCharacter) << "'";
106 return o;
107 }
108
109 //! operator<<
110 std::ostream &operator<<(std::ostream &o, WPSParagraph const &pp)
111 {
112 if (pp.m_margins[0]<0||pp.m_margins[0]>0)
113 o << "textIndent=" << pp.m_margins[0] << ",";
114 if (pp.m_margins[1]<0||pp.m_margins[1]>0)
115 o << "leftMarg=" << pp.m_margins[1] << ",";
116 if (pp.m_margins[2]<0||pp.m_margins[2]>0)
117 o << "rightMarg=" << pp.m_margins[2] << ",";
118
119 if (pp.m_spacings[0] < 1.0 || pp.m_spacings[0] > 1.0)
120 {
121 o << "interLineSpacing=" << pp.m_spacings[0];
122 if (pp.m_spacingsInterlineUnit==librevenge::RVNG_PERCENT)
123 o << "%";
124 else if (pp.m_spacingsInterlineUnit==librevenge::RVNG_POINT)
125 o << "pt";
126 else if (pp.m_spacingsInterlineUnit==librevenge::RVNG_INCH)
127 o << "in";
128 if (pp.m_spacingsInterlineType==WPSParagraph::AtLeast)
129 o << "[atLeast]";
130 o << ",";
131 }
132 if (pp.m_spacings[1]<0||pp.m_spacings[1]>0)
133 o << "befSpacing=" << pp.m_spacings[1] << ",";
134 if (pp.m_spacings[2]<0||pp.m_spacings[2]>0)
135 o << "aftSpacing=" << pp.m_spacings[2] << ",";
136
137 if (pp.m_breakStatus & libwps::NoBreakBit) o << "dontbreak,";
138 if (pp.m_breakStatus & libwps::NoBreakWithNextBit) o << "dontbreakafter,";
139
140 switch (pp.m_justify)
141 {
142 case libwps::JustificationLeft:
143 break;
144 case libwps::JustificationCenter:
145 o << "just=centered, ";
146 break;
147 case libwps::JustificationRight:
148 o << "just=right, ";
149 break;
150 case libwps::JustificationFull:
151 o << "just=full, ";
152 break;
153 case libwps::JustificationFullAllLines:
154 o << "just=fullAllLines, ";
155 break;
156 default:
157 WPS_DEBUG_MSG(("WPSParagraph:operator<<: called with unknown justification\n"));
158 o << "just=" << static_cast<unsigned>(pp.m_justify) << ", ";
159 break;
160 }
161
162 if (pp.m_tabs.size())
163 {
164 o << "tabs=(";
165 for (auto const &tab : pp.m_tabs)
166 o << tab << ",";
167 o << "),";
168 }
169 if (!pp.m_backgroundColor.isWhite())
170 o << "backgroundColor=" << pp.m_backgroundColor << ",";
171 if (pp.m_listLevelIndex >= 1)
172 o << pp.m_listLevel << ":" << pp.m_listLevelIndex <<",";
173
174 if (pp.m_border)
175 {
176 o << "bord(" << pp.m_borderStyle << ")";
177 o << "=";
178 if (pp.m_border&WPSBorder::TopBit) o << "T";
179 if (pp.m_border&WPSBorder::BottomBit) o << "B";
180 if (pp.m_border&WPSBorder::LeftBit) o << "L";
181 if (pp.m_border&WPSBorder::RightBit) o << "R";
182 o << ",";
183 }
184
185 if (!pp.m_extra.empty()) o << "extras=(" << pp.m_extra << ")";
186 return o;
187 }
188
189 WPSParagraph::~WPSParagraph()
190 {
191 }
192
193 void WPSParagraph::addTo(librevenge::RVNGPropertyList &propList, bool inTable) const
194 {
195 switch (m_justify)
196 {
197 case libwps::JustificationLeft:
198 // doesn't require a paragraph prop - it is the default
199 propList.insert("fo:text-align", "left");
200 break;
201 case libwps::JustificationCenter:
202 propList.insert("fo:text-align", "center");
203 break;
204 case libwps::JustificationRight:
205 propList.insert("fo:text-align", "end");
206 break;
207 case libwps::JustificationFull:
208 propList.insert("fo:text-align", "justify");
209 break;
210 case libwps::JustificationFullAllLines:
211 propList.insert("fo:text-align", "justify");
212 propList.insert("fo:text-align-last", "justify");
213 break;
214 default:
215 break;
216 }
217 if (!inTable)
218 {
219 // these properties are not appropriate when a table is opened..
220 propList.insert("fo:margin-left", m_listLevelIndex >= 1 ? m_listLevel.m_labelIndent : m_margins[1]);
221 propList.insert("fo:text-indent", m_margins[0]);
222 propList.insert("fo:margin-right", m_margins[2]);
223 if (!m_backgroundColor.isWhite())
224 propList.insert("fo:background-color", m_backgroundColor.str().c_str());
225 if (m_border && m_borderStyle.m_style != WPSBorder::None)
226 {
227 int border = m_border;
228 if (border == 0xF)
229 m_borderStyle.addTo(propList);
230 else
231 {
232 if (border & WPSBorder::LeftBit)
233 m_borderStyle.addTo(propList, "left");
234 if (border & WPSBorder::RightBit)
235 m_borderStyle.addTo(propList, "right");
236 if (border & WPSBorder::TopBit)
237 m_borderStyle.addTo(propList, "top");
238 if (border & WPSBorder::BottomBit)
239 m_borderStyle.addTo(propList, "bottom");
240 }
241 }
242 }
243 // Note:
244 // as we can not use percent, this may give a good approximation
245 propList.insert("fo:margin-top", (10.*m_spacings[1])/72., librevenge::RVNG_INCH);
246 propList.insert("fo:margin-bottom", (10.*m_spacings[2])/72., librevenge::RVNG_INCH);
247 switch (m_spacingsInterlineType)
248 {
249 case Fixed:
250 if (m_spacings[0] >= 0)
251 propList.insert("fo:line-height", m_spacings[0], m_spacingsInterlineUnit);
252 break;
253 case AtLeast:
254 if (m_spacings[0] <= 0 && m_spacings[0] >= 0)
255 break;
256 if (m_spacings[0] < 0)
257 {
258 static bool first = true;
259 if (first)
260 {
261 WPS_DEBUG_MSG(("WPSParagraph::addTo: interline spacing seems bad\n"));
262 first = false;
263 }
264 }
265 else if (m_spacingsInterlineUnit != librevenge::RVNG_PERCENT)
266 propList.insert("style:line-height-at-least", m_spacings[0], m_spacingsInterlineUnit);
267 else
268 {
269 propList.insert("style:line-height-at-least", m_spacings[0]*12.0, librevenge::RVNG_POINT);
270 static bool first = true;
271 if (first)
272 {
273 first = false;
274 WPS_DEBUG_MSG(("WPSParagraph::addTo: assume height=12 to set line spacing at least with percent type\n"));
275 }
276 }
277 break;
278 default:
279 WPS_DEBUG_MSG(("WPSParagraph::addTo: can not set line spacing type: %d\n",int(m_spacingsInterlineType)));
280 break;
281 }
282 if (m_breakStatus & libwps::NoBreakBit)
283 propList.insert("fo:keep-together", "always");
284 if (m_breakStatus & libwps::NoBreakWithNextBit)
285 propList.insert("fo:keep-with-next", "always");
286 librevenge::RVNGPropertyListVector tabStops;
287 for (auto const &tab : m_tabs)
288 tab.addTo(tabStops, 0);
289 if (tabStops.count())
290 propList.insert("style:tab-stops", tabStops);
291 }
292 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */