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;
59 /// Invalid font span containing no character
60 FontSpan() : first(0), last(-1) {}
61 /// Span including first and last
62 FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
65 /// Range including first and last.
68 inline bool operator<(FontSpan const & s) const
70 return first < s.first;
73 inline bool operator==(FontSpan const & s) const
75 return first == s.first && last == s.last;
78 inline bool contains(pos_type p) const
80 return first <= p && p <= last;
83 inline size_t size() const
85 return empty() ? 0 : last - first;
89 inline FontSpan intersect(FontSpan const & f) const
91 FontSpan result = FontSpan();
92 if (contains(f.first))
93 result.first = f.first;
94 else if (f.contains(first))
100 else if (f.contains(last))
105 inline bool empty() const
116 text_capitalization = 1,
123 enum AsStringParameter
125 AS_STR_NONE = 0, ///< No option, only printable characters.
126 AS_STR_LABEL = 1, ///< Prefix with paragraph label.
127 AS_STR_INSETS = 2, ///< Go into insets.
128 AS_STR_NEWLINES = 4, ///< Get also newline characters.
129 AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
130 AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
134 /// A Paragraph holds all text, attributes and insets in a text paragraph
140 /// Copy constructor.
141 Paragraph(Paragraph const &);
142 /// Partial copy constructor.
143 /// Copy the Paragraph contents from \p beg to \p end (without end).
144 Paragraph(Paragraph const & par, pos_type beg, pos_type end);
146 Paragraph & operator=(Paragraph const &);
155 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
156 bool output_active) const;
157 /// set the buffer flag if there are changes in the paragraph
158 void addChangesToBuffer(Buffer const & buf) const;
160 bool isChangeUpdateRequired() const;
162 Language const * getParLanguage(BufferParams const &) const;
164 bool isRTL(BufferParams const &) const;
166 void changeLanguage(BufferParams const & bparams,
167 Language const * from, Language const * to);
169 bool isMultiLingual(BufferParams const &) const;
171 void getLanguages(std::set<Language const *> &) const;
173 /// Convert the paragraph to a string.
174 /// \param AsStringParameter options. This can contain any combination of
175 /// asStringParameter values. Valid examples:
176 /// asString(AS_STR_LABEL)
177 /// asString(AS_STR_LABEL | AS_STR_INSETS)
178 /// asString(AS_STR_INSETS)
179 docstring asString(int options = AS_STR_NONE) const;
181 /// Convert the paragraph to a string.
182 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
183 docstring asString(pos_type beg, pos_type end,
184 int options = AS_STR_NONE,
185 const OutputParams *runparams = 0) const;
187 void forOutliner(docstring &, size_t const maxlen,
188 bool const shorten = true) const;
191 void write(std::ostream &, BufferParams const &,
192 depth_type & depth) const;
194 void validate(LaTeXFeatures &) const;
196 /// \param force means: output even if layout.inpreamble is true.
197 void latex(BufferParams const &, Font const & outerfont, otexstream &,
198 OutputParams const &, int start_pos = 0, int end_pos = -1,
199 bool force = false) const;
201 /// Can we drop the standard paragraph wrapper?
202 bool emptyTag() const;
204 /// Get the id of the paragraph, usefull for docbook
205 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
207 /// Output the first word of a paragraph, return the position where it left.
208 pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
210 /// Output the first word of a paragraph, return the position where it left.
211 pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
213 /// Writes to stream the docbook representation
214 void simpleDocBookOnePar(Buffer const & buf,
216 OutputParams const & runparams,
217 Font const & outerfont,
218 pos_type initial = 0) const;
219 /// \return any material that has had to be deferred until after the
220 /// paragraph has closed.
221 docstring simpleLyXHTMLOnePar(Buffer const & buf,
223 OutputParams const & runparams,
224 Font const & outerfont,
225 pos_type initial = 0) const;
228 bool hasSameLayout(Paragraph const & par) const;
231 void makeSameLayout(Paragraph const & par);
234 void setInsetOwner(Inset const * inset);
236 Inset const & inInset() const;
238 bool allowParagraphCustomization() const;
240 bool usePlainLayout() const;
242 bool isPassThru() const;
244 pos_type size() const;
249 Layout const & layout() const;
250 /// Do not pass a temporary to this!
251 void setLayout(Layout const & layout);
253 void setPlainOrDefaultLayout(DocumentClass const & tc);
255 void setDefaultLayout(DocumentClass const & tc);
257 void setPlainLayout(DocumentClass const & tc);
259 /// This is the item depth, only used by enumerate and itemize
260 signed char itemdepth;
262 /// look up change at given pos
263 Change const & lookupChange(pos_type pos) const;
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;
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;
280 /// set change for the entire par
281 void setChange(Change const & change);
283 /// set change at given pos
284 void setChange(pos_type pos, Change const & change);
286 /// accept changes within the given range
287 void acceptChanges(pos_type start, pos_type end);
289 /// reject changes within the given range
290 void rejectChanges(pos_type start, pos_type end);
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();
302 docstring expandLabel(Layout const &, BufferParams const &) const;
304 docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
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;
318 void applyLayout(Layout const & new_layout);
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);
326 void resetFonts(Font const & font);
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
333 getFontSettings(BufferParams const &, pos_type pos) const;
335 Font const & getFirstFontSettings(BufferParams const &) const;
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
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;
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.
356 FontSpan fontSpan(pos_type pos) const;
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 /// Returns the height of the highest font in range
364 FontSize highestFontInRange(pos_type startpos,
365 pos_type endpos, FontSize def_size) const;
367 void insert(pos_type pos, docstring const & str,
368 Font const & font, Change const & change);
371 void appendString(docstring const & s, Font const & font,
372 Change const & change);
374 void appendChar(char_type c, Font const & font, Change const & change);
376 void insertChar(pos_type pos, char_type c, bool trackChanges);
378 void insertChar(pos_type pos, char_type c,
379 Font const &, bool trackChanges);
381 void insertChar(pos_type pos, char_type c,
382 Font const &, Change const & change);
383 /// Insert \p inset at position \p pos with \p change traking status and
385 /// \return true if successful.
386 bool insertInset(pos_type pos, Inset * inset,
387 Font const & font, Change const & change);
389 Inset * getInset(pos_type pos);
391 Inset const * getInset(pos_type pos) const;
393 /// Release inset at given position.
394 /// \warning does not honour change tracking!
395 /// Therefore, it should only be used for breaking and merging
397 Inset * releaseInset(pos_type pos);
400 InsetList const & insetList() const;
402 void setBuffer(Buffer &);
405 bool isHfill(pos_type pos) const;
407 /// hinted by profiler
408 bool isInset(pos_type pos) const;
410 bool isNewline(pos_type pos) const;
412 bool isEnvSeparator(pos_type pos) const;
413 /// return true if the char is a word separator
414 bool isSeparator(pos_type pos) const;
416 bool isLineSeparator(pos_type pos) const;
417 /// True if the character/inset at this point is a word separator.
418 /// Note that digits in particular are not considered as word separator.
419 bool isWordSeparator(pos_type pos) const;
420 /// True if the element at this point is a character that is not a letter.
421 bool isChar(pos_type pos) const;
422 /// True if the element at this point is a space
423 bool isSpace(pos_type pos) const;
424 /// True if the element at this point is a hard hyphen or a apostrophe
425 /// If it is enclosed by spaces return false
426 bool isHardHyphenOrApostrophe(pos_type pos) const;
428 /// returns true if at least one line break or line separator has been deleted
429 /// at the beginning of the paragraph (either physically or logically)
430 bool stripLeadingSpaces(bool trackChanges);
432 /// return true if we allow multiple spaces
433 bool isFreeSpacing() const;
435 /// return true if we allow this par to stay empty
436 bool allowEmpty() const;
438 ParagraphParameters & params();
440 ParagraphParameters const & params() const;
442 /// Check whether a call to fixBiblio is needed.
443 bool brokenBiblio() const;
444 /// Check if we are in a Biblio environment and insert or
445 /// delete InsetBibitems as necessary.
446 /// \retval int 1, if we had to add an inset, in which case
447 /// the cursor will need to move cursor forward; -pos, if we deleted
448 /// an inset, in which case pos is the position from which the inset
449 /// was deleted, and the cursor will need to be moved back one if it
450 /// was previously past that position. Return 0 otherwise.
451 int fixBiblio(Buffer const & buffer);
453 /// For each author, set 'used' to true if there is a change
454 /// by this author in the paragraph.
455 void checkAuthors(AuthorList const & authorList);
458 void changeCase(BufferParams const & bparams, pos_type pos,
459 pos_type & right, TextCase action);
461 /// find \param str string inside Paragraph.
462 /// \return non-zero if the specified string is at the specified
463 /// position; returned value is the actual match length in positions
464 /// \param del specifies whether deleted strings in ct mode will be considered
466 docstring const & str, ///< string to search
469 pos_type pos, ///< start from here.
470 bool del = true) const;
472 void locateWord(pos_type & from, pos_type & to,
473 word_location const loc) const;
477 /// Spellcheck word at position \p from and fill in found misspelled word
478 /// and \p suggestions if \p do_suggestion is true.
479 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
480 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
481 docstring_list & suggestions, bool do_suggestion = true,
482 bool check_learned = false) const;
484 /// Spell checker status at position \p pos.
485 /// If \p check_boundary is true the status of position immediately
486 /// before \p pos is tested too if it is at word boundary.
487 /// \return true if one of the tested positions is misspelled.
488 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
490 /// \return the spell range (misspelled area) around position.
491 /// Range is empty if word at position is correctly spelled.
492 FontSpan const & getSpellRange(pos_type pos) const;
494 /// spell check of whole paragraph
495 /// remember results until call of requestSpellCheck()
496 void spellCheck() const;
498 /// query state of spell checker results
499 bool needsSpellCheck() const;
500 /// mark position of text manipulation to inform the spell checker
501 /// default value -1 marks the whole paragraph to be checked (again)
502 void requestSpellCheck(pos_type pos = -1);
504 /// an automatically generated identifying label for this paragraph.
505 /// presently used only in the XHTML output routines.
506 std::string magicLabel() const;
509 /// Expand the counters for the labelstring of \c layout
510 docstring expandParagraphLabel(Layout const &, BufferParams const &,
511 bool process_appendix) const;
513 void deregisterWords();
517 void registerWords();
522 friend class Paragraph::Private;
529 #endif // PARAGRAPH_H