1 /* This file is part of the Calligra project
2 
3    Copyright (C) 2009 Benjamin Cail <cricketc@gmail.com>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the Library GNU General Public
7    version 2 of the License, or (at your option) version 3 or,
8    at the discretion of KDE e.V (which shall act as a proxy as in
9    section 14 of the GPLv3), any later version..
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public License
17    along with this library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef PARAGRAPH_H
23 #define PARAGRAPH_H
24 
25 #include <QBuffer>
26 #include <QList>
27 #include <QStack>
28 #include <KoGenStyle.h>
29 #include <KoGenStyles.h>
30 #include <KoXmlWriter.h>
31 
32 #include <wv2/src/styles.h>
33 #include <wv2/src/paragraphproperties.h>
34 #include <wv2/src/parser.h>
35 
36 class Paragraph
37 {
38 public:
39     explicit Paragraph(KoGenStyles* mainStyles, const QString& bgColor, bool inStylesDotXml = false,
40                        bool isHeading = false, bool inHeader = false, int outlineLevel = 0);
41     ~Paragraph();
42 
43     /**
44      * Write the paragraph content into the @writer.
45      *
46      * While applying the paragraph style, store the tab leader (leader-text)
47      * character into @tabLeader if requested by the calling handler.
48      *
49      * @return the name of the last KoGenStyle inserted into the styles
50      * collection.
51      */
52     QString writeToFile(KoXmlWriter* writer, bool openNewTextBox, QChar* tabLeader=0);
53 
54     void addRunOfText(QString text, wvWare::SharedPtr<const wvWare::Word97::CHP> chp, const QString &fontName,
55                       const wvWare::StyleSheet& styles, bool addCompleteElement=false);
56 
57     /**
58      * TODO:
59      */
60     void openInnerParagraph();
61 
62     /**
63      * TODO:
64      */
65     void closeInnerParagraph();
66 
67     /**
68      * TODO:
69      */
70     int strings() const;
71 
72     /**
73      * TODO:
74      */
75     QString string(int index) const;
76 
77     /**
78      * Set the paragraph properties (PAP) that apply to the paragraph.
79      */
80     void setParagraphProperties(wvWare::SharedPtr<const wvWare::ParagraphProperties> pap);
81 
82     /**
83      * Set the character properties (CHP) that apply to the paragraph.
84      *
85      * @param CHPs provided by wv2 for empty paragraphs to set proper
86      * font-size, line-height, etc. into text-properties.
87      */
setCharacterProperties(wvWare::SharedPtr<const wvWare::Word97::CHP> chp)88     void setCharacterProperties(wvWare::SharedPtr<const wvWare::Word97::CHP> chp) {
89         m_characterProperties = chp;
90     }
91 
92     /**
93      * Set the built-in (named) style that applies to the paragraph.
94      */
95     void setParagraphStyle(const wvWare::Style* paragraphStyle);
96 
97     /**
98      * @return the built-in (named) style that applies to the paragraph.
99      */
paragraphStyle()100     const wvWare::Style* paragraphStyle() const { return m_paragraphStyle; }
101 
102     /**
103      * @return the KoGenStyle of family Paragraph prepared for the current
104      * paragraph to other handlers.
105      */
koGenStyle()106     KoGenStyle* koGenStyle() const { return m_odfParagraphStyle; };
107 
108     /**
109      * @return true in case this paragraph is a heading
110      */
isHeading()111     bool isHeading() const { return m_isHeading; };
112 
113     // DropCaps related
114     typedef enum { NoDropCap, IsDropCapPara, HasDropCapIntegrated }  DropCapStatus;
115 
116     DropCapStatus dropCapStatus() const;
117     void getDropCapData(QString *string, int *type, int *lines, qreal *distance, QString *style) const;
118     void addDropCap(QString &string, int type, int lines, qreal distance, const QString &style);
119 
120     /**
121      * @return true in case the current paragraph contains a field of type in
122      * {PAGE, NUMPAGES}.
123      */
containsPageNumberField()124     bool containsPageNumberField() const { return m_containsPageNumberField; }
125 
126     /**
127      * Provide the information that a field of type in {PAGE, NUMPAGES} was
128      * detected in the current paragraph.
129      */
setContainsPageNumberField(bool containsPageNumberField)130     void setContainsPageNumberField(bool containsPageNumberField) {
131         m_containsPageNumberField = containsPageNumberField;
132     }
133 
134     /**
135      * Set the combined characters flag.
136      */
setCombinedCharacters(bool isCombined)137     void setCombinedCharacters(bool isCombined) {
138         m_combinedCharacters = isCombined;
139     }
140 
141     // Static functions which process wvWare properties and store them into
142     // corresponding properties of a KoGenStyle.
143     static void applyParagraphProperties(const wvWare::ParagraphProperties& properties,
144                                          KoGenStyle* style, const wvWare::Style* parentStyle,
145                                          bool setDefaultAlign, Paragraph *paragraph,
146                                          QChar* tabLeader = 0,
147                                          const QString& bgColor = QString());
148 
149     static void applyCharacterProperties(const wvWare::Word97::CHP* chp,
150                                          KoGenStyle* style, const wvWare::Style* parentStyle,
151                                          bool suppressFontSize = false, bool combineCharacters = false,
152                                          const QString& bgColor = QString());
153 
154     /**
155      * Add a color item to the background-color stack.
156      * @param color in the format "#RRGGBB"
157      */
pushBgColor(const QString & val)158     static void pushBgColor(const QString& val) { m_bgColors.push(val); }
159 
160     /**
161      * Remove the last item from the background-color stack.
162      */
163     static void popBgColor(void);
164 
165     /**
166      * Update the last item of the background-color stack.
167      * @param color in the format "#RRGGBB"
168      */
169     static void updateBgColor(const QString& val);
170 
171     /**
172      * @return the background color in the format "#RRGGBB" or an empty string.
173      */
currentBgColor(void)174     static QString currentBgColor(void) { return m_bgColors.isEmpty() ? QString() : m_bgColors.top(); }
175 
176     /**
177      * NOTE: DEPRECATED
178      * A special purpose method, which creates a KoGenStyle for a <text:span>
179      * element and inserts it into the styles collection.  Use this function if
180      * you have to create XML snippets.  In any other case use addRunOfText.
181      * @return the style name.
182      */
183     QString createTextStyle(wvWare::SharedPtr<const wvWare::Word97::CHP> chp, const wvWare::StyleSheet& styles);
184 
185 private:
186     wvWare::SharedPtr<const wvWare::ParagraphProperties> m_paragraphProperties;
187     wvWare::SharedPtr<const wvWare::ParagraphProperties> m_paragraphProperties2;
188     wvWare::SharedPtr<const wvWare::Word97::CHP> m_characterProperties;
189 
190     // ODF styles.  The MS equivalents are below.
191     KoGenStyle* m_odfParagraphStyle; //pointer to Calligra structure for paragraph formatting
192     KoGenStyle* m_odfParagraphStyle2; //place to store original style when we have an inner paragraph
193     KoGenStyles* m_mainStyles; //pointer to style collection for this document
194 
195     // MS Styles
196     const wvWare::Style* m_paragraphStyle;  // style for the paragraph
197     const wvWare::Style* m_paragraphStyle2; // style when in inner paragraph
198 
199     //std::vector<QString> m_textStrings; // list of text strings within a paragraph
200     //std::vector<QString> m_textStrings2; // original list when in inner paragraph
201     QList<QString> m_textStrings;  // list of text strings within a paragraph
202     QList<QString> m_textStrings2; // original list when in inner paragraph
203     QList<const KoGenStyle*> m_textStyles;  // list of styles for text within a paragraph
204     QList<const KoGenStyle*> m_textStyles2; // original list when in inner paragraph
205     std::vector<bool> m_addCompleteElement; // flags controlling if the paragraph should be processed
206     std::vector<bool> m_addCompleteElement2; // original list when in inner paragraph
207 
208     bool m_inStylesDotXml; //let us know if we're in content.xml or styles.xml
209     bool m_isHeading; //information for writing a heading instead of a paragraph
210     bool m_inHeaderFooter;
211 
212     int m_outlineLevel;
213 
214     DropCapStatus  m_dropCapStatus; // True if this paragraph has a dropcap
215     QString m_dropCapStyleName;
216     qreal m_dropCapDistance;
217     int m_dcs_fdct;
218     int m_dcs_lines;
219 
220     bool m_containsPageNumberField;
221     bool m_combinedCharacters;            // is true when the next characters are combined
222 
223     //A stack for background-colors, which represents a background color context
224     //for automatic colors.
225     static QStack<QString> m_bgColors;
226 
227 }; //end class Paragraph
228 #endif //PARAGRAPH_H
229