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
75 typedef char value_type;
77 typedef lyx::depth_type depth_type;
79 typedef std::vector<value_type> TextContainer;
84 Paragraph(Paragraph const &);
86 Paragraph & operator=(Paragraph const &);
94 Language const * getParLanguage(BufferParams const &) const;
96 bool isRightToLeftPar(BufferParams const &) const;
98 void changeLanguage(BufferParams const & bparams,
99 Language const * from, Language const * to);
101 bool isMultiLingual(BufferParams const &) const;
104 std::string const asString(Buffer const &,
105 OutputParams const & runparams,
108 std::string const asString(Buffer const &, bool label) const;
110 std::string const asString(Buffer const & buffer,
115 std::string const asString(Buffer const &,
116 OutputParams const & runparams,
122 void write(Buffer const &, std::ostream &, BufferParams const &,
123 depth_type & depth) const;
125 void validate(LaTeXFeatures &) const;
128 int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
131 int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
135 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
136 LyXFont const & outerfont, std::ostream &,
137 TexRow & texrow, OutputParams const &) const;
139 /// Writes to stream the content of the paragraph for linuxdoc
140 void simpleLinuxDocOnePar(Buffer const & buf,
142 LyXFont const & outerfont,
143 OutputParams const & runparams,
144 lyx::depth_type depth) const;
146 /// Can we drop the standard paragraph wrapper?
147 bool emptyTag() const;
149 /// Get the id of the paragraph, usefull for docbook and linuxdoc
150 std::string getID(Buffer const & buf,
151 OutputParams const & runparams) const;
153 // Get the first word of a paragraph, return the position where it left
154 lyx::pos_type getFirstWord(Buffer const & buf,
156 OutputParams const & runparams) const;
158 /// Checks if the paragraph contains only text and no inset or font change.
159 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
160 lyx::pos_type initial) const;
162 /// Writes to stream the docbook representation
163 void simpleDocBookOnePar(Buffer const & buf,
165 OutputParams const & runparams,
166 LyXFont const & outerfont,
167 lyx::pos_type initial = 0) const;
170 bool hasSameLayout(Paragraph const & par) const;
173 void makeSameLayout(Paragraph const & par);
176 void setInsetOwner(InsetBase * inset);
178 InsetBase * inInset() const;
180 InsetBase::Code ownerCode() const;
182 bool forceDefaultParagraphs() const;
185 lyx::pos_type size() const { return text_.size(); }
187 bool empty() const { return text_.empty(); }
189 void setContentsFromPar(Paragraph const & par);
191 void clearContents();
194 LyXLayout_ptr const & layout() const;
196 void layout(LyXLayout_ptr const & new_layout);
198 /// This is the item depth, only used by enumerate and itemize
199 signed char itemdepth;
202 InsetBibitem * bibitem() const; // ale970302
204 /// initialise tracking for this par
205 void trackChanges(Change::Type = Change::UNCHANGED);
208 void untrackChanges();
210 /// set entire paragraph to new text for change tracking
213 /// look up change type at given pos
214 Change::Type lookupChange(lyx::pos_type pos) const;
216 /// look up change at given pos
217 Change const lookupChangeFull(lyx::pos_type pos) const;
219 /// is there a change within the given range ?
220 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
222 /// is there a non-addition in this range ?
223 bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
225 /// set change at pos
226 void setChange(lyx::pos_type pos, Change::Type type);
228 /// set full change at pos
229 void setChangeFull(lyx::pos_type pos, Change change);
232 void acceptChange(lyx::pos_type start, lyx::pos_type end);
235 void rejectChange(lyx::pos_type start, lyx::pos_type end);
237 /// mark whole par as erased or not
238 void markErased(bool erased);
240 /// Paragraphs can contain "manual labels", for example, Description
241 /// environment. The text for this user-editable label is stored in
242 /// the paragraph alongside the text of the rest of the paragraph
243 /// (the body). This function returns the starting position of the
244 /// body of the text in the paragraph.
245 lyx::pos_type beginOfBody() const;
246 /// recompute this value
247 void setBeginOfBody();
250 std::string const & getLabelstring() const;
252 /// the next two functions are for the manual labels
253 std::string const getLabelWidthString() const;
255 void setLabelWidthString(std::string const & s);
257 char getAlign() const;
258 /// The nesting depth of a paragraph
259 depth_type getDepth() const;
260 /// The maximal possible depth of a paragraph after this one
261 depth_type getMaxDepthAfter() const;
263 void applyLayout(LyXLayout_ptr const & new_layout);
266 void eraseIntern(lyx::pos_type pos);
267 /// erase the char at the given position
268 bool erase(lyx::pos_type pos);
269 /// erase the given range. Returns the number of chars actually erased
270 int erase(lyx::pos_type start, lyx::pos_type end);
272 /** Get uninstantiated font setting. Returns the difference
273 between the characters font and the layoutfont.
274 This is what is stored in the fonttable
277 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
279 LyXFont const getFirstFontSettings(BufferParams const &) const;
281 /** Get fully instantiated font. If pos == -1, use the layout
282 font attached to this paragraph.
283 If pos == -2, use the label font of the layout attached here.
284 In all cases, the font is instantiated, i.e. does not have any
285 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
288 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
289 LyXFont const & outerfont) const;
290 LyXFont const getLayoutFont(BufferParams const &,
291 LyXFont const & outerfont) const;
292 LyXFont const getLabelFont(BufferParams const &,
293 LyXFont const & outerfont) const;
295 * The font returned by the above functions is the same in a
296 * span of characters. This method will return the first and
297 * the last positions in the paragraph for which that font is
298 * the same. This can be used to avoid unnecessary calls to
301 FontSpan fontSpan(lyx::pos_type pos) const;
303 /// this is a bottleneck.
304 value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
305 /// Get the char, but mirror all bracket characters if it is right-to-left
306 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
307 /// The position must already exist.
308 void setChar(lyx::pos_type pos, value_type c);
309 /// pos <= size() (there is a dummy font change at the end of each par)
310 void setFont(lyx::pos_type pos, LyXFont const & font);
311 /// Returns the height of the highest font in range
312 LyXFont_size highestFontInRange(lyx::pos_type startpos,
313 lyx::pos_type endpos,
314 LyXFont_size def_size) const;
316 void insert(lyx::pos_type pos, std::string const & str,
317 LyXFont const & font);
319 void insertChar(lyx::pos_type pos, value_type c,
320 Change change = Change::INSERTED);
322 void insertChar(lyx::pos_type pos, value_type c,
323 LyXFont const &, Change change = Change::INSERTED);
325 void insertInset(lyx::pos_type pos, InsetBase * inset,
326 Change change = Change::INSERTED);
328 void insertInset(lyx::pos_type pos, InsetBase * inset,
329 LyXFont const &, Change change = Change::INSERTED);
331 bool insetAllowed(InsetBase_code code);
333 InsetBase * getInset(lyx::pos_type pos) {
334 return insetlist.get(pos);
337 InsetBase const * getInset(lyx::pos_type pos) const {
338 return insetlist.get(pos);
342 bool isHfill(lyx::pos_type pos) const {
344 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
346 /// hinted by profiler
347 bool isInset(lyx::pos_type pos) const {
348 return getChar(pos) == static_cast<value_type>(META_INSET);
351 bool isNewline(lyx::pos_type pos) const;
352 /// return true if the char is a word separator
353 bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
355 bool isLineSeparator(lyx::pos_type pos) const;
356 /// True if the character/inset at this point can be part of a word
357 // Note that digits in particular are considered as letters
358 bool isLetter(lyx::pos_type pos) const;
360 /// returns -1 if inset not found
361 int getPositionOfInset(InsetBase const * inset) const;
363 /// Returns the number of line breaks and white-space stripped at the start
364 int stripLeadingSpaces();
366 /// return true if we allow multiple spaces
367 bool isFreeSpacing() const;
369 /// return true if we allow this par to stay empty
370 bool allowEmpty() const;
372 unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
374 ParagraphParameters & params();
376 ParagraphParameters const & params() const;
379 Row & getRow(lyx::pos_type pos, bool boundary);
381 Row const & getRow(lyx::pos_type pos, bool boundary) const;
383 size_t pos2row(lyx::pos_type pos) const;
385 /// total height of paragraph
386 unsigned int height() const { return dim_.height(); }
387 /// total width of paragraph, may differ from workwidth
388 unsigned int width() const { return dim_.width(); }
389 /// ascend of paragraph above baseline
390 unsigned int ascent() const { return dim_.ascent(); }
391 /// descend of paragraph below baseline
392 unsigned int descent() const { return dim_.descent(); }
393 /// LyXText updates the rows using this access point
394 RowList & rows() { return rows_; }
395 /// The painter and others use this
396 RowList const & rows() const { return rows_; }
398 RowSignature & rowSignature() const { return rowSignature_; }
400 /// LyXText::redoParagraph updates this
401 Dimension & dim() { return dim_; }
403 /// dump some information to lyxerr
411 /// cached dimensions of paragraph
415 mutable RowList rows_;
417 mutable RowSignature rowSignature_;
420 LyXLayout_ptr layout_;
422 * Keeping this here instead of in the pimpl makes LyX >10% faster
423 * for average tasks as buffer loading/switching etc.
427 lyx::pos_type begin_of_body_;
432 friend class Paragraph::Pimpl;
438 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
440 return par.lookupChange(pos) == Change::INSERTED;
444 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
446 return par.lookupChange(pos) == Change::DELETED;
449 #endif // PARAGRAPH_H