]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
change tracking:
[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 "changes.h"
20 #include "dimension.h"
21 #include "InsetList.h"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "RowList_fwd.h"
24
25 #include "insets/insetbase.h" // only for InsetBase::Code
26
27
28 namespace lyx {
29
30
31 class Buffer;
32 class BufferParams;
33 class BufferView;
34 class Counters;
35 class InsetBase;
36 class InsetBibitem;
37 class LaTeXFeatures;
38 class InsetBase_code;
39 class Language;
40 class LyXFont;
41 class LyXFont_size;
42 class MetricsInfo;
43 class OutputParams;
44 class PainterInfo;
45 class ParagraphParameters;
46 class TexRow;
47
48
49 class FontSpan {
50 public:
51         /// Invalid font span containing no character
52         FontSpan() : first(0), last(-1) {}
53         /// Span including first and last
54         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
55
56 public:
57         /// Range including first and last.
58         pos_type first, last;
59 };
60
61
62 /// A Paragraph holds all text, attributes and insets in a text paragraph
63 class Paragraph  {
64 public:
65         ///
66         enum {
67                 /// Note that this is 1 right now to avoid
68                 /// crashes where getChar() is called wrongly
69                 /// (returning 0) - if this was 0, then we'd
70                 /// try getInset() and crash. We should fix
71                 /// all these places.
72                 //META_INSET = 1 // as in trunk
73                 META_INSET = 0x200001  // above 0x10ffff, for ucs-4
74         };
75         ///
76         typedef char_type value_type;
77         ///
78         typedef std::vector<value_type> TextContainer;
79
80         ///
81         Paragraph();
82         ///
83         Paragraph(Paragraph const &);
84         ///
85         Paragraph & operator=(Paragraph const &);
86         ///
87         ~Paragraph();
88         ///
89         int id() const;
90
91
92         ///
93         Language const * getParLanguage(BufferParams const &) const;
94         ///
95         bool isRightToLeftPar(BufferParams const &) const;
96         ///
97         void changeLanguage(BufferParams const & bparams,
98                             Language const * from, Language const * to);
99         ///
100         bool isMultiLingual(BufferParams const &) const;
101
102         ///
103         docstring const asString(Buffer const &,
104                                    OutputParams const & runparams,
105                                    bool label) const;
106         ///
107         docstring const asString(Buffer const &, bool label) const;
108         ///
109         docstring const asString(Buffer const & buffer,
110                                    pos_type beg,
111                                    pos_type end,
112                                    bool label) const;
113         ///
114         docstring const asString(Buffer const &,
115                                    OutputParams const & runparams,
116                                    pos_type beg,
117                                    pos_type end,
118                                    bool label) const;
119
120         ///
121         void write(Buffer const &, std::ostream &, BufferParams const &,
122                    depth_type & depth) const;
123         ///
124         void validate(LaTeXFeatures &) const;
125
126         ///
127         int startTeXParParams(BufferParams const &, odocstream &, bool) const;
128
129         ///
130         int endTeXParParams(BufferParams const &, odocstream &, bool) const;
131
132
133         ///
134         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
135                              LyXFont const & outerfont, odocstream &,
136                              TexRow & texrow, OutputParams const &) const;
137
138         /// Can we drop the standard paragraph wrapper?
139         bool emptyTag() const;
140
141         /// Get the id of the paragraph, usefull for docbook
142         std::string getID(Buffer const & buf,
143                           OutputParams const & runparams) const;
144
145         // Get the first word of a paragraph, return the position where it left
146         pos_type getFirstWord(Buffer const & buf,
147                                    odocstream & os,
148                                    OutputParams const & runparams) const;
149
150         /// Checks if the paragraph contains only text and no inset or font change.
151         bool onlyText(Buffer const & buf, LyXFont const & outerfont,
152                       pos_type initial) const;
153
154         /// Writes to stream the docbook representation
155         void simpleDocBookOnePar(Buffer const & buf,
156                                  odocstream &,
157                                  OutputParams const & runparams,
158                                  LyXFont const & outerfont,
159                                  pos_type initial = 0) const;
160
161         ///
162         bool hasSameLayout(Paragraph const & par) const;
163
164         ///
165         void makeSameLayout(Paragraph const & par);
166
167         ///
168         void setInsetOwner(InsetBase * inset);
169         ///
170         InsetBase * inInset() const;
171         ///
172         InsetBase::Code ownerCode() const;
173         ///
174         bool forceDefaultParagraphs() const;
175
176         ///
177         pos_type size() const { return text_.size(); }
178         ///
179         bool empty() const { return text_.empty(); }
180         ///
181         void setContentsFromPar(Paragraph const & par);
182         ///
183         void clearContents();
184
185         ///
186         LyXLayout_ptr const & layout() const;
187         ///
188         void layout(LyXLayout_ptr const & new_layout);
189
190         /// This is the item depth, only used by enumerate and itemize
191         signed char itemdepth;
192
193         ///
194         InsetBibitem * bibitem() const;  // ale970302
195
196         /// look up change at given pos
197         Change const lookupChange(pos_type pos) const;
198
199         /// is there a change within the given range ?
200         bool isChanged(pos_type start, pos_type end) const;
201         /// is there an unchanged char at the given pos ?
202         bool isUnchanged(pos_type pos) const {
203                 return lookupChange(pos).type == Change::UNCHANGED;
204         }
205         /// is there an insertion at the given pos ?
206         bool isInserted(pos_type pos) const {
207                 return lookupChange(pos).type == Change::INSERTED;
208         }
209         /// is there a deletion at the given pos ?
210         bool isDeleted(pos_type pos) const {
211                 return lookupChange(pos).type == Change::DELETED;
212         }
213
214         /// set change for the entire par
215         void setChange(Change const & change);
216
217         /// set change at given pos
218         void setChange(pos_type pos, Change const & change);
219
220         /// accept changes within the given range
221         void acceptChanges(pos_type start, pos_type end);
222
223         /// reject change
224         void rejectChange(pos_type start, pos_type end);
225
226         /// Paragraphs can contain "manual labels", for example, Description
227         /// environment. The text for this user-editable label is stored in
228         /// the paragraph alongside the text of the rest of the paragraph
229         /// (the body). This function returns the starting position of the
230         /// body of the text in the paragraph.
231         pos_type beginOfBody() const;
232         /// recompute this value
233         void setBeginOfBody();
234
235         ///
236         docstring const & getLabelstring() const;
237
238         /// the next two functions are for the manual labels
239         docstring const getLabelWidthString() const;
240         ///
241         void setLabelWidthString(docstring const & s);
242         ///
243         char getAlign() const;
244         /// The nesting depth of a paragraph
245         depth_type getDepth() const;
246         /// The maximal possible depth of a paragraph after this one
247         depth_type getMaxDepthAfter() const;
248         ///
249         void applyLayout(LyXLayout_ptr const & new_layout);
250
251         /// (logically) erase the char at pos; return true if it was actually erased
252         bool eraseChar(pos_type pos, bool trackChanges);
253         /// (logically) erase the given range; return the number of chars actually erased
254         int eraseChars(pos_type start, pos_type end, bool trackChanges);
255
256         /** Get uninstantiated font setting. Returns the difference
257             between the characters font and the layoutfont.
258             This is what is stored in the fonttable
259         */
260         LyXFont const
261         getFontSettings(BufferParams const &, pos_type pos) const;
262         ///
263         LyXFont const getFirstFontSettings(BufferParams const &) const;
264
265         /** Get fully instantiated font. If pos == -1, use the layout
266             font attached to this paragraph.
267             If pos == -2, use the label font of the layout attached here.
268             In all cases, the font is instantiated, i.e. does not have any
269             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
270             LyXFont::TOGGLE.
271         */
272         LyXFont const getFont(BufferParams const &, pos_type pos,
273                               LyXFont const & outerfont) const;
274         LyXFont const getLayoutFont(BufferParams const &,
275                                     LyXFont const & outerfont) const;
276         LyXFont const getLabelFont(BufferParams const &,
277                                    LyXFont const & outerfont) const;
278         /**
279          * The font returned by the above functions is the same in a
280          * span of characters. This method will return the first and
281          * the last positions in the paragraph for which that font is
282          * the same. This can be used to avoid unnecessary calls to
283    * getFont.
284          */
285         FontSpan fontSpan(pos_type pos) const;
286         ///
287         /// this is a bottleneck.
288         value_type getChar(pos_type pos) const { return text_[pos]; }
289         /// Get the char, but mirror all bracket characters if it is right-to-left
290         value_type getUChar(BufferParams const &, pos_type pos) const;
291         /// pos <= size() (there is a dummy font change at the end of each par)
292         void setFont(pos_type pos, LyXFont const & font);
293         /// Returns the height of the highest font in range
294         LyXFont_size highestFontInRange(pos_type startpos,
295                                         pos_type endpos, LyXFont_size def_size) const;
296         ///
297         void insert(pos_type pos, docstring const & str,
298                     LyXFont const & font, Change const & change);
299         ///
300         void insertChar(pos_type pos, value_type c, bool trackChanges);
301         ///
302         void insertChar(pos_type pos, value_type c,
303                         LyXFont const &, bool trackChanges);
304         ///
305         void insertChar(pos_type pos, value_type c,
306                         LyXFont const &, Change const & change);
307         ///
308         void insertInset(pos_type pos, InsetBase * inset,
309                          Change const & change);
310         ///
311         void insertInset(pos_type pos, InsetBase * inset,
312                          LyXFont const &, Change const & change);
313         ///
314         bool insetAllowed(InsetBase_code code);
315         ///
316         InsetBase * getInset(pos_type pos) {
317                 return insetlist.get(pos);
318         }
319         ///
320         InsetBase const * getInset(pos_type pos) const {
321                 return insetlist.get(pos);
322         }
323
324         ///
325         bool isHfill(pos_type pos) const {
326                 return isInset(pos)
327                        && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
328         }
329         /// hinted by profiler
330         bool isInset(pos_type pos) const {
331                 return getChar(pos) == static_cast<value_type>(META_INSET);
332         }
333         ///
334         bool isNewline(pos_type pos) const;
335         /// return true if the char is a word separator
336         bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
337         ///
338         bool isLineSeparator(pos_type pos) const;
339         /// True if the character/inset at this point can be part of a word
340         // Note that digits in particular are considered as letters
341         bool isLetter(pos_type pos) const;
342
343         /// returns -1 if inset not found
344         int getPositionOfInset(InsetBase const * inset) const;
345
346         /// Returns the number of line breaks and white-space stripped at the start
347         int stripLeadingSpaces();
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         ///
362         Row & getRow(pos_type pos, bool boundary);
363         ///
364         Row const & getRow(pos_type pos, bool boundary) const;
365         ///
366         size_t pos2row(pos_type pos) const;
367
368         /// total height of paragraph
369         unsigned int height() const { return dim_.height(); }
370         /// total width of paragraph, may differ from workwidth
371         unsigned int width() const { return dim_.width(); }
372         /// ascend of paragraph above baseline
373         unsigned int ascent() const { return dim_.ascent(); }
374         /// descend of paragraph below baseline
375         unsigned int descent() const { return dim_.descent(); }
376         /// LyXText updates the rows using this access point
377         RowList & rows() { return rows_; }
378         /// The painter and others use this
379         RowList const & rows() const { return rows_; }
380         ///
381         RowSignature & rowSignature() const { return rowSignature_; }
382         ///
383         bool hfillExpansion(Row const & row, pos_type pos) const;
384
385         /// LyXText::redoParagraph updates this
386         Dimension & dim() { return dim_; }
387
388         /// dump some information to lyxerr
389         void dump() const;
390
391 public:
392         ///
393         InsetList insetlist;
394
395 private:
396         /// cached dimensions of paragraph
397         Dimension dim_;
398
399         ///
400         mutable RowList rows_;
401         ///
402         mutable RowSignature rowSignature_;
403
404         ///
405         LyXLayout_ptr layout_;
406         /**
407          * Keeping this here instead of in the pimpl makes LyX >10% faster
408          * for average tasks as buffer loading/switching etc.
409          */
410         TextContainer text_;
411         /// end of label
412         pos_type begin_of_body_;
413
414         /// Pimpl away stuff
415         class Pimpl;
416         ///
417         friend class Paragraph::Pimpl;
418         ///
419         Pimpl * pimpl_;
420 };
421
422
423 } // namespace lyx
424
425 #endif // PARAGRAPH_H