Coverage Report

Created: 2026-09-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmwaw/src/lib/Canvas5BMParser.cxx
Line
Count
Source
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 (wrlach@gmail.com)
18
* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20
* Copyright (C) 2006, 2007 Andrew Ziem
21
* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
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
0
    : m_isWindowsFile(false)
78
0
    , m_stream()
79
80
0
    , m_dimension()
81
0
    , m_image()
82
0
  {
83
0
  }
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
0
  : MWAWGraphicParser(input, rsrcParser, header)
102
0
  , m_state()
103
0
{
104
0
  resetGraphicListener();
105
0
  setAsciiName("main-1");
106
107
0
  m_state.reset(new Canvas5BMParserInternal::State);
108
109
0
  getPageSpan().setMargins(0);
110
0
}
111
112
Canvas5BMParser::~Canvas5BMParser()
113
0
{
114
0
}
115
116
bool Canvas5BMParser::isWindowsFile() const
117
0
{
118
0
  return m_state->m_isWindowsFile;
119
0
}
120
121
////////////////////////////////////////////////////////////
122
// the parser
123
////////////////////////////////////////////////////////////
124
void Canvas5BMParser::parse(librevenge::RVNGDrawingInterface *docInterface)
125
0
{
126
0
  if (!getInput().get() || !checkHeader(nullptr))  throw(libmwaw::ParseException());
127
0
  bool ok = false;
128
0
  try {
129
0
    checkHeader(nullptr);
130
131
0
    auto input=getInput();
132
0
    if (!input)
133
0
      throw(libmwaw::ParseException());
134
135
    // create the main stream
136
0
    m_state->m_stream=std::make_shared<Canvas5Structure::Stream>(input);
137
0
    m_state->m_stream->ascii().open(asciiName());
138
139
0
    ok = createZones() && createDocument(docInterface);
140
0
  }
141
0
  catch (...) {
142
0
    MWAW_DEBUG_MSG(("Canvas5BMParser::parse: exception catched when parsing\n"));
143
0
    ok = false;
144
0
  }
145
146
0
  ascii().reset();
147
0
  resetGraphicListener();
148
0
  if (!ok) throw(libmwaw::ParseException());
149
0
}
150
151
////////////////////////////////////////////////////////////
152
// create the document
153
////////////////////////////////////////////////////////////
154
bool Canvas5BMParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
155
0
{
156
0
  if (!documentInterface) return false;
157
0
  if (getGraphicListener()) {
158
0
    MWAW_DEBUG_MSG(("Canvas5BMParser::createDocument: listener already exist\n"));
159
0
    return false;
160
0
  }
161
162
0
  if (m_state->m_dimension[0]<=0 || m_state->m_dimension[1]<=0 || m_state->m_image.isEmpty()) {
163
0
    MWAW_DEBUG_MSG(("Canvas5BMParser::createDocument: can not find the image\n"));
164
0
    return false;
165
0
  }
166
0
  std::vector<MWAWPageSpan> pageList;
167
0
  MWAWPageSpan ps(getPageSpan());
168
0
  ps.setFormLength(double(m_state->m_dimension[1])/72);
169
0
  ps.setFormWidth(double(m_state->m_dimension[0])/72);
170
0
  ps.setPageSpan(1);
171
0
  pageList.push_back(ps);
172
0
  MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
173
0
  setGraphicListener(listen);
174
0
  listen->startDocument();
175
176
0
  MWAWPosition pos(MWAWVec2f(0, 0), MWAWVec2f(m_state->m_dimension), librevenge::RVNG_POINT);
177
0
  pos.setRelativePosition(MWAWPosition::Page);
178
0
  pos.m_wrapping = MWAWPosition::WNone;
179
0
  listen->insertPicture(pos, m_state->m_image);
180
0
  return true;
181
0
}
182
183
////////////////////////////////////////////////////////////
184
//
185
// Intermediate level
186
//
187
////////////////////////////////////////////////////////////
188
bool Canvas5BMParser::createZones()
189
0
{
190
0
  MWAWRSRCParserPtr rsrcParser = getRSRCParser();
191
0
  if (rsrcParser)
192
0
    rsrcParser->getEntriesMap();
193
194
0
  auto stream=m_state->m_stream;
195
0
  if (!stream || !stream->input())
196
0
    return false;
197
0
  if (!readFileHeader(*stream))
198
0
    return false;
199
0
  if (!Canvas5Structure::readBitmapDAD58Bim(*stream, version(), m_state->m_image))
200
0
    return false;
201
202
0
  if (!stream->input()->isEnd()) {
203
0
    MWAW_DEBUG_MSG(("Canvas5BMParser::createZones: find extra data\n"));
204
0
    auto &ascFile=stream->ascii();
205
0
    ascFile.addPos(stream->input()->tell());
206
0
    ascFile.addNote("Entries(Extra):###");
207
0
  }
208
0
  return !m_state->m_image.isEmpty();
209
0
}
210
211
////////////////////////////////////////////////////////////
212
// read the header
213
////////////////////////////////////////////////////////////
214
bool Canvas5BMParser::checkHeader(MWAWHeader *header, bool /*strict*/)
215
0
{
216
0
  MWAWInputStreamPtr input = getInput();
217
0
  if (!input || !input->hasDataFork() || !input->checkPosition(0x100))
218
0
    return false;
219
220
0
  input->setReadInverted(false);
221
0
  input->seek(0, librevenge::RVNG_SEEK_SET);
222
0
  int val=int(input->readULong(4));
223
0
  if ((val!=1&&val!=2) || input->readULong(4)!=0x44414435 || input->readULong(4)!=0x50524f58) // DAD5, PROX
224
0
    return false;
225
226
0
  int vers=val==1 ? 5 : 9;
227
0
  setVersion(vers);
228
0
  if (header)
229
0
    header->reset(MWAWDocument::MWAW_T_CANVAS, vers, MWAWDocument::MWAW_K_PAINT);
230
231
0
  input->seek(12, librevenge::RVNG_SEEK_SET);
232
0
  return true;
233
0
}
234
235
bool Canvas5BMParser::readFileHeader(Canvas5Structure::Stream &stream)
236
0
{
237
0
  auto input=stream.input();
238
0
  if (!input) return false;
239
240
0
  int const vers=version();
241
0
  if (!input->checkPosition(vers<9 ? 36 : 40)) {
242
0
    MWAW_DEBUG_MSG(("Canvas5BMParser::readFileHeader: the zone is too short\n"));
243
0
    return false;
244
0
  }
245
0
  input->seek(12, librevenge::RVNG_SEEK_SET);
246
0
  libmwaw::DebugStream f;
247
0
  auto &ascFile=stream.ascii();
248
0
  f << "FileHeader:";
249
0
  f << "len=" << input->readULong(4) << ","; // seems a little greater than file length
250
0
  int dim[2];
251
0
  for (auto &d : dim) d=int(input->readULong(4));
252
0
  m_state->m_dimension=MWAWVec2i(dim[1], dim[0]);
253
0
  f << "dim=" << m_state->m_dimension << ",";
254
0
  int numPlanes=int(input->readLong(4)); // 1-4
255
0
  if (numPlanes!=1)
256
0
    f << "num[planes]=" << numPlanes << ",";
257
0
  int numBytes=int(input->readLong(4)); // number of byte?
258
0
  if (numBytes!=8)
259
0
    f << "num[bytes]=" << numBytes << ",";
260
0
  double res=72;
261
0
  if (vers<9)
262
0
    res=double(input->readULong(4))/65536.f;
263
0
  else {
264
0
    bool isNan;
265
0
    if (!input->readDouble8(res, isNan))
266
0
      f << "###";
267
0
  }
268
0
  if (res<72 || res>72)
269
0
    f << "res=" << res << ",";
270
0
  ascFile.addPos(0);
271
0
  ascFile.addNote(f.str().c_str());
272
0
  return true;
273
0
}
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: