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;
58 /// Invalid font span containing no character
59 FontSpan() : first(0), last(-1) {}
60 /// Span including first and last
61 FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
64 /// Range including first and last.
67 inline bool operator<(FontSpan const & s) const
69 return first < s.first;
72 inline bool operator==(FontSpan const & s) const
74 return first == s.first && last == s.last;
77 inline bool contains(pos_type p) const
79 return first <= p && p <= last;
82 inline size_t size() const
84 return empty() ? 0 : last - first;
88 inline FontSpan intersect(FontSpan const & f) const
90 FontSpan result = FontSpan();
91 if (contains(f.first))
92 result.first = f.first;
93 else if (f.contains(first))
99 else if (f.contains(last))
104 inline bool empty() const
115 text_capitalization = 1,
122 enum AsStringParameter
124 AS_STR_NONE = 0, ///< No option, only printable characters.
125 AS_STR_LABEL = 1, ///< Prefix with paragraph label.
126 AS_STR_INSETS = 2, ///< Go into insets.
127 AS_STR_NEWLINES = 4, ///< Get also newline characters.
128 AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
129 AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
133 /// A Paragraph holds all text, attributes and insets in a text paragraph
139 /// Copy constructor.
140 Paragraph(Paragraph const &);
141 /// Partial copy constructor.
142 /// Copy the Paragraph contents from \p beg to \p end (without end).
143 Paragraph(Paragraph const & par, pos_type beg, pos_type end);
145 Paragraph & operator=(Paragraph const &);
154 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
155 bool output_active) const;
157 Language const * getParLanguage(BufferParams const &) const;
159 bool isRTL(BufferParams const &) const;
161 void changeLanguage(BufferParams const & bparams,
162 Language const * from, Language const * to);
164 bool isMultiLingual(BufferParams const &) const;
166 void getLanguages(std::set<Language const *> &) const;
168 /// Convert the paragraph to a string.
169 /// \param AsStringParameter options. This can contain any combination of
170 /// asStringParameter values. Valid examples:
171 /// asString(AS_STR_LABEL)
172 /// asString(AS_STR_LABEL | AS_STR_INSETS)
173 /// asString(AS_STR_INSETS)
174 docstring asString(int options = AS_STR_NONE) const;
176 /// Convert the paragraph to a string.
177 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
178 docstring asString(pos_type beg, pos_type end,
179 int options = AS_STR_NONE,
180 const OutputParams *runparams = 0) const;
182 void forOutliner(docstring &, size_t const maxlen,
183 bool const shorten = true) const;
186 void write(std::ostream &, BufferParams const &,
187 depth_type & depth) const;
189 void validate(LaTeXFeatures &) const;
191 /// \param force means: output even if layout.inpreamble is true.
192 void latex(BufferParams const &, Font const & outerfont, otexstream &,
193 OutputParams const &, int start_pos = 0, int end_pos = -1,
194 bool force = false) const;
196 /// Can we drop the standard paragraph wrapper?
197 bool emptyTag() const;
199 /// Get the id of the paragraph, usefull for docbook
200 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
202 /// Output the first word of a paragraph, return the position where it left.
203 pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
205 /// Output the first word of a paragraph, return the position where it left.
206 pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
208 /// Writes to stream the docbook representation
209 void simpleDocBookOnePar(Buffer const & buf,
211 OutputParams const & runparams,
212 Font const & outerfont,
213 pos_type initial = 0) const;
214 /// \return any material that has had to be deferred until after the
215 /// paragraph has closed.
216 docstring simpleLyXHTMLOnePar(Buffer const & buf,
218 OutputParams const & runparams,
219 Font const & outerfont,
220 pos_type initial = 0) const;
223 bool hasSameLayout(Paragraph const & par) const;
226 void makeSameLayout(Paragraph const & par);
229 void setInsetOwner(Inset const * inset);
231 Inset const & inInset() const;
233 bool allowParagraphCustomization() const;
235 bool usePlainLayout() const;
237 bool isPassThru() const;
239 pos_type size() const;
244 Layout const & layout() const;
245 /// Do not pass a temporary to this!
246 void setLayout(Layout const & layout);
248 void setPlainOrDefaultLayout(DocumentClass const & tc);
250 void setDefaultLayout(DocumentClass const & tc);
252 void setPlainLayout(DocumentClass const & tc);
254 /// This is the item depth, only used by enumerate and itemize
255 signed char itemdepth;
257 /// look up change at given pos
258 Change const & lookupChange(pos_type pos) const;
260 /// is there a change within the given range ?
261 bool isChanged(pos_type start, pos_type end) const;
262 /// is there an unchanged char at the given pos ?
263 bool isChanged(pos_type pos) const;
264 /// is there an insertion at the given pos ?
265 bool isInserted(pos_type pos) const;
266 /// is there a deletion at the given pos ?
267 bool isDeleted(pos_type pos) const;
268 /// is the whole paragraph deleted ?
269 bool isDeleted(pos_type start, pos_type end) const;
271 /// will the paragraph be physically merged with the next
272 /// one if the imaginary end-of-par character is logically deleted?
273 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
275 /// set change for the entire par
276 void setChange(Change const & change);
278 /// set change at given pos
279 void setChange(pos_type pos, Change const & change);
281 /// accept changes within the given range
282 void acceptChanges(pos_type start, pos_type end);
284 /// reject changes within the given range
285 void rejectChanges(pos_type start, pos_type end);
287 /// Paragraphs can contain "manual labels", for example, Description
288 /// environment. The text for this user-editable label is stored in
289 /// the paragraph alongside the text of the rest of the paragraph
290 /// (the body). This function returns the starting position of the
291 /// body of the text in the paragraph.
292 pos_type beginOfBody() const;
293 /// recompute this value
294 void setBeginOfBody();
297 docstring expandLabel(Layout const &, BufferParams const &) const;
299 docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
301 docstring const & labelString() const;
302 /// the next two functions are for the manual labels
303 docstring const getLabelWidthString() const;
304 /// Set label width string.
305 void setLabelWidthString(docstring const & s);
306 /// Actual paragraph alignment used
307 char getAlign() const;
308 /// The nesting depth of a paragraph
309 depth_type getDepth() const;
310 /// The maximal possible depth of a paragraph after this one
311 depth_type getMaxDepthAfter() const;
313 void applyLayout(Layout const & new_layout);
315 /// (logically) erase the char at pos; return true if it was actually erased
316 bool eraseChar(pos_type pos, bool trackChanges);
317 /// (logically) erase the given range; return the number of chars actually erased
318 int eraseChars(pos_type start, pos_type end, bool trackChanges);
321 void resetFonts(Font const & font);
323 /** Get uninstantiated font setting. Returns the difference
324 between the characters font and the layoutfont.
325 This is what is stored in the fonttable
328 getFontSettings(BufferParams const &, pos_type pos) const;
330 Font const & getFirstFontSettings(BufferParams const &) const;
332 /** Get fully instantiated font. If pos == -1, use the layout
333 font attached to this paragraph.
334 If pos == -2, use the label font of the layout attached here.
335 In all cases, the font is instantiated, i.e. does not have any
336 attributes with values FONT_INHERIT, FONT_IGNORE or
339 Font const getFont(BufferParams const &, pos_type pos,
340 Font const & outerfont) const;
341 Font const getLayoutFont(BufferParams const &,
342 Font const & outerfont) const;
343 Font const getLabelFont(BufferParams const &,
344 Font const & outerfont) const;
346 * The font returned by the above functions is the same in a
347 * span of characters. This method will return the first and
348 * the last positions in the paragraph for which that font is
349 * the same. This can be used to avoid unnecessary calls to getFont.
351 FontSpan fontSpan(pos_type pos) const;
353 char_type getChar(pos_type pos) const;
354 /// Get the char, but mirror all bracket characters if it is right-to-left
355 char_type getUChar(BufferParams const &, pos_type pos) const;
356 /// pos <= size() (there is a dummy font change at the end of each par)
357 void setFont(pos_type pos, Font const & font);
358 /// Returns the height of the highest font in range
359 FontSize highestFontInRange(pos_type startpos,
360 pos_type endpos, FontSize def_size) const;
362 void insert(pos_type pos, docstring const & str,
363 Font const & font, Change const & change);
366 void appendString(docstring const & s, Font const & font,
367 Change const & change);
369 void appendChar(char_type c, Font const & font, Change const & change);
371 void insertChar(pos_type pos, char_type c, bool trackChanges);
373 void insertChar(pos_type pos, char_type c,
374 Font const &, bool trackChanges);
376 void insertChar(pos_type pos, char_type c,
377 Font const &, Change const & change);
378 /// Insert \p inset at position \p pos with \p change traking status and
380 /// \return true if successful.
381 bool insertInset(pos_type pos, Inset * inset,
382 Font const & font, Change const & change);
384 Inset * getInset(pos_type pos);
386 Inset const * getInset(pos_type pos) const;
388 /// Release inset at given position.
389 /// \warning does not honour change tracking!
390 /// Therefore, it should only be used for breaking and merging
392 Inset * releaseInset(pos_type pos);
395 InsetList const & insetList() const;
397 void setBuffer(Buffer &);
400 bool isHfill(pos_type pos) const;
402 /// hinted by profiler
403 bool isInset(pos_type pos) const;
405 bool isNewline(pos_type pos) const;
407 bool isEnvSeparator(pos_type pos) const;
408 /// return true if the char is a word separator
409 bool isSeparator(pos_type pos) const;
411 bool isLineSeparator(pos_type pos) const;
412 /// True if the character/inset at this point is a word separator.
413 /// Note that digits in particular are not considered as word separator.
414 bool isWordSeparator(pos_type pos) const;
415 /// True if the element at this point is a character that is not a letter.
416 bool isChar(pos_type pos) const;
417 /// True if the element at this point is a space
418 bool isSpace(pos_type pos) const;
419 /// True if the element at this point is a hard hyphen or a apostrophe
420 /// If it is enclosed by spaces return false
421 bool isHardHyphenOrApostrophe(pos_type pos) const;
423 /// returns true if at least one line break or line separator has been deleted
424 /// at the beginning of the paragraph (either physically or logically)
425 bool stripLeadingSpaces(bool trackChanges);
427 /// return true if we allow multiple spaces
428 bool isFreeSpacing() const;
430 /// return true if we allow this par to stay empty
431 bool allowEmpty() const;
433 ParagraphParameters & params();
435 ParagraphParameters const & params() const;
437 /// Check whether a call to fixBiblio is needed.
438 bool brokenBiblio() const;
439 /// Check if we are in a Biblio environment and insert or
440 /// delete InsetBibitems as necessary.
441 /// \retval int 1, if we had to add an inset, in which case
442 /// the cursor will need to move cursor forward; -pos, if we deleted
443 /// an inset, in which case pos is the position from which the inset
444 /// was deleted, and the cursor will need to be moved back one if it
445 /// was previously past that position. Return 0 otherwise.
446 int fixBiblio(Buffer const & buffer);
448 /// For each author, set 'used' to true if there is a change
449 /// by this author in the paragraph.
450 void checkAuthors(AuthorList const & authorList);
453 void changeCase(BufferParams const & bparams, pos_type pos,
454 pos_type & right, TextCase action);
456 /// find \param str string inside Paragraph.
457 /// \return non-zero if the specified string is at the specified
458 /// position; returned value is the actual match length in positions
459 /// \param del specifies whether deleted strings in ct mode will be considered
461 docstring const & str, ///< string to search
464 pos_type pos, ///< start from here.
465 bool del = true) const;
467 void locateWord(pos_type & from, pos_type & to,
468 word_location const loc) const;
472 /// Spellcheck word at position \p from and fill in found misspelled word
473 /// and \p suggestions if \p do_suggestion is true.
474 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
475 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
476 docstring_list & suggestions, bool do_suggestion = true,
477 bool check_learned = false) const;
479 /// Spell checker status at position \p pos.
480 /// If \p check_boundary is true the status of position immediately
481 /// before \p pos is tested too if it is at word boundary.
482 /// \return true if one of the tested positions is misspelled.
483 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
485 /// \return the spell range (misspelled area) around position.
486 /// Range is empty if word at position is correctly spelled.
487 FontSpan const & getSpellRange(pos_type pos) const;
489 /// spell check of whole paragraph
490 /// remember results until call of requestSpellCheck()
491 void spellCheck() const;
493 /// query state of spell checker results
494 bool needsSpellCheck() const;
495 /// mark position of text manipulation to inform the spell checker
496 /// default value -1 marks the whole paragraph to be checked (again)
497 void requestSpellCheck(pos_type pos = -1);
499 /// an automatically generated identifying label for this paragraph.
500 /// presently used only in the XHTML output routines.
501 std::string magicLabel() const;
504 /// Expand the counters for the labelstring of \c layout
505 docstring expandParagraphLabel(Layout const &, BufferParams const &,
506 bool process_appendix) const;
508 void deregisterWords();
512 void registerWords();
517 friend class Paragraph::Private;
524 #endif // PARAGRAPH_H