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