| 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 "EPUBListStyleManager.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 |
void EPUBListStyleManager::List::setLevel(int lvl, librevenge::RVNGPropertyList const &pList, bool ordered) |
| 26 |
{ |
| 27 |
if (lvl < 0 || lvl > 30) |
| 28 |
{ |
| 29 |
EPUBGEN_DEBUG_MSG(("EPUBListStyleManager::List::setLevel: level is %d\n", lvl)); |
| 30 |
return; |
| 31 |
} |
| 32 |
if (size_t(lvl) >= m_contentsList.size()) |
| 33 |
m_contentsList.resize(size_t(lvl)+1); |
| 34 |
|
| 35 |
EPUBCSSProperties cssProps; |
| 36 |
|
| 37 |
// fixme: read text:space-before, text:min-label-width, text:min-label-distance |
| 38 |
if (!ordered) |
| 39 |
{ |
| 40 |
// fixme read text:bullet-char |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
// fixme read style:num-prefix, style:num-suffix, text:start-value |
| 45 |
if (pList["style:num-format"]) |
| 46 |
{ |
| 47 |
if (strcmp(pList["style:num-format"]->getStr().cstr(),"a")==0) |
| 48 |
cssProps["list-style-type"] = "lower-alpha"; |
| 49 |
else if (strcmp(pList["style:num-format"]->getStr().cstr(),"A")==0) |
| 50 |
cssProps["list-style-type"] = "upper-alpha"; |
| 51 |
else if (strcmp(pList["style:num-format"]->getStr().cstr(),"i")==0) |
| 52 |
cssProps["list-style-type"] = "lower-roman"; |
| 53 |
else if (strcmp(pList["style:num-format"]->getStr().cstr(),"I")==0) |
| 54 |
cssProps["list-style-type"] = "upper-roman"; |
| 55 |
else if (strcmp(pList["style:num-format"]->getStr().cstr(),"1")==0) |
| 56 |
cssProps["list-style-type"] = "decimal"; |
| 57 |
else |
| 58 |
{ |
| 59 |
EPUBGEN_DEBUG_MSG(("EPUBListStyleManager::List::setLevel: unknown format %s\n", |
| 60 |
pList["style:num-format"]->getStr().cstr())); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
m_contentsList[size_t(lvl)] = cssProps; |
| 65 |
} |
| 66 |
|
| 67 |
const EPUBCSSProperties &EPUBListStyleManager::List::getCurrentProperties() const |
| 68 |
{ |
| 69 |
if (m_level < 0 || size_t(m_level) >= m_contentsList.size()) |
| 70 |
{ |
| 71 |
EPUBGEN_DEBUG_MSG(("EPUBListStyleManager::List::str: level %d is not set\n", m_level)); |
| 72 |
static EPUBCSSProperties dummy; |
| 73 |
return dummy; |
| 74 |
} |
| 75 |
return m_contentsList[size_t(m_level)]; |
| 76 |
} |
| 77 |
|
| 78 |
void EPUBListStyleManager::send(EPUBCSSContent &out) |
| 79 |
{ |
| 80 |
EPUBParagraphStyleManager::send(out); |
| 81 |
|
| 82 |
for (List::ContentNameMap_t::const_iterator it=m_levelNameMap.begin(); m_levelNameMap.end() != it; ++it) |
| 83 |
{ |
| 84 |
RVNGPropertyList props; |
| 85 |
fillPropertyList(it->first, props); |
| 86 |
out.insertRule(("." + it->second).c_str(), props); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
std::string EPUBListStyleManager::getClass(RVNGPropertyList const &pList) |
| 91 |
{ |
| 92 |
EPUBCSSProperties content; |
| 93 |
extractProperties(pList, true, content); |
| 94 |
List::ContentNameMap_t::const_iterator it = m_contentNameMap.find(content); |
| 95 |
if (it != m_contentNameMap.end()) |
| 96 |
return it->second; |
| 97 |
std::stringstream s; |
| 98 |
s << "listElt" << m_contentNameMap.size(); |
| 99 |
m_contentNameMap[content]=s.str(); |
| 100 |
return s.str(); |
| 101 |
} |
| 102 |
|
| 103 |
void EPUBListStyleManager::defineLevel(RVNGPropertyList const &pList, bool ordered) |
| 104 |
{ |
| 105 |
int id=-1; |
| 106 |
if (pList["librevenge:list-id"]) |
| 107 |
id=pList["librevenge:list-id"]->getInt(); |
| 108 |
if (m_idListMap.find(id)==m_idListMap.end()) |
| 109 |
m_idListMap[id]=List(); |
| 110 |
if (!pList["librevenge:level"]) |
| 111 |
{ |
| 112 |
EPUBGEN_DEBUG_MSG(("EPUBListStyleManager::defineLevel: can not find list level\n")); |
| 113 |
return; |
| 114 |
} |
| 115 |
m_idListMap.find(id)->second.setLevel(pList["librevenge:level"]->getInt(), pList, ordered); |
| 116 |
} |
| 117 |
|
| 118 |
std::string EPUBListStyleManager::openLevel(RVNGPropertyList const &pList, bool ordered) |
| 119 |
{ |
| 120 |
int id = -1; |
| 121 |
if (pList["librevenge:list-id"]) |
| 122 |
id=pList["librevenge:list-id"]->getInt(); |
| 123 |
else // anomynous list |
| 124 |
defineLevel(pList, ordered); |
| 125 |
m_actualIdStack.push_back(id); |
| 126 |
|
| 127 |
EPUBCSSProperties content; |
| 128 |
extractProperties(pList, true, content); |
| 129 |
if (id >= 0 && m_idListMap.find(id)!=m_idListMap.end()) |
| 130 |
{ |
| 131 |
m_idListMap.find(id)->second.openLevel(); |
| 132 |
const EPUBCSSProperties &levelProps = m_idListMap.find(id)->second.getCurrentProperties(); |
| 133 |
content.insert(levelProps.begin(), levelProps.end()); |
| 134 |
} |
| 135 |
List::ContentNameMap_t::const_iterator it = m_levelNameMap.find(content); |
| 136 |
if (it != m_levelNameMap.end()) |
| 137 |
return it->second; |
| 138 |
std::stringstream s; |
| 139 |
s << "listLevel" << m_levelNameMap.size(); |
| 140 |
m_levelNameMap[content]=s.str(); |
| 141 |
|
| 142 |
return s.str(); |
| 143 |
} |
| 144 |
|
| 145 |
void EPUBListStyleManager::closeLevel() |
| 146 |
{ |
| 147 |
if (!m_actualIdStack.size()) |
| 148 |
{ |
| 149 |
EPUBGEN_DEBUG_MSG(("EPUBListStyleManager::closeLevel: can not find level\n")); |
| 150 |
return; |
| 151 |
} |
| 152 |
int id=m_actualIdStack.back(); |
| 153 |
if (id >= 0 && m_idListMap.find(id)!=m_idListMap.end()) |
| 154 |
m_idListMap.find(id)->second.closeLevel(); |
| 155 |
|
| 156 |
m_actualIdStack.pop_back(); |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |