/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ /* libmwaw * Version: MPL 2.0 / LGPLv2+ * * The contents of this file are subject to the Mozilla Public License Version * 2.0 (the "License"); you may not use this file except in compliance with * the License or as specified alternatively below. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * Major Contributor(s): * Copyright (C) 2002 William Lachance (wrlach@gmail.com) * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net) * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) * Copyright (C) 2006, 2007 Andrew Ziem * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr) * * * All Rights Reserved. * * For minor contributions see the git repository. * * Alternatively, the contents of this file may be used under the terms of * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), * in which case the provisions of the LGPLv2+ are applicable * instead of those above. */ #include #include #include #include #include #include #include "MWAWCell.hxx" #include "MWAWTextListener.hxx" #include "MWAWFont.hxx" #include "MWAWFontConverter.hxx" #include "MWAWParagraph.hxx" #include "MWAWPictMac.hxx" #include "MWAWPosition.hxx" #include "MWAWSection.hxx" #include "MWAWTable.hxx" #include "WriteNowParser.hxx" #include "WriteNowEntry.hxx" #include "WriteNowText.hxx" /** Internal: the structures of a WriteNowText */ namespace WriteNowTextInternal { //////////////////////////////////////// //! Internal: the fonts struct Font { //! the constructor Font() : m_font() { for (auto &fl : m_flags) fl = 0; for (auto &id : m_styleId) id = -1; } //! operator<< friend std::ostream &operator<<(std::ostream &o, Font const &font) { for (int i = 0; i < 3; i++) { if (!font.m_flags[i]) continue; o << "ft" << i << "="; if (i == 0) o << std::hex; o << font.m_flags[i] << std::dec << ","; } if (font.m_styleId[0] >= 0) o << "id[charStyle]=" << font.m_styleId[0] << ","; if (font.m_styleId[1] >= 0) o << "id[rulerStyle]=" << font.m_styleId[1] << ","; return o; } //! the font MWAWFont m_font; //! the char/ruler style id int m_styleId[2]; //! some unknown flag int m_flags[3]; }; /** Internal: class to store the paragraph properties */ struct Paragraph final : public MWAWParagraph { //! constructor Paragraph() : MWAWParagraph() { m_marginsUnit = librevenge::RVNG_POINT; for (auto &val : m_values) val=0; } Paragraph(Paragraph const &)=default; Paragraph &operator=(Paragraph const &)=default; Paragraph &operator=(Paragraph &&)=default; //! destructor ~Paragraph() final; //! operator<< friend std::ostream &operator<<(std::ostream &o, Paragraph const &ind) { o << static_cast(ind); for (int i = 0; i < 8; i++) { if (!ind.m_values[i]) continue; o << "fR" << i << "=" << ind.m_values[i] << ","; } return o; } //! some unknown value int m_values[8]; }; Paragraph::~Paragraph() { } /** Internal: class to store a style */ struct Style { Style() : m_name(""), m_nextId(-1), m_font(), m_paragraph() { for (auto &val : m_values) val=0; for (auto &fl : m_flags) fl=0; } //! operator<< friend std::ostream &operator<<(std::ostream &o, Style const &st) { if (st.m_name.length()) o << st.m_name << ","; if (st.m_nextId >= 0) o << "nextId?=" << st.m_nextId << ","; for (int i = 0; i < 12; i++) { if (st.m_values[i]) o << "f" << i << "=" << st.m_values[i] << ","; } o << "flags=[" << std::hex; for (auto fl : st.m_flags) { if (fl) o << fl << ","; else o << "_,"; } o << "]," << std::dec; return o; } /** the style name */ std::string m_name; /** the next style ? */ int m_nextId; /** the font properties */ Font m_font; /** the paragraph properties */ Paragraph m_paragraph; /** some unknown value */ int m_values[12]; /** some unknown flags */ int m_flags[6]; }; //////////////////////////////////////// //! Internal: the token of a WriteNowText struct Token { Token() : m_graphicZone(-1) , m_box() , m_error("") { for (auto &val : m_values) val=0; } //! operator<< friend std::ostream &operator<<(std::ostream &o, Token const &tkn) { o << "bdbox=" << tkn.m_box << ","; if (tkn.m_graphicZone >= 0) o << "zId=" << tkn.m_graphicZone << ","; MWAWVec2i tknSize = tkn.m_box.size(); for (int i = 0; i < 2; i++) { if (tknSize == tkn.m_pos[i]) continue; o << "pos" << i << "=" << tkn.m_pos[i] << ","; } for (int i = 0; i < 19; i++) { if (!tkn.m_values[i]) continue; if (tkn.m_values[i]>0 && (tkn.m_values[i]&0xF000)) o << "f" << i << "=0x" << std::hex << tkn.m_values[i] << std::dec << ","; else o << "f" << i << "=" << tkn.m_values[i] << ","; } if (tkn.m_error.length()) o << tkn.m_error << ","; return o; } //! the graphic zone id int m_graphicZone; //! the bdbox MWAWBox2i m_box; //! two positions ( seen relative to the RB point) MWAWVec2i m_pos[2]; //! some unknown values int m_values[19]; /** the parsing errors */ std::string m_error; }; //////////////////////////////////////// //! Internal: the table of a WriteNowText struct TableData { //! constructor TableData() : m_type(-1) , m_box() , m_color(MWAWColor::white()) , m_error("") { for (auto &val : m_values) val=0; for (auto &fl : m_flags) fl=0; } //! update a cell void updateCell(MWAWCell &cell) const { // as the cells can overlap a little, we build a new box cell.setBdBox(MWAWBox2f(MWAWVec2f(m_box.min()), MWAWVec2f(m_box.max()-MWAWVec2i(1,1)))); cell.setBackgroundColor(m_color); for (int i=0; i<4; i++) { MWAWBorder border; switch (m_flags[i]&0x7f) { case 1: break; case 3: border.m_width=2; break; case 5: border.m_type=MWAWBorder::Double; break; case 0x11: border.m_style=MWAWBorder::Dot; break; // 21: ? case 0x61: border.m_width=0.5; break; case 0: default: border.m_width=0; break; } if (!border.isEmpty()) { static int const wh[4]= { libmwaw::TopBit, libmwaw::RightBit, libmwaw::BottomBit, libmwaw::LeftBit }; cell.setBorders(wh[i], border); } } } //! operator<< friend std::ostream &operator<<(std::ostream &o, TableData const &table) { switch (table.m_type) { case -1: break; case 0: o << "end,"; break; case 1: o << "def,"; break; case 2: o << "cell,"; break; default: o << "type=#" << table.m_type << ","; break; } if (table.m_box.size()[0] || table.m_box.size()[1]) o << table.m_box << ","; if (!table.m_color.isWhite()) o << "color=" << table.m_color << ","; for (int i = 0; i < 4; i++) { static char const *wh[4]= {"T", "R", "B", "L"}; if (table.m_flags[i]&0xFF00) o << "#bFlags" << wh[i] << "[high]=" << (table.m_flags[i]>>8) << ","; // flags&0x80 : duplicated? int fl=table.m_flags[i]&0x7F; if (fl==1) continue; // normal o << "bFlags" << wh[i] << "="; switch (fl) { case 0: o << "none,"; break; case 3: o << "w=2,"; break; case 5: o << "double,"; break; case 0x11: o << "dots,"; break; // 21: ? case 0x61: // in fact always 0xe1? o << "w=0.5,"; break; default: o << "#" << std::hex << fl << std::dec << ","; break; } } if (table.m_values[0]==1) o << "selected,"; else if (table.m_values[0]) o << "#selected=" << table.m_values[0] << ","; for (int i = 1; i < 10; i++) { if (!table.m_values[i]) continue; o << "f" << i << "=" << table.m_values[i] << ","; } if (table.m_error.length()) o << table.m_error << ","; return o; } //! the type int m_type; //! the bdbox MWAWBox2i m_box; //! the background color MWAWColor m_color; //! some unknown flags : T, R, B, L int m_flags[4]; //! some unknown values int m_values[10]; /** the parsing errors */ std::string m_error; }; //////////////////////////////////////// //! Internal: structure used to store the content structure struct ContentZone { ContentZone() : m_type(-1) , m_value(0) { for (auto &pos : m_pos) pos=-1; } //! operator<< friend std::ostream &operator<<(std::ostream &o, ContentZone const &z); /** the type 0 : text, 1<->7 : a char flag 8<->f : a zone 10 : used to store a break */ int m_type; //! the begin and the end pos long m_pos[2]; //! a value int m_value; }; std::ostream &operator<<(std::ostream &o, ContentZone const &z) { switch (z.m_type) { case 0: o << "text,"; break; case 3: o << "[hyphen],"; break; case 4: o << "[footnote],"; break; case 5: o << "[header],"; break; case 6: o << "[footer],"; break; case 9: if (z.m_value<0) o << "sub[fontMod],"; else if (z.m_value>0) o << "super[fontMod],"; else o << "normal[fontMod],"; break; case 0xa: { switch (z.m_value) { case 0: o << "table[end],"; break; case 1: o << "table[header],"; break; case 2: o << "table[zone],"; break; default: o << "table[#" << int(z.m_value>>4) << "#],"; break; } break; } case 0xb: o << "decimal[" << char(z.m_value) << "],"; break; case 0xc: o << "ruler,"; break; case 0xd: { switch (z.m_value) { case 0: o << "page[field],"; break; case 1: o << "date[field],"; break; case 2: o << "time[field],"; break; case 3: o << "note[field],"; break; default: o << "#field=" << z.m_value <<","; break; } break; } case 0xe: o << "token,"; break; case 0xf: o << "font,"; break; case 0x10: o << "break,"; break; default: o << "type=#" << z.m_type << ","; break; } return o; } //////////////////////////////////////// //! Internal: structure used to store the content structure struct ContentZones { ContentZones() : m_entry() , m_id(-1) , m_type(0) , m_zonesList() , m_textCalledTypesList() , m_footnoteList() , m_sent(false) { } /** returns true if the entry corresponds to a page/column break */ bool hasPageColumnBreak() const { return m_type == 0 && (m_entry.m_val[0] >> 4) == 7; } int getNumberOfZonesWithType(int type) const { int res = 0; for (auto const &zone : m_zonesList) if (zone.m_type == type) res++; return res; } //! the general zone WriteNowEntry m_entry; //! the zone id int m_id; //! the zone type : 0 : main, 1 header/footer, 2 footnote, int m_type; //! the list of zone std::vector m_zonesList; //! the list of type of text zone called by this zone std::vector m_textCalledTypesList; //! a list to retrieve the footnote content std::vector > m_footnoteList; //! true if this zone was sent to the listener mutable bool m_sent; }; //////////////////////////////////////// //! Internal: the cell of a WriteNowText struct Cell final : public MWAWCell { //! constructor explicit Cell(WriteNowText &parser) : MWAWCell() , m_parser(parser) , m_zonesList() , m_footnoteList() { } //! send the content bool sendContent(MWAWListenerPtr, MWAWTable &) final; //! the text parser WriteNowText &m_parser; //! the list of zone std::vector m_zonesList; //! a list to retrieve the footnote content std::vector > m_footnoteList; }; //////////////////////////////////////// //////////////////////////////////////// struct Table final : public MWAWTable { //! constructor Table() : MWAWTable() { } //! destructor ~Table() final; //! return a cell corresponding to id Cell *get(int id) { if (id < 0 || id >= numCells()) { MWAW_DEBUG_MSG(("WriteNowTextInternal::Table::get: cell %d does not exists\n",id)); return nullptr; } return static_cast(MWAWTable::get(id).get()); } }; Table::~Table() { } //////////////////////////////////////// //! Internal: structure used to store the content structure struct Zone { //! constructor Zone() : m_zones() { } //! a zones std::vector > m_zones; }; //////////////////////////////////////// //! Internal: the state of a WriteNowText struct State { //! constructor State() : m_version(-1) , m_numColumns(1) , m_numPages(1) , m_actualPage(1) , m_paragraph() , m_header() , m_footer() , m_localFIdMap() , m_styleMap() , m_styleList() , m_contentMap() { } //! return a paragraph corresponding to 0:body, 1: header/footer, 2: footnote Paragraph getDefaultParagraph(int type) const { int styleId = type == 0 ? 0 : type==1 ? 3 : type==2 ? 2 : -1; if (styleId >= 0 && styleId < int(m_styleList.size())) return m_styleList[size_t(styleId)].m_paragraph; Paragraph res; if (m_version>=0 && m_version <= 2 && type==0) { res.m_margins[1] = 90.0; res.m_margins[2] = 60.0-28.0; static double const defPos[2] = {1.5,5.5}; for (double i : defPos) { MWAWTabStop defTab; defTab.m_position=i; res.m_tabs->push_back(defTab); } } if (type==1) res.m_justify = MWAWParagraph::JustificationCenter; if (type==2) res.m_margins[0] = 10; return res; } //! return a mac font id corresponding to a local id int getFontId(int localId) const { if (m_localFIdMap.find(localId)==m_localFIdMap.end()) return localId; return m_localFIdMap.find(localId)->second; } //! return the content corresponding to a pos std::shared_ptr getContentZone(long pos) const { auto it = m_contentMap.find(pos); if (it == m_contentMap.end()) return std::shared_ptr(); return it->second; } //! the file version mutable int m_version; //! the number of column int m_numColumns; int m_numPages /* the number of pages */, m_actualPage /* the actual page */; /** the paragraph properties */ Paragraph m_paragraph; //! the header and the footer std::shared_ptr m_header, m_footer; //! a map local fontId->fontId std::map m_localFIdMap; //! the style indirection table std::map m_styleMap; //! the list of styles (body, footer, footnote, header, table ) std::vector