| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 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) 2002-2004 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2002 Marc Maurer ([email protected]) |
| 12 | * Copyright (C) 2004 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com) |
| 14 | * |
| 15 | * For minor contributions see the git repository. |
| 16 | * |
| 17 | * Alternatively, the contents of this file may be used under the terms |
| 18 | * of the GNU Lesser General Public License Version 2.1 or later |
| 19 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 20 | * applicable instead of those above. |
| 21 | */ |
| 22 | |
| 23 | #include "RVNGHTMLTextTextStyle.h" |
| 24 | |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include <cstring> |
| 28 | #include <iostream> |
| 29 | #include <sstream> |
| 30 | |
| 31 | #include <librevenge/librevenge.h> |
| 32 | |
| 33 | #include "librevenge_internal.h" |
| 34 | |
| 35 | namespace librevenge |
| 36 | { |
| 37 | //////////////////////////////////////////////////////////// |
| 38 | // list manager |
| 39 | //////////////////////////////////////////////////////////// |
| 40 | void RVNGHTMLTextListStyleManager::List::setLevel(int lvl, RVNGPropertyList const &pList, bool ordered) |
| 41 | { |
| 42 | if (lvl < 0 || lvl > 30) |
| 43 | { |
| 44 | RVNG_DEBUG_MSG(("RVNGHTMLTextListStyleManager::List::setLevel: level is %d\n", lvl)); |
| 45 | return; |
| 46 | } |
| 47 | if (size_t(lvl) >= m_contentsList.size()) |
| 48 | m_contentsList.resize(size_t(lvl)+1); |
| 49 | |
| 50 | std::stringstream s; |
| 51 | // fixme: read text:space-before, text:min-label-width, text:min-label-distance |
| 52 | if (!ordered) |
| 53 | { |
| 54 | // fixme read text:bullet-char |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | // fixme read style:num-prefix, style:num-suffix, text:start-value |
| 59 | if (pList["style:num-format"]) |
| 60 | { |
| 61 | if (strcmp(pList["style:num-format"]->getStr().cstr(),"a")==0) |
| 62 | s << " list-style-type:lower-alpha;"; |
| 63 | else if (strcmp(pList["style:num-format"]->getStr().cstr(),"A")==0) |
| 64 | s << " list-style-type:upper-alpha;"; |
| 65 | else if (strcmp(pList["style:num-format"]->getStr().cstr(),"i")==0) |
| 66 | s << " list-style-type:lower-roman;"; |
| 67 | else if (strcmp(pList["style:num-format"]->getStr().cstr(),"I")==0) |
| 68 | s << " list-style-type:upper-roman;"; |
| 69 | else if (strcmp(pList["style:num-format"]->getStr().cstr(),"1")==0) |
| 70 | s << " list-style-type:decimal;"; |
| 71 | else |
| 72 | { |
| 73 | RVNG_DEBUG_MSG(("RVNGHTMLTextListStyleManager::List::setLevel: unknown format %s\n", |
| 74 | pList["style:num-format"]->getStr().cstr())); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | s << "\n"; |
| 79 | m_contentsList[size_t(lvl)]=s.str(); |
| 80 | } |
| 81 | |
| 82 | std::string RVNGHTMLTextListStyleManager::List::str() const |
| 83 | { |
| 84 | if (m_level < 0 || size_t(m_level) >= m_contentsList.size()) |
| 85 | { |
| 86 | RVNG_DEBUG_MSG(("RVNGHTMLTextListStyleManager::List::str: level %d is not set\n", m_level)); |
| 87 | return ""; |
| 88 | } |
| 89 | return m_contentsList[size_t(m_level)]; |
| 90 | } |
| 91 | |
| 92 | void RVNGHTMLTextListStyleManager::send(std::ostream &out) |
| 93 | { |
| 94 | RVNGHTMLTextParagraphStyleManager::send(out); |
| 95 | auto it=m_levelNameMap.begin(); |
| 96 | while (it != m_levelNameMap.end()) |
| 97 | { |
| 98 | out << "." << it->second << " {\n" << it->first << "}\n"; |
| 99 | ++it; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | std::string RVNGHTMLTextListStyleManager::getClass(RVNGPropertyList const &pList) |
| 104 | { |
| 105 | std::string content=getContent(pList, true); |
| 106 | auto it=m_contentNameMap.find(content); |
| 107 | if (it != m_contentNameMap.end()) |
| 108 | return it->second; |
| 109 | std::stringstream s; |
| 110 | s << "listElt" << m_contentNameMap.size(); |
| 111 | m_contentNameMap[content]=s.str(); |
| 112 | return s.str(); |
| 113 | } |
| 114 | |
| 115 | void RVNGHTMLTextListStyleManager::defineLevel(RVNGPropertyList const &pList, bool ordered) |
| 116 | { |
| 117 | int id=-1; |
| 118 | if (pList["librevenge:list-id"]) |
| 119 | id=pList["librevenge:list-id"]->getInt(); |
| 120 | if (m_idListMap.find(id)==m_idListMap.end()) |
| 121 | m_idListMap[id]=List(); |
| 122 | if (!pList["librevenge:level"]) |
| 123 | { |
| 124 | RVNG_DEBUG_MSG(("RVNGHTMLTextListStyleManager::defineLevel: can not find list level\n")); |
| 125 | return; |
| 126 | } |
| 127 | m_idListMap.find(id)->second.setLevel(pList["librevenge:level"]->getInt(), pList, ordered); |
| 128 | } |
| 129 | |
| 130 | std::string RVNGHTMLTextListStyleManager::openLevel(RVNGPropertyList const &pList, bool ordered) |
| 131 | { |
| 132 | int id = -1; |
| 133 | if (pList["librevenge:list-id"]) |
| 134 | id=pList["librevenge:list-id"]->getInt(); |
| 135 | else // anomynous list |
| 136 | defineLevel(pList, ordered); |
| 137 | m_actualIdStack.push_back(id); |
| 138 | |
| 139 | std::string content=getContent(pList, true); |
| 140 | if (id >= 0 && m_idListMap.find(id)!=m_idListMap.end()) |
| 141 | { |
| 142 | m_idListMap.find(id)->second.openLevel(); |
| 143 | content+=m_idListMap.find(id)->second.str(); |
| 144 | } |
| 145 | auto it=m_levelNameMap.find(content); |
| 146 | if (it != m_levelNameMap.end()) |
| 147 | return it->second; |
| 148 | std::stringstream s; |
| 149 | s << "listLevel" << m_levelNameMap.size(); |
| 150 | m_levelNameMap[content]=s.str(); |
| 151 | |
| 152 | return s.str(); |
| 153 | } |
| 154 | |
| 155 | void RVNGHTMLTextListStyleManager::closeLevel() |
| 156 | { |
| 157 | if (!m_actualIdStack.size()) |
| 158 | { |
| 159 | RVNG_DEBUG_MSG(("RVNGHTMLTextListStyleManager::closeLevel: can not find level\n")); |
| 160 | return; |
| 161 | } |
| 162 | int id=m_actualIdStack.back(); |
| 163 | if (id >= 0 && m_idListMap.find(id)!=m_idListMap.end()) |
| 164 | m_idListMap.find(id)->second.closeLevel(); |
| 165 | |
| 166 | m_actualIdStack.pop_back(); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | //////////////////////////////////////////////////////////// |
| 171 | // paragraph manager |
| 172 | //////////////////////////////////////////////////////////// |
| 173 | std::string RVNGHTMLTextParagraphStyleManager::getClass(RVNGPropertyList const &pList) |
| 174 | { |
| 175 | if (pList["librevenge:paragraph-id"]) |
| 176 | { |
| 177 | int id=pList["librevenge:paragraph-id"]->getInt(); |
| 178 | if (m_idNameMap.find(id)!=m_idNameMap.end()) |
| 179 | return m_idNameMap.find(id)->second; |
| 180 | } |
| 181 | std::string content=getContent(pList, false); |
| 182 | auto it=m_contentNameMap.find(content); |
| 183 | if (it != m_contentNameMap.end()) |
| 184 | return it->second; |
| 185 | std::stringstream s; |
| 186 | s << "para" << m_contentNameMap.size(); |
| 187 | m_contentNameMap[content]=s.str(); |
| 188 | return s.str(); |
| 189 | } |
| 190 | |
| 191 | void RVNGHTMLTextParagraphStyleManager::defineParagraph(RVNGPropertyList const &propList) |
| 192 | { |
| 193 | if (!propList["librevenge:paragraph-id"]) |
| 194 | { |
| 195 | RVNG_DEBUG_MSG(("RVNGHTMLTextParagraphStyleManager::defineStyle: can not find the paragraph id\n")); |
| 196 | return; |
| 197 | } |
| 198 | int id=propList["librevenge:paragraph-id"]->getInt(); |
| 199 | RVNGPropertyList pList(propList); |
| 200 | pList.remove("librevenge:paragraph-id"); |
| 201 | m_idNameMap[id]=getClass(pList); |
| 202 | } |
| 203 | |
| 204 | void RVNGHTMLTextParagraphStyleManager::send(std::ostream &out) |
| 205 | { |
| 206 | auto it=m_contentNameMap.begin(); |
| 207 | while (it != m_contentNameMap.end()) |
| 208 | { |
| 209 | out << "." << it->second << " {\n" << it->first << "}\n"; |
| 210 | ++it; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | std::string RVNGHTMLTextParagraphStyleManager::getContent(RVNGPropertyList const &pList, bool isList) |
| 215 | { |
| 216 | std::stringstream s; |
| 217 | if (pList["fo:text-align"]) |
| 218 | { |
| 219 | if (pList["fo:text-align"]->getStr() == RVNGString("end")) // stupid OOo convention.. |
| 220 | s << " text-align:right;"; |
| 221 | else |
| 222 | s << " text-align:" << pList["fo:text-align"]->getStr().cstr() << ";"; |
| 223 | } |
| 224 | // the margins |
| 225 | s << " margin:"; |
| 226 | if (pList["fo:margin-top"]) |
| 227 | s << " " << pList["fo:margin-top"]->getStr().cstr(); |
| 228 | else |
| 229 | s << " 0px"; |
| 230 | if (pList["fo:margin-right"]) |
| 231 | s << " " << pList["fo:margin-right"]->getStr().cstr(); |
| 232 | else |
| 233 | s << " 0px"; |
| 234 | if (pList["fo:margin-bottom"]) |
| 235 | s << " " << pList["fo:margin-bottom"]->getStr().cstr(); |
| 236 | else |
| 237 | s << " 0px"; |
| 238 | if (isList) |
| 239 | { |
| 240 | double val=0; |
| 241 | if (pList["fo:margin-left"]) |
| 242 | { |
| 243 | RVNGUnit unit=pList["fo:margin-left"]->getUnit(); |
| 244 | if (unit==RVNG_POINT) val=pList["fo:margin-left"]->getDouble(); |
| 245 | else if (unit==RVNG_INCH) val=pList["fo:margin-left"]->getDouble()*72.; |
| 246 | else if (unit==RVNG_TWIP) val=pList["fo:margin-left"]->getDouble()*20.; |
| 247 | } |
| 248 | if (pList["fo:text-indent"]) |
| 249 | { |
| 250 | RVNGUnit unit=pList["fo:text-indent"]->getUnit(); |
| 251 | if (unit==RVNG_POINT) val+=pList["fo:text-indent"]->getDouble(); |
| 252 | else if (unit==RVNG_INCH) val+=pList["fo:text-indent"]->getDouble()*72.; |
| 253 | else if (unit==RVNG_TWIP) val+=pList["fo:text-indent"]->getDouble()*20.; |
| 254 | } |
| 255 | val -= 10; // checkme: seems to big, so decrease it |
| 256 | s << " " << val << "px"; |
| 257 | } |
| 258 | else if (pList["fo:margin-left"]) |
| 259 | s << " " << pList["fo:margin-left"]->getStr().cstr(); |
| 260 | else |
| 261 | s << " 0px"; |
| 262 | s << ";"; |
| 263 | if (pList["fo:text-indent"]) |
| 264 | { |
| 265 | s << " text-indent:" << pList["fo:text-indent"]->getStr().cstr() << ";"; |
| 266 | if (isList && pList["fo:text-indent"]->getStr().cstr()[0]=='-') |
| 267 | s << " padding-left:" << pList["fo:text-indent"]->getStr().cstr()+1 << ";"; |
| 268 | } |
| 269 | // line height |
| 270 | if (pList["fo:line-height"] && (pList["fo:line-height"]->getDouble()<0.999||pList["fo:line-height"]->getDouble()>1.001)) |
| 271 | s << " line-height:" << pList["fo:line-height"]->getStr().cstr() << ";"; |
| 272 | if (pList["style:line-height-at-least"] && (pList["style:line-height-at-least"]->getDouble()<0.999||pList["style:line-height-at-least"]->getDouble()>1.001)) |
| 273 | s << " min-height:" << pList["style:line-height-at-least"]->getStr().cstr() << ";"; |
| 274 | // other: background, border |
| 275 | if (pList["fo:background-color"]) |
| 276 | s << " background-color: " << pList["fo:background-color"]->getStr().cstr() << ";"; |
| 277 | parseBorders(pList, s); |
| 278 | s << "\n"; |
| 279 | return s.str(); |
| 280 | } |
| 281 | |
| 282 | void RVNGHTMLTextParagraphStyleManager::parseBorders(RVNGPropertyList const &pList, std::ostream &out) |
| 283 | { |
| 284 | static char const *type[] = {"border", "border-left", "border-top", "border-right", "border-bottom" }; |
| 285 | for (int i = 0; i < 5; i++) |
| 286 | { |
| 287 | std::string field("fo:"); |
| 288 | field+=type[i]; |
| 289 | if (!pList[field.c_str()]) |
| 290 | continue; |
| 291 | out << " " << type[i] << ": " << pList[field.c_str()]->getStr().cstr() << ";"; |
| 292 | // does not seems to works with negative text-indent, so add a padding |
| 293 | if (i<=1 && pList["fo:text-indent"] && pList["fo:text-indent"]->getDouble()<0 && |
| 294 | pList["fo:text-indent"]->getStr().cstr()[0]=='-') |
| 295 | out << "\tpadding-left:" << pList["fo:text-indent"]->getStr().cstr()+1 << ";\n"; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
| 300 | //////////////////////////////////////////////////////////// |
| 301 | // span manager |
| 302 | //////////////////////////////////////////////////////////// |
| 303 | std::string RVNGHTMLTextSpanStyleManager::getClass(RVNGPropertyList const &pList) |
| 304 | { |
| 305 | if (pList["librevenge:span-id"]) |
| 306 | { |
| 307 | int id=pList["librevenge:span-id"]->getInt(); |
| 308 | if (m_idNameMap.find(id)!=m_idNameMap.end()) |
| 309 | return m_idNameMap.find(id)->second; |
| 310 | } |
| 311 | |
| 312 | std::string content=getContent(pList); |
| 313 | auto it=m_contentNameMap.find(content); |
| 314 | if (it != m_contentNameMap.end()) |
| 315 | return it->second; |
| 316 | std::stringstream s; |
| 317 | s << "span" << m_contentNameMap.size(); |
| 318 | m_contentNameMap[content]=s.str(); |
| 319 | return s.str(); |
| 320 | } |
| 321 | |
| 322 | void RVNGHTMLTextSpanStyleManager::defineSpan(RVNGPropertyList const &propList) |
| 323 | { |
| 324 | if (!propList["librevenge:span-id"]) |
| 325 | { |
| 326 | RVNG_DEBUG_MSG(("RVNGHTMLTextSpanStyleManager::defineStyle: can not find the span id\n")); |
| 327 | return; |
| 328 | } |
| 329 | int id=propList["librevenge:span-id"]->getInt(); |
| 330 | RVNGPropertyList pList(propList); |
| 331 | pList.remove("librevenge:span-id"); |
| 332 | m_idNameMap[id]=getClass(pList); |
| 333 | } |
| 334 | |
| 335 | void RVNGHTMLTextSpanStyleManager::send(std::ostream &out) |
| 336 | { |
| 337 | auto it=m_contentNameMap.begin(); |
| 338 | while (it != m_contentNameMap.end()) |
| 339 | { |
| 340 | out << "." << it->second << " " << it->first << "\n"; |
| 341 | ++it; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | std::string RVNGHTMLTextSpanStyleManager::getContent(RVNGPropertyList const &pList) |
| 346 | { |
| 347 | std::stringstream s; |
| 348 | s << "{\n"; |
| 349 | if (pList["fo:background-color"]) |
| 350 | s << " background-color: " << pList["fo:background-color"]->getStr().cstr() << ";"; |
| 351 | if (pList["fo:color"]) |
| 352 | s << " color: " << pList["fo:color"]->getStr().cstr() << ";"; |
| 353 | if (pList["fo:font-size"]) |
| 354 | s << " font-size: " << pList["fo:font-size"]->getStr().cstr() << ";"; |
| 355 | if (pList["fo:font-style"]) |
| 356 | s << " font-style: " << pList["fo:font-style"]->getStr().cstr() << ";"; |
| 357 | if (pList["fo:font-variant"]) |
| 358 | s << " font-variant: " << pList["fo:font-variant"]->getStr().cstr() << ";"; |
| 359 | if (pList["fo:font-weight"]) |
| 360 | s << " font-weight: " << pList["fo:font-weight"]->getStr().cstr() << ";"; |
| 361 | if (pList["fo:letter-spacing"]) |
| 362 | s << " letter-spacing: " << pList["fo:letter-spacing"]->getStr().cstr() << ";"; |
| 363 | if (pList["fo:text-shadow"]) |
| 364 | s << " text-shadow: 1px 1px 1px #666666;"; |
| 365 | if (pList["fo:text-transform"]) |
| 366 | s << " text-transform: " << pList["fo:text-transform"]->getStr().cstr() << ";"; |
| 367 | |
| 368 | if (pList["style:font-name"]) |
| 369 | s << " font-family: \'" << pList["style:font-name"]->getStr().cstr() << "\';"; |
| 370 | if (pList["style:text-blinking"]) |
| 371 | s << " text-decoration: blink;"; |
| 372 | parseDecorations(pList, s); |
| 373 | if (pList["style:text-position"]) |
| 374 | parseTextPosition(pList["style:text-position"]->getStr().cstr(),s); |
| 375 | |
| 376 | if (pList["text:display"]) |
| 377 | s << " display: " << pList["text:display"]->getStr().cstr() << ";"; |
| 378 | |
| 379 | // checkme not working with Safari 6.02... |
| 380 | if (pList["style:font-relief"] && pList["style:font-relief"]->getStr().cstr()) |
| 381 | { |
| 382 | if (strcmp(pList["style:font-relief"]->getStr().cstr(),"embossed")==0) |
| 383 | s << " font-effect: emboss;"; |
| 384 | else if (strcmp(pList["style:font-relief"]->getStr().cstr(),"engraved")==0) |
| 385 | s << " font-effect: engrave;"; |
| 386 | } |
| 387 | if (pList["style:text-outline"]) |
| 388 | s << " font-effect: outline;"; |
| 389 | |
| 390 | if (pList["style:text-scale"]) |
| 391 | { |
| 392 | if (pList["style:text-scale"]->getDouble() < 0.2) |
| 393 | s << " font-stretch: ultra-condensed;"; |
| 394 | else if (pList["style:text-scale"]->getDouble() < 0.4) |
| 395 | s << " font-stretch: extra-condensed;"; |
| 396 | else if (pList["style:text-scale"]->getDouble() < 0.6) |
| 397 | s << " font-stretch: condensed;"; |
| 398 | else if (pList["style:text-scale"]->getDouble() < 0.8) |
| 399 | s << " font-stretch: semi-condensed;"; |
| 400 | else if (pList["style:text-scale"]->getDouble() > 2.0) |
| 401 | s << " font-stretch: ultra-expanded;"; |
| 402 | else if (pList["style:text-scale"]->getDouble() > 1.6) |
| 403 | s << " font-stretch: extra-expanded;"; |
| 404 | else if (pList["style:text-scale"]->getDouble() > 1.4) |
| 405 | s << " font-stretch: expanded;"; |
| 406 | else if (pList["style:text-scale"]->getDouble() > 1.2) |
| 407 | s << " font-stretch: semi-expanded;"; |
| 408 | |
| 409 | } |
| 410 | s << "\n}"; |
| 411 | |
| 412 | return s.str(); |
| 413 | } |
| 414 | |
| 415 | void RVNGHTMLTextSpanStyleManager::parseDecorations(RVNGPropertyList const &pList, std::ostream &out) |
| 416 | { |
| 417 | // replaceme by text-decoration-line when its implementation will appear in browser |
| 418 | std::stringstream s; |
| 419 | |
| 420 | if (pList["style:text-line-through-style"] || pList["style:text-line-through-type"]) |
| 421 | s << " line-through"; |
| 422 | if (pList["style:text-overline-style"] || pList["style:text-overline-type"]) |
| 423 | s << " overline"; |
| 424 | if (pList["style:text-underline-style"] || pList["style:text-underline-type"]) |
| 425 | s << " underline"; |
| 426 | if (s.str().length()) |
| 427 | out << " text-decoration:" << s.str() << ";"; |
| 428 | } |
| 429 | |
| 430 | void RVNGHTMLTextSpanStyleManager::parseTextPosition(char const *value, std::ostream &out) |
| 431 | { |
| 432 | if (!value) return; |
| 433 | // first try to retrieve the position |
| 434 | std::stringstream s(""); |
| 435 | double pos=0; |
| 436 | if (strncmp(value,"super", 5)==0) |
| 437 | { |
| 438 | pos=33; |
| 439 | s << value+5; |
| 440 | } |
| 441 | else if (strncmp(value,"sub", 3)==0) |
| 442 | { |
| 443 | pos=-33; |
| 444 | s << value+3; |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | s << value; |
| 449 | s >> pos; |
| 450 | if (!s.good()) |
| 451 | { |
| 452 | RVNG_DEBUG_MSG(("RVNGHTMLTextSpanStyleManager::parseTextPosition: can not read %s\n", value)); |
| 453 | return; |
| 454 | } |
| 455 | if (s.peek()=='%') |
| 456 | { |
| 457 | char c; |
| 458 | s >> c; |
| 459 | } |
| 460 | } |
| 461 | double percent; |
| 462 | s >> percent; |
| 463 | if (!s.good() || percent <= 0 || percent > 100) |
| 464 | { |
| 465 | RVNG_DEBUG_MSG(("RVNGHTMLTextSpanStyleManager::parseTextPosition: can not read percent: find %s\n", s.str().c_str())); |
| 466 | percent=100; |
| 467 | } |
| 468 | if (pos <= 0 && pos >= 0) return; |
| 469 | out << " vertical-align: baseline;"; |
| 470 | out << " position: relative;"; |
| 471 | out << " top: " << -pos/100. << "em;"; |
| 472 | if (percent < 100) |
| 473 | out << " font-size:" << percent/100. << "em;"; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |