| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libepubgen 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 |
|
| 10 |
#include "EPUBParagraphStyleManager.h" |
| 11 |
|
| 12 |
#include <sstream> |
| 13 |
|
| 14 |
#include <librevenge/librevenge.h> |
| 15 |
|
| 16 |
#include "libepubgen_utils.h" |
| 17 |
#include "EPUBCSSContent.h" |
| 18 |
|
| 19 |
namespace libepubgen |
| 20 |
{ |
| 21 |
|
| 22 |
using librevenge::RVNGPropertyList; |
| 23 |
using librevenge::RVNGString; |
| 24 |
|
| 25 |
std::string EPUBParagraphStyleManager::getClass(RVNGPropertyList const &pList) |
| 26 |
{ |
| 27 |
if (pList["librevenge:paragraph-id"]) |
| 28 |
{ |
| 29 |
int id=pList["librevenge:paragraph-id"]->getInt(); |
| 30 |
if (m_idNameMap.find(id)!=m_idNameMap.end()) |
| 31 |
return m_idNameMap.find(id)->second; |
| 32 |
} |
| 33 |
EPUBCSSProperties content; |
| 34 |
extractProperties(pList, false, content); |
| 35 |
ContentNameMap_t::const_iterator it=m_contentNameMap.find(content); |
| 36 |
if (it != m_contentNameMap.end()) |
| 37 |
return it->second; |
| 38 |
std::stringstream s; |
| 39 |
s << "para" << m_contentNameMap.size(); |
| 40 |
m_contentNameMap[content]=s.str(); |
| 41 |
return s.str(); |
| 42 |
} |
| 43 |
|
| 44 |
std::string EPUBParagraphStyleManager::getStyle(RVNGPropertyList const &pList) |
| 45 |
{ |
| 46 |
EPUBCSSProperties content; |
| 47 |
extractProperties(pList, false, content); |
| 48 |
|
| 49 |
std::stringstream s; |
| 50 |
for (const auto &property : content) |
| 51 |
s << property.first << ": " << property.second << "; "; |
| 52 |
return s.str(); |
| 53 |
} |
| 54 |
|
| 55 |
void EPUBParagraphStyleManager::defineParagraph(RVNGPropertyList const &propList) |
| 56 |
{ |
| 57 |
if (!propList["librevenge:paragraph-id"]) |
| 58 |
{ |
| 59 |
EPUBGEN_DEBUG_MSG(("EPUBParagraphStyleManager::defineStyle: can not find the paragraph id\n")); |
| 60 |
return; |
| 61 |
} |
| 62 |
int id=propList["librevenge:paragraph-id"]->getInt(); |
| 63 |
RVNGPropertyList pList(propList); |
| 64 |
pList.remove("librevenge:paragraph-id"); |
| 65 |
m_idNameMap[id]=getClass(pList); |
| 66 |
} |
| 67 |
|
| 68 |
void EPUBParagraphStyleManager::send(EPUBCSSContent &out) |
| 69 |
{ |
| 70 |
for (ContentNameMap_t::const_iterator it=m_contentNameMap.begin(); m_contentNameMap.end() != it; ++it) |
| 71 |
{ |
| 72 |
RVNGPropertyList props; |
| 73 |
fillPropertyList(it->first, props); |
| 74 |
out.insertRule(("." + it->second).c_str(), props); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void EPUBParagraphStyleManager::extractProperties(RVNGPropertyList const &pList, bool isList, EPUBCSSProperties &cssProps) const |
| 79 |
{ |
| 80 |
if (pList["fo:text-align"]) |
| 81 |
{ |
| 82 |
if (pList["fo:text-align"]->getStr() == librevenge::RVNGString("end")) // stupid OOo convention.. |
| 83 |
cssProps["text-align"] = "right"; |
| 84 |
else |
| 85 |
cssProps["text-align"] = pList["fo:text-align"]->getStr().cstr(); |
| 86 |
} |
| 87 |
|
| 88 |
{ |
| 89 |
// the margins |
| 90 |
std::ostringstream s; |
| 91 |
if (pList["fo:margin-top"]) |
| 92 |
s << " " << pList["fo:margin-top"]->getStr().cstr(); |
| 93 |
else |
| 94 |
s << " 0px"; |
| 95 |
if (pList["fo:margin-right"]) |
| 96 |
s << " " << pList["fo:margin-right"]->getStr().cstr(); |
| 97 |
else |
| 98 |
s << " 0px"; |
| 99 |
if (pList["fo:margin-bottom"]) |
| 100 |
s << " " << pList["fo:margin-bottom"]->getStr().cstr(); |
| 101 |
else |
| 102 |
s << " 0px"; |
| 103 |
if (isList) |
| 104 |
{ |
| 105 |
double val=0; |
| 106 |
if (pList["fo:margin-left"]) |
| 107 |
{ |
| 108 |
librevenge::RVNGUnit unit=pList["fo:margin-left"]->getUnit(); |
| 109 |
if (unit==librevenge::RVNG_POINT) val=pList["fo:margin-left"]->getDouble(); |
| 110 |
else if (unit==librevenge::RVNG_INCH) val=pList["fo:margin-left"]->getDouble()*72.; |
| 111 |
else if (unit==librevenge::RVNG_TWIP) val=pList["fo:margin-left"]->getDouble()*20.; |
| 112 |
} |
| 113 |
if (pList["fo:text-indent"]) |
| 114 |
{ |
| 115 |
librevenge::RVNGUnit unit=pList["fo:text-indent"]->getUnit(); |
| 116 |
if (unit==librevenge::RVNG_POINT) val+=pList["fo:text-indent"]->getDouble(); |
| 117 |
else if (unit==librevenge::RVNG_INCH) val+=pList["fo:text-indent"]->getDouble()*72.; |
| 118 |
else if (unit==librevenge::RVNG_TWIP) val+=pList["fo:text-indent"]->getDouble()*20.; |
| 119 |
} |
| 120 |
val -= 10; // checkme: seems to big, so decrease it |
| 121 |
s << " " << val << "px"; |
| 122 |
} |
| 123 |
else if (pList["fo:margin-left"]) |
| 124 |
s << " " << pList["fo:margin-left"]->getStr().cstr(); |
| 125 |
else |
| 126 |
s << " 0px"; |
| 127 |
|
| 128 |
cssProps["margin"] = s.str(); |
| 129 |
} |
| 130 |
|
| 131 |
if (pList["fo:text-indent"]) |
| 132 |
{ |
| 133 |
cssProps["text-indent"] = pList["fo:text-indent"]->getStr().cstr(); |
| 134 |
if (isList && pList["fo:text-indent"]->getStr().cstr()[0]=='-') |
| 135 |
cssProps["padding-left"] = pList["fo:text-indent"]->getStr().cstr()+1; |
| 136 |
} |
| 137 |
// line height |
| 138 |
if (pList["fo:line-height"] && (pList["fo:line-height"]->getDouble()<0.999||pList["fo:line-height"]->getDouble()>1.001)) |
| 139 |
cssProps["line-height"] = pList["fo:line-height"]->getStr().cstr(); |
| 140 |
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)) |
| 141 |
cssProps["min-height"] = pList["style:line-height-at-least"]->getStr().cstr(); |
| 142 |
// other: background, border |
| 143 |
if (pList["fo:background-color"]) |
| 144 |
cssProps["background-color"] = pList["fo:background-color"]->getStr().cstr(); |
| 145 |
|
| 146 |
extractBorders(pList, cssProps); |
| 147 |
} |
| 148 |
|
| 149 |
void EPUBParagraphStyleManager::extractBorders(RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const |
| 150 |
{ |
| 151 |
static char const *type[] = {"border", "border-left", "border-top", "border-right", "border-bottom" }; |
| 152 |
for (int i = 0; i < 5; i++) |
| 153 |
{ |
| 154 |
std::string field("fo:"); |
| 155 |
field+=type[i]; |
| 156 |
if (!pList[field.c_str()]) |
| 157 |
continue; |
| 158 |
cssProps[type[i]] = pList[field.c_str()]->getStr().cstr(); |
| 159 |
// does not seems to works with negative text-indent, so add a padding |
| 160 |
if (i<=1 && pList["fo:text-indent"] && pList["fo:text-indent"]->getDouble()<0 && |
| 161 |
pList["fo:text-indent"]->getStr().cstr()[0]=='-') |
| 162 |
cssProps["padding-left"] = pList["fo:text-indent"]->getStr().cstr()+1; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |