Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* TextRunStyle: Stores (and writes) paragraph/span-style-based information
3 : * (e.g.: a paragraph might be bold) that is needed at the head of an OO
4 : * document.
5 : *
6 : * This Source Code Form is subject to the terms of the Mozilla Public
7 : * License, v. 2.0. If a copy of the MPL was not distributed with this
8 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 : *
10 : * For further information visit http://libwpd.sourceforge.net
11 : */
12 :
13 : /* "This product is not manufactured, approved, or supported by
14 : * Corel Corporation or Corel Corporation Limited."
15 : */
16 : #include "FilterInternal.hxx"
17 : #include "TextRunStyle.hxx"
18 : #include "DocumentElement.hxx"
19 :
20 : #ifdef _MSC_VER
21 : #include <minmax.h>
22 : #endif
23 :
24 : #include <string.h>
25 :
26 0 : ParagraphStyle::ParagraphStyle(const WPXPropertyList &pPropList, const WPXPropertyListVector &xTabStops, const WPXString &sName) :
27 : mpPropList(pPropList),
28 : mxTabStops(xTabStops),
29 0 : msName(sName)
30 : {
31 0 : }
32 :
33 0 : ParagraphStyle::~ParagraphStyle()
34 : {
35 0 : }
36 :
37 0 : void ParagraphStyle::write(OdfDocumentHandler *pHandler) const
38 : {
39 : WRITER_DEBUG_MSG(("Writing a paragraph style..\n"));
40 :
41 0 : WPXPropertyList propList;
42 0 : propList.insert("style:name", msName.cstr());
43 0 : propList.insert("style:family", "paragraph");
44 0 : if (mpPropList["style:parent-style-name"])
45 0 : propList.insert("style:parent-style-name", mpPropList["style:parent-style-name"]->getStr());
46 0 : if (mpPropList["style:master-page-name"])
47 0 : propList.insert("style:master-page-name", mpPropList["style:master-page-name"]->getStr());
48 0 : pHandler->startElement("style:style", propList);
49 :
50 0 : propList.clear();
51 0 : WPXPropertyList::Iter i(mpPropList);
52 0 : for (i.rewind(); i.next(); )
53 : {
54 0 : if (strncmp(i.key(), "fo:margin-",10) == 0)
55 : {
56 0 : if (strcmp(i.key(), "fo:margin-left") == 0 ||
57 0 : strcmp(i.key(), "fo:margin-right") == 0 ||
58 0 : strcmp(i.key(), "fo:margin-top") == 0)
59 0 : propList.insert(i.key(), i()->getStr());
60 0 : else if (strcmp(i.key(), "fo:margin-bottom") == 0)
61 : {
62 0 : if (i()->getDouble() > 0.0)
63 0 : propList.insert("fo:margin-bottom", i()->getStr());
64 : else
65 0 : propList.insert("fo:margin-bottom", 0.0);
66 : }
67 : }
68 0 : else if (strcmp(i.key(), "fo:text-indent") == 0)
69 0 : propList.insert("fo:text-indent", i()->getStr());
70 0 : else if (strcmp(i.key(), "fo:line-height") == 0)
71 0 : propList.insert("fo:line-height", i()->getStr());
72 0 : else if (strcmp(i.key(), "fo:break-before") == 0)
73 0 : propList.insert("fo:break-before", i()->getStr());
74 0 : else if (strcmp(i.key(), "fo:text-align") == 0)
75 0 : propList.insert("fo:text-align", i()->getStr());
76 0 : else if (strcmp(i.key(), "fo:text-align-last") == 0)
77 0 : propList.insert("fo:text-align-last", i()->getStr());
78 0 : else if (strcmp(i.key(), "style:page-number") == 0)
79 0 : propList.insert("style:page-number", i()->getStr());
80 0 : else if (strncmp(i.key(), "fo:border", 9) == 0)
81 : {
82 0 : if (strcmp(i.key(), "fo:border") == 0 ||
83 0 : strcmp(i.key(), "fo:border-left") == 0 ||
84 0 : strcmp(i.key(), "fo:border-right") == 0 ||
85 0 : strcmp(i.key(), "fo:border-top") == 0 ||
86 0 : strcmp(i.key(), "fo:border-bottom") == 0)
87 0 : propList.insert(i.key(), i()->getStr());
88 : }
89 : }
90 :
91 0 : propList.insert("style:justify-single-word", "false");
92 0 : pHandler->startElement("style:paragraph-properties", propList);
93 :
94 0 : if (mxTabStops.count() > 0)
95 : {
96 0 : TagOpenElement tabListOpen("style:tab-stops");
97 0 : tabListOpen.write(pHandler);
98 0 : WPXPropertyListVector::Iter k(mxTabStops);
99 0 : for (k.rewind(); k.next();)
100 : {
101 0 : if (k()["style:position"] && k()["style:position"]->getDouble() < 0.0)
102 0 : continue;
103 0 : TagOpenElement tabStopOpen("style:tab-stop");
104 :
105 0 : WPXPropertyList::Iter j(k());
106 0 : for (j.rewind(); j.next(); )
107 : {
108 0 : tabStopOpen.addAttribute(j.key(), j()->getStr().cstr());
109 : }
110 0 : tabStopOpen.write(pHandler);
111 0 : pHandler->endElement("style:tab-stop");
112 0 : }
113 0 : pHandler->endElement("style:tab-stops");
114 : }
115 :
116 0 : pHandler->endElement("style:paragraph-properties");
117 0 : pHandler->endElement("style:style");
118 0 : }
119 :
120 0 : SpanStyle::SpanStyle(const char *psName, const WPXPropertyList &xPropList) :
121 : Style(psName),
122 0 : mPropList(xPropList)
123 : {
124 0 : }
125 :
126 0 : void SpanStyle::write(OdfDocumentHandler *pHandler) const
127 : {
128 : WRITER_DEBUG_MSG(("Writing a span style..\n"));
129 0 : WPXPropertyList styleOpenList;
130 0 : styleOpenList.insert("style:name", getName());
131 0 : styleOpenList.insert("style:family", "text");
132 0 : pHandler->startElement("style:style", styleOpenList);
133 :
134 0 : WPXPropertyList propList(mPropList);
135 :
136 0 : if (mPropList["style:font-name"])
137 : {
138 0 : propList.insert("style:font-name-asian", mPropList["style:font-name"]->getStr());
139 0 : propList.insert("style:font-name-complex", mPropList["style:font-name"]->getStr());
140 : }
141 :
142 0 : if (mPropList["fo:font-size"])
143 : {
144 0 : if (mPropList["fo:font-size"]->getDouble() > 0.0)
145 : {
146 0 : propList.insert("style:font-size-asian", mPropList["fo:font-size"]->getStr());
147 0 : propList.insert("style:font-size-complex", mPropList["fo:font-size"]->getStr());
148 : }
149 : else
150 0 : propList.remove("fo:font-size");
151 : }
152 :
153 0 : if (mPropList["fo:font-weight"])
154 : {
155 0 : propList.insert("style:font-weight-asian", mPropList["fo:font-weight"]->getStr());
156 0 : propList.insert("style:font-weight-complex", mPropList["fo:font-weight"]->getStr());
157 : }
158 :
159 0 : if (mPropList["fo:font-style"])
160 : {
161 0 : propList.insert("style:font-style-asian", mPropList["fo:font-style"]->getStr());
162 0 : propList.insert("style:font-style-complex", mPropList["fo:font-style"]->getStr());
163 : }
164 :
165 0 : pHandler->startElement("style:text-properties", propList);
166 :
167 0 : pHandler->endElement("style:text-properties");
168 0 : pHandler->endElement("style:style");
169 0 : }
170 :
171 0 : void ParagraphStyleManager::clean()
172 : {
173 0 : mNameHash.clear();
174 0 : mStyleHash.clear();
175 0 : }
176 :
177 0 : void ParagraphStyleManager::write(OdfDocumentHandler *pHandler) const
178 : {
179 0 : for (std::map<WPXString, shared_ptr<ParagraphStyle>, ltstr>::const_iterator iter = mStyleHash.begin();
180 0 : iter != mStyleHash.end(); ++iter)
181 : {
182 0 : if (strcmp(iter->second->getName().cstr(), "Standard") == 0)
183 0 : continue;
184 0 : (iter->second)->write(pHandler);
185 : }
186 0 : }
187 :
188 0 : WPXString ParagraphStyleManager::getKey(const WPXPropertyList &xPropList, const WPXPropertyListVector &tabStops) const
189 : {
190 0 : WPXString sKey = propListToStyleKey(xPropList);
191 :
192 0 : WPXString sTabStops;
193 0 : sTabStops.sprintf("[num-tab-stops:%i]", tabStops.count());
194 0 : WPXPropertyListVector::Iter i(tabStops);
195 0 : for (i.rewind(); i.next();)
196 : {
197 0 : sTabStops.append(propListToStyleKey(i()));
198 : }
199 0 : sKey.append(sTabStops);
200 :
201 0 : return sKey;
202 : }
203 :
204 0 : WPXString ParagraphStyleManager::findOrAdd(const WPXPropertyList &propList, const WPXPropertyListVector &tabStops)
205 : {
206 0 : WPXString hashKey = getKey(propList, tabStops);
207 : std::map<WPXString, WPXString, ltstr>::const_iterator iter =
208 0 : mNameHash.find(hashKey);
209 0 : if (iter!=mNameHash.end()) return iter->second;
210 :
211 : // ok create a new list
212 : WRITER_DEBUG_MSG(("ParagraphStyleManager::findOrAdd: Paragraph Hash Key: %s\n", hashKey.cstr()));
213 :
214 0 : WPXString sName;
215 0 : sName.sprintf("S%i", mStyleHash.size());
216 0 : shared_ptr<ParagraphStyle> parag(new ParagraphStyle(propList, tabStops, sName));
217 0 : mStyleHash[sName] =parag;
218 0 : mNameHash[hashKey] = sName;
219 0 : return sName;
220 : }
221 :
222 0 : shared_ptr<ParagraphStyle> const ParagraphStyleManager::get(const WPXString &name) const
223 : {
224 : std::map<WPXString, shared_ptr<ParagraphStyle>, ltstr>::const_iterator iter
225 0 : = mStyleHash.find(name);
226 0 : if (iter == mStyleHash.end()) return shared_ptr<ParagraphStyle>();
227 0 : return iter->second;
228 : }
229 :
230 0 : void SpanStyleManager::clean()
231 : {
232 0 : mNameHash.clear();
233 0 : mStyleHash.clear();
234 0 : }
235 :
236 0 : void SpanStyleManager::write(OdfDocumentHandler *pHandler) const
237 : {
238 0 : for (std::map<WPXString, shared_ptr<SpanStyle>, ltstr>::const_iterator iter = mStyleHash.begin();
239 0 : iter != mStyleHash.end(); ++iter)
240 : {
241 0 : (iter->second)->write(pHandler);
242 : }
243 0 : }
244 :
245 0 : WPXString SpanStyleManager::findOrAdd(const WPXPropertyList &propList)
246 : {
247 0 : WPXString hashKey = propListToStyleKey(propList);
248 : std::map<WPXString, WPXString, ltstr>::const_iterator iter =
249 0 : mNameHash.find(hashKey);
250 0 : if (iter!=mNameHash.end()) return iter->second;
251 :
252 : // ok create a new list
253 : WRITER_DEBUG_MSG(("SpanStyleManager::findOrAdd: Span Hash Key: %s\n", hashKey.cstr()));
254 :
255 0 : WPXString sName;
256 0 : sName.sprintf("Span%i", mStyleHash.size());
257 0 : shared_ptr<SpanStyle> span(new SpanStyle(sName.cstr(), propList));
258 0 : mStyleHash[sName] = span;
259 0 : mNameHash[hashKey] = sName;
260 0 : return sName;
261 : }
262 :
263 0 : shared_ptr<SpanStyle> const SpanStyleManager::get(const WPXString &name) const
264 : {
265 : std::map<WPXString, shared_ptr<SpanStyle>, ltstr>::const_iterator iter
266 0 : = mStyleHash.find(name);
267 0 : if (iter == mStyleHash.end()) return shared_ptr<SpanStyle>();
268 0 : return iter->second;
269 : }
270 :
271 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|