]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
Fix glitch in drawing fractions
[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 "FontEnums.h"
20 #include "SpellChecker.h"
21
22 #include "support/strfwd.h"
23 #include "support/types.h"
24
25 #include <set>
26
27 namespace lyx {
28
29 class AuthorList;
30 class Buffer;
31 class BufferParams;
32 class Change;
33 class Counters;
34 class Cursor;
35 class CursorSlice;
36 class DocIterator;
37 class docstring_list;
38 class DocumentClass;
39 class Inset;
40 class InsetBibitem;
41 class LaTeXFeatures;
42 class InsetList;
43 class Language;
44 class Layout;
45 class Font;
46 class MetricsInfo;
47 class OutputParams;
48 class PainterInfo;
49 class ParagraphParameters;
50 class TocBackend;
51 class WordLangTuple;
52 class XHTMLStream;
53 class otexstream;
54
55 class FontSpan {
56 public:
57         /// Invalid font span containing no character
58         FontSpan() : first(0), last(-1) {}
59         /// Span including first and last
60         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
61
62 public:
63         /// Range including first and last.
64         pos_type first, last;
65
66         inline bool operator<(FontSpan const & s) const
67         {
68                 return first < s.first;
69         }
70         
71         inline bool operator==(FontSpan const & s) const
72         {
73                 return first == s.first && last == s.last;
74         }
75
76         inline bool contains(pos_type p) const
77         {
78                 return first <= p && p <= last;
79         }
80
81         inline size_t size() const
82         {
83                 return empty() ? 0 : last - first;
84         }
85         
86
87         inline FontSpan intersect(FontSpan const & f) const
88         {
89                 FontSpan result = FontSpan();
90                 if (contains(f.first))
91                         result.first = f.first;
92                 else if (f.contains(first))
93                         result.first = first;
94                 else
95                         return result;
96                 if (contains(f.last))
97                         result.last = f.last;
98                 else if (f.contains(last))
99                         result.last = last;
100                 return result;
101         }
102         
103         inline bool empty() const
104         {
105                 return first > last;
106         }
107 };
108
109 ///
110 enum TextCase {
111         ///
112         text_lowercase = 0,
113         ///
114         text_capitalization = 1,
115         ///
116         text_uppercase = 2
117 };
118
119
120 ///
121 enum AsStringParameter
122 {
123         AS_STR_NONE = 0, ///< No option, only printable characters.
124         AS_STR_LABEL = 1, ///< Prefix with paragraph label.
125         AS_STR_INSETS = 2, ///< Go into insets.
126         AS_STR_NEWLINES = 4, ///< Get also newline characters.
127         AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
128         AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
129 };
130
131
132 /// A Paragraph holds all text, attributes and insets in a text paragraph
133 class Paragraph
134 {
135 public:
136         ///
137         Paragraph();
138         /// Copy constructor.
139         Paragraph(Paragraph const &);
140         /// Partial copy constructor.
141         /// Copy the Paragraph contents from \p beg to \p end (without end).
142         Paragraph(Paragraph const & par, pos_type beg, pos_type end);
143         ///
144         Paragraph & operator=(Paragraph const &);
145         ///
146         ~Paragraph();
147         ///
148         int id() const;
149         ///
150         void setId(int id);
151
152         ///
153         void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
154                              bool output_active, TocBackend & backend) const;
155         /// set the buffer flag if there are changes in the paragraph
156         void addChangesToBuffer(Buffer const & buf) const;
157         ///
158         bool isChangeUpdateRequired() const;
159         ///
160         Language const * getParLanguage(BufferParams const &) const;
161         ///
162         bool isRTL(BufferParams const &) const;
163         ///
164         void changeLanguage(BufferParams const & bparams,
165                             Language const * from, Language const * to);
166         ///
167         bool isMultiLingual(BufferParams const &) const;
168         ///
169         void getLanguages(std::set<Language const *> &) const;
170
171         /// Convert the paragraph to a string.
172         /// \param AsStringParameter options. This can contain any combination of
173         /// asStringParameter values. Valid examples:
174         ///             asString(AS_STR_LABEL)
175         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
176         ///             asString(AS_STR_INSETS)
177         docstring asString(int options = AS_STR_NONE) const;
178
179         /// Convert the paragraph to a string.
180         /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
181         docstring asString(pos_type beg, pos_type end,
182                            int options = AS_STR_NONE,
183                            const OutputParams *runparams = 0) const;
184         ///
185         void forOutliner(docstring &, size_t maxlen, bool shorten = true,
186                          bool label = true) const;
187
188         ///
189         void write(std::ostream &, BufferParams const &,
190                 depth_type & depth) const;
191         ///
192         void validate(LaTeXFeatures &) const;
193
194         /// \param force means: output even if layout.inpreamble is true.
195         void latex(BufferParams const &, Font const & outerfont, otexstream &,
196                 OutputParams const &, int start_pos = 0, int end_pos = -1,
197                 bool force = false) const;
198
199         /// Can we drop the standard paragraph wrapper?
200         bool emptyTag() const;
201
202         /// Get the id of the paragraph, usefull for docbook
203         std::string getID(Buffer const & buf, OutputParams const & runparams) const;
204
205         /// Output the first word of a paragraph, return the position where it left.
206         pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
207
208         /// Output the first word of a paragraph, return the position where it left.
209         pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
210
211         /// Writes to stream the docbook representation
212         void simpleDocBookOnePar(Buffer const & buf,
213                                  odocstream &,
214                                  OutputParams const & runparams,
215                                  Font const & outerfont,
216                                  pos_type initial = 0) const;
217         /// \return any material that has had to be deferred until after the
218         /// paragraph has closed.
219         docstring simpleLyXHTMLOnePar(Buffer const & buf,
220                                  XHTMLStream & xs,
221                                  OutputParams const & runparams,
222                                  Font const & outerfont,
223                                  bool start_paragraph = true,
224                                  bool close_paragraph = true,
225                                  pos_type initial = 0) const;
226
227         ///
228         bool hasSameLayout(Paragraph const & par) const;
229
230         ///
231         void makeSameLayout(Paragraph const & par);
232
233         ///
234         void setInsetOwner(Inset const * inset);
235         ///
236         Inset const & inInset() const;
237         ///
238         bool allowParagraphCustomization() const;
239         ///
240         bool usePlainLayout() const;
241         ///
242         bool isPassThru() const;
243         ///
244         pos_type size() const;
245         ///
246         bool empty() const;
247
248         ///
249         Layout const & layout() const;
250         /// Do not pass a temporary to this!
251         void setLayout(Layout const & layout);
252         ///
253         void setPlainOrDefaultLayout(DocumentClass const & tc);
254         ///
255         void setDefaultLayout(DocumentClass const & tc);
256         ///
257         void setPlainLayout(DocumentClass const & tc);
258
259         /// This is the item depth, only used by enumerate and itemize
260         signed char itemdepth;
261
262         /// look up change at given pos
263         Change const & lookupChange(pos_type pos) const;
264
265         /// is there a change within the given range ?
266         bool isChanged(pos_type start, pos_type end) const;
267         /// is there an unchanged char at the given pos ?
268         bool isChanged(pos_type pos) const;
269         /// is there an insertion at the given pos ?
270         bool isInserted(pos_type pos) const;
271         /// is there a deletion at the given pos ?
272         bool isDeleted(pos_type pos) const;
273         /// is the whole paragraph deleted ?
274         bool isDeleted(pos_type start, pos_type end) const;
275
276         /// will the paragraph be physically merged with the next
277         /// one if the imaginary end-of-par character is logically deleted?
278         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
279
280         /// set change for the entire par
281         void setChange(Change const & change);
282
283         /// set change at given pos
284         void setChange(pos_type pos, Change const & change);
285
286         /// accept changes within the given range
287         void acceptChanges(pos_type start, pos_type end);
288
289         /// reject changes within the given range
290         void rejectChanges(pos_type start, pos_type end);
291
292         /// Paragraphs can contain "manual labels", for example, Description
293         /// environment. The text for this user-editable label is stored in
294         /// the paragraph alongside the text of the rest of the paragraph
295         /// (the body). This function returns the starting position of the
296         /// body of the text in the paragraph.
297         pos_type beginOfBody() const;
298         /// recompute this value
299         void setBeginOfBody();
300
301         ///
302         docstring expandLabel(Layout const &, BufferParams const &) const;
303         ///
304         docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
305         ///
306         docstring const & labelString() const;
307         /// the next two functions are for the manual labels
308         docstring const getLabelWidthString() const;
309         /// Set label width string.
310         void setLabelWidthString(docstring const & s);
311         /// Actual paragraph alignment used
312         char getAlign() const;
313         /// The nesting depth of a paragraph
314         depth_type getDepth() const;
315         /// The maximal possible depth of a paragraph after this one
316         depth_type getMaxDepthAfter() const;
317         ///
318         void applyLayout(Layout const & new_layout);
319
320         /// (logically) erase the char at pos; return true if it was actually erased
321         bool eraseChar(pos_type pos, bool trackChanges);
322         /// (logically) erase the given range; return the number of chars actually erased
323         int eraseChars(pos_type start, pos_type end, bool trackChanges);
324
325         ///
326         void resetFonts(Font const & font);
327
328         /** Get uninstantiated font setting. Returns the difference
329             between the characters font and the layoutfont.
330             This is what is stored in the fonttable
331         */
332         Font const &
333         getFontSettings(BufferParams const &, pos_type pos) const;
334         ///
335         Font const & getFirstFontSettings(BufferParams const &) const;
336
337         /** Get fully instantiated font. If pos == -1, use the layout
338             font attached to this paragraph.
339             If pos == -2, use the label font of the layout attached here.
340             In all cases, the font is instantiated, i.e. does not have any
341             attributes with values FONT_INHERIT, FONT_IGNORE or
342             FONT_TOGGLE.
343         */
344         Font const getFont(BufferParams const &, pos_type pos,
345                               Font const & outerfont) const;
346         Font const getLayoutFont(BufferParams const &,
347                                     Font const & outerfont) const;
348         Font const getLabelFont(BufferParams const &,
349                                    Font const & outerfont) const;
350         /**
351          * The font returned by the above functions is the same in a
352          * span of characters. This method will return the first and
353          * the last positions in the paragraph for which that font is
354          * the same. This can be used to avoid unnecessary calls to getFont.
355          */
356         FontSpan fontSpan(pos_type pos) const;
357         ///
358         char_type getChar(pos_type pos) const;
359         /// Get the char, but mirror all bracket characters if it is right-to-left
360         char_type getUChar(BufferParams const &, pos_type pos) const;
361         /// pos <= size() (there is a dummy font change at the end of each par)
362         void setFont(pos_type pos, Font const & font);
363         ///
364         void insert(pos_type pos, docstring const & str,
365                     Font const & font, Change const & change);
366
367         ///
368         void appendString(docstring const & s, Font const & font,
369                 Change const & change);
370         ///
371         void appendChar(char_type c, Font const & font, Change const & change);
372         ///
373         void insertChar(pos_type pos, char_type c, bool trackChanges);
374         ///
375         void insertChar(pos_type pos, char_type c,
376                         Font const &, bool trackChanges);
377         ///
378         void insertChar(pos_type pos, char_type c,
379                         Font const &, Change const & change);
380         /// Insert \p inset at position \p pos with \p change traking status and
381         /// \p font.
382         /// \return true if successful.
383         bool insertInset(pos_type pos, Inset * inset,
384                          Font const & font, Change const & change);
385         ///
386         Inset * getInset(pos_type pos);
387         ///
388         Inset const * getInset(pos_type pos) const;
389
390         /// Release inset at given position.
391         /// \warning does not honour change tracking!
392         /// Therefore, it should only be used for breaking and merging
393         /// paragraphs
394         Inset * releaseInset(pos_type pos);
395
396         ///
397         InsetList const & insetList() const;
398         ///
399         void setBuffer(Buffer &);
400         ///
401         void resetBuffer();
402
403         ///
404         bool isHfill(pos_type pos) const;
405
406         /// hinted by profiler
407         bool isInset(pos_type pos) const;
408         ///
409         bool isNewline(pos_type pos) const;
410         ///
411         bool isEnvSeparator(pos_type pos) const;
412         /// return true if the char is a word separator
413         bool isSeparator(pos_type pos) const;
414         ///
415         bool isLineSeparator(pos_type pos) const;
416         /// True if the character/inset at this point is a word separator.
417         /// Note that digits in particular are not considered as word separator.
418         bool isWordSeparator(pos_type pos) const;
419         /// True if the element at this point is a character that is not a letter.
420         bool isChar(pos_type pos) const;
421         /// True if the element at this point is a space
422         bool isSpace(pos_type pos) const;
423         /// True if the element at this point is a hard hyphen or a apostrophe
424         /// If it is enclosed by spaces return false
425         bool isHardHyphenOrApostrophe(pos_type pos) const;
426
427         /// returns true if at least one line break or line separator has been deleted
428         /// at the beginning of the paragraph (either physically or logically)
429         bool stripLeadingSpaces(bool trackChanges);
430
431         /// return true if we allow multiple spaces
432         bool isFreeSpacing() const;
433
434         /// return true if we allow this par to stay empty
435         bool allowEmpty() const;
436         ///
437         ParagraphParameters & params();
438         ///
439         ParagraphParameters const & params() const;
440
441         /// Check whether a call to fixBiblio is needed.
442         bool brokenBiblio() const;
443         /// Check if we are in a Biblio environment and insert or
444         /// delete InsetBibitems as necessary.
445         /// \retval int 1, if we had to add an inset, in which case
446         /// the cursor will need to move cursor forward; -pos, if we deleted
447         /// an inset, in which case pos is the position from which the inset
448         /// was deleted, and the cursor will need to be moved back one if it
449         /// was previously past that position. Return 0 otherwise.
450         int fixBiblio(Buffer const & buffer);
451
452         /// For each author, set 'used' to true if there is a change
453         /// by this author in the paragraph.
454         void checkAuthors(AuthorList const & authorList);
455
456         ///
457         void changeCase(BufferParams const & bparams, pos_type pos,
458                 pos_type & right, TextCase action);
459
460         /// find \param str string inside Paragraph.
461         /// \return non-zero if the specified string is at the specified
462         ///     position; returned value is the actual match length in positions
463         /// \param del specifies whether deleted strings in ct mode will be considered
464         int find(
465                 docstring const & str, ///< string to search
466                 bool cs, ///<
467                 bool mw, ///<
468                 pos_type pos, ///< start from here.
469                 bool del = true) const;
470         
471         void locateWord(pos_type & from, pos_type & to,
472                 word_location const loc) const;
473         ///
474         void updateWords();
475
476         /// Spellcheck word at position \p from and fill in found misspelled word
477         /// and \p suggestions if \p do_suggestion is true.
478         /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
479         SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
480                 docstring_list & suggestions, bool do_suggestion =  true,
481                 bool check_learned = false) const;
482
483         /// Spell checker status at position \p pos.
484         /// If \p check_boundary is true the status of position immediately
485         /// before \p pos is tested too if it is at word boundary.
486         /// \return true if one of the tested positions is misspelled.
487         bool isMisspelled(pos_type pos, bool check_boundary = false) const;
488
489         /// \return the spell range (misspelled area) around position.
490         /// Range is empty if word at position is correctly spelled.
491         FontSpan const & getSpellRange(pos_type pos) const;
492
493         /// spell check of whole paragraph
494         /// remember results until call of requestSpellCheck()
495         void spellCheck() const;
496
497         /// query state of spell checker results
498         bool needsSpellCheck() const;
499         /// mark position of text manipulation to inform the spell checker
500         /// default value -1 marks the whole paragraph to be checked (again)
501         void requestSpellCheck(pos_type pos = -1);
502
503         /// an automatically generated identifying label for this paragraph.
504         /// presently used only in the XHTML output routines.
505         std::string magicLabel() const;
506
507 private:
508         /// Expand the counters for the labelstring of \c layout
509         docstring expandParagraphLabel(Layout const &, BufferParams const &,
510                 bool process_appendix) const;
511         ///
512         void deregisterWords();
513         ///
514         void collectWords();
515         ///
516         void registerWords();
517
518         /// Pimpl away stuff
519         class Private;
520         ///
521         friend class Paragraph::Private;
522         ///
523         Private * d;
524 };
525
526 } // namespace lyx
527
528 #endif // PARAGRAPH_H