]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
a661950e4e6cbb5ed7b467bd0fe7cf9757f9f1eb
[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                                    std::ostream & 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                                  std::ostream &,
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);
293         ///
294         void insertChar(lyx::pos_type pos, value_type c, Change const & change);
295         ///
296         void insertChar(lyx::pos_type pos, value_type c,
297                 LyXFont const &, Change const & change);
298         ///
299         void insertInset(lyx::pos_type pos, InsetBase * inset,
300                          Change const & change);
301         ///
302         void insertInset(lyx::pos_type pos, InsetBase * inset,
303                 LyXFont const &, Change const & change);
304         ///
305         bool insetAllowed(InsetBase_code code);
306         ///
307         InsetBase * getInset(lyx::pos_type pos) {
308                 return insetlist.get(pos);
309         }
310         ///
311         InsetBase const * getInset(lyx::pos_type pos) const {
312                 return insetlist.get(pos);
313         }
314
315         ///
316         bool isHfill(lyx::pos_type pos) const {
317                 return isInset(pos)
318                        && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
319         }
320         /// hinted by profiler
321         bool isInset(lyx::pos_type pos) const {
322                 return getChar(pos) == static_cast<value_type>(META_INSET);
323         }
324         ///
325         bool isNewline(lyx::pos_type pos) const;
326         /// return true if the char is a word separator
327         bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
328         ///
329         bool isLineSeparator(lyx::pos_type pos) const;
330         /// True if the character/inset at this point can be part of a word
331         // Note that digits in particular are considered as letters
332         bool isLetter(lyx::pos_type pos) const;
333
334         /// returns -1 if inset not found
335         int getPositionOfInset(InsetBase const * inset) const;
336
337         /// Returns the number of line breaks and white-space stripped at the start
338         int stripLeadingSpaces();
339
340         /// return true if we allow multiple spaces
341         bool isFreeSpacing() const;
342
343         /// return true if we allow this par to stay empty
344         bool allowEmpty() const;
345         ///
346         lyx::char_type transformChar(lyx::char_type c, lyx::pos_type pos) const;
347         ///
348         ParagraphParameters & params();
349         ///
350         ParagraphParameters const & params() const;
351
352         ///
353         Row & getRow(lyx::pos_type pos, bool boundary);
354         ///
355         Row const & getRow(lyx::pos_type pos, bool boundary) const;
356         ///
357         size_t pos2row(lyx::pos_type pos) const;
358
359         /// total height of paragraph
360         unsigned int height() const { return dim_.height(); }
361         /// total width of paragraph, may differ from workwidth
362         unsigned int width() const { return dim_.width(); }
363         /// ascend of paragraph above baseline
364         unsigned int ascent() const { return dim_.ascent(); }
365         /// descend of paragraph below baseline
366         unsigned int descent() const { return dim_.descent(); }
367         /// LyXText updates the rows using this access point
368         RowList & rows() { return rows_; }
369         /// The painter and others use this
370         RowList const & rows() const { return rows_; }
371         ///
372         RowSignature & rowSignature() const { return rowSignature_; }
373
374         /// LyXText::redoParagraph updates this
375         Dimension & dim() { return dim_; }
376
377         /// dump some information to lyxerr
378         void dump() const;
379
380 public:
381         ///
382         InsetList insetlist;
383
384 private:
385         /// cached dimensions of paragraph
386         Dimension dim_;
387
388         ///
389         mutable RowList rows_;
390         ///
391         mutable RowSignature rowSignature_;
392
393         ///
394         LyXLayout_ptr layout_;
395         /**
396          * Keeping this here instead of in the pimpl makes LyX >10% faster
397          * for average tasks as buffer loading/switching etc.
398          */
399         TextContainer text_;
400         /// end of label
401         lyx::pos_type begin_of_body_;
402
403         /// Pimpl away stuff
404         class Pimpl;
405         ///
406         friend class Paragraph::Pimpl;
407         ///
408         Pimpl * pimpl_;
409 };
410
411 #endif // PARAGRAPH_H