| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 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 | * Major Contributor(s): |
| 10 | * Copyright (C) 2002-2004 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2002 Marc Maurer ([email protected]) |
| 12 | * Copyright (C) 2004 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com) |
| 14 | * |
| 15 | * For minor contributions see the git repository. |
| 16 | * |
| 17 | * Alternatively, the contents of this file may be used under the terms |
| 18 | * of the GNU Lesser General Public License Version 2.1 or later |
| 19 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 20 | * applicable instead of those above. |
| 21 | */ |
| 22 | |
| 23 | #include "RVNGHTMLTextTableStyle.h" |
| 24 | |
| 25 | #include <iostream> |
| 26 | #include <sstream> |
| 27 | |
| 28 | #include <librevenge/librevenge.h> |
| 29 | |
| 30 | #include "librevenge_internal.h" |
| 31 | |
| 32 | namespace librevenge |
| 33 | { |
| 34 | void RVNGHTMLTextTableStyleManager::openTable(RVNGPropertyListVector const &colList) |
| 35 | { |
| 36 | std::vector<double> colWidths; |
| 37 | for (unsigned long i = 0; i < colList.count(); i++) |
| 38 | { |
| 39 | RVNGPropertyList const &prop=colList[i]; |
| 40 | double width=0; |
| 41 | if (prop["style:column-width"]) |
| 42 | { |
| 43 | RVNGUnit unit=prop["style:column-width"]->getUnit(); |
| 44 | if (unit==RVNG_POINT) |
| 45 | width=prop["style:column-width"]->getDouble()/72.; |
| 46 | else if (unit==RVNG_INCH) |
| 47 | width=prop["style:column-width"]->getDouble(); |
| 48 | else if (unit==RVNG_TWIP) |
| 49 | width=prop["style:column-width"]->getDouble()/1440.; |
| 50 | } |
| 51 | colWidths.push_back(width); |
| 52 | } |
| 53 | m_columWitdhsStack.push_back(colWidths); |
| 54 | } |
| 55 | |
| 56 | void RVNGHTMLTextTableStyleManager::closeTable() |
| 57 | { |
| 58 | if (!m_columWitdhsStack.size()) |
| 59 | { |
| 60 | RVNG_DEBUG_MSG(("RVNGHTMLTextTableStyleManager::closeTable: can not find the columns witdh\n")); |
| 61 | return; |
| 62 | } |
| 63 | m_columWitdhsStack.pop_back(); |
| 64 | } |
| 65 | |
| 66 | bool RVNGHTMLTextTableStyleManager::getColumnsWidth(int col, int numSpanned, double &w) const |
| 67 | { |
| 68 | if (!m_columWitdhsStack.size()) |
| 69 | return false; |
| 70 | std::vector<double> const &widths=m_columWitdhsStack.back(); |
| 71 | if (col < 0 || size_t(col+numSpanned-1) >= widths.size()) |
| 72 | { |
| 73 | RVNG_DEBUG_MSG(("RVNGHTMLTextTableStyleManager::getColumnsWidth: can not compute the columns witdh\n")); |
| 74 | return false; |
| 75 | } |
| 76 | bool fixed = true; |
| 77 | w = 0; |
| 78 | for (auto i=size_t(col); i < size_t(col+numSpanned); i++) |
| 79 | { |
| 80 | if (widths[i] < 0) |
| 81 | { |
| 82 | w += -widths[i]; |
| 83 | fixed = false; |
| 84 | } |
| 85 | else if (widths[i] > 0) |
| 86 | w += widths[i]; |
| 87 | else |
| 88 | { |
| 89 | w = 0; |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | if (!fixed) w = -w; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | std::string RVNGHTMLTextTableStyleManager::getCellClass(RVNGPropertyList const &pList) |
| 98 | { |
| 99 | std::string content=getCellContent(pList); |
| 100 | auto it=m_cellContentNameMap.find(content); |
| 101 | if (it != m_cellContentNameMap.end()) |
| 102 | return it->second; |
| 103 | std::stringstream s; |
| 104 | s << "cellTable" << m_cellContentNameMap.size(); |
| 105 | m_cellContentNameMap[content]=s.str(); |
| 106 | return s.str(); |
| 107 | } |
| 108 | |
| 109 | std::string RVNGHTMLTextTableStyleManager::getRowClass(RVNGPropertyList const &pList) |
| 110 | { |
| 111 | std::string content=getRowContent(pList); |
| 112 | auto it=m_rowContentNameMap.find(content); |
| 113 | if (it != m_rowContentNameMap.end()) |
| 114 | return it->second; |
| 115 | std::stringstream s; |
| 116 | s << "rowTable" << m_rowContentNameMap.size(); |
| 117 | m_rowContentNameMap[content]=s.str(); |
| 118 | return s.str(); |
| 119 | } |
| 120 | |
| 121 | void RVNGHTMLTextTableStyleManager::send(std::ostream &out) |
| 122 | { |
| 123 | auto it=m_cellContentNameMap.begin(); |
| 124 | while (it != m_cellContentNameMap.end()) |
| 125 | { |
| 126 | out << "." << it->second << " " << it->first << "\n"; |
| 127 | ++it; |
| 128 | } |
| 129 | it=m_rowContentNameMap.begin(); |
| 130 | while (it != m_rowContentNameMap.end()) |
| 131 | { |
| 132 | out << "." << it->second << " " << it->first << "\n"; |
| 133 | ++it; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | std::string RVNGHTMLTextTableStyleManager::getCellContent(RVNGPropertyList const &pList) const |
| 138 | { |
| 139 | std::stringstream s; |
| 140 | s << "{\n"; |
| 141 | // try to get the cell width |
| 142 | if (pList["librevenge:column"]) |
| 143 | { |
| 144 | int c=pList["librevenge:column"]->getInt(); |
| 145 | int span=1; |
| 146 | if (pList["table:number-columns-spanned"]) |
| 147 | span = pList["table:number-columns-spanned"]->getInt(); |
| 148 | double w; |
| 149 | if (!getColumnsWidth(c,span,w)) |
| 150 | { |
| 151 | RVNG_DEBUG_MSG(("RVNGHTMLTextTableStyleManager::getCellContent: can not find columns witdth for %d[%d]\n", c, span)); |
| 152 | } |
| 153 | else if (w > 0) |
| 154 | s << "\twidth:" << w << "in;\n"; |
| 155 | else if (w < 0) |
| 156 | s << "\tmin-width:" << -w << "in;\n"; |
| 157 | } |
| 158 | if (pList["fo:text-align"]) |
| 159 | { |
| 160 | if (pList["fo:text-align"]->getStr() == RVNGString("end")) // stupid OOo convention.. |
| 161 | s << "\ttext-align:right;\n"; |
| 162 | else |
| 163 | s << "\ttext-align:" << pList["fo:text-align"]->getStr().cstr() << ";\n"; |
| 164 | } |
| 165 | if (pList["style:vertical-align"]) |
| 166 | s << "\tvertical-align:" << pList["style:vertical-align"]->getStr().cstr() << ";\n"; |
| 167 | else |
| 168 | s << "\tvertical-align:top;\n"; |
| 169 | if (pList["fo:background-color"]) |
| 170 | s << "\tbackground-color:" << pList["fo:background-color"]->getStr().cstr() << ";\n"; |
| 171 | |
| 172 | static char const *type[] = {"border", "border-left", "border-top", "border-right", "border-bottom" }; |
| 173 | for (auto &i : type) |
| 174 | { |
| 175 | std::string field("fo:"); |
| 176 | field+=i; |
| 177 | if (!pList[field.c_str()]) |
| 178 | continue; |
| 179 | s << "\t" << i << ": " << pList[field.c_str()]->getStr().cstr() << ";\n"; |
| 180 | } |
| 181 | |
| 182 | s << "}"; |
| 183 | return s.str(); |
| 184 | } |
| 185 | |
| 186 | std::string RVNGHTMLTextTableStyleManager::getRowContent(RVNGPropertyList const &pList) |
| 187 | { |
| 188 | std::stringstream s; |
| 189 | s << "{\n"; |
| 190 | if (pList["style:min-row-height"]) |
| 191 | s << "\tmin-height:" << pList["style:min-row-height"]->getStr().cstr() << ";\n"; |
| 192 | else if (pList["style:row-height"]) |
| 193 | s << "\theight:" << pList["style:row-height"]->getStr().cstr() << ";\n"; |
| 194 | s << "}"; |
| 195 | return s.str(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |