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.
20 #include "dimension.h"
21 #include "InsetList.h"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "RowList_fwd.h"
25 #include "insets/insetbase.h" // only for InsetBase::Code
27 #include "support/types.h"
45 class ParagraphParameters;
51 /// Invalid font span containing no character
52 FontSpan() : first(0), last(-1) {}
53 /// Span including first and last
54 FontSpan(lyx::pos_type f, lyx::pos_type l) : first(f), last(l) {}
57 /// Range including first and last.
58 lyx::pos_type first, last;
62 /// A Paragraph holds all text, attributes and insets in a text paragraph
67 /// Note that this is 1 right now to avoid
68 /// crashes where getChar() is called wrongly
69 /// (returning 0) - if this was 0, then we'd
70 /// try getInset() and crash. We should fix
73 //META_INSET = 0x200001 // above 0x10ffff, for ucs-4
77 /// Change tracking is "on" in this buffer
79 /// Change tracking is "off" in this buffer
81 /// Change tracking is "unknown" in this buffer
85 typedef lyx::char_type value_type;
87 typedef lyx::depth_type depth_type;
89 typedef std::vector<value_type> TextContainer;
94 Paragraph(Paragraph const &);
96 Paragraph & operator=(Paragraph const &);
104 Language const * getParLanguage(BufferParams const &) const;
106 bool isRightToLeftPar(BufferParams const &) const;
108 void changeLanguage(BufferParams const & bparams,
109 Language const * from, Language const * to);
111 bool isMultiLingual(BufferParams const &) const;
114 std::string const asString(Buffer const &,
115 OutputParams const & runparams,
118 std::string const asString(Buffer const &, bool label) const;
120 std::string const asString(Buffer const & buffer,
125 std::string const asString(Buffer const &,
126 OutputParams const & runparams,
132 void write(Buffer const &, std::ostream &, BufferParams const &,
133 depth_type & depth) const;
135 void validate(LaTeXFeatures &) const;
138 int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
141 int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
145 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
146 LyXFont const & outerfont, std::ostream &,
147 TexRow & texrow, OutputParams const &) const;
149 /// Writes to stream the content of the paragraph for linuxdoc
150 void simpleLinuxDocOnePar(Buffer const & buf,
152 LyXFont const & outerfont,
153 OutputParams const & runparams,
154 lyx::depth_type depth) const;
156 /// Can we drop the standard paragraph wrapper?
157 bool emptyTag() const;
159 /// Get the id of the paragraph, usefull for docbook and linuxdoc
160 std::string getID(Buffer const & buf,
161 OutputParams const & runparams) const;
163 // Get the first word of a paragraph, return the position where it left
164 lyx::pos_type getFirstWord(Buffer const & buf,
166 OutputParams const & runparams) const;
168 /// Checks if the paragraph contains only text and no inset or font change.
169 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
170 lyx::pos_type initial) const;
172 /// Writes to stream the docbook representation
173 void simpleDocBookOnePar(Buffer const & buf,
175 OutputParams const & runparams,
176 LyXFont const & outerfont,
177 lyx::pos_type initial = 0) const;
180 bool hasSameLayout(Paragraph const & par) const;
183 void makeSameLayout(Paragraph const & par);
186 void setInsetOwner(InsetBase * inset);
188 InsetBase * inInset() const;
190 InsetBase::Code ownerCode() const;
192 bool forceDefaultParagraphs() const;
195 lyx::pos_type size() const { return text_.size(); }
197 bool empty() const { return text_.empty(); }
199 void setContentsFromPar(Paragraph const & par);
201 void clearContents();
204 LyXLayout_ptr const & layout() const;
206 void layout(LyXLayout_ptr const & new_layout);
208 /// This is the item depth, only used by enumerate and itemize
209 signed char itemdepth;
212 InsetBibitem * bibitem() const; // ale970302
214 /// initialise tracking for this par
215 void trackChanges(Change::Type = Change::UNCHANGED);
218 void untrackChanges();
220 /// set entire paragraph to new text for change tracking
221 void cleanChanges(ChangeTracking ct = trackingUnknown);
223 /// look up change at given pos
224 Change const lookupChange(lyx::pos_type pos) const;
226 /// is there a change within the given range ?
227 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
229 /// is there a non-addition in this range ?
230 bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
232 /// set change type at given pos
233 void setChangeType(lyx::pos_type pos, Change::Type type);
235 /// set change at given pos
236 void setChange(lyx::pos_type pos, Change change);
239 void acceptChange(lyx::pos_type start, lyx::pos_type end);
242 void rejectChange(lyx::pos_type start, lyx::pos_type end);
244 /// mark whole par as erased or not
245 void markErased(bool erased);
247 /// Paragraphs can contain "manual labels", for example, Description
248 /// environment. The text for this user-editable label is stored in
249 /// the paragraph alongside the text of the rest of the paragraph
250 /// (the body). This function returns the starting position of the
251 /// body of the text in the paragraph.
252 lyx::pos_type beginOfBody() const;
253 /// recompute this value
254 void setBeginOfBody();
257 std::string const & getLabelstring() const;
259 /// the next two functions are for the manual labels
260 std::string const getLabelWidthString() const;
262 void setLabelWidthString(std::string const & s);
264 char getAlign() const;
265 /// The nesting depth of a paragraph
266 depth_type getDepth() const;
267 /// The maximal possible depth of a paragraph after this one
268 depth_type getMaxDepthAfter() const;
270 void applyLayout(LyXLayout_ptr const & new_layout);
273 void eraseIntern(lyx::pos_type pos);
274 /// erase the char at the given position
275 bool erase(lyx::pos_type pos);
276 /// erase the given range. Returns the number of chars actually erased
277 int erase(lyx::pos_type start, lyx::pos_type end);
279 /** Get uninstantiated font setting. Returns the difference
280 between the characters font and the layoutfont.
281 This is what is stored in the fonttable
284 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
286 LyXFont const getFirstFontSettings(BufferParams const &) const;
288 /** Get fully instantiated font. If pos == -1, use the layout
289 font attached to this paragraph.
290 If pos == -2, use the label font of the layout attached here.
291 In all cases, the font is instantiated, i.e. does not have any
292 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
295 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
296 LyXFont const & outerfont) const;
297 LyXFont const getLayoutFont(BufferParams const &,
298 LyXFont const & outerfont) const;
299 LyXFont const getLabelFont(BufferParams const &,
300 LyXFont const & outerfont) const;
302 * The font returned by the above functions is the same in a
303 * span of characters. This method will return the first and
304 * the last positions in the paragraph for which that font is
305 * the same. This can be used to avoid unnecessary calls to
308 FontSpan fontSpan(lyx::pos_type pos) const;
310 /// this is a bottleneck.
311 value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
312 /// Get the char, but mirror all bracket characters if it is right-to-left
313 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
314 /// The position must already exist.
315 void setChar(lyx::pos_type pos, value_type c);
316 /// pos <= size() (there is a dummy font change at the end of each par)
317 void setFont(lyx::pos_type pos, LyXFont const & font);
318 /// Returns the height of the highest font in range
319 LyXFont_size highestFontInRange(lyx::pos_type startpos,
320 lyx::pos_type endpos,
321 LyXFont_size def_size) const;
323 void insert(lyx::pos_type pos, std::string const & str,
324 LyXFont const & font);
326 void insertChar(lyx::pos_type pos, value_type c,
327 Change change = Change::INSERTED);
329 void insertChar(lyx::pos_type pos, value_type c,
330 LyXFont const &, Change change = Change::INSERTED);
332 void insertInset(lyx::pos_type pos, InsetBase * inset,
333 Change change = Change::INSERTED);
335 void insertInset(lyx::pos_type pos, InsetBase * inset,
336 LyXFont const &, Change change = Change::INSERTED);
338 bool insetAllowed(InsetBase_code code);
340 InsetBase * getInset(lyx::pos_type pos) {
341 return insetlist.get(pos);
344 InsetBase const * getInset(lyx::pos_type pos) const {
345 return insetlist.get(pos);
349 bool isHfill(lyx::pos_type pos) const {
351 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
353 /// hinted by profiler
354 bool isInset(lyx::pos_type pos) const {
355 return getChar(pos) == static_cast<value_type>(META_INSET);
358 bool isNewline(lyx::pos_type pos) const;
359 /// return true if the char is a word separator
360 bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
362 bool isLineSeparator(lyx::pos_type pos) const;
363 /// True if the character/inset at this point can be part of a word
364 // Note that digits in particular are considered as letters
365 bool isLetter(lyx::pos_type pos) const;
367 /// returns -1 if inset not found
368 int getPositionOfInset(InsetBase const * inset) const;
370 /// Returns the number of line breaks and white-space stripped at the start
371 int stripLeadingSpaces();
373 /// return true if we allow multiple spaces
374 bool isFreeSpacing() const;
376 /// return true if we allow this par to stay empty
377 bool allowEmpty() const;
379 unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
381 ParagraphParameters & params();
383 ParagraphParameters const & params() const;
386 Row & getRow(lyx::pos_type pos, bool boundary);
388 Row const & getRow(lyx::pos_type pos, bool boundary) const;
390 size_t pos2row(lyx::pos_type pos) const;
392 /// total height of paragraph
393 unsigned int height() const { return dim_.height(); }
394 /// total width of paragraph, may differ from workwidth
395 unsigned int width() const { return dim_.width(); }
396 /// ascend of paragraph above baseline
397 unsigned int ascent() const { return dim_.ascent(); }
398 /// descend of paragraph below baseline
399 unsigned int descent() const { return dim_.descent(); }
400 /// LyXText updates the rows using this access point
401 RowList & rows() { return rows_; }
402 /// The painter and others use this
403 RowList const & rows() const { return rows_; }
405 RowSignature & rowSignature() const { return rowSignature_; }
407 /// LyXText::redoParagraph updates this
408 Dimension & dim() { return dim_; }
410 /// dump some information to lyxerr
418 /// cached dimensions of paragraph
422 mutable RowList rows_;
424 mutable RowSignature rowSignature_;
427 LyXLayout_ptr layout_;
429 * Keeping this here instead of in the pimpl makes LyX >10% faster
430 * for average tasks as buffer loading/switching etc.
434 lyx::pos_type begin_of_body_;
439 friend class Paragraph::Pimpl;
445 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
447 return par.lookupChange(pos).type == Change::INSERTED;
451 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
453 return par.lookupChange(pos).type == Change::DELETED;
456 #endif // PARAGRAPH_H