4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Asger Alstrup
8 * \author Lars Gullik Bjønnes
10 * \author André Pönitz
11 * \author Jürgen Vigna
13 * Full author contact details are available in file CREDITS.
19 #include "FontEnums.h"
20 #include "SpellChecker.h"
22 #include "support/strfwd.h"
23 #include "support/types.h"
51 class ParagraphParameters;
60 /// Invalid font span containing no character
61 FontSpan() : first(0), last(-1) {}
62 /// Span including first and last
63 FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
66 /// Range including first and last.
69 inline bool operator<(FontSpan const & s) const
71 return first < s.first;
74 inline bool operator==(FontSpan const & s) const
76 return first == s.first && last == s.last;
79 inline bool inside(pos_type p) const
81 return first <= p && p <= last;
84 inline size_t size() const
86 return empty() ? 0 : last - first;
90 inline FontSpan intersect(FontSpan const & f) const
92 FontSpan result = FontSpan();
94 result.first = f.first;
95 else if (f.inside(first))
100 result.last = f.last;
101 else if (f.inside(last))
106 inline bool empty() const
117 text_capitalization = 1,
124 enum AsStringParameter
126 AS_STR_NONE = 0, ///< No option, only printable characters.
127 AS_STR_LABEL = 1, ///< Prefix with paragraph label.
128 AS_STR_INSETS = 2, ///< Go into insets.
129 AS_STR_NEWLINES = 4, ///< Get also newline characters.
130 AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
131 AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
135 /// A Paragraph holds all text, attributes and insets in a text paragraph
141 /// Copy constructor.
142 Paragraph(Paragraph const &);
143 /// Partial copy constructor.
144 /// Copy the Paragraph contents from \p beg to \p end (without end).
145 Paragraph(Paragraph const & par, pos_type beg, pos_type end);
147 Paragraph & operator=(Paragraph const &);
156 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
157 bool output_active) const;
159 Language const * getParLanguage(BufferParams const &) const;
161 bool isRTL(BufferParams const &) const;
163 void changeLanguage(BufferParams const & bparams,
164 Language const * from, Language const * to);
166 bool isMultiLingual(BufferParams const &) const;
168 void getLanguages(std::set<Language const *> &) const;
170 /// Convert the paragraph to a string.
171 /// \param AsStringParameter options. This can contain any combination of
172 /// asStringParameter values. Valid examples:
173 /// asString(AS_STR_LABEL)
174 /// asString(AS_STR_LABEL | AS_STR_INSETS)
175 /// asString(AS_STR_INSETS)
176 docstring asString(int options = AS_STR_NONE) const;
178 /// Convert the paragraph to a string.
179 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
180 docstring asString(pos_type beg, pos_type end,
181 int options = AS_STR_NONE,
182 const OutputParams *runparams = 0) const;
184 void forOutliner(docstring &, size_t maxlen) const;
187 void write(std::ostream &, BufferParams const &,
188 depth_type & depth) const;
190 void validate(LaTeXFeatures &) const;
192 /// \param force means: output even if layout.inpreamble is true.
193 void latex(BufferParams const &, Font const & outerfont, otexstream &,
194 OutputParams const &, int start_pos = 0, int end_pos = -1,
195 bool force = false) const;
197 /// Can we drop the standard paragraph wrapper?
198 bool emptyTag() const;
200 /// Get the id of the paragraph, usefull for docbook
201 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
203 /// Output the first word of a paragraph, return the position where it left.
204 pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
206 /// Output the first word of a paragraph, return the position where it left.
207 pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
209 /// Writes to stream the docbook representation
210 void simpleDocBookOnePar(Buffer const & buf,
212 OutputParams const & runparams,
213 Font const & outerfont,
214 pos_type initial = 0) const;
215 /// \return any material that has had to be deferred until after the
216 /// paragraph has closed.
217 docstring simpleLyXHTMLOnePar(Buffer const & buf,
219 OutputParams const & runparams,
220 Font const & outerfont,
221 pos_type initial = 0) const;
224 bool hasSameLayout(Paragraph const & par) const;
227 void makeSameLayout(Paragraph const & par);
230 void setInsetOwner(Inset const * inset);
232 Inset const & inInset() const;
234 bool allowParagraphCustomization() const;
236 bool usePlainLayout() const;
238 bool isPassThru() const;
240 pos_type size() const;
245 Layout const & layout() const;
246 /// Do not pass a temporary to this!
247 void setLayout(Layout const & layout);
249 void setPlainOrDefaultLayout(DocumentClass const & tc);
251 void setDefaultLayout(DocumentClass const & tc);
253 void setPlainLayout(DocumentClass const & tc);
255 /// This is the item depth, only used by enumerate and itemize
256 signed char itemdepth;
258 /// look up change at given pos
259 Change const & lookupChange(pos_type pos) const;
261 /// is there a change within the given range ?
262 bool isChanged(pos_type start, pos_type end) const;
263 /// is there an unchanged char at the given pos ?
264 bool isChanged(pos_type pos) const;
265 /// is there an insertion at the given pos ?
266 bool isInserted(pos_type pos) const;
267 /// is there a deletion at the given pos ?
268 bool isDeleted(pos_type pos) const;
269 /// is the whole paragraph deleted ?
270 bool isDeleted(pos_type start, pos_type end) const;
272 /// will the paragraph be physically merged with the next
273 /// one if the imaginary end-of-par character is logically deleted?
274 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
276 /// set change for the entire par
277 void setChange(Change const & change);
279 /// set change at given pos
280 void setChange(pos_type pos, Change const & change);
282 /// accept changes within the given range
283 void acceptChanges(pos_type start, pos_type end);
285 /// reject changes within the given range
286 void rejectChanges(pos_type start, pos_type end);
288 /// Paragraphs can contain "manual labels", for example, Description
289 /// environment. The text for this user-editable label is stored in
290 /// the paragraph alongside the text of the rest of the paragraph
291 /// (the body). This function returns the starting position of the
292 /// body of the text in the paragraph.
293 pos_type beginOfBody() const;
294 /// recompute this value
295 void setBeginOfBody();
298 docstring expandLabel(Layout const &, BufferParams const &) const;
300 docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
302 docstring const & labelString() const;
303 /// the next two functions are for the manual labels
304 docstring const getLabelWidthString() const;
305 /// Set label width string.
306 void setLabelWidthString(docstring const & s);
307 /// Actual paragraph alignment used
308 char getAlign() const;
309 /// The nesting depth of a paragraph
310 depth_type getDepth() const;
311 /// The maximal possible depth of a paragraph after this one
312 depth_type getMaxDepthAfter() const;
314 void applyLayout(Layout const & new_layout);
316 /// (logically) erase the char at pos; return true if it was actually erased
317 bool eraseChar(pos_type pos, bool trackChanges);
318 /// (logically) erase the given range; return the number of chars actually erased
319 int eraseChars(pos_type start, pos_type end, bool trackChanges);
322 void resetFonts(Font const & font);
324 /** Get uninstantiated font setting. Returns the difference
325 between the characters font and the layoutfont.
326 This is what is stored in the fonttable
329 getFontSettings(BufferParams const &, pos_type pos) const;
331 Font const & getFirstFontSettings(BufferParams const &) const;
333 /** Get fully instantiated font. If pos == -1, use the layout
334 font attached to this paragraph.
335 If pos == -2, use the label font of the layout attached here.
336 In all cases, the font is instantiated, i.e. does not have any
337 attributes with values FONT_INHERIT, FONT_IGNORE or
340 Font const getFont(BufferParams const &, pos_type pos,
341 Font const & outerfont) const;
342 Font const getLayoutFont(BufferParams const &,
343 Font const & outerfont) const;
344 Font const getLabelFont(BufferParams const &,
345 Font const & outerfont) const;
347 * The font returned by the above functions is the same in a
348 * span of characters. This method will return the first and
349 * the last positions in the paragraph for which that font is
350 * the same. This can be used to avoid unnecessary calls to getFont.
352 FontSpan fontSpan(pos_type pos) const;
354 char_type getChar(pos_type pos) const;
355 /// Get the char, but mirror all bracket characters if it is right-to-left
356 char_type getUChar(BufferParams const &, pos_type pos) const;
357 /// pos <= size() (there is a dummy font change at the end of each par)
358 void setFont(pos_type pos, Font const & font);
359 /// Returns the height of the highest font in range
360 FontSize highestFontInRange(pos_type startpos,
361 pos_type endpos, FontSize def_size) const;
363 void insert(pos_type pos, docstring const & str,
364 Font const & font, Change const & change);
367 void appendString(docstring const & s, Font const & font,
368 Change const & change);
370 void appendChar(char_type c, Font const & font, Change const & change);
372 void insertChar(pos_type pos, char_type c, bool trackChanges);
374 void insertChar(pos_type pos, char_type c,
375 Font const &, bool trackChanges);
377 void insertChar(pos_type pos, char_type c,
378 Font const &, Change const & change);
379 /// Insert \p inset at position \p pos with \p change traking status and
381 /// \return true if successful.
382 bool insertInset(pos_type pos, Inset * inset,
383 Font const & font, Change const & change);
385 Inset * getInset(pos_type pos);
387 Inset const * getInset(pos_type pos) const;
389 /// Release inset at given position.
390 /// \warning does not honour change tracking!
391 /// Therefore, it should only be used for breaking and merging
393 Inset * releaseInset(pos_type pos);
396 InsetList const & insetList() const;
398 void setBuffer(Buffer &);
401 bool isHfill(pos_type pos) const;
403 /// hinted by profiler
404 bool isInset(pos_type pos) const;
406 bool isNewline(pos_type pos) const;
408 bool isEnvSeparator(pos_type pos) const;
409 /// return true if the char is a word separator
410 bool isSeparator(pos_type pos) const;
412 bool isLineSeparator(pos_type pos) const;
413 /// True if the character/inset at this point is a word separator.
414 /// Note that digits in particular are not considered as word separator.
415 bool isWordSeparator(pos_type pos) const;
416 /// True if the element at this point is a character that is not a letter.
417 bool isChar(pos_type pos) const;
418 /// True if the element at this point is a space
419 bool isSpace(pos_type pos) const;
420 /// True if the element at this point is a hard hyphen or a apostrophe
421 /// If it is enclosed by spaces return false
422 bool isHardHyphenOrApostrophe(pos_type pos) const;
424 /// returns true if at least one line break or line separator has been deleted
425 /// at the beginning of the paragraph (either physically or logically)
426 bool stripLeadingSpaces(bool trackChanges);
428 /// return true if we allow multiple spaces
429 bool isFreeSpacing() const;
431 /// return true if we allow this par to stay empty
432 bool allowEmpty() const;
434 char_type transformChar(char_type c, pos_type pos) const;
436 ParagraphParameters & params();
438 ParagraphParameters const & params() const;
440 /// Check whether a call to fixBiblio is needed.
441 bool brokenBiblio() const;
442 /// Check if we are in a Biblio environment and insert or
443 /// delete InsetBibitems as necessary.
444 /// \retval int 1, if we had to add an inset, in which case
445 /// the cursor will need to move cursor forward; -pos, if we deleted
446 /// an inset, in which case pos is the position from which the inset
447 /// was deleted, and the cursor will need to be moved back one if it
448 /// was previously past that position. Return 0 otherwise.
449 int fixBiblio(Buffer const & buffer);
451 /// For each author, set 'used' to true if there is a change
452 /// by this author in the paragraph.
453 void checkAuthors(AuthorList const & authorList);
456 void changeCase(BufferParams const & bparams, pos_type pos,
457 pos_type & right, TextCase action);
459 /// find \param str string inside Paragraph.
460 /// \return non-zero if the specified string is at the specified
461 /// position; returned value is the actual match length in positions
462 /// \param del specifies whether deleted strings in ct mode will be considered
464 docstring const & str, ///< string to search
467 pos_type pos, ///< start from here.
468 bool del = true) const;
470 void locateWord(pos_type & from, pos_type & to,
471 word_location const loc) const;
475 /// Spellcheck word at position \p from and fill in found misspelled word
476 /// and \p suggestions if \p do_suggestion is true.
477 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
478 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
479 docstring_list & suggestions, bool do_suggestion = true,
480 bool check_learned = false) const;
482 /// Spell checker status at position \p pos.
483 /// If \p check_boundary is true the status of position immediately
484 /// before \p pos is tested too if it is at word boundary.
485 /// \return true if one of the tested positions is misspelled.
486 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
488 /// \return true if both positions are inside the same
489 /// spell range - i.e. the same word.
490 /// use it for positions inside misspelled range only.
491 bool isSameSpellRange(pos_type pos1, pos_type pos2) const;
493 /// spell check of whole paragraph
494 /// remember results until call of requestSpellCheck()
495 void spellCheck() const;
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);
503 /// an automatically generated identifying label for this paragraph.
504 /// presently used only in the XHTML output routines.
505 std::string magicLabel() const;
508 /// Expand the counters for the labelstring of \c layout
509 docstring expandParagraphLabel(Layout const &, BufferParams const &,
510 bool process_appendix) const;
512 void deregisterWords();
516 void registerWords();
521 friend class Paragraph::Private;
528 #endif // PARAGRAPH_H