Debian Codegraph @

src/lib/EPUBImageManager.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 "EPUBImageManager.h"
11
12 #include <algorithm>
13 #include <cassert>
14 #include <iomanip>
15 #include <sstream>
16
17 #include <boost/functional/hash.hpp>
18
19 #include "EPUBBinaryContent.h"
20 #include "EPUBCSSContent.h"
21 #include "EPUBManifest.h"
22 #include "EPUBPath.h"
23
24 namespace libepubgen
25 {
26
27 using librevenge::RVNGBinaryData;
28
29 using std::string;
30
31 namespace
32 {
33
34 string getExtension(const string &mimetype)
35 {
36 static const std::unordered_map<string, string> extensionMap =
37 {
38 {"image/gif", "gif"},
39 {"image/jpeg", "jpg"},
40 {"image/png", "png"},
41 {"image/svg+xml", "svg"},
42 };
43
44 const auto it = extensionMap.find(mimetype);
45 return (extensionMap.end() == it) ? string("img") : it->second;
46 }
47
48 }
49
50 std::size_t EPUBImageManager::BinaryDataHash::operator()(const librevenge::RVNGBinaryData &data) const
51 {
52 size_t seed = 0;
53
54 const unsigned char *const buf = data.getDataBuffer();
55 for (size_t i = 0; data.size() != i; ++i)
56 boost::hash_combine(seed, buf[i]);
57
58 return seed;
59 }
60
61 bool EPUBImageManager::BinaryDataEqual::operator()(const librevenge::RVNGBinaryData &left, const librevenge::RVNGBinaryData &right) const
62 {
63 if (left.empty() && right.empty())
64 return true;
65 if (left.size() != right.size())
66 return false;
67 const unsigned char *const leftData = left.getDataBuffer();
68 return std::equal(leftData, leftData + left.size(), right.getDataBuffer());
69 }
70
71 EPUBImageManager::EPUBImageManager(EPUBManifest &manifest)
72 : m_manifest(manifest)
73 , m_map()
74 , m_number()
75 , m_imageContentNameMap()
76 {
77 }
78
79 const EPUBPath &EPUBImageManager::insert(const librevenge::RVNGBinaryData &data, const librevenge::RVNGString &mimetype, const librevenge::RVNGString &properties)
80 {
81 MapType_t::const_iterator it = m_map.find(data);
82 if (m_map.end() == it)
83 {
84 const string mime(mimetype.cstr());
85
86 std::ostringstream nameBuf;
87 nameBuf << "image" << std::setw(4) << std::setfill('0') << m_number.next();
88 const string id = nameBuf.str();
89
90 nameBuf << "." << getExtension(mime);
91
92 const EPUBPath path(EPUBPath("OEBPS/images") / nameBuf.str());
93
94 m_manifest.insert(path, mime, id, properties.cstr());
95 it = m_map.insert(MapType_t::value_type(data, path)).first;
96 }
97
98 assert(m_map.end() != it); // the image must be present at this point
99
100 return it->second;
101 }
102
103 void EPUBImageManager::writeTo(EPUBPackage &package)
104 {
105 for (MapType_t::const_iterator it = m_map.begin(); m_map.end() != it; ++it)
106 {
107 EPUBBinaryContent image;
108 image.insertBinaryData(it->first);
109 image.writeTo(package, it->second.str().c_str());
110 }
111 }
112
113 std::string EPUBImageManager::getFrameClass(librevenge::RVNGPropertyList const &pList)
114 {
115 EPUBCSSProperties content;
116 extractImageProperties(pList, content);
117 ContentNameMap_t::const_iterator it=m_imageContentNameMap.find(content);
118 if (it != m_imageContentNameMap.end())
119 return it->second;
120 std::stringstream s;
121 s << "frame" << m_imageContentNameMap.size();
122 m_imageContentNameMap[content]=s.str();
123 return s.str();
124 }
125
126 std::string EPUBImageManager::getFrameStyle(librevenge::RVNGPropertyList const &pList)
127 {
128 EPUBCSSProperties content;
129 extractImageProperties(pList, content);
130
131 std::stringstream s;
132 for (const auto &property : content)
133 s << property.first << ": " << property.second << "; ";
134 return s.str();
135 }
136
137 void EPUBImageManager::extractImageProperties(librevenge::RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
138 {
139 // Positioning.
140 librevenge::RVNGString anchorType;
141 if (pList["text:anchor-type"])
142 anchorType = pList["text:anchor-type"]->getStr();
143 if (anchorType != "as-char")
144 {
145 // Horizontal position.
146 librevenge::RVNGString horizontalPos;
147 if (pList["style:horizontal-pos"])
148 horizontalPos = pList["style:horizontal-pos"]->getStr();
149
150 if (horizontalPos == "right")
151 cssProps["float"] = "right";
152 else
153 cssProps["float"] = "left";
154 }
155
156 // Extract borders.
157 static char const *type[] = {"border", "border-left", "border-top", "border-right", "border-bottom" };
158 for (int i = 0; i < 5; i++)
159 {
160 std::string field("fo:");
161 field+=type[i];
162 if (!pList[field.c_str()])
163 continue;
164 cssProps[type[i]] = pList[field.c_str()]->getStr().cstr();
165 }
166
167 // Extract size.
168 if (auto pRelWidth = pList["style:rel-width"])
169 cssProps["width"] = pRelWidth->getStr().cstr();
170 else if (auto pWidth = pList["svg:width"])
171 cssProps["width"] = pWidth->getStr().cstr();
172 }
173
174 std::string EPUBImageManager::getWrapStyle(librevenge::RVNGPropertyList const &pList)
175 {
176 librevenge::RVNGString wrap;
177 librevenge::RVNGString anchorType;
178 std::string ret;
179
180 if (pList["style:wrap"])
181 wrap = pList["style:wrap"]->getStr();
182 if (pList["text:anchor-type"])
183 anchorType = pList["text:anchor-type"]->getStr();
184
185 if (anchorType == "as-char")
186 return ret;
187
188 // Emulate wrap type with a break after the image.
189 if (wrap == "none")
190 ret = "clear: both;";
191 else if (wrap == "left")
192 // We want content on the left side, space on the right side, so the next
193 // element should clear on its left.
194 ret = "clear: left;";
195 else if (wrap == "right")
196 ret = "clear: right;";
197 else if (wrap == "parallel")
198 ret = "clear: none;";
199
200 return ret;
201 }
202
203 void EPUBImageManager::send(EPUBCSSContent &out)
204 {
205 for (auto it = m_imageContentNameMap.begin(); m_imageContentNameMap.end() != it; ++it)
206 {
207 librevenge::RVNGPropertyList props;
208 fillPropertyList(it->first, props);
209 out.insertRule(("." + it->second).c_str(), props);
210 }
211 }
212
213 }
214
215 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */