]> git.lyx.org Git - features.git/blob - src/paragraph.h
Use UTF8 for LaTeX export.
[features.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         /// definite erase
243         void eraseIntern(lyx::pos_type pos);
244         /// erase the char at the given position
245         bool erase(lyx::pos_type pos);
246         /// erase the given range. Returns the number of chars actually erased
247         int erase(lyx::pos_type start, lyx::pos_type end);
248
249         /** Get uninstantiated font setting. Returns the difference
250             between the characters font and the layoutfont.
251             This is what is stored in the fonttable
252         */
253         LyXFont const
254         getFontSettings(BufferParams const &, lyx::pos_type pos) const;
255         ///
256         LyXFont const getFirstFontSettings(BufferParams const &) const;
257
258         /** Get fully instantiated font. If pos == -1, use the layout
259             font attached to this paragraph.
260             If pos == -2, use the label font of the layout attached here.
261             In all cases, the font is instantiated, i.e. does not have any
262             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
263             LyXFont::TOGGLE.
264         */
265         LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
266                               LyXFont const & outerfont) const;
267         LyXFont const getLayoutFont(BufferParams const &,
268                                     LyXFont const & outerfont) const;
269         LyXFont const getLabelFont(BufferParams const &,
270                                    LyXFont const & outerfont) const;
271         /**
272          * The font returned by the above functions is the same in a
273          * span of characters. This method will return the first and
274          * the last positions in the paragraph for which that font is
275          * the same. This can be used to avoid unnecessary calls to
276    * getFont.
277          */
278         FontSpan fontSpan(lyx::pos_type pos) const;
279         ///
280         /// this is a bottleneck.
281         value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
282         /// Get the char, but mirror all bracket characters if it is right-to-left
283         value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
284         /// The position must already exist.
285         void setChar(lyx::pos_type pos, value_type c);
286         /// pos <= size() (there is a dummy font change at the end of each par)
287         void setFont(lyx::pos_type pos, LyXFont const & font);
288         /// Returns the height of the highest font in range
289         LyXFont_size highestFontInRange(lyx::pos_type startpos,
290                                         lyx::pos_type endpos,
291                                         LyXFont_size def_size) const;
292         ///
293         void insert(lyx::pos_type pos, std::string const & str,
294                     LyXFont const & font);
295         ///
296         void insertChar(lyx::pos_type pos, value_type c, Change const & change);
297         ///
298         void insertChar(lyx::pos_type pos, value_type c,
299                 LyXFont const &, Change const & change);
300         ///
301         void insertInset(lyx::pos_type pos, InsetBase * inset,
302                          Change const & change);
303         ///
304         void insertInset(lyx::pos_type pos, InsetBase * inset,
305                 LyXFont const &, Change const & change);
306         ///
307         bool insetAllowed(InsetBase_code code);
308         ///
309         InsetBase * getInset(lyx::pos_type pos) {
310                 return insetlist.get(pos);
311         }
312         ///
313         InsetBase const * getInset(lyx::pos_type pos) const {
314                 return insetlist.get(pos);
315         }
316
317         ///
318         bool isHfill(lyx::pos_type pos) const {
319                 return isInset(pos)
320                        && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
321         }
322         /// hinted by profiler
323         bool isInset(lyx::pos_type pos) const {
324                 return getChar(pos) == static_cast<value_type>(META_INSET);
325         }
326         ///
327         bool isNewline(lyx::pos_type pos) const;
328         /// return true if the char is a word separator
329         bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
330         ///
331         bool isLineSeparator(lyx::pos_type pos) const;
332         /// True if the character/inset at this point can be part of a word
333         // Note that digits in particular are considered as letters
334         bool isLetter(lyx::pos_type pos) const;
335
336         /// returns -1 if inset not found
337         int getPositionOfInset(InsetBase const * inset) const;
338
339         /// Returns the number of line breaks and white-space stripped at the start
340         int stripLeadingSpaces();
341
342         /// return true if we allow multiple spaces
343         bool isFreeSpacing() const;
344
345         /// return true if we allow this par to stay empty
346         bool allowEmpty() const;
347         ///
348         lyx::char_type transformChar(lyx::char_type c, lyx::pos_type pos) const;
349         ///
350         ParagraphParameters & params();
351         ///
352         ParagraphParameters const & params() const;
353
354         ///
355         Row & getRow(lyx::pos_type pos, bool boundary);
356         ///
357         Row const & getRow(lyx::pos_type pos, bool boundary) const;
358         ///
359         size_t pos2row(lyx::pos_type pos) const;
360
361         /// total height of paragraph
362         unsigned int height() const { return dim_.height(); }
363         /// total width of paragraph, may differ from workwidth
364         unsigned int width() const { return dim_.width(); }
365         /// ascend of paragraph above baseline
366         unsigned int ascent() const { return dim_.ascent(); }
367         /// descend of paragraph below baseline
368         unsigned int descent() const { return dim_.descent(); }
369         /// LyXText updates the rows using this access point
370         RowList & rows() { return rows_; }
371         /// The painter and others use this
372         RowList const & rows() const { return rows_; }
373         ///
374         RowSignature & rowSignature() const { return rowSignature_; }
375
376         /// LyXText::redoParagraph updates this
377         Dimension & dim() { return dim_; }
378
379         /// dump some information to lyxerr
380         void dump() const;
381
382 public:
383         ///
384         InsetList insetlist;
385
386 private:
387         /// cached dimensions of paragraph
388         Dimension dim_;
389
390         ///
391         mutable RowList rows_;
392         ///
393         mutable RowSignature rowSignature_;
394
395         ///
396         LyXLayout_ptr layout_;
397         /**
398          * Keeping this here instead of in the pimpl makes LyX >10% faster
399          * for average tasks as buffer loading/switching etc.
400          */
401         TextContainer text_;
402         /// end of label
403         lyx::pos_type begin_of_body_;
404
405         /// Pimpl away stuff
406         class Pimpl;
407         ///
408         friend class Paragraph::Pimpl;
409         ///
410         Pimpl * pimpl_;
411 };
412
413 #endif // PARAGRAPH_H