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 /*
35  * Class used to read main style by AppleWorks/ClarisWorks parser
36  *
37  */
38 #ifndef CLARIS_WKS_STYLE_MANAGER
39 #  define CLARIS_WKS_STYLE_MANAGER
40 
41 #include <iostream>
42 #include <string>
43 #include <vector>
44 
45 #include "libmwaw_internal.hxx"
46 
47 #include "MWAWCell.hxx"
48 #include "MWAWDebug.hxx"
49 #include "MWAWGraphicStyle.hxx"
50 #include "MWAWInputStream.hxx"
51 
52 class ClarisWksDocument;
53 
54 namespace ClarisWksStyleManagerInternal
55 {
56 struct State;
57 }
58 
59 //! a structure to store the style list and the lookup zone
60 class ClarisWksStyleManager
61 {
62 public:
63   struct CellFormat;
64   struct KSEN;
65   struct Style;
66 public:
67   //! constructor
68   ClarisWksStyleManager(MWAWParserStatePtr const &parserState, ClarisWksDocument *document=nullptr);
69   //! destructor
70   ~ClarisWksStyleManager();
71 
72   //! reads a color map zone ( v4-v6)
73   bool readColorList(MWAWEntry const &entry);
74   //! reads a pattern map zone ( v2)
75   bool readPatternList(long endPos=-1);
76   //! reads a gradient map zone ( v2)
77   bool readGradientList(long endPos=-1);
78   /** try to read the styles definition (in v4-6) */
79   bool readStyles(MWAWEntry const &entry);
80   /** read the font name style zone (method to store font names in v2/v3 files) */
81   bool readFontNames();
82   //! update a style using a gradiant id
83   bool updateGradient(int grad, MWAWGraphicStyle &style) const;
84   //! update a style using a wall paper id
85   bool updateWallPaper(int wall, MWAWGraphicStyle &style) const;
86 
87   //! return a mac font id corresponding to a local id
88   int getFontId(int localId) const;
89   //! return the color which corresponds to an id (if possible)
90   bool getColor(int id, MWAWColor &col) const;
91   //! return the pattern which corresponds to an id.
92   bool getPattern(int id, MWAWGraphicStyle::Pattern &pattern, float &percent) const;
93   //! return the name corresponding to a styleId
94   bool getRulerName(int id, std::string &name) const;
95 
96   //! return the style corresponding to a styleId
97   bool get(int styleId, Style &style) const;
98   //! return the font corresponding to a fontId
99   bool get(int fontId, MWAWFont &font) const;
100   //! return the cell format corresponding to a cellFormatId
101   bool get(int formatId, CellFormat &format) const;
102   //! return the ksen style corresponding to a ksenId
103   bool get(int ksenId, KSEN &ksen) const;
104   //! return the graphic style corresponding to a graphicId
105   bool get(int graphId, MWAWGraphicStyle &graph) const;
106 
107   //! try to read a named font
108   bool readFont(int id, int fontSize, MWAWFont &font);
109   //! try to read a font
110   bool readFontAndPos(int id, int &posC, MWAWFont &font);
111 
112 protected:
113   //! return the file version
114   int version() const;
115 
116   /** try to read a STYL_ subzone (in v4-6) */
117   bool readGenStyle(int id);
118 
119   //! try to read the style definition zone
120   bool readStylesDef(int N, int fSz);
121   //! try to read the lookup zone
122   bool readLookUp(int N, int fSz);
123 
124   /* read the STYL CELL sequence */
125   bool readCellStyles(int N, int fSz);
126   /** read the font name style zone (in the style zones) */
127   bool readFontNames(int N, int fSz);
128   /** read a GraphicStyle sequence */
129   bool readGraphStyles(int N, int fSz);
130   //! read a KSEN sequence
131   bool readKSEN(int N, int fSz);
132   /** read a STYL Name sequence */
133   bool readStyleNames(int N, int fSz);
134   /** read a STYL_CHAR Font sequence */
135   bool readStyleFonts(int N, int fSz);
136 
137 protected:
138   //! the document
139   ClarisWksDocument *m_document;
140   //! the parser state
141   MWAWParserStatePtr m_parserState;
142   //! the state
143   std::shared_ptr<ClarisWksStyleManagerInternal::State> m_state;
144 
145 private:
146   ClarisWksStyleManager(ClarisWksStyleManager const &orig) = delete;
147   ClarisWksStyleManager &operator=(ClarisWksStyleManager const &orig) = delete;
148 
149 public:
150   //! the CELL structure a structure related to number/date format
151   struct CellFormat final : public MWAWCell::Format {
152     //! constructor from cell
CellFormatClarisWksStyleManager::CellFormat153     explicit CellFormat(MWAWCell::Format const &format=MWAWCell::Format())
154       : MWAWCell::Format(format)
155       , m_hAlign(MWAWCell::HALIGN_DEFAULT)
156       , m_fileFormat(-1)
157       , m_borders(0)
158       , m_wrap(false)
159       , m_extra("")
160     {
161     }
162     CellFormat(CellFormat const &)=default;
163     CellFormat &operator=(CellFormat const &)=default;
164     CellFormat &operator=(CellFormat &&)=default;
165     //! destructor
166     ~CellFormat() final;
167     //! operator<<
168     friend std::ostream &operator<<(std::ostream &o, CellFormat const &form);
169     //! the cell alignment : by default nothing
170     MWAWCell::HorizontalAlignment m_hAlign;
171     //! the field format: number, string, currency, ..
172     int m_fileFormat;
173     //! the borders
174     int m_borders;
175     //! true if the cell content is wrapped
176     bool m_wrap;
177     //! extra data
178     std::string m_extra;
179   };
180 
181   //! the KSEN structure a structure related to paragraph and cell style
182   struct KSEN {
183     //! constructor
KSENClarisWksStyleManager::KSEN184     KSEN()
185       : m_valign(0)
186       , m_lineType(MWAWBorder::Simple)
187       , m_lineRepeat(MWAWBorder::Single)
188       , m_lines(0)
189       , m_extra("")
190     {
191     }
192     //! operator<<
193     friend std::ostream &operator<<(std::ostream &o, KSEN const &ksen);
194     //! the vertical alignment
195     int m_valign;
196     //! the line type
197     MWAWBorder::Style m_lineType;
198     //! the line repetition
199     MWAWBorder::Type m_lineRepeat;
200     //! an int used to add some oblique line ( or cross )
201     int m_lines;
202     //! extra data
203     std::string m_extra;
204   };
205 
206   //! the structure to store the style in a ClarisWksStyleManager
207   struct Style {
208     //! constructor
StyleClarisWksStyleManager::Style209     Style()
210       : m_fontId(-1)
211       , m_cellFormatId(-1)
212       , m_rulerId(-1)
213       , m_rulerPId(-1)
214       , m_nameId(-1)
215       , m_ksenId(-1), m_graphicId(-1)
216       , m_localStyleId(-1)
217       , m_styleId(-1)
218       , m_extra("")
219     {
220     }
221 
222     //! operator<<
223     friend std::ostream &operator<<(std::ostream &o, Style const &style);
224 
225     //! the char
226     int m_fontId;
227     //! the formatId
228     int m_cellFormatId;
229     //! the ruler
230     int m_rulerId;
231     //! the ruler parent id ( or maybe the style parent)
232     int m_rulerPId;
233     //! the style name id
234     int m_nameId;
235     //! the ksen id
236     int m_ksenId;
237     //! the graphic (checkme)
238     int m_graphicId;
239     //! a local style id
240     int m_localStyleId;
241     //! the style id
242     int m_styleId;
243     //! extra data
244     std::string m_extra;
245   };
246 };
247 
248 #endif
249