Debian Codegraph @

src/lib/EPUBFontManager.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 "EPUBFontManager.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 std::string getFontExtension(const std::string &mimetype)
35 {
36 static const std::unordered_map<std::string, std::string> extensionMap =
37 {
38 {"application/vnd.ms-opentype", "otf"},
39 };
40
41 const auto it = extensionMap.find(mimetype);
42 return it == extensionMap.end() ? std::string("ttf") : it->second;
43 }
44
45 }
46
47 std::size_t EPUBFontManager::BinaryDataHash::operator()(const librevenge::RVNGBinaryData &data) const
48 {
49 size_t seed = 0;
50
51 const unsigned char *const buf = data.getDataBuffer();
52 for (size_t i = 0; data.size() != i; ++i)
53 boost::hash_combine(seed, buf[i]);
54
55 return seed;
56 }
57
58 bool EPUBFontManager::BinaryDataEqual::operator()(const librevenge::RVNGBinaryData &left, const librevenge::RVNGBinaryData &right) const
59 {
60 if (left.empty() && right.empty())
61 return true;
62 if (left.size() != right.size())
63 return false;
64 const unsigned char *const leftData = left.getDataBuffer();
65 return std::equal(leftData, leftData + left.size(), right.getDataBuffer());
66 }
67
68 EPUBFontManager::EPUBFontManager(EPUBManifest &manifest)
69 : m_manifest(manifest)
70 , m_map()
71 , m_number()
72 , m_set()
73 {
74 }
75
76 void EPUBFontManager::insert(const librevenge::RVNGPropertyList &propertyList, const EPUBPath &base)
77 {
78 librevenge::RVNGBinaryData data(propertyList["office:binary-data"]->getStr());
79 librevenge::RVNGString mimetype(propertyList["librevenge:mime-type"]->getStr());
80 if (mimetype == "truetype")
81 // librevenge's truetype is EPUB's opentype.
82 mimetype = "application/vnd.ms-opentype";
83
84 MapType_t::const_iterator it = m_map.find(data);
85 if (m_map.end() == it)
86 {
87 const std::string mime(mimetype.cstr());
88
89 std::ostringstream nameBuf;
90 nameBuf << "font" << std::setw(4) << std::setfill('0') << m_number.next();
91 const std::string id = nameBuf.str();
92
93 nameBuf << "." << getFontExtension(mime);
94
95 const EPUBPath path(EPUBPath("OEBPS/fonts") / nameBuf.str());
96
97 m_manifest.insert(path, mime, id, "");
98 it = m_map.insert(MapType_t::value_type(data, path)).first;
99 }
100
101 assert(m_map.end() != it); // the font must be present at this point
102
103 // Now collect CSS properties.
104 EPUBCSSProperties content;
105 extractFontProperties(propertyList, content);
106 std::stringstream ss;
107 ss << "url(";
108 ss << it->second.relativeTo(base).str();
109 ss << ")";
110 content["src"] = ss.str();
111 SetType_t::const_iterator contentIt = m_set.find(content);
112 if (contentIt != m_set.end())
113 return;
114
115 m_set.insert(content);
116 }
117
118 void EPUBFontManager::extractFontProperties(librevenge::RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
119 {
120 if (pList["librevenge:name"])
121 {
122 std::ostringstream name;
123 name << '\'' << pList["librevenge:name"]->getStr().cstr() << '\'';
124 cssProps["font-family"] = name.str();
125 }
126
127 if (pList["librevenge:font-style"])
128 cssProps["font-style"] = pList["librevenge:font-style"]->getStr().cstr();
129
130 if (pList["librevenge:font-weight"])
131 cssProps["font-weight"] = pList["librevenge:font-weight"]->getStr().cstr();
132 }
133
134 void EPUBFontManager::writeTo(EPUBPackage &package)
135 {
136 for (MapType_t::const_iterator it = m_map.begin(); m_map.end() != it; ++it)
137 {
138 EPUBBinaryContent font;
139 font.insertBinaryData(it->first);
140 font.writeTo(package, it->second.str().c_str());
141 }
142 }
143
144 void EPUBFontManager::send(EPUBCSSContent &out)
145 {
146 for (const auto &fontProperties : m_set)
147 {
148 librevenge::RVNGPropertyList props;
149 fillPropertyList(fontProperties, props);
150 out.insertRule("@font-face", props);
151 }
152 }
153
154 }
155
156 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */