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