]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
9a5bc37704aae39f73a324014e0586c9ce06e0d2
[lyx.git] / src / Paragraph.h
1 // -*- C++ -*-
2 /**
3  * \file Paragraph.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Jürgen Vigna
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #ifndef PARAGRAPH_H
17 #define PARAGRAPH_H
18
19 #include "lyxlayout_ptr_fwd.h"
20
21 #include "insets/InsetCode.h"
22
23 #include "support/docstring.h"
24 // FIXME: would be nice to forward declare odocstream instead of
25 // including this:
26 #include "support/docstream.h"
27
28 #include <vector>
29
30 namespace lyx {
31
32 class AuthorList;
33 class Buffer;
34 class BufferParams;
35 class Change;
36 class Counters;
37 class Inset;
38 class InsetBibitem;
39 class LaTeXFeatures;
40 class Inset_code;
41 class InsetList;
42 class Language;
43 class Font;
44 class Font_size;
45 class MetricsInfo;
46 class OutputParams;
47 class PainterInfo;
48 class ParagraphParameters;
49 class TexRow;
50
51
52 class FontSpan {
53 public:
54         /// Invalid font span containing no character
55         FontSpan() : first(0), last(-1) {}
56         /// Span including first and last
57         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
58
59 public:
60         /// Range including first and last.
61         pos_type first, last;
62 };
63
64
65 /// A Paragraph holds all text, attributes and insets in a text paragraph
66 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
67 /// should go in order to complete the Model/View separation of this class.
68 class Paragraph  {
69 public:
70         ///
71         enum {
72                 /// Note that this is 1 right now to avoid
73                 /// crashes where getChar() is called wrongly
74                 /// (returning 0) - if this was 0, then we'd
75                 /// try getInset() and crash. We should fix
76                 /// all these places.
77                 //META_INSET = 1 // as in trunk
78                 META_INSET = 0x200001  // above 0x10ffff, for ucs-4
79         };
80         ///
81         typedef char_type value_type;
82         ///
83         typedef std::vector<value_type> TextContainer;
84
85         ///
86         Paragraph();
87         ///
88         Paragraph(Paragraph const &);
89         ///
90         Paragraph & operator=(Paragraph const &);
91         ///
92         ~Paragraph();
93         ///
94         int id() const;
95
96
97         ///
98         Language const * getParLanguage(BufferParams const &) const;
99         ///
100         bool isRTL(BufferParams const &) const;
101         ///
102         void changeLanguage(BufferParams const & bparams,
103                             Language const * from, Language const * to);
104         ///
105         bool isMultiLingual(BufferParams const &) const;
106
107         ///
108         docstring const asString(Buffer const &, bool label) const;
109         ///
110         docstring const asString(Buffer const & buffer,
111                                    pos_type beg,
112                                    pos_type end,
113                                    bool label) const;
114
115         ///
116         void write(Buffer const &, std::ostream &, BufferParams const &,
117                    depth_type & depth) const;
118         ///
119         void validate(LaTeXFeatures &) const;
120
121         ///
122         int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
123                               bool) const;
124
125         ///
126         int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
127                             bool) const;
128
129
130         ///
131         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
132                              Font const & outerfont, odocstream &,
133                              TexRow & texrow, OutputParams const &) const;
134
135         /// Can we drop the standard paragraph wrapper?
136         bool emptyTag() const;
137
138         /// Get the id of the paragraph, usefull for docbook
139         std::string getID(Buffer const & buf,
140                           OutputParams const & runparams) const;
141
142         /// Get the first word of a paragraph, return the position where it left
143         pos_type getFirstWord(Buffer const & buf,
144                                    odocstream & os,
145                                    OutputParams const & runparams) const;
146
147         /// Checks if the paragraph contains only text and no inset or font change.
148         bool onlyText(Buffer const & buf, Font const & outerfont,
149                       pos_type initial) const;
150
151         /// Writes to stream the docbook representation
152         void simpleDocBookOnePar(Buffer const & buf,
153                                  odocstream &,
154                                  OutputParams const & runparams,
155                                  Font const & outerfont,
156                                  pos_type initial = 0) const;
157
158         ///
159         bool hasSameLayout(Paragraph const & par) const;
160
161         ///
162         void makeSameLayout(Paragraph const & par);
163
164         ///
165         void setInsetOwner(Inset * inset);
166         ///
167         Inset * inInset() const;
168         ///
169         InsetCode ownerCode() const;
170         ///
171         bool forceDefaultParagraphs() const;
172
173         ///
174         pos_type size() const { return text_.size(); }
175         ///
176         bool empty() const { return text_.empty(); }
177
178         ///
179         LayoutPtr const & layout() const;
180         ///
181         void layout(LayoutPtr const & new_layout);
182
183         /// This is the item depth, only used by enumerate and itemize
184         signed char itemdepth;
185
186         ///
187         InsetBibitem * bibitem() const;  // ale970302
188
189         /// look up change at given pos
190         Change const & lookupChange(pos_type pos) const;
191
192         /// is there a change within the given range ?
193         bool isChanged(pos_type start, pos_type end) const;
194         /// is there an unchanged char at the given pos ?
195         bool isUnchanged(pos_type pos) const;
196         /// is there an insertion at the given pos ?
197         bool isInserted(pos_type pos) const;
198         /// is there a deletion at the given pos ?
199         bool isDeleted(pos_type pos) const;
200
201         /// will the paragraph be physically merged with the next
202         /// one if the imaginary end-of-par character is logically deleted?
203         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
204
205         /// set change for the entire par
206         void setChange(Change const & change);
207
208         /// set change at given pos
209         void setChange(pos_type pos, Change const & change);
210
211         /// accept changes within the given range
212         void acceptChanges(BufferParams const & bparams, pos_type start, pos_type end);
213
214         /// reject changes within the given range
215         void rejectChanges(BufferParams const & bparams, pos_type start, pos_type end);
216
217         /// Paragraphs can contain "manual labels", for example, Description
218         /// environment. The text for this user-editable label is stored in
219         /// the paragraph alongside the text of the rest of the paragraph
220         /// (the body). This function returns the starting position of the
221         /// body of the text in the paragraph.
222         pos_type beginOfBody() const;
223         /// recompute this value
224         void setBeginOfBody();
225
226         ///
227         docstring const & getLabelstring() const;
228
229         /// the next two functions are for the manual labels
230         docstring const getLabelWidthString() const;
231         /// Set label width string.
232         void setLabelWidthString(docstring const & s);
233         /// translate \p label to the paragraph language if possible.
234         docstring const translateIfPossible(docstring const & label,
235                 BufferParams const & bparams) const;
236         /// Expand the counters for the labelstring of \c layout
237         docstring expandLabel(LayoutPtr const &, BufferParams const &,
238                 bool process_appendix = true) const;
239         /// Actual paragraph alignment used
240         char getAlign() const;
241         /// The nesting depth of a paragraph
242         depth_type getDepth() const;
243         /// The maximal possible depth of a paragraph after this one
244         depth_type getMaxDepthAfter() const;
245         ///
246         void applyLayout(LayoutPtr const & new_layout);
247
248         /// (logically) erase the char at pos; return true if it was actually erased
249         bool eraseChar(pos_type pos, bool trackChanges);
250         /// (logically) erase the given range; return the number of chars actually erased
251         int eraseChars(pos_type start, pos_type end, bool trackChanges);
252
253         /** Get uninstantiated font setting. Returns the difference
254             between the characters font and the layoutfont.
255             This is what is stored in the fonttable
256         */
257         Font const
258         getFontSettings(BufferParams const &, pos_type pos) const;
259         ///
260         Font const getFirstFontSettings(BufferParams const &) const;
261
262         /** Get fully instantiated font. If pos == -1, use the layout
263             font attached to this paragraph.
264             If pos == -2, use the label font of the layout attached here.
265             In all cases, the font is instantiated, i.e. does not have any
266             attributes with values Font::INHERIT, Font::IGNORE or
267             Font::TOGGLE.
268         */
269         Font const getFont(BufferParams const &, pos_type pos,
270                               Font const & outerfont) const;
271         Font const getLayoutFont(BufferParams const &,
272                                     Font const & outerfont) const;
273         Font const getLabelFont(BufferParams const &,
274                                    Font const & outerfont) const;
275         /**
276          * The font returned by the above functions is the same in a
277          * span of characters. This method will return the first and
278          * the last positions in the paragraph for which that font is
279          * the same. This can be used to avoid unnecessary calls to getFont.
280          */
281         FontSpan fontSpan(pos_type pos) const;
282         ///
283         /// this is a bottleneck.
284         value_type getChar(pos_type pos) const { return text_[pos]; }
285         /// Get the char, but mirror all bracket characters if it is right-to-left
286         value_type getUChar(BufferParams const &, pos_type pos) const;
287         /// pos <= size() (there is a dummy font change at the end of each par)
288         void setFont(pos_type pos, Font const & font);
289         /// Returns the height of the highest font in range
290         Font_size highestFontInRange(pos_type startpos,
291                                         pos_type endpos, Font_size def_size) const;
292         ///
293         void insert(pos_type pos, docstring const & str,
294                     Font const & font, Change const & change);
295         ///
296         void insertChar(pos_type pos, value_type c, bool trackChanges);
297         ///
298         void insertChar(pos_type pos, value_type c,
299                         Font const &, bool trackChanges);
300         ///
301         void insertChar(pos_type pos, value_type c,
302                         Font const &, Change const & change);
303         ///
304         void insertInset(pos_type pos, Inset * inset,
305                          Change const & change);
306         ///
307         void insertInset(pos_type pos, Inset * inset,
308                          Font const &, Change const & change);
309         ///
310         bool insetAllowed(InsetCode code);
311         ///
312         Inset * getInset(pos_type pos);
313         ///
314         Inset const * getInset(pos_type pos) const;
315
316         /// Release inset at given position.
317         /// \warning does not honour change tracking!
318         /// Therefore, it should only be used for breaking and merging
319         /// paragraphs
320         Inset * releaseInset(pos_type pos);
321
322         ///
323         InsetList const & insetList() const;
324
325         ///
326         bool isHfill(pos_type pos) const;
327
328         /// hinted by profiler
329         bool isInset(pos_type pos) const {
330                 return getChar(pos) == static_cast<value_type>(META_INSET);
331         }
332         ///
333         bool isNewline(pos_type pos) const;
334         /// return true if the char is a word separator
335         bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
336         ///
337         bool isLineSeparator(pos_type pos) const;
338         /// True if the character/inset at this point can be part of a word.
339         /// Note that digits in particular are considered as letters
340         bool isLetter(pos_type pos) const;
341
342         /// returns -1 if inset not found
343         int getPositionOfInset(Inset const * inset) const;
344
345         /// returns true if at least one line break or line separator has been deleted
346         /// at the beginning of the paragraph (either physically or logically)
347         bool stripLeadingSpaces(bool trackChanges);
348
349         /// return true if we allow multiple spaces
350         bool isFreeSpacing() const;
351
352         /// return true if we allow this par to stay empty
353         bool allowEmpty() const;
354         ///
355         char_type transformChar(char_type c, pos_type pos) const;
356         ///
357         ParagraphParameters & params();
358         ///
359         ParagraphParameters const & params() const;
360
361         /// Check if we are in a Biblio environment and insert or
362         /// delete InsetBibitems as necessary.
363         /// \retval int 1, if we had to add an inset, in which case
364         /// the cursor will need to move cursor forward; -pos, if we deleted
365         /// an inset, in which case pos is the position from which the inset
366         /// was deleted, and the cursor will need to be moved back one if it
367         /// was previously past that position. Return 0 otherwise.
368         int checkBiblio(bool track_changes);
369
370         /// For each author, set 'used' to true if there is a change
371         /// by this author in the paragraph.
372         void checkAuthors(AuthorList const & authorList);
373
374         /// return the number of InsetOptArg in a paragraph
375         int numberOfOptArgs() const;
376
377 private:
378         ///
379         LayoutPtr layout_;
380         /**
381          * Keeping this here instead of in the pimpl makes LyX >10% faster
382          * for average tasks as buffer loading/switching etc.
383          */
384         TextContainer text_;
385         /// end of label
386         pos_type begin_of_body_;
387
388         /// Pimpl away stuff
389         class Private;
390         ///
391         friend class Paragraph::Private;
392         ///
393         Private * d;
394 };
395
396 } // namespace lyx
397
398 #endif // PARAGRAPH_H