| 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 |
#ifndef MS_WKS4_TEXT |
| 35 |
# define MS_WKS4_TEXT |
| 36 |
|
| 37 |
#include <vector> |
| 38 |
|
| 39 |
#include "MWAWEntry.hxx" |
| 40 |
#include "MWAWDebug.hxx" |
| 41 |
#include "MWAWInputStream.hxx" |
| 42 |
|
| 43 |
namespace MsWks4TextInternal |
| 44 |
{ |
| 45 |
struct Font; |
| 46 |
struct Paragraph; |
| 47 |
struct State; |
| 48 |
} |
| 49 |
|
| 50 |
class MsWksDocument; |
| 51 |
class MsWks4Zone; |
| 52 |
|
| 53 |
/** The class which parses text zones in a mac MS Works document v4 |
| 54 |
* |
| 55 |
* This class must be associated with a MsWks4Zone. It reads the entries: |
| 56 |
* - TEXT : the text strings |
| 57 |
* - FONT : the fonts name |
| 58 |
* - FDPC, BTEC : the fonts properties |
| 59 |
* - FDPP, BTEP : the paragraph properties |
| 60 |
* - FTNT : the footnote definition |
| 61 |
* - PGD : the page break (only parsed) |
| 62 |
* - TOKN : the field properties (pagenumber, date, ...) |
| 63 |
*/ |
| 64 |
class MsWks4Text |
| 65 |
{ |
| 66 |
friend class MsWks4Zone; |
| 67 |
protected: |
| 68 |
struct DataFOD; |
| 69 |
/** callback when a new attribute is found in an FDPP/FDPC entry |
| 70 |
* |
| 71 |
* \param input defines the input |
| 72 |
* \param endPos the last position in the input |
| 73 |
* \return true and filled id if this attribute can be parsed |
| 74 |
* \note mess can be filled to add a message in debugFile */ |
| 75 |
typedef bool (MsWks4Text::* FDPParser)(MWAWInputStreamPtr &input, long endPos, |
| 76 |
int &id, std::string &mess); |
| 77 |
public: |
| 78 |
//! constructor |
| 79 |
explicit MsWks4Text(MsWksDocument &document); |
| 80 |
|
| 81 |
//! destructor |
| 82 |
~MsWks4Text(); |
| 83 |
|
| 84 |
//! sets the default font |
| 85 |
void setDefault(MWAWFont const &font); |
| 86 |
|
| 87 |
//! returns the number of pages |
| 88 |
int numPages() const; |
| 89 |
|
| 90 |
//! sends the data which have not been sent: actually do nothing |
| 91 |
void flushExtra(MWAWInputStreamPtr /*input*/) {} |
| 92 |
|
| 93 |
protected: |
| 94 |
/** finds and parses all structures which correspond to the text |
| 95 |
* |
| 96 |
* More precisely the TEXT, FONT, FDPC/FDPP, BTEC/BTEP, FTNT, PGD, TOKN entries */ |
| 97 |
bool readStructures(MWAWInputStreamPtr input, bool mainOle); |
| 98 |
|
| 99 |
//! reads a text section and send it to the listener |
| 100 |
bool readText(MWAWInputStreamPtr input, MWAWEntry const &entry, bool mainOle); |
| 101 |
|
| 102 |
//! sends the text which corresponds to footnote \a id to the listner |
| 103 |
bool readFootNote(MWAWInputStreamPtr input, int id); |
| 104 |
|
| 105 |
//---------------------------------------- |
| 106 |
// PLC parsing, setting |
| 107 |
//---------------------------------------- |
| 108 |
/** definition of the plc data parser (low level) |
| 109 |
* |
| 110 |
* \param endPos the end of the properties' definition, |
| 111 |
* \param bot defined the begin of the text zone corresponding to these properties |
| 112 |
* \param eot defined the end of the text zone corresponding to these properties |
| 113 |
* \param id the number of this properties |
| 114 |
* \param mess a string which can be filled to indicate unparsed data */ |
| 115 |
typedef bool (MsWks4Text::* DataParser) |
| 116 |
(MWAWInputStreamPtr input, long endPos, long bot, long eot, int id, std::string &mess); |
| 117 |
|
| 118 |
/** reads a PLC (Pointer List Composant ?) in zone entry |
| 119 |
* |
| 120 |
* \param input the file's input |
| 121 |
* \param entry the zone which contains the plc |
| 122 |
* \param textPtrs lists of offset in text zones where properties changes |
| 123 |
* \param listValues lists of properties values (filled only if values are simple types: int, ..) |
| 124 |
* \param parser the parser to use to read the values */ |
| 125 |
bool readPLC(MWAWInputStreamPtr input, MWAWEntry const &entry, |
| 126 |
std::vector<long> &textPtrs, std::vector<long> &listValues, |
| 127 |
DataParser parser = &MsWks4Text::defDataParser); |
| 128 |
/** reads a PLC (Pointer List Composant ?) in zone entry |
| 129 |
* |
| 130 |
* \param input the file's input |
| 131 |
* \param entry the zone which contains the plc |
| 132 |
* \param textPtrs lists of offset in text zones where properties changes |
| 133 |
* \param listValues lists of properties values (filled only if values are simple types: int, ..) |
| 134 |
*/ |
| 135 |
bool readSimplePLC(MWAWInputStreamPtr &input, MWAWEntry const &entry, |
| 136 |
std::vector<long> &textPtrs, |
| 137 |
std::vector<long> &listValues) |
| 138 |
{ |
| 139 |
return readPLC(input, entry, textPtrs, listValues); |
| 140 |
} |
| 141 |
|
| 142 |
//! the default parser (does nothing) |
| 143 |
bool defDataParser(MWAWInputStreamPtr input, long endPos, |
| 144 |
long bot, long eot, int id, std::string &mess); |
| 145 |
|
| 146 |
//! reads the font names entry : FONT |
| 147 |
bool readFontNames(MWAWInputStreamPtr input, MWAWEntry const &entry); |
| 148 |
|
| 149 |
//! reads a font properties |
| 150 |
bool readFont(MWAWInputStreamPtr &input, long endPos, |
| 151 |
int &id, std::string &mess); |
| 152 |
|
| 153 |
/** sends a paragraph properties to the listener */ |
| 154 |
void setProperty(MsWks4TextInternal::Paragraph const &tabs); |
| 155 |
//! reads a paragraph properties |
| 156 |
bool readParagraph(MWAWInputStreamPtr &input, long endPos, |
| 157 |
int &id, std::string &mess); |
| 158 |
|
| 159 |
//! parses the footnote position : FTNT |
| 160 |
bool ftntDataParser(MWAWInputStreamPtr input, long endPos, |
| 161 |
long bot, long eot, int id, std::string &mess); |
| 162 |
|
| 163 |
//! parses the object position : EOBJ |
| 164 |
bool eobjDataParser(MWAWInputStreamPtr input, long endPos, |
| 165 |
long bot, long eot, int id, std::string &mess); |
| 166 |
|
| 167 |
/** parses the field properties entries : TOKN. |
| 168 |
* |
| 169 |
* \note the read data are not used to create the document */ |
| 170 |
bool toknDataParser(MWAWInputStreamPtr input, long endPos, |
| 171 |
long bot, long eot, int id, std::string &mess); |
| 172 |
|
| 173 |
/** parses the pagebreak positin entries : PGD |
| 174 |
* |
| 175 |
* \note the read data are not used to create the document */ |
| 176 |
bool pgdDataParser(MWAWInputStreamPtr input, long endPos, |
| 177 |
long, long, int id, std::string &mess); |
| 178 |
|
| 179 |
//! sends to the listener the text which corresponds to noteId |
| 180 |
void flushNote(int noteId); |
| 181 |
|
| 182 |
protected: |
| 183 |
/** function which takes two sorted list of attribute (by text position). |
| 184 |
\return a list of attribute */ |
| 185 |
std::vector<DataFOD> mergeSortedLists |
| 186 |
(std::vector<DataFOD> const &lst1, std::vector<DataFOD> const &lst2) const; |
| 187 |
|
| 188 |
/** parses a FDPP or a FDPC entry (which contains a list of ATTR_TEXT/ATTR_PARAG |
| 189 |
* with their definition ) and adds found data in listFODs */ |
| 190 |
bool readFDP(MWAWInputStreamPtr &input, MWAWEntry const &entry, |
| 191 |
std::vector<DataFOD> &fods, FDPParser parser); |
| 192 |
|
| 193 |
/** Fills the vector of (FDPCs/FDPPs) paragraph/characters strutures |
| 194 |
* |
| 195 |
* Uses the entry BTEC/BTEP : the normal ways, and calls readSimplePLC on each entry to check that the parsing is correct |
| 196 |
* \param input the file input |
| 197 |
* \param which set to 0 for paragraphs structures and to 1 for characters structures |
| 198 |
*/ |
| 199 |
bool findFDPStructures(MWAWInputStreamPtr &input, int which); |
| 200 |
/** Fills the vector of (FDPCs/FDPPs) paragraph/characters strutures, |
| 201 |
* a function to call when the normal ways fails |
| 202 |
* |
| 203 |
* Uses all entries FDPCs/FDPPs and calls readSimplePLC on each entry to check that the parsing is correct. |
| 204 |
* \param input the file input |
| 205 |
* \param which set to 0 for paragraphs structures and to 1 for characters structures |
| 206 |
*/ |
| 207 |
bool findFDPStructuresByHand(MWAWInputStreamPtr &input, int which); |
| 208 |
|
| 209 |
protected: |
| 210 |
//! structure which retrieves data information which correspond to a text position |
| 211 |
struct DataFOD { |
| 212 |
/** different type which can be associated to a text position |
| 213 |
* |
| 214 |
* ATTR_TEXT: all text attributes (font, size, ...) |
| 215 |
* ATTR_PARAG: all paragraph attributes (margin, tabs, ...) |
| 216 |
* ATTR_PLC: other attribute (note, fields ... ) |
| 217 |
*/ |
| 218 |
enum Type { ATTR_TEXT, ATTR_PARAG, ATTR_PLC, ATTR_UNKN }; |
| 219 |
|
| 220 |
//! the constructor |
| 221 |
DataFOD() |
| 222 |
: m_type(ATTR_UNKN) |
| 223 |
, m_pos(-1) |
| 224 |
, m_defPos(0) |
| 225 |
, m_id(-1) |
| 226 |
{ |
| 227 |
} |
| 228 |
|
| 229 |
//! the type of the attribute |
| 230 |
Type m_type; |
| 231 |
//! the offset position of the text modified by this attribute |
| 232 |
long m_pos; |
| 233 |
//! the offset position of the definition of the attribute in the file |
| 234 |
long m_defPos; |
| 235 |
//! an identificator (which must be unique by category) |
| 236 |
int m_id; |
| 237 |
}; |
| 238 |
|
| 239 |
private: |
| 240 |
MsWks4Text(MsWks4Text const &orig) = delete; |
| 241 |
MsWks4Text &operator=(MsWks4Text const &orig) = delete; |
| 242 |
protected: |
| 243 |
//! the main parser; |
| 244 |
MWAWParser *m_mainParser; |
| 245 |
//! the parser state |
| 246 |
MWAWParserStatePtr m_parserState; |
| 247 |
|
| 248 |
//! the main document |
| 249 |
MsWksDocument &m_document; |
| 250 |
|
| 251 |
//! an entry which corresponds to the complete text zone |
| 252 |
MWAWEntry m_textPositions; |
| 253 |
|
| 254 |
//! the internal state |
| 255 |
mutable std::shared_ptr<MsWks4TextInternal::State> m_state; |
| 256 |
|
| 257 |
//! the list of a FOD |
| 258 |
std::vector<DataFOD> m_FODsList; |
| 259 |
|
| 260 |
//! the list of FDPC entries |
| 261 |
std::vector<MWAWEntry const *> m_FDPCs; |
| 262 |
//! the list of FDPP entries |
| 263 |
std::vector<MWAWEntry const *> m_FDPPs; |
| 264 |
}; |
| 265 |
|
| 266 |
#endif |
| 267 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |