| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 18 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 19 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 20 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 21 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 22 |
* |
| 23 |
* |
| 24 |
* All Rights Reserved. |
| 25 |
* |
| 26 |
* For minor contributions see the git repository. |
| 27 |
* |
| 28 |
* Alternatively, the contents of this file may be used under the terms of |
| 29 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 30 |
* in which case the provisions of the LGPLv2+ are applicable |
| 31 |
* instead of those above. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <cmath> |
| 35 |
#include <cctype> |
| 36 |
#include <iomanip> |
| 37 |
#include <iostream> |
| 38 |
#include <limits> |
| 39 |
#include <algorithm> |
| 40 |
#include <set> |
| 41 |
#include <sstream> |
| 42 |
#include <stack> |
| 43 |
#include <string> |
| 44 |
#include <utility> |
| 45 |
|
| 46 |
#include <librevenge/librevenge.h> |
| 47 |
|
| 48 |
#include "MWAWFontConverter.hxx" |
| 49 |
#include "MWAWGraphicListener.hxx" |
| 50 |
#include "MWAWGraphicShape.hxx" |
| 51 |
#include "MWAWGraphicStyle.hxx" |
| 52 |
#include "MWAWHeader.hxx" |
| 53 |
#include "MWAWOLEParser.hxx" |
| 54 |
#include "MWAWParagraph.hxx" |
| 55 |
#include "MWAWPictBitmap.hxx" |
| 56 |
#include "MWAWPictData.hxx" |
| 57 |
#include "MWAWPrinter.hxx" |
| 58 |
#include "MWAWPosition.hxx" |
| 59 |
#include "MWAWRSRCParser.hxx" |
| 60 |
#include "MWAWStringStream.hxx" |
| 61 |
|
| 62 |
#include "Canvas5Graph.hxx" |
| 63 |
#include "Canvas5Image.hxx" |
| 64 |
#include "Canvas5Structure.hxx" |
| 65 |
#include "Canvas5StyleManager.hxx" |
| 66 |
|
| 67 |
#include "Canvas5BMParser.hxx" |
| 68 |
|
| 69 |
/** Internal: the structures of a Canvas5BMParser */ |
| 70 |
namespace Canvas5BMParserInternal |
| 71 |
{ |
| 72 |
//////////////////////////////////////// |
| 73 |
//! Internal: the state of a Canvas5BMParser |
| 74 |
struct State { |
| 75 |
//! constructor |
| 76 |
State() |
| 77 |
: m_isWindowsFile(false) |
| 78 |
, m_stream() |
| 79 |
|
| 80 |
, m_dimension() |
| 81 |
, m_image() |
| 82 |
{ |
| 83 |
} |
| 84 |
|
| 85 |
//! true if this is a windows file |
| 86 |
bool m_isWindowsFile; |
| 87 |
//! the current stream |
| 88 |
std::shared_ptr<Canvas5Structure::Stream> m_stream; |
| 89 |
//! the image dimension |
| 90 |
MWAWVec2i m_dimension; |
| 91 |
//! the image |
| 92 |
MWAWEmbeddedObject m_image; |
| 93 |
}; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
//////////////////////////////////////////////////////////// |
| 98 |
// constructor/destructor, ... |
| 99 |
//////////////////////////////////////////////////////////// |
| 100 |
Canvas5BMParser::Canvas5BMParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 101 |
: MWAWGraphicParser(input, rsrcParser, header) |
| 102 |
, m_state() |
| 103 |
{ |
| 104 |
resetGraphicListener(); |
| 105 |
setAsciiName("main-1"); |
| 106 |
|
| 107 |
m_state.reset(new Canvas5BMParserInternal::State); |
| 108 |
|
| 109 |
getPageSpan().setMargins(0); |
| 110 |
} |
| 111 |
|
| 112 |
Canvas5BMParser::~Canvas5BMParser() |
| 113 |
{ |
| 114 |
} |
| 115 |
|
| 116 |
bool Canvas5BMParser::isWindowsFile() const |
| 117 |
{ |
| 118 |
return m_state->m_isWindowsFile; |
| 119 |
} |
| 120 |
|
| 121 |
//////////////////////////////////////////////////////////// |
| 122 |
// the parser |
| 123 |
//////////////////////////////////////////////////////////// |
| 124 |
void Canvas5BMParser::parse(librevenge::RVNGDrawingInterface *docInterface) |
| 125 |
{ |
| 126 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 127 |
bool ok = false; |
| 128 |
try { |
| 129 |
checkHeader(nullptr); |
| 130 |
|
| 131 |
auto input=getInput(); |
| 132 |
if (!input) |
| 133 |
throw(libmwaw::ParseException()); |
| 134 |
|
| 135 |
// create the main stream |
| 136 |
m_state->m_stream=std::make_shared<Canvas5Structure::Stream>(input); |
| 137 |
m_state->m_stream->ascii().open(asciiName()); |
| 138 |
|
| 139 |
ok = createZones() && createDocument(docInterface); |
| 140 |
} |
| 141 |
catch (...) { |
| 142 |
MWAW_DEBUG_MSG(("Canvas5BMParser::parse: exception catched when parsing\n")); |
| 143 |
ok = false; |
| 144 |
} |
| 145 |
|
| 146 |
ascii().reset(); |
| 147 |
resetGraphicListener(); |
| 148 |
if (!ok) throw(libmwaw::ParseException()); |
| 149 |
} |
| 150 |
|
| 151 |
//////////////////////////////////////////////////////////// |
| 152 |
// create the document |
| 153 |
//////////////////////////////////////////////////////////// |
| 154 |
bool Canvas5BMParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface) |
| 155 |
{ |
| 156 |
if (!documentInterface) return false; |
| 157 |
if (getGraphicListener()) { |
| 158 |
MWAW_DEBUG_MSG(("Canvas5BMParser::createDocument: listener already exist\n")); |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
if (m_state->m_dimension[0]<=0 || m_state->m_dimension[1]<=0 || m_state->m_image.isEmpty()) { |
| 163 |
MWAW_DEBUG_MSG(("Canvas5BMParser::createDocument: can not find the image\n")); |
| 164 |
return false; |
| 165 |
} |
| 166 |
std::vector<MWAWPageSpan> pageList; |
| 167 |
MWAWPageSpan ps(getPageSpan()); |
| 168 |
ps.setFormLength(double(m_state->m_dimension[1])/72); |
| 169 |
ps.setFormWidth(double(m_state->m_dimension[0])/72); |
| 170 |
ps.setPageSpan(1); |
| 171 |
pageList.push_back(ps); |
| 172 |
MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface)); |
| 173 |
setGraphicListener(listen); |
| 174 |
listen->startDocument(); |
| 175 |
|
| 176 |
MWAWPosition pos(MWAWVec2f(0, 0), MWAWVec2f(m_state->m_dimension), librevenge::RVNG_POINT); |
| 177 |
pos.setRelativePosition(MWAWPosition::Page); |
| 178 |
pos.m_wrapping = MWAWPosition::WNone; |
| 179 |
listen->insertPicture(pos, m_state->m_image); |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
//////////////////////////////////////////////////////////// |
| 184 |
// |
| 185 |
// Intermediate level |
| 186 |
// |
| 187 |
//////////////////////////////////////////////////////////// |
| 188 |
bool Canvas5BMParser::createZones() |
| 189 |
{ |
| 190 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 191 |
if (rsrcParser) |
| 192 |
rsrcParser->getEntriesMap(); |
| 193 |
|
| 194 |
auto stream=m_state->m_stream; |
| 195 |
if (!stream || !stream->input()) |
| 196 |
return false; |
| 197 |
if (!readFileHeader(*stream)) |
| 198 |
return false; |
| 199 |
if (!Canvas5Structure::readBitmapDAD58Bim(*stream, version(), m_state->m_image)) |
| 200 |
return false; |
| 201 |
|
| 202 |
if (!stream->input()->isEnd()) { |
| 203 |
MWAW_DEBUG_MSG(("Canvas5BMParser::createZones: find extra data\n")); |
| 204 |
auto &ascFile=stream->ascii(); |
| 205 |
ascFile.addPos(stream->input()->tell()); |
| 206 |
ascFile.addNote("Entries(Extra):###"); |
| 207 |
} |
| 208 |
return !m_state->m_image.isEmpty(); |
| 209 |
} |
| 210 |
|
| 211 |
//////////////////////////////////////////////////////////// |
| 212 |
// read the header |
| 213 |
//////////////////////////////////////////////////////////// |
| 214 |
bool Canvas5BMParser::checkHeader(MWAWHeader *header, bool /*strict*/) |
| 215 |
{ |
| 216 |
MWAWInputStreamPtr input = getInput(); |
| 217 |
if (!input || !input->hasDataFork() || !input->checkPosition(0x100)) |
| 218 |
return false; |
| 219 |
|
| 220 |
input->setReadInverted(false); |
| 221 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 222 |
int val=int(input->readULong(4)); |
| 223 |
if ((val!=1&&val!=2) || input->readULong(4)!=0x44414435 || input->readULong(4)!=0x50524f58) // DAD5, PROX |
| 224 |
return false; |
| 225 |
|
| 226 |
int vers=val==1 ? 5 : 9; |
| 227 |
setVersion(vers); |
| 228 |
if (header) |
| 229 |
header->reset(MWAWDocument::MWAW_T_CANVAS, vers, MWAWDocument::MWAW_K_PAINT); |
| 230 |
|
| 231 |
input->seek(12, librevenge::RVNG_SEEK_SET); |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
bool Canvas5BMParser::readFileHeader(Canvas5Structure::Stream &stream) |
| 236 |
{ |
| 237 |
auto input=stream.input(); |
| 238 |
if (!input) return false; |
| 239 |
|
| 240 |
int const vers=version(); |
| 241 |
if (!input->checkPosition(vers<9 ? 36 : 40)) { |
| 242 |
MWAW_DEBUG_MSG(("Canvas5BMParser::readFileHeader: the zone is too short\n")); |
| 243 |
return false; |
| 244 |
} |
| 245 |
input->seek(12, librevenge::RVNG_SEEK_SET); |
| 246 |
libmwaw::DebugStream f; |
| 247 |
auto &ascFile=stream.ascii(); |
| 248 |
f << "FileHeader:"; |
| 249 |
f << "len=" << input->readULong(4) << ","; // seems a little greater than file length |
| 250 |
int dim[2]; |
| 251 |
for (auto &d : dim) d=int(input->readULong(4)); |
| 252 |
m_state->m_dimension=MWAWVec2i(dim[1], dim[0]); |
| 253 |
f << "dim=" << m_state->m_dimension << ","; |
| 254 |
int numPlanes=int(input->readLong(4)); // 1-4 |
| 255 |
if (numPlanes!=1) |
| 256 |
f << "num[planes]=" << numPlanes << ","; |
| 257 |
int numBytes=int(input->readLong(4)); // number of byte? |
| 258 |
if (numBytes!=8) |
| 259 |
f << "num[bytes]=" << numBytes << ","; |
| 260 |
double res=72; |
| 261 |
if (vers<9) |
| 262 |
res=double(input->readULong(4))/65536.f; |
| 263 |
else { |
| 264 |
bool isNan; |
| 265 |
if (!input->readDouble8(res, isNan)) |
| 266 |
f << "###"; |
| 267 |
} |
| 268 |
if (res<72 || res>72) |
| 269 |
f << "res=" << res << ","; |
| 270 |
ascFile.addPos(0); |
| 271 |
ascFile.addNote(f.str().c_str()); |
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
// ------------------------------------------------------------ |
| 276 |
// mac resource fork |
| 277 |
// ------------------------------------------------------------ |
| 278 |
|
| 279 |
// ------------------------------------------------------------ |
| 280 |
// windows resource fork |
| 281 |
// ------------------------------------------------------------ |
| 282 |
|
| 283 |
//////////////////////////////////////////////////////////// |
| 284 |
// |
| 285 |
// send data |
| 286 |
// |
| 287 |
//////////////////////////////////////////////////////////// |
| 288 |
|
| 289 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |