Debian Codegraph @

src/lib/MWAWFont.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <map>
35 #include <sstream>
36
37 #include <librevenge/librevenge.h>
38
39 #include "libmwaw_internal.hxx"
40
41 #include "MWAWTextListener.hxx"
42 #include "MWAWFontConverter.hxx"
43 #include "MWAWPosition.hxx"
44
45 #include "MWAWFont.hxx"
46
47 // line function
48
49 std::ostream &operator<<(std::ostream &o, MWAWFont::Line const &line)
50 {
51 if (!line.isSet())
52 return o;
53 switch (line.m_style) {
54 case MWAWFont::Line::Dot:
55 o << "dotted";
56 break;
57 case MWAWFont::Line::LargeDot:
58 o << "dotted[large]";
59 break;
60 case MWAWFont::Line::Dash:
61 o << "dash";
62 break;
63 case MWAWFont::Line::Simple:
64 o << "solid";
65 break;
66 case MWAWFont::Line::Wave:
67 o << "wave";
68 break;
69 case MWAWFont::Line::None:
70 #if !defined(__clang__)
71 default:
72 #endif
73 break;
74 }
75 switch (line.m_type) {
76 case MWAWFont::Line::Double:
77 o << ":double";
78 break;
79 case MWAWFont::Line::Triple:
80 o << ":triple";
81 break;
82 case MWAWFont::Line::Single:
83 #if !defined(__clang__)
84 default:
85 #endif
86 break;
87 }
88 if (line.m_word) o << ":byword";
89 if (line.m_width < 1 || line.m_width > 1)
90 o << ":w=" << line.m_width ;
91 if (line.m_color.isSet())
92 o << ":col=" << line.m_color.get();
93 return o;
94 }
95
96 void MWAWFont::Line::addTo(librevenge::RVNGPropertyList &propList, std::string const &type) const
97 {
98 if (!isSet()) return;
99
100 std::stringstream s;
101 s << "style:text-" << type << "-type";
102 propList.insert(s.str().c_str(), (m_type==Single) ? "single" : "double");
103
104 if (m_word) {
105 s.str("");
106 s << "style:text-" << type << "-mode";
107 propList.insert(s.str().c_str(), "skip-white-space");
108 }
109
110 s.str("");
111 s << "style:text-" << type << "-style";
112 switch (m_style) {
113 case Dot:
114 case LargeDot:
115 propList.insert(s.str().c_str(), "dotted");
116 break;
117 case Dash:
118 propList.insert(s.str().c_str(), "dash");
119 break;
120 case Simple:
121 propList.insert(s.str().c_str(), "solid");
122 break;
123 case Wave:
124 propList.insert(s.str().c_str(), "wave");
125 break;
126 case None:
127 #if !defined(__clang__)
128 default:
129 #endif
130 break;
131 }
132 if (m_color.isSet()) {
133 s.str("");
134 s << "style:text-" << type << "-color";
135 propList.insert(s.str().c_str(), m_color.get().str().c_str());
136 }
137 //normal, bold, thin, dash, medium, and thick
138 s.str("");
139 s << "style:text-" << type << "-width";
140 if (m_width <= 0.6f)
141 propList.insert(s.str().c_str(), "thin");
142 else if (m_width >= 1.5f)
143 propList.insert(s.str().c_str(), "thick");
144 }
145
146 // script function
147
148 std::string MWAWFont::Script::str(float fSize) const
149 {
150 if (!isSet() || ((m_delta<=0&&m_delta>=0) && m_scale==100))
151 return "";
152 std::stringstream o;
153 if (m_deltaUnit == librevenge::RVNG_GENERIC) {
154 MWAW_DEBUG_MSG(("MWAWFont::Script::str: can not be called with generic position\n"));
155 return "";
156 }
157 float delta = m_delta;
158 if (m_deltaUnit != librevenge::RVNG_PERCENT) {
159 // first transform in point
160 if (m_deltaUnit != librevenge::RVNG_POINT)
161 delta=MWAWPosition::getScaleFactor(m_deltaUnit, librevenge::RVNG_POINT)*delta;
162 // now transform in percent
163 if (fSize<=0) {
164 static bool first=true;
165 if (first) {
166 MWAW_DEBUG_MSG(("MWAWFont::Script::str: can not be find font size (supposed 12pt)\n"));
167 first = false;
168 }
169 fSize=12;
170 }
171 delta=100.f*delta/fSize;
172 if (delta > 100) delta = 100;
173 else if (delta < -100) delta = -100;
174 }
175 o << delta << "% " << m_scale << "%";
176 return o.str();
177 }
178
179 // font function
180
181 std::string MWAWFont::getDebugString(std::shared_ptr<MWAWFontConverter> &converter) const
182 {
183 std::stringstream o;
184 o << std::dec;
185 if (id() != -1) {
186 if (converter)
187 o << "nam='" << converter->getName(id()) << "',";
188 else
189 o << "id=" << id() << ",";
190 }
191 if (size() > 0) {
192 if (m_sizeIsRelative.get())
193 o << "sz=" << 100*size() << "%,";
194 else
195 o << "sz=" << size() << ",";
196 }
197 if (m_deltaSpacing.isSet()) {
198 if (m_deltaSpacingUnit.get()==librevenge::RVNG_PERCENT)
199 o << "extend/condensed=" << m_deltaSpacing.get() << "%,";
200 else if (m_deltaSpacing.get() > 0)
201 o << "extended=" << m_deltaSpacing.get() << ",";
202 else if (m_deltaSpacing.get() < 0)
203 o << "condensed=" << -m_deltaSpacing.get() << ",";
204 }
205 if (m_widthStreching.isSet())
206 o << "scaling[width]=" << m_widthStreching.get()*100.f << "%,";
207 if (m_scriptPosition.isSet() && m_scriptPosition.get().isSet())
208 o << "script=" << m_scriptPosition.get().str(size()) << ",";
209 if (m_flags.isSet() && m_flags.get()) {
210 o << "fl=";
211 uint32_t flag = m_flags.get();
212 if (flag&boldBit) o << "b:";
213 if (flag&italicBit) o << "it:";
214 if (flag&embossBit) o << "emboss:";
215 if (flag&shadowBit) o << "shadow:";
216 if (flag&outlineBit) o << "outline:";
217 if (flag&smallCapsBit) o << "smallCaps:";
218 if (flag&uppercaseBit) o << "uppercase:";
219 if (flag&lowercaseBit) o << "lowercase:";
220 if (flag&initialcaseBit) o << "capitalise:";
221 if (flag&hiddenBit) o << "hidden:";
222 if (flag&reverseVideoBit) o << "reverseVideo:";
223 if (flag&blinkBit) o << "blink:";
224 if (flag&boxedBit) o << "box:";
225 if (flag&boxedRoundedBit) o << "box[rounded]:";
226 if (flag&reverseWritingBit) o << "reverseWriting:";
227 o << ",";
228 }
229 if (m_overline.isSet() && m_overline->isSet())
230 o << "overline=[" << m_overline.get() << "],";
231 if (m_strikeoutline.isSet() && m_strikeoutline->isSet())
232 o << "strikeOut=[" << m_strikeoutline.get() << "],";
233 if (m_underline.isSet() && m_underline->isSet())
234 o << "underline=[" << m_underline.get() << "],";
235 if (hasColor())
236 o << "col=" << m_color.get()<< ",";
237 if (m_backgroundColor.isSet() && !m_backgroundColor.get().isWhite())
238 o << "backCol=" << m_backgroundColor.get() << ",";
239 if (m_language.isSet() && m_language.get().length())
240 o << "lang=" << m_language.get() << ",";
241 o << m_extra;
242 return o.str();
243 }
244
245 void MWAWFont::addTo(librevenge::RVNGPropertyList &pList, std::shared_ptr<MWAWFontConverter> convert) const
246 {
247 int dSize = 0;
248 std::string fName("");
249 if (!convert) {
250 MWAW_DEBUG_MSG(("MWAWFont::addTo: called without any font converter\n"));
251 }
252 else
253 convert->getOdtInfo(id(), fName, dSize);
254 if (fName.length())
255 pList.insert("style:font-name", fName.c_str());
256 float fSize = 0;
257 if (m_sizeIsRelative.get())
258 pList.insert("fo:font-size", double(size()), librevenge::RVNG_PERCENT);
259 else {
260 fSize = size()+float(dSize);
261 if (fSize>=0)
262 pList.insert("fo:font-size", double(fSize), librevenge::RVNG_POINT);
263 }
264 uint32_t attributeBits = m_flags.get();
265 if (attributeBits & italicBit)
266 pList.insert("fo:font-style", "italic");
267 if (attributeBits & boldBit)
268 pList.insert("fo:font-weight", "bold");
269 if (attributeBits & outlineBit)
270 pList.insert("style:text-outline", "true");
271 if (attributeBits & blinkBit)
272 pList.insert("style:text-blinking", "true");
273 if (attributeBits & shadowBit)
274 pList.insert("fo:text-shadow", "1pt 1pt");
275 if (attributeBits & hiddenBit)
276 pList.insert("text:display", "none");
277 if (attributeBits & lowercaseBit)
278 pList.insert("fo:text-transform", "lowercase");
279 else if (attributeBits & uppercaseBit)
280 pList.insert("fo:text-transform", "uppercase");
281 else if (attributeBits & initialcaseBit)
282 pList.insert("fo:text-transform", "capitalize");
283 if (attributeBits & smallCapsBit)
284 pList.insert("fo:font-variant", "small-caps");
285 if (attributeBits & embossBit)
286 pList.insert("style:font-relief", "embossed");
287 else if (attributeBits & engraveBit)
288 pList.insert("style:font-relief", "engraved");
289
290 if (m_scriptPosition.isSet() && m_scriptPosition->isSet()) {
291 std::string pos=m_scriptPosition->str(fSize);
292 if (pos.length())
293 pList.insert("style:text-position", pos.c_str());
294 }
295
296 if (m_overline.isSet() && m_overline->isSet())
297 m_overline->addTo(pList, "overline");
298 if (m_strikeoutline.isSet() && m_strikeoutline->isSet())
299 m_strikeoutline->addTo(pList, "line-through");
300 if (m_underline.isSet() && m_underline->isSet())
301 m_underline->addTo(pList, "underline");
302 if ((attributeBits & boxedBit) || (attributeBits & boxedRoundedBit)) {
303 // do minimum: add a overline and a underline box
304 Line simple(Line::Simple);
305 if (!m_overline.isSet() || !m_overline->isSet())
306 simple.addTo(pList, "overline");
307 if (!m_underline.isSet() || !m_underline->isSet())
308 simple.addTo(pList, "underline");
309 }
310 if (m_deltaSpacing.isSet()) {
311 if (m_deltaSpacingUnit.get()==librevenge::RVNG_PERCENT) {
312 if (m_deltaSpacing.get() < 1 || m_deltaSpacing.get()>1) {
313 if (fSize>0) // assume width is equivalent to height
314 pList.insert("fo:letter-spacing", (double(m_deltaSpacing.get())-1)*double(fSize), librevenge::RVNG_POINT);
315 else {
316 // FIXME: this is not allowed in odf
317 std::stringstream s;
318 s << m_deltaSpacing.get() << "em";
319 pList.insert("fo:letter-spacing", s.str().c_str());
320 }
321 }
322 }
323 else if (m_deltaSpacing.get() < 0 || m_deltaSpacing.get()>0)
324 pList.insert("fo:letter-spacing", double(m_deltaSpacing.get()), librevenge::RVNG_POINT);
325 }
326 if (m_widthStreching.isSet() && m_widthStreching.get() > 0 &&
327 (m_widthStreching.get()>1||m_widthStreching.get()<1))
328 pList.insert("style:text-scale", double(m_widthStreching.get()), librevenge::RVNG_PERCENT);
329 if (attributeBits & reverseVideoBit) {
330 pList.insert("fo:color", m_backgroundColor->str().c_str());
331 pList.insert("fo:background-color", m_color->str().c_str());
332 }
333 else {
334 pList.insert("fo:color", m_color->str().c_str());
335 if (m_backgroundColor.isSet() && !m_backgroundColor->isWhite())
336 pList.insert("fo:background-color", m_backgroundColor->str().c_str());
337 }
338 if (m_language.isSet()) {
339 size_t len=m_language->length();
340 std::string lang(m_language.get());
341 std::string country("none");
342 if (len > 3 && lang[2]=='_') {
343 country=lang.substr(3);
344 lang=m_language->substr(0,2);
345 }
346 else if (len==0)
347 lang="none";
348 pList.insert("fo:language", lang.c_str());
349 pList.insert("fo:country", country.c_str());
350 }
351 if (attributeBits & reverseWritingBit) {
352 static bool first = true;
353 if (first) {
354 first = false;
355 MWAW_DEBUG_MSG(("MWAWFont::addTo: sorry, reverse writing is not umplemented\n"));
356 }
357 }
358 }
359
360 void MWAWFont::addToListLevel(librevenge::RVNGPropertyList &pList, std::shared_ptr<MWAWFontConverter> convert) const
361 {
362 int dSize = 0;
363 if (m_id.isSet()) {
364 std::string fName("");
365 if (!convert) {
366 MWAW_DEBUG_MSG(("MWAWFont::addToListLevel: called without any font converter\n"));
367 }
368 else
369 convert->getOdtInfo(id(), fName, dSize);
370 if (fName.length())
371 pList.insert("style:font-name", fName.c_str());
372 }
373 if (m_sizeIsRelative.get())
374 pList.insert("fo:font-size", double(size()), librevenge::RVNG_PERCENT);
375 else if (m_size.isSet()) {
376 float fSize = size()+float(dSize);
377 if (fSize>=0)
378 pList.insert("fo:font-size", double(fSize), librevenge::RVNG_POINT);
379 }
380 if (m_color.isSet())
381 pList.insert("fo:color", m_color->str().c_str());
382 }
383
384 ////////////////////////////////////////////////////////////
385 // the font manager
386 ////////////////////////////////////////////////////////////
387
388 //! namespace used to define structure for the font manager
389 namespace MWAWFontManagerInternal
390 {
391 /*! \struct FontCompare
392 * \brief internal struct used to create sorted map of font
393 */
394 struct FontCompare {
395 //! comparaison function
396 bool operator()(MWAWFont const &s1, MWAWFont const &s2) const
397 {
398 return s1.cmp(s2) < 0;
399 }
400 };
401 /// a map font to int
402 typedef std::map<MWAWFont, int, FontCompare> FontToIdMap;
403 //! the state of a MWAWFontManager
404 struct State {
405 //! constructor
406 explicit State(std::shared_ptr<MWAWFontConverter> const &fontConverter)
407 : m_fontConverter(fontConverter)
408 , m_fontToSpanIdMap()
409 , m_idToFontMap()
410 {
411 }
412 //! the font converter
413 std::shared_ptr<MWAWFontConverter> m_fontConverter;
414 //! a map font to id map (used to retrieve span id)
415 FontToIdMap m_fontToSpanIdMap;
416 //! a map id to font map
417 std::map<int, MWAWFont> m_idToFontMap;
418 };
419 }
420
421 MWAWFontManager::MWAWFontManager(std::shared_ptr<MWAWFontConverter> const &fontConverter)
422 : m_state(new MWAWFontManagerInternal::State(fontConverter))
423 {
424 }
425
426 MWAWFontManager::~MWAWFontManager()
427 {
428 }
429
430 std::shared_ptr<MWAWFontConverter> MWAWFontManager::getFontConverter()
431 {
432 return m_state->m_fontConverter;
433 }
434
435 int MWAWFontManager::getId(MWAWFont const &font)
436 {
437 auto it=m_state->m_fontToSpanIdMap.find(font);
438 if (it != m_state->m_fontToSpanIdMap.end())
439 return it->second;
440 int newId=1+int(m_state->m_fontToSpanIdMap.size());
441 m_state->m_fontToSpanIdMap[font]=newId;
442 m_state->m_idToFontMap[newId]=font;
443 return newId;
444 }
445
446 bool MWAWFontManager::getFont(int id, MWAWFont &font) const
447 {
448 if (m_state->m_idToFontMap.find(id) != m_state->m_idToFontMap.end()) {
449 font=m_state->m_idToFontMap.find(id)->second;
450 return true;
451 }
452 MWAW_DEBUG_MSG(("MWAWFontManager::getFont: can not find font with id=%d\n", id));
453 return false;
454 }
455
456 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: