Coverage Report

Created: 2026-03-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmwaw/src/lib/RagTime5StyleManager.hxx
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
#ifndef RAG_TIME_5_STYLE_MANAGER
35
#  define RAG_TIME_5_STYLE_MANAGER
36
37
#include <map>
38
#include <ostream>
39
#include <sstream>
40
#include <set>
41
#include <string>
42
#include <vector>
43
44
#include "libmwaw_internal.hxx"
45
#include "MWAWDebug.hxx"
46
#include "MWAWEntry.hxx"
47
#include "MWAWGraphicStyle.hxx"
48
49
#include "RagTime5ClusterManager.hxx"
50
#include "RagTime5StructManager.hxx"
51
52
class MWAWCell;
53
54
namespace RagTime5StyleManagerInternal
55
{
56
struct State;
57
}
58
59
class RagTime5Document;
60
61
//! basic class used to read/store RagTime 5/6 styles
62
class RagTime5StyleManager
63
{
64
  friend class RagTime5Document;
65
public:
66
  //! constructor
67
  explicit RagTime5StyleManager(RagTime5Document &doc);
68
  //! destructor
69
  ~RagTime5StyleManager();
70
71
  struct GraphicStyle;
72
  struct TextStyle;
73
74
  //! try to read a graphic color zone
75
  bool readGraphicColors(RagTime5ClusterManager::Cluster &cluster);
76
  //! try to read a main graphic styles
77
  bool readGraphicStyles(RagTime5ClusterManager::Cluster &cluster);
78
  //! try to read a main text styles
79
  bool readTextStyles(RagTime5ClusterManager::Cluster &cluster);
80
  //! try to read the list of format
81
  bool readFormats(RagTime5ClusterManager::Cluster &cluster);
82
83
  //! updates a graphic style
84
  bool updateSurfaceStyle(int graphicId, MWAWGraphicStyle &surfaceStyle) const;
85
  //! updates a graphic style (used for textbox)
86
  bool updateFrameStyle(int graphicId, MWAWGraphicStyle &surfaceStyle) const;
87
  //! updates a graphic style(border)
88
  bool updateBorderStyle(int graphicId, MWAWGraphicStyle &borderStyle, bool isLine) const;
89
  //! returns the line color corresponding to a graphic style
90
  bool getLineColor(int graphicId, MWAWColor &color) const;
91
  //! update the font and the paragraph properties using a text style
92
  bool updateTextStyles(int textId, MWAWFont &font, MWAWParagraph &para, MWAWSection &section, double totalWidth=0) const;
93
  //! returns a cell border
94
  bool getCellBorder(int graphicId, MWAWBorder &border) const;
95
  //! returns the cell background
96
  bool getCellBackgroundColor(int graphicId, MWAWColor &color) const;
97
  //! updates the cell format
98
  bool updateCellFormat(int formatId, MWAWCell &cell) const;
99
100
protected:
101
  //! recursive function use to update the style list
102
  void updateTextStyles(size_t id, RagTime5StyleManager::TextStyle const &style,
103
                        std::vector<RagTime5StyleManager::TextStyle> const &listReadStyles,
104
                        std::multimap<size_t, size_t> const &idToChildIpMap,
105
                        std::set<size_t> &seens);
106
  //! recursive function use to update the style list
107
  void updateGraphicStyles(size_t id, RagTime5StyleManager::GraphicStyle const &style,
108
                           std::vector<RagTime5StyleManager::GraphicStyle> const &listReadStyles,
109
                           std::multimap<size_t, size_t> const &idToChildIpMap,
110
                           std::set<size_t> &seens);
111
112
public:
113
  //! the graphic style of a RagTime v5-v6 document
114
  struct GraphicStyle {
115
    //! constructor
116
    GraphicStyle()
117
380k
      : m_parentId(-1000)
118
380k
      , m_width(-1)
119
380k
      , m_dash()
120
380k
      , m_pattern()
121
380k
      , m_gradient(-1)
122
380k
      , m_gradientRotation(-1000)
123
380k
      , m_gradientCenter(MWAWVec2f(0.5f,0.5f))
124
380k
      , m_position(-1)
125
380k
      , m_cap(1)
126
380k
      , m_mitter(-1)
127
380k
      , m_limitPercent(-1)
128
380k
      , m_hidden(false)
129
380k
      , m_extra("")
130
380k
    {
131
380k
      m_colors[0]=MWAWVariable<MWAWColor>(MWAWColor::black());
132
380k
      m_colors[1]=MWAWVariable<MWAWColor>(MWAWColor::white());
133
761k
      for (auto &alpha : m_colorsAlpha) alpha=-1;
134
380k
    }
135
    //! destructor
136
    virtual ~GraphicStyle();
137
    //! returns true if the line style is default
138
    bool isDefault() const
139
0
    {
140
0
      return m_parentId<=-1000 && m_width<0 && !m_dash.isSet() && !m_pattern &&
141
0
             m_gradient<0 && m_gradientRotation<=-1000 && !m_gradientCenter.isSet() &&
142
0
             m_position<0 && m_cap<0 && m_mitter<0 &&
143
0
             !m_colors[0].isSet() && !m_colors[1].isSet() && m_colorsAlpha[0]<0 && m_colorsAlpha[1]<0 &&
144
0
             m_limitPercent<0 && !m_hidden.isSet() && m_extra.empty();
145
0
    }
146
    //! update the first/second color using the color list
147
    bool updateColor(bool first, int colorId, std::vector<MWAWColor> const &colorList);
148
    //! operator<<
149
    friend std::ostream &operator<<(std::ostream &o, GraphicStyle const &style);
150
    //! update the current style
151
    void insert(GraphicStyle const &childStyle);
152
    //! try to read a graphic style
153
    bool read(MWAWInputStreamPtr &input, RagTime5StructManager::Field const &field, std::vector<MWAWColor> const &colorList);
154
    //! the parent id
155
    int m_parentId;
156
    //! the line width (in point)
157
    float m_width;
158
    //! the first and second color
159
    MWAWVariable<MWAWColor> m_colors[2];
160
    //! alpha of the first and second color
161
    float m_colorsAlpha[2];
162
    //! the line dash/...
163
    MWAWVariable<std::vector<long> > m_dash;
164
    //! the line pattern
165
    std::shared_ptr<MWAWGraphicStyle::Pattern> m_pattern;
166
    //! the gradient 0: none, normal, radial
167
    int m_gradient;
168
    //! the gradient rotation(checkme)
169
    float m_gradientRotation;
170
    //! the rotation center(checkme)
171
    MWAWVariable<MWAWVec2f> m_gradientCenter;
172
    //! the line position inside=1/normal/outside/round
173
    int m_position;
174
    //! the line caps ( normal=1, round, square)
175
    int m_cap;
176
    //! the line mitter ( triangle=1, round, out)
177
    int m_mitter;
178
    //! the line limit
179
    float m_limitPercent;
180
    //! flag to know if we need to print the shape
181
    MWAWVariable<bool> m_hidden;
182
    //! extra data
183
    std::string m_extra;
184
185
187k
    GraphicStyle(GraphicStyle const &)=default; // removeme
186
    GraphicStyle(GraphicStyle &&)=default;
187
121k
    GraphicStyle &operator=(GraphicStyle const &)=default; // removeme
188
    GraphicStyle &operator=(GraphicStyle &&)=default;
189
  };
190
  //! the text style of a RagTime v5-v6 document
191
  struct TextStyle {
192
    //! constructor
193
    TextStyle()
194
1.26M
      : m_linkIdList()
195
1.26M
      , m_dateStyleId(-1)
196
1.26M
      , m_graphStyleId(-1)
197
1.26M
      , m_graphLineStyleId(-1)
198
1.26M
      , m_keepWithNext(false)
199
1.26M
      , m_justify(-1)
200
1.26M
      , m_breakMethod(-1)
201
1.26M
      , m_tabList()
202
1.26M
      , m_fontName("")
203
1.26M
      , m_fontId(-1)
204
1.26M
      , m_fontSize(-1)
205
1.26M
      , m_scriptPosition(0)
206
1.26M
      , m_fontScaling(-1)
207
1.26M
      , m_underline(-1)
208
1.26M
      , m_caps(-1)
209
1.26M
      , m_language(-1)
210
1.26M
      , m_widthStreching(-1)
211
1.26M
      , m_numColumns(-1)
212
1.26M
      , m_columnGap(-1)
213
1.26M
      , m_extra("")
214
1.26M
    {
215
2.52M
      for (auto &parentId : m_parentId) parentId=-1;
216
2.52M
      for (auto &fontFlag : m_fontFlags) fontFlag=0;
217
3.79M
      for (auto &margin : m_margins) margin=-1;
218
3.79M
      for (auto &spacing : m_spacings) spacing=-1;
219
3.79M
      for (auto &spacingUnit : m_spacingUnits) spacingUnit=-1;
220
5.05M
      for (auto &letterSpacing : m_letterSpacings) letterSpacing=0;
221
1.26M
    }
222
    //! destructor
223
    virtual ~TextStyle();
224
    //! returns true if the line style is default
225
    bool isDefault() const
226
0
    {
227
0
      if (m_parentId[0]>=0 || m_parentId[1]>=0 || !m_linkIdList.empty() ||
228
0
          m_dateStyleId>=0 || m_graphStyleId>=0 || m_graphLineStyleId>=0 ||
229
0
          m_keepWithNext.isSet() || m_justify>=0 || m_breakMethod>=0 || !m_tabList.empty() ||
230
0
          !m_fontName.empty() || m_fontId>=0 || m_fontSize>=0 || m_fontFlags[0] || m_fontFlags[1] || m_scriptPosition.isSet() ||
231
0
          m_fontScaling>=0 || m_underline>=0 || m_caps>=0 || m_language>=0 || m_widthStreching>=0 ||
232
0
          m_numColumns>=0 || m_columnGap>=0 || !m_extra.empty())
233
0
        return false;
234
0
      for (int i=0; i<3; ++i) {
235
0
        if (m_margins[i]>=0 || m_spacings[i]>=0 || m_spacingUnits[i]>=0)
236
0
          return false;
237
0
      }
238
0
      for (auto spacing : m_letterSpacings) {
239
0
        if (spacing>0 || spacing<0)
240
0
          return false;
241
0
      }
242
0
      return true;
243
0
    }
244
    //! returns the language locale name corresponding to an id ( if known)
245
    static std::string getLanguageLocale(int id);
246
247
    //! operator<<
248
    friend std::ostream &operator<<(std::ostream &o, TextStyle const &style);
249
    //! update the current style
250
    void insert(TextStyle const &childStyle);
251
    //! try to read a text style
252
    bool read(RagTime5StructManager::Field const &field);
253
    //! the parent id ( main and style ?)
254
    int m_parentId[2];
255
    //! the link id list
256
    std::vector<int> m_linkIdList;
257
    //! the date style id
258
    int m_dateStyleId;
259
    //! the graphic style id
260
    int m_graphStyleId;
261
    //! the graphic line style id
262
    int m_graphLineStyleId;
263
264
    // paragraph
265
266
    //! the keep with next flag
267
    MWAWVariable<bool> m_keepWithNext;
268
    //! justify 0: left, 1:center, 2:right, 3:full, 4:full all
269
    int m_justify;
270
    //! the interline/before/after value
271
    double m_spacings[3];
272
    //! the interline/before/after unit 0: line, 1:point
273
    int m_spacingUnits[3];
274
    //! the break method 0: asIs, next container, next page, next even page, next odd page
275
    int m_breakMethod;
276
    //! the spacings in point ( left, right, first)
277
    double m_margins[3];
278
    //! the tabulations
279
    std::vector<RagTime5StructManager::TabStop> m_tabList;
280
281
    // character
282
283
    //! the font name
284
    librevenge::RVNGString m_fontName;
285
    //! the font id
286
    int m_fontId;
287
    //! the font size
288
    float m_fontSize;
289
    //! the font flags (add and remove )
290
    uint32_t m_fontFlags[2];
291
    //! the font script position ( in percent)
292
    MWAWVariable<float> m_scriptPosition;
293
    //! the font script position ( in percent)
294
    float m_fontScaling;
295
    //! underline : none, single, double
296
    int m_underline;
297
    //! caps : none, all caps, lower caps, inital caps + other lowers
298
    int m_caps;
299
    //! the language
300
    int m_language;
301
    //! the spacings in percent ( normal, minimum, maximum)
302
    double m_letterSpacings[4];
303
    //! the width streching
304
    double m_widthStreching;
305
306
    // column
307
308
    //! the number of columns
309
    int m_numColumns;
310
    //! the gap between columns
311
    double m_columnGap;
312
313
    //! extra data
314
    std::string m_extra;
315
316
1.41M
    TextStyle(TextStyle const &)=default; // removeme
317
    TextStyle(TextStyle &&)=default;
318
571k
    TextStyle &operator=(TextStyle const &)=default; // removeme
319
    TextStyle &operator=(TextStyle &&)=default;
320
  };
321
322
public:
323
  //! debug: print a file type
324
  static std::string printType(unsigned long fileType)
325
60.5k
  {
326
60.5k
    return RagTime5StructManager::printType(fileType);
327
60.5k
  }
328
329
protected:
330
  //
331
  // data
332
  //
333
334
  //! the parser
335
  RagTime5Document &m_document;
336
  //! the parser state
337
  MWAWParserStatePtr m_parserState;
338
339
  //! the state
340
  std::shared_ptr<RagTime5StyleManagerInternal::State> m_state;
341
342
private:
343
  RagTime5StyleManager(RagTime5StyleManager const &orig) = delete;
344
  RagTime5StyleManager operator=(RagTime5StyleManager const &orig) = delete;
345
};
346
347
#endif
348
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: