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