Debian Codegraph @

src/lib/EPUBSpanStyleManager.cpp

raw
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 "EPUBSpanStyleManager.h"
11
12 #include <cstring>
13 #include <sstream>
14
15 #include <librevenge/librevenge.h>
16
17 #include "libepubgen_utils.h"
18 #include "EPUBCSSContent.h"
19
20 namespace libepubgen
21 {
22
23 using librevenge::RVNGPropertyList;
24
25 std::string EPUBSpanStyleManager::getClass(RVNGPropertyList const &pList)
26 {
27 if (pList["librevenge:span-id"])
28 {
29 int id=pList["librevenge:span-id"]->getInt();
30 if (m_idNameMap.find(id)!=m_idNameMap.end())
31 return m_idNameMap.find(id)->second;
32 }
33
34 EPUBCSSProperties content;
35
36 extractProperties(pList, content);
37
38 ContentNameMap_t::const_iterator it = m_contentNameMap.find(content);
39 if (it != m_contentNameMap.end())
40 return it->second;
41
42 std::stringstream s;
43 s << m_classNamePrefix << m_contentNameMap.size();
44
45 m_contentNameMap[content]=s.str();
46 return s.str();
47 }
48
49 std::string EPUBSpanStyleManager::getStyle(RVNGPropertyList const &pList)
50 {
51 EPUBCSSProperties content;
52 extractProperties(pList, content);
53
54 std::stringstream s;
55 for (const auto &property : content)
56 s << property.first << ": " << property.second << "; ";
57 return s.str();
58 }
59
60 void EPUBSpanStyleManager::defineSpan(RVNGPropertyList const &propList)
61 {
62 if (!propList["librevenge:span-id"])
63 {
64 EPUBGEN_DEBUG_MSG(("EPUBSpanStyleManager::defineStyle: can not find the span id\n"));
65 return;
66
67 }
68 int id=propList["librevenge:span-id"]->getInt();
69
70 RVNGPropertyList pList(propList);
71 pList.remove("librevenge:span-id");
72 m_idNameMap[id]=getClass(pList);
73 }
74
75 void EPUBSpanStyleManager::send(EPUBCSSContent &out)
76 {
77 for (ContentNameMap_t::const_iterator it=m_contentNameMap.begin(); m_contentNameMap.end() != it; ++it)
78 {
79 RVNGPropertyList props;
80 fillPropertyList(it->first, props);
81 out.insertRule(("." + it->second).c_str(), props);
82 }
83 }
84
85 void EPUBSpanStyleManager::extractProperties(RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
86 {
87 if (pList["fo:background-color"])
88 cssProps["background-color"] = pList["fo:background-color"]->getStr().cstr();
89 if (pList["fo:color"])
90 cssProps["color"] = pList["fo:color"]->getStr().cstr();
91 if (pList["fo:font-size"])
92 cssProps["font-size"] = pList["fo:font-size"]->getStr().cstr();
93 if (pList["fo:font-style"])
94 cssProps["font-style"] = pList["fo:font-style"]->getStr().cstr();
95 if (pList["fo:font-variant"])
96 cssProps["font-variant"] = pList["fo:font-variant"]->getStr().cstr();
97 if (pList["fo:font-weight"])
98 cssProps["font-weight"] = pList["fo:font-weight"]->getStr().cstr();
99 if (pList["fo:letter-spacing"])
100 cssProps["letter-spacing"] = pList["fo:letter-spacing"]->getStr().cstr();
101 if (pList["fo:text-shadow"])
102 cssProps["text-shadow"] = "1px 1px 1px #666666";
103 if (pList["fo:text-transform"])
104 cssProps["text-transform"] = pList["fo:text-transform"]->getStr().cstr();
105
106 if (pList["style:font-name"])
107 {
108 std::ostringstream name;
109 name << '\'' << pList["style:font-name"]->getStr().cstr() << '\'';
110 cssProps["font-family"] = name.str();
111 }
112 if (pList["style:text-blinking"])
113 cssProps["text-decoration"] = "blink";
114 extractDecorations(pList, cssProps);
115 if (pList["style:text-position"])
116 extractTextPosition(pList["style:text-position"]->getStr().cstr(), cssProps);
117
118 if (pList["text:display"])
119 cssProps["display"] = pList["text:display"]->getStr().cstr();
120
121 // checkme not working with Safari 6.02...
122 if (pList["style:font-relief"] && pList["style:font-relief"]->getStr().cstr())
123 {
124 if (strcmp(pList["style:font-relief"]->getStr().cstr(),"embossed")==0)
125 cssProps["font-effect"] = "emboss";
126 else if (strcmp(pList["style:font-relief"]->getStr().cstr(),"engraved")==0)
127 cssProps["font-effect"] = "engrave";
128 }
129 if (pList["style:text-outline"])
130 cssProps["font-effect"] = "outline";
131
132 if (pList["style:text-scale"])
133 {
134 if (pList["style:text-scale"]->getDouble() < 0.2)
135 cssProps["font-stretch"] = "ultra-condensed";
136 else if (pList["style:text-scale"]->getDouble() < 0.4)
137 cssProps["font-stretch"] = "extra-condensed";
138 else if (pList["style:text-scale"]->getDouble() < 0.6)
139 cssProps["font-stretch"] = "condensed";
140 else if (pList["style:text-scale"]->getDouble() < 0.8)
141 cssProps["font-stretch"] = "semi-condensed";
142 else if (pList["style:text-scale"]->getDouble() > 2.0)
143 cssProps["font-stretch"] = "ultra-expanded";
144 else if (pList["style:text-scale"]->getDouble() > 1.6)
145 cssProps["font-stretch"] = "extra-expanded";
146 else if (pList["style:text-scale"]->getDouble() > 1.4)
147 cssProps["font-stretch"] = "expanded";
148 else if (pList["style:text-scale"]->getDouble() > 1.2)
149 cssProps["font-stretch"] = "semi-expanded";
150 }
151 }
152
153 void EPUBSpanStyleManager::extractDecorations(RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
154 {
155 // replaceme by text-decoration-line when its implementation will appear in browser
156 std::stringstream s;
157
158 // line-though style or type 'none' is not line-though, everything else is.
159 const librevenge::RVNGProperty *textLineThoughStyle = pList["style:text-line-through-style"];
160 bool lineThough = textLineThoughStyle && textLineThoughStyle->getStr() != "none";
161 if (!lineThough)
162 {
163 const librevenge::RVNGProperty *textLineThoughType = pList["style:text-line-through-type"];
164 lineThough = textLineThoughType && textLineThoughType->getStr() != "none";
165 }
166 if (lineThough)
167 s << " line-through";
168
169 if (pList["style:text-overline-style"] || pList["style:text-overline-type"])
170 s << " overline";
171 const librevenge::RVNGProperty *textUnderlineStyle = pList["style:text-underline-style"];
172 bool underline = textUnderlineStyle && textUnderlineStyle->getStr() != "none";
173 if (!underline)
174 {
175 const librevenge::RVNGProperty *textUnderlineType = pList["style:text-underline-type"];
176 underline = textUnderlineType && textUnderlineType->getStr() != "none";
177 }
178 if (underline)
179 s << " underline";
180 if (s.str().length())
181 cssProps["text-decoration"] = s.str();
182 }
183
184 void EPUBSpanStyleManager::extractTextPosition(char const *value, EPUBCSSProperties &cssProps) const
185 {
186 if (!value) return;
187 // first try to retrieve the position
188 std::stringstream s("");
189 double pos=0;
190 if (strncmp(value,"super", 5)==0)
191 {
192 pos=33;
193 s << value+5;
194 }
195 else if (strncmp(value,"sub", 3)==0)
196 {
197 pos=-33;
198 s << value+3;
199 }
200 else
201 {
202 s << value;
203 s >> pos;
204 if (!s.good())
205 {
206 EPUBGEN_DEBUG_MSG(("EPUBSpanStyleManager::parseTextPosition: can not read %s\n", value));
207 return;
208 }
209 if (s.peek()=='%')
210 {
211 char c;
212 s >> c;
213 }
214 }
215 double percent;
216 s >> percent;
217 if (!s.good() || percent <= 0 || percent > 100)
218 {
219 EPUBGEN_DEBUG_MSG(("EPUBSpanStyleManager::parseTextPosition: can not read percent: find %s\n", s.str().c_str()));
220 percent=100;
221 }
222 if (pos <= 0 && pos >= 0) return;
223 cssProps["vertical-align"] = "baseline";
224 cssProps["position"] = "relative";
225
226 std::ostringstream top;
227 top << (-pos/100.) << "em";
228 cssProps["top"] = top.str();
229 if (percent < 100)
230 {
231 std::ostringstream fontSize;
232 fontSize << (percent/100.) << "em";
233 cssProps["font-size"] = fontSize.str();
234 }
235 }
236
237 }
238
239 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */