| 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 |
/* |
| 35 |
* Class to read/store the MsWrd structures |
| 36 |
*/ |
| 37 |
|
| 38 |
#ifndef MS_WRD_STRUCT |
| 39 |
# define MS_WRD_STRUCT |
| 40 |
|
| 41 |
#include <iostream> |
| 42 |
#include <string> |
| 43 |
#include <vector> |
| 44 |
|
| 45 |
#include "libmwaw_internal.hxx" |
| 46 |
|
| 47 |
#include "MWAWFont.hxx" |
| 48 |
#include "MWAWParagraph.hxx" |
| 49 |
|
| 50 |
/** namespace to store the main structure which appears in a Microsoft Word 3.0-5.0 file */ |
| 51 |
namespace MsWrdStruct |
| 52 |
{ |
| 53 |
//! generic function use to fill a border using the read data |
| 54 |
MWAWBorder getBorder(int val, std::string &extra); |
| 55 |
|
| 56 |
//! the font structure of a Microsoft Word file |
| 57 |
struct Font { |
| 58 |
enum { NumFlags /** the number of flags needed to store all datas*/=9 }; |
| 59 |
|
| 60 |
//! the constructor |
| 61 |
Font() |
| 62 |
: m_font(MWAWFont(-1,0)) |
| 63 |
, m_size(0) |
| 64 |
, m_value(0) |
| 65 |
, m_picturePos(0) |
| 66 |
, m_unknown(0) |
| 67 |
, m_extra("") |
| 68 |
{ |
| 69 |
for (auto &fl : m_flags) fl=MWAWVariable<int>(0); |
| 70 |
} |
| 71 |
Font(Font const &)=default; |
| 72 |
Font &operator=(Font const &)=default; |
| 73 |
//! insert new font data ( beginning by updating font flags ) |
| 74 |
void insert(Font const &font, Font const *styleFont=nullptr); |
| 75 |
|
| 76 |
//! update the font to obtain the final font |
| 77 |
void updateFontToFinalState(Font const *styleFont=nullptr); |
| 78 |
|
| 79 |
//! operator<< |
| 80 |
friend std::ostream &operator<<(std::ostream &o, Font const &font); |
| 81 |
|
| 82 |
//! a comparison function |
| 83 |
int cmp(Font const &oth) const |
| 84 |
{ |
| 85 |
int diff = m_font.get().cmp(oth.m_font.get()); |
| 86 |
if (diff) return diff; |
| 87 |
if (m_size.get() < oth.m_size.get()) return -1; |
| 88 |
if (m_size.get() > oth.m_size.get()) return 1; |
| 89 |
diff = m_value.get()-oth.m_value.get(); |
| 90 |
if (diff) return diff; |
| 91 |
for (int i = 0; i < NumFlags; i++) { |
| 92 |
diff = m_flags[i].get()-oth.m_flags[i].get(); |
| 93 |
if (diff) return diff; |
| 94 |
} |
| 95 |
if (m_picturePos.get()<oth.m_picturePos.get()) return -1; |
| 96 |
if (m_picturePos.get()>oth.m_picturePos.get()) return 1; |
| 97 |
diff = m_unknown.get()-oth.m_unknown.get(); |
| 98 |
if (diff) return diff; |
| 99 |
return 0; |
| 100 |
} |
| 101 |
//! the font |
| 102 |
MWAWVariable<MWAWFont> m_font; |
| 103 |
//! a second size |
| 104 |
MWAWVariable<float> m_size; |
| 105 |
//! a unknown value |
| 106 |
MWAWVariable<int> m_value; |
| 107 |
//! a list of flags |
| 108 |
MWAWVariable<int> m_flags[NumFlags]; |
| 109 |
//! a picture file position (if this corresponds to a picture) |
| 110 |
MWAWVariable<long> m_picturePos; |
| 111 |
//! some unknown flag |
| 112 |
MWAWVariable<int> m_unknown; |
| 113 |
//! extra data |
| 114 |
std::string m_extra; |
| 115 |
}; |
| 116 |
|
| 117 |
//! the section structure of a Microsoft Word file |
| 118 |
struct Section { |
| 119 |
//! constructor |
| 120 |
Section() |
| 121 |
: m_id(-1) |
| 122 |
, m_type(0) |
| 123 |
, m_paragraphId(-9999) |
| 124 |
, m_col(1) |
| 125 |
, m_colSep(0.5) |
| 126 |
, m_colBreak(false) |
| 127 |
, m_flag(0) |
| 128 |
, m_extra("") |
| 129 |
{ |
| 130 |
} |
| 131 |
//! returns a section |
| 132 |
MWAWSection getSection(double pageWidth) const; |
| 133 |
|
| 134 |
//! insert the new values |
| 135 |
void insert(Section const &sec) |
| 136 |
{ |
| 137 |
m_id.insert(sec.m_id); |
| 138 |
m_type.insert(sec.m_type); |
| 139 |
m_paragraphId.insert(sec.m_paragraphId); |
| 140 |
m_col.insert(sec.m_col); |
| 141 |
m_colSep.insert(sec.m_colSep); |
| 142 |
m_colBreak.insert(sec.m_colBreak); |
| 143 |
m_flag.insert(sec.m_flag); |
| 144 |
m_extra+=sec.m_extra; |
| 145 |
} |
| 146 |
//! try to read a data |
| 147 |
bool read(MWAWInputStreamPtr &input, long endPos); |
| 148 |
//! try to read a data ( v3 code ) |
| 149 |
bool readV3(MWAWInputStreamPtr &input, long endPos); |
| 150 |
|
| 151 |
//! operator<< |
| 152 |
friend std::ostream &operator<<(std::ostream &o, Section const §ion); |
| 153 |
|
| 154 |
//! the identificator |
| 155 |
MWAWVariable<int> m_id; |
| 156 |
//! the type |
| 157 |
MWAWVariable<int> m_type; |
| 158 |
//! the paragraph id |
| 159 |
MWAWVariable<int> m_paragraphId; |
| 160 |
//! the num of columns |
| 161 |
MWAWVariable<int> m_col; |
| 162 |
//! the spacing between column |
| 163 |
MWAWVariable<float> m_colSep; |
| 164 |
//! only a column break |
| 165 |
MWAWVariable<bool> m_colBreak; |
| 166 |
//! some flag ( in the main position) |
| 167 |
MWAWVariable<int> m_flag; |
| 168 |
/** the errors */ |
| 169 |
std::string m_extra; |
| 170 |
}; |
| 171 |
|
| 172 |
//! the table in a Microsoft Word file |
| 173 |
struct Table { |
| 174 |
struct Cell; |
| 175 |
//! constructor |
| 176 |
Table() |
| 177 |
: m_height(0) |
| 178 |
, m_justify(MWAWParagraph::JustificationLeft) |
| 179 |
, m_indent(0) |
| 180 |
, m_columns() |
| 181 |
, m_columnsWidthMod() |
| 182 |
, m_cells() |
| 183 |
, m_badCell() |
| 184 |
, m_extra("") |
| 185 |
{ |
| 186 |
} |
| 187 |
//! insert the new values |
| 188 |
void insert(Table const &table); |
| 189 |
//! try to read a data |
| 190 |
bool read(MWAWInputStreamPtr &input, long endPos); |
| 191 |
//! returns the ith Cell |
| 192 |
MWAWVariable<Cell> &getCell(int id); |
| 193 |
|
| 194 |
//! operator<< |
| 195 |
friend std::ostream &operator<<(std::ostream &o, Table const &table); |
| 196 |
|
| 197 |
//! the cells definitions in a Microsoft Word Table |
| 198 |
struct Cell { |
| 199 |
//! constructor |
| 200 |
Cell() |
| 201 |
: m_borders() |
| 202 |
, m_backColor(1.0f) |
| 203 |
, m_extra("") |
| 204 |
{ |
| 205 |
} |
| 206 |
//! update the cell data by merging |
| 207 |
void insert(Cell const &cell) |
| 208 |
{ |
| 209 |
size_t cNumBorders = cell.m_borders.size(); |
| 210 |
if (cNumBorders > m_borders.size()) |
| 211 |
m_borders.resize(cNumBorders); |
| 212 |
for (size_t i=0; i < cNumBorders; i++) |
| 213 |
if (cell.m_borders[i].isSet()) m_borders[i]=*cell.m_borders[i]; |
| 214 |
m_backColor.insert(cell.m_backColor); |
| 215 |
m_extra+=cell.m_extra; |
| 216 |
} |
| 217 |
//! returns true if the cell has borders |
| 218 |
bool hasBorders() const |
| 219 |
{ |
| 220 |
for (auto const &bd : m_borders) |
| 221 |
if (bd.isSet() && bd->m_style != MWAWBorder::None) |
| 222 |
return true; |
| 223 |
return false; |
| 224 |
} |
| 225 |
//! operator<< |
| 226 |
friend std::ostream &operator<<(std::ostream &o, Cell const &cell); |
| 227 |
/** the borders TLBR */ |
| 228 |
std::vector<MWAWVariable<MWAWBorder> > m_borders; |
| 229 |
/** the background gray color */ |
| 230 |
MWAWVariable<float> m_backColor; |
| 231 |
/** extra data */ |
| 232 |
std::string m_extra; |
| 233 |
}; |
| 234 |
|
| 235 |
//! the row height in inches |
| 236 |
MWAWVariable<float> m_height; |
| 237 |
//! the justification |
| 238 |
MWAWVariable<MWAWParagraph::Justification> m_justify; |
| 239 |
//! the indent |
| 240 |
MWAWVariable<float> m_indent; |
| 241 |
//! the table columns |
| 242 |
MWAWVariable<std::vector<float> > m_columns; |
| 243 |
//! the columns width modifier |
| 244 |
MWAWVariable<std::vector<float> > m_columnsWidthMod; |
| 245 |
//! the table cells |
| 246 |
std::vector<MWAWVariable<Cell> > m_cells; |
| 247 |
//! empty cell used by getCell to return unknown cell |
| 248 |
MWAWVariable<Cell> m_badCell; |
| 249 |
/** the errors */ |
| 250 |
std::string m_extra; |
| 251 |
}; |
| 252 |
|
| 253 |
//! the paragraph information of a Microsoft Word file (PHE) |
| 254 |
struct ParagraphInfo { |
| 255 |
//! constructor |
| 256 |
ParagraphInfo() |
| 257 |
: m_type(0) |
| 258 |
, m_dim() |
| 259 |
, m_numLines(-1) |
| 260 |
, m_error("") |
| 261 |
{ |
| 262 |
} |
| 263 |
//! returns true if num lines is set |
| 264 |
bool isLineSet() const |
| 265 |
{ |
| 266 |
return *m_numLines!=0; |
| 267 |
} |
| 268 |
//! returns true if no data are been set |
| 269 |
bool isEmpty() const |
| 270 |
{ |
| 271 |
if (*m_numLines || *m_type) return false; |
| 272 |
if (!m_dim.isSet()) return true; |
| 273 |
if ((*m_dim)[0] > 0 || (*m_dim)[1] > 0) return false; |
| 274 |
return true; |
| 275 |
} |
| 276 |
//! try to read a data |
| 277 |
bool read(MWAWInputStreamPtr &input, long endPos, int vers); |
| 278 |
//! operator<< |
| 279 |
friend std::ostream &operator<<(std::ostream &o, ParagraphInfo const &pInfo) |
| 280 |
{ |
| 281 |
// find also pInfo.m_type&0x40 : ? |
| 282 |
if (*pInfo.m_type&0xd0) o << "type?=" << ((*pInfo.m_type&0xd0)>>4) << ","; |
| 283 |
if (*pInfo.m_type&0x0f) o << "#unkn=" << (*pInfo.m_type&0xf) << ","; |
| 284 |
if (pInfo.m_dim.isSet()) { |
| 285 |
if ((*pInfo.m_dim)[0] > 0) |
| 286 |
o << "width=" << (*pInfo.m_dim)[0] << ","; |
| 287 |
if ((*pInfo.m_dim)[1] > 0) { |
| 288 |
o << "height=" << (*pInfo.m_dim)[1]; |
| 289 |
if (*pInfo.m_type&0x20) |
| 290 |
o << "[total]"; |
| 291 |
o << ","; |
| 292 |
} |
| 293 |
} |
| 294 |
if (pInfo.m_numLines.isSet() && *pInfo.m_numLines!=-1 && *pInfo.m_numLines!=1) |
| 295 |
o << "nLines=" << *pInfo.m_numLines << ","; |
| 296 |
if (pInfo.m_error.length()) o << pInfo.m_error << ","; |
| 297 |
return o; |
| 298 |
} |
| 299 |
//! insert the new values |
| 300 |
void insert(ParagraphInfo const &pInfo); |
| 301 |
//! the type |
| 302 |
MWAWVariable<int> m_type; |
| 303 |
//! the zone dimension |
| 304 |
MWAWVariable<MWAWVec2f> m_dim; |
| 305 |
//! the number of lines |
| 306 |
MWAWVariable<int> m_numLines; |
| 307 |
/** the errors */ |
| 308 |
std::string m_error; |
| 309 |
}; |
| 310 |
|
| 311 |
//! the paragraph structure of a Microsoft Word file |
| 312 |
struct Paragraph final : public MWAWParagraph { |
| 313 |
//! Constructor |
| 314 |
explicit Paragraph(int version) |
| 315 |
: MWAWParagraph() |
| 316 |
, m_version(version) |
| 317 |
, m_styleId(-1000) |
| 318 |
, m_interline(0) |
| 319 |
, m_deletedTabs() |
| 320 |
, m_info() |
| 321 |
, m_font() |
| 322 |
, m_modFont() |
| 323 |
, m_section() |
| 324 |
, m_bordersStyle() |
| 325 |
, m_inCell(false) |
| 326 |
, m_tableDef(false) |
| 327 |
, m_table() |
| 328 |
{ |
| 329 |
m_tabsRelativeToLeftMargin=false; |
| 330 |
} |
| 331 |
Paragraph(Paragraph const &)=default; |
| 332 |
Paragraph &operator=(Paragraph const &)=default; |
| 333 |
Paragraph &operator=(Paragraph &&)=default; |
| 334 |
//! destructor |
| 335 |
~Paragraph() final; |
| 336 |
//! insert the new values |
| 337 |
void insert(Paragraph const ¶, bool insertModif=true); |
| 338 |
//! try to read a data |
| 339 |
bool read(MWAWInputStreamPtr &input, long endPos); |
| 340 |
//! update the paragraph to obtain the final paragraph |
| 341 |
void updateParagraphToFinalState(Paragraph const *style=nullptr); |
| 342 |
//! returns the font which correspond to the paragraph if possible |
| 343 |
bool getFont(Font &font, Font const *styleFont=nullptr) const; |
| 344 |
//! returns true if we are in table |
| 345 |
bool inTable() const |
| 346 |
{ |
| 347 |
return m_inCell.get(); |
| 348 |
} |
| 349 |
//! operator<< |
| 350 |
friend std::ostream &operator<<(std::ostream &o, Paragraph const &ind); |
| 351 |
|
| 352 |
//! operator<< |
| 353 |
void print(std::ostream &o, MWAWFontConverterPtr converter) const; |
| 354 |
|
| 355 |
//! returns the number of line stored in m_info or -1 |
| 356 |
int getNumLines() const |
| 357 |
{ |
| 358 |
return m_info.get().m_numLines.get(); |
| 359 |
} |
| 360 |
//! the file version |
| 361 |
int m_version; |
| 362 |
//! the style id (if known) |
| 363 |
MWAWVariable<int> m_styleId; |
| 364 |
//! the interline if set |
| 365 |
MWAWVariable<double> m_interline; |
| 366 |
//! the delete tabulation |
| 367 |
std::vector<float> m_deletedTabs; |
| 368 |
//! the dimension |
| 369 |
MWAWVariable<ParagraphInfo> m_info; |
| 370 |
//! the font |
| 371 |
MWAWVariable<Font> m_font, m_modFont /** font (modifier) */; |
| 372 |
//! the section |
| 373 |
MWAWVariable<Section> m_section; |
| 374 |
//! the border style ( old v3) |
| 375 |
MWAWVariable<MWAWBorder> m_bordersStyle; |
| 376 |
//! a cell/textbox |
| 377 |
MWAWVariable<bool> m_inCell; |
| 378 |
//! a table flag |
| 379 |
MWAWVariable<bool> m_tableDef; |
| 380 |
//! the table |
| 381 |
MWAWVariable<Table> m_table; |
| 382 |
}; |
| 383 |
} |
| 384 |
#endif |
| 385 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |