]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
reduce number of calls to LyXText::getFont
[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 <boost/assert.hpp>
30
31 #include <string>
32 #include <utility>
33
34 class Buffer;
35 class BufferParams;
36 class BufferView;
37 class Counters;
38 class InsetBase;
39 class InsetBibitem;
40 class LaTeXFeatures;
41 class InsetBase_code;
42 class Language;
43 class LyXFont;
44 class LyXFont_size;
45 class MetricsInfo;
46 class OutputParams;
47 class PainterInfo;
48 class ParagraphParameters;
49 class TexRow;
50 class UpdatableInset;
51
52 /// A Paragraph holds all text, attributes and insets in a text paragraph
53 class Paragraph  {
54 public:
55         ///
56         enum {
57                 /// Note that this is 1 right now to avoid
58                 /// crashes where getChar() is called wrongly
59                 /// (returning 0) - if this was 0, then we'd
60                 /// try getInset() and crash. We should fix
61                 /// all these places.
62                 META_INSET = 1
63         };
64         ///
65         typedef char value_type;
66         ///
67         typedef lyx::depth_type depth_type;
68         ///
69         typedef std::vector<value_type> TextContainer;
70
71         ///
72         Paragraph();
73         ///
74         Paragraph(Paragraph const &);
75         ///
76         Paragraph & operator=(Paragraph const &);
77         ///
78         ~Paragraph();
79
80         ///
81         int id() const;
82
83
84         ///
85         Language const * getParLanguage(BufferParams const &) const;
86         ///
87         bool isRightToLeftPar(BufferParams const &) const;
88         ///
89         void changeLanguage(BufferParams const & bparams,
90                             Language const * from, Language const * to);
91         ///
92         bool isMultiLingual(BufferParams const &) const;
93
94         ///
95         std::string const asString(Buffer const &,
96                                    OutputParams const & runparams,
97                                    bool label) const;
98         ///
99         std::string const asString(Buffer const &, bool label) const;
100         ///
101         std::string const Paragraph::asString(Buffer const & buffer,
102                                               lyx::pos_type beg,
103                                               lyx::pos_type end,
104                                               bool label) const;
105         ///
106         std::string const asString(Buffer const &,
107                                    OutputParams const & runparams,
108                                    lyx::pos_type beg,
109                                    lyx::pos_type end,
110                                    bool label) const;
111
112         ///
113         void write(Buffer const &, std::ostream &, BufferParams const &,
114                    depth_type & depth) const;
115         ///
116         void validate(LaTeXFeatures &) const;
117
118         ///
119         int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
120
121         ///
122         int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
123
124
125         ///
126         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
127                              LyXFont const & outerfont, std::ostream &,
128                              TexRow & texrow, OutputParams const &) const;
129
130         /// Writes to stream the content of the paragraph for linuxdoc
131         void simpleLinuxDocOnePar(Buffer const & buf,
132                                   std::ostream & os,
133                                   LyXFont const & outerfont,
134                                   OutputParams const & runparams,
135                                   lyx::depth_type depth) 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 and linuxdoc
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(UpdatableInset * inset);
168         ///
169         UpdatableInset * 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         /// initialise tracking for this par
196         void trackChanges(Change::Type = Change::UNCHANGED);
197
198         /// stop tracking
199         void untrackChanges();
200
201         /// set entire paragraph to new text for change tracking
202         void cleanChanges();
203
204         /// look up change type at given pos
205         Change::Type lookupChange(lyx::pos_type pos) const;
206
207         /// look up change at given pos
208         Change const lookupChangeFull(lyx::pos_type pos) const;
209
210         /// is there a change within the given range ?
211         bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
212
213         /// is there a non-addition in this range ?
214         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
215
216         /// set change at pos
217         void setChange(lyx::pos_type pos, Change::Type type);
218
219         /// accept change
220         void acceptChange(lyx::pos_type start, lyx::pos_type end);
221
222         /// reject change
223         void rejectChange(lyx::pos_type start, lyx::pos_type end);
224
225         /// mark whole par as erased
226         void markErased();
227
228         /// Paragraphs can contain "manual labels", for example, Description
229         /// environment. The text for this user-editable label is stored in
230         /// the paragraph alongside the text of the rest of the paragraph
231         /// (the body). This function returns the starting position of the
232         /// body of the text in the paragraph.
233         lyx::pos_type beginOfBody() const;
234         /// recompute this value
235         void setBeginOfBody();
236
237         ///
238         std::string const & getLabelstring() const;
239
240         /// the next two functions are for the manual labels
241         std::string const getLabelWidthString() const;
242         ///
243         void setLabelWidthString(std::string const & s);
244         ///
245         char getAlign() const;
246         /// The nesting depth of a paragraph
247         depth_type getDepth() const;
248         /// The maximal possible depth of a paragraph after this one
249         depth_type getMaxDepthAfter() const;
250         ///
251         void applyLayout(LyXLayout_ptr const & new_layout);
252
253         /// definite erase
254         void eraseIntern(lyx::pos_type pos);
255         /// erase the char at the given position
256         bool erase(lyx::pos_type pos);
257         /// erase the given range. Returns the number of chars actually erased
258         int erase(lyx::pos_type start, lyx::pos_type end);
259
260         /** Get uninstantiated font setting. Returns the difference
261             between the characters font and the layoutfont.
262             This is what is stored in the fonttable
263         */
264         LyXFont const
265         getFontSettings(BufferParams const &, lyx::pos_type pos) const;
266         ///
267         LyXFont const getFirstFontSettings() const;
268
269         /** Get fully instantiated font. If pos == -1, use the layout
270             font attached to this paragraph.
271             If pos == -2, use the label font of the layout attached here.
272             In all cases, the font is instantiated, i.e. does not have any
273             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
274             LyXFont::TOGGLE.
275         */
276         LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
277                               LyXFont const & outerfont) const;
278         LyXFont const getLayoutFont(BufferParams const &,
279                                     LyXFont const & outerfont) const;
280         LyXFont const getLabelFont(BufferParams const &,
281                                    LyXFont const & outerfont) const;
282         /**
283          * The font returned by the above functions is the same in a
284          * span of characters. This method will return the first and
285          * the last last positions in the paragraph for which that
286          * font is the same. This can be used to avoid unnecessary
287          * calls to getFont.
288          */
289         std::pair<lyx::pos_type, lyx::pos_type> getFontSpan(lyx::pos_type pos) const;
290         ///
291         /// this is a bottleneck.
292         value_type getChar(lyx::pos_type pos) const
293         {
294                 BOOST_ASSERT(pos >= 0);
295                 BOOST_ASSERT(pos < int(text_.size()));
296                 return text_[pos];
297         }
298         /// Get the char, but mirror all bracket characters if it is right-to-left
299         value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
300         /// The position must already exist.
301         void setChar(lyx::pos_type pos, value_type c);
302         /// pos <= size() (there is a dummy font change at the end of each par)
303         void setFont(lyx::pos_type pos, LyXFont const & font);
304         /// Returns the height of the highest font in range
305         LyXFont_size highestFontInRange(lyx::pos_type startpos,
306                                         lyx::pos_type endpos,
307                                         LyXFont_size def_size) const;
308         ///
309         void insert(lyx::pos_type pos, std::string const & str,
310                     LyXFont const & font);
311         ///
312         void insertChar(lyx::pos_type pos, value_type c,
313                         Change change = Change(Change::INSERTED));
314         ///
315         void insertChar(lyx::pos_type pos, value_type c,
316                 LyXFont const &, Change change = Change(Change::INSERTED));
317         ///
318         bool checkInsertChar(LyXFont &);
319         ///
320         void insertInset(lyx::pos_type pos, InsetBase * inset,
321                          Change change = Change(Change::INSERTED));
322         ///
323         void insertInset(lyx::pos_type pos, InsetBase * inset,
324                 LyXFont const &, Change change = Change(Change::INSERTED));
325         ///
326         bool insetAllowed(InsetBase_code code);
327         ///
328         InsetBase * getInset(lyx::pos_type pos);
329         ///
330         InsetBase const * getInset(lyx::pos_type pos) const;
331         ///
332         InsetList insetlist;
333
334
335         ///
336         bool isHfill(lyx::pos_type pos) const;
337         /// hinted by profiler
338         bool isInset(lyx::pos_type pos) const { return getChar(pos) == static_cast<value_type>(META_INSET); }
339         ///
340         bool isNewline(lyx::pos_type pos) const;
341         ///
342         bool isSeparator(lyx::pos_type pos) const;
343         ///
344         bool isLineSeparator(lyx::pos_type pos) const;
345         /// True if the character/inset at this point can be part of a word
346         // Note that digits in particular are considered as letters
347         bool isLetter(lyx::pos_type pos) const;
348
349         /// returns -1 if inset not found
350         int getPositionOfInset(InsetBase const * inset) const;
351
352         /// Returns the number of line breaks and white-space stripped at the start
353         int stripLeadingSpaces();
354
355         /// return true if we allow multiple spaces
356         bool isFreeSpacing() const;
357
358         /// return true if we allow this par to stay empty
359         bool allowEmpty() const;
360         ///
361         unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
362         ///
363         ParagraphParameters & params();
364         ///
365         ParagraphParameters const & params() const;
366
367         ///
368         Row & getRow(lyx::pos_type pos);
369         ///
370         Row const & getRow(lyx::pos_type pos) const;
371         ///
372         size_t pos2row(lyx::pos_type pos) const;
373
374         /// total height of paragraph
375         unsigned int height() const { return dim_.height(); }
376         /// total width of paragraph, may differ from workwidth
377         unsigned int width() const { return dim_.width(); }
378         unsigned int ascent() const { return dim_.ascent(); }
379         unsigned int descent() const { return dim_.descent(); }
380         /// LyXText updates the rows using this access point
381         RowList & rows() { return rows_; }
382         /// The painter and others use this
383         RowList const & rows() const { return rows_; }
384
385         /// LyXText::redoParagraph updates this
386         Dimension & dim() { return dim_; }
387
388         /// dump some information to lyxerr
389         void dump() const;
390 private:
391         /// cached dimensions of paragraph
392         Dimension dim_;
393
394         ///
395         mutable RowList rows_;
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         class Pimpl;
407         ///
408         friend class Paragraph::Pimpl;
409         ///
410         Pimpl * pimpl_;
411 };
412
413
414 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
415 {
416         return par.lookupChange(pos) == Change::INSERTED;
417 }
418
419
420 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
421 {
422         return par.lookupChange(pos) == Change::DELETED;
423 }
424
425 #endif // PARAGRAPH_H