| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* libwps |
| 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) 2009, 2011 Alonso Laurent ([email protected]) |
| 11 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 14 | * Copyright (C) 2003-2005 William Lachance ([email protected]) |
| 15 | * |
| 16 | * For minor contributions see the git repository. |
| 17 | * |
| 18 | * Alternatively, the contents of this file may be used under the terms |
| 19 | * of the GNU Lesser General Public License Version 2.1 or later |
| 20 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 21 | * applicable instead of those above. |
| 22 | * |
| 23 | * For further information visit http://libwps.sourceforge.net |
| 24 | */ |
| 25 | |
| 26 | #include <iomanip> |
| 27 | #include <iostream> |
| 28 | |
| 29 | #include <librevenge/librevenge.h> |
| 30 | |
| 31 | #include "libwps_internal.h" |
| 32 | #include "WPSContentListener.h" |
| 33 | #include "WPSEntry.h" |
| 34 | #include "WPSFont.h" |
| 35 | #include "WPSOLEObject.h" |
| 36 | #include "WPSOLEParser.h" |
| 37 | #include "WPSParagraph.h" |
| 38 | #include "WPSPosition.h" |
| 39 | #include "WPSStream.h" |
| 40 | |
| 41 | #include "WPS4.h" |
| 42 | |
| 43 | #include "WPS4Graph.h" |
| 44 | |
| 45 | /** Internal: the structures of a WPS4Graph */ |
| 46 | namespace WPS4GraphInternal |
| 47 | { |
| 48 | //! Internal: the state of a WPS4Graph |
| 49 | struct State |
| 50 | { |
| 51 | State() |
| 52 | : m_version(-1) |
| 53 | , m_numPages(0) |
| 54 | , m_idToObjectMap() |
| 55 | {} |
| 56 | //! the version |
| 57 | int m_version; |
| 58 | //! the number page |
| 59 | int m_numPages; |
| 60 | |
| 61 | //! the map id to objects |
| 62 | std::map<int,WPSEmbeddedObject> m_idToObjectMap; |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | // constructor/destructor |
| 67 | WPS4Graph::WPS4Graph(WPS4Parser &parser) |
| 68 | : m_listener() |
| 69 | , m_mainParser(parser) |
| 70 | , m_state(new WPS4GraphInternal::State) |
| 71 | , m_asciiFile(parser.ascii()) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | WPS4Graph::~WPS4Graph() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | // small functions: version/numpages/update position |
| 80 | int WPS4Graph::version() const |
| 81 | { |
| 82 | if (m_state->m_version <= 0) |
| 83 | m_state->m_version = m_mainParser.version(); |
| 84 | return m_state->m_version; |
| 85 | } |
| 86 | |
| 87 | int WPS4Graph::numPages() const |
| 88 | { |
| 89 | return m_state->m_idToObjectMap.empty() ? 0 : 1; |
| 90 | } |
| 91 | |
| 92 | void WPS4Graph::computePositions() const |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | // update the positions and send data to the listener |
| 97 | void WPS4Graph::storeObjects(std::map<int,WPSEmbeddedObject> const &objectsMap) |
| 98 | { |
| 99 | for (auto const &it : objectsMap) |
| 100 | { |
| 101 | if (m_state->m_idToObjectMap.find(it.first)!=m_state->m_idToObjectMap.end()) |
| 102 | continue; |
| 103 | m_state->m_idToObjectMap[it.first]=it.second; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // send object |
| 108 | void WPS4Graph::sendObject(WPSPosition const &position, int id) |
| 109 | { |
| 110 | if (m_listener.get() == nullptr) |
| 111 | { |
| 112 | WPS_DEBUG_MSG(("WPS4Graph::sendObject: listener is not set\n")); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | auto it=m_state->m_idToObjectMap.find(id); |
| 117 | if (it==m_state->m_idToObjectMap.end()) |
| 118 | { |
| 119 | WPS_DEBUG_MSG(("WPS4Graph::sendObject: can not find %d object\n", id)); |
| 120 | return; |
| 121 | } |
| 122 | auto const &obj=it->second; |
| 123 | obj.m_sent=true; |
| 124 | WPSPosition posi(position); |
| 125 | posi.setNaturalSize(obj.m_size); |
| 126 | m_listener->insertObject(posi, obj); |
| 127 | } |
| 128 | |
| 129 | void WPS4Graph::sendObjects(int page) |
| 130 | { |
| 131 | if (page != -1) return; |
| 132 | if (m_listener.get() == nullptr) |
| 133 | { |
| 134 | WPS_DEBUG_MSG(("WPS4Graph::sendObjects: listener is not set\n")); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | #ifdef DEBUG |
| 139 | bool firstSend = false; |
| 140 | #endif |
| 141 | for (auto const &it : m_state->m_idToObjectMap) |
| 142 | { |
| 143 | if (it.second.m_sent) continue; |
| 144 | #ifdef DEBUG |
| 145 | if (!firstSend) |
| 146 | { |
| 147 | firstSend = true; |
| 148 | WPS_DEBUG_MSG(("WPS4Graph::sendObjects: find some extra pictures\n")); |
| 149 | m_listener->setFont(WPSFont::getDefault()); |
| 150 | m_listener->setParagraph(WPSParagraph()); |
| 151 | m_listener->insertEOL(); |
| 152 | librevenge::RVNGString message = "--------- The original document has some extra pictures: -------- "; |
| 153 | m_listener->insertUnicodeString(message); |
| 154 | m_listener->insertEOL(); |
| 155 | } |
| 156 | #endif |
| 157 | // as we do not have the size of the data, we insert small picture |
| 158 | auto const &obj=it.second; |
| 159 | obj.m_sent=true; |
| 160 | WPSPosition pos(Vec2f(),obj.m_size==Vec2f() ? Vec2f(1.,1.) : obj.m_size); |
| 161 | pos.setRelativePosition(WPSPosition::CharBaseLine); |
| 162 | pos.m_wrapping = WPSPosition::WDynamic; |
| 163 | m_listener->insertObject(pos, obj); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | //////////////////////////////////////////////////////////// |
| 168 | // low level |
| 169 | //////////////////////////////////////////////////////////// |
| 170 | int WPS4Graph::readObject(RVNGInputStreamPtr input, WPSEntry const &entry) |
| 171 | { |
| 172 | if (!entry.valid() || entry.length() <= 4) |
| 173 | { |
| 174 | WPS_DEBUG_MSG(("WPS4Graph::readObject: invalid object\n")); |
| 175 | return -1; |
| 176 | } |
| 177 | long pos=entry.begin(); |
| 178 | long endPos = entry.end(); |
| 179 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 180 | libwps::DebugStream f; |
| 181 | f << "ZZEOBJ" << entry.id() << "(Contents):"; |
| 182 | int type = libwps::readU16(input); |
| 183 | int oleId=-1; |
| 184 | if (type == 0x4f4d && entry.length()>=8) // OM |
| 185 | { |
| 186 | oleId = libwps::read16(input); |
| 187 | f << "Ole" << oleId << ","; |
| 188 | int numData=entry.length()>=10 ? 3 : 2; |
| 189 | for (int i=0; i<numData; ++i) |
| 190 | { |
| 191 | auto val = int(libwps::readU16(input)); |
| 192 | if (val) f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 193 | } |
| 194 | ascii().addPos(pos); |
| 195 | ascii().addNote(f.str().c_str()); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | WPSEmbeddedObject object; |
| 200 | auto stream=std::make_shared<WPSStream>(input, ascii()); |
| 201 | bool ok=false; |
| 202 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 203 | if (type==0x501) |
| 204 | ok=WPSOLEObject::readOLE(stream, object, endPos); |
| 205 | else // type==8 seems be followed by a standart metafile |
| 206 | ok=WPSOLEObject::readMetafile(stream, object, endPos,type!=8); |
| 207 | if (ok) |
| 208 | { |
| 209 | if (m_state->m_idToObjectMap.empty()) |
| 210 | oleId=0; |
| 211 | else |
| 212 | { |
| 213 | auto it=m_state->m_idToObjectMap.end(); |
| 214 | oleId=(--it)->first+1; |
| 215 | } |
| 216 | m_state->m_idToObjectMap[oleId]=object; |
| 217 | } |
| 218 | } |
| 219 | if (input->tell() != endPos) |
| 220 | { |
| 221 | if (type!=0x4f4d || m_state->m_idToObjectMap.find(oleId)==m_state->m_idToObjectMap.end()) |
| 222 | { |
| 223 | WPS_DEBUG_MSG(("WPS4Graph::readObject: find extra data\n")); |
| 224 | ascii().addPos(input->tell()); |
| 225 | ascii().addNote("ZZEOBJ(Contents):##extra"); |
| 226 | ascii().addPos(endPos); |
| 227 | ascii().addNote("_"); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | ascii().addPos(input->tell()); |
| 232 | ascii().addNote("_"); |
| 233 | ascii().addPos(endPos); |
| 234 | ascii().addNote("_"); |
| 235 | } |
| 236 | } |
| 237 | return oleId; |
| 238 | } |
| 239 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |