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 FontSpan() : first(0), last(0) {}
52 FontSpan(lyx::pos_type f, lyx::pos_type l) : first(f), last(l) {}
58 /// A Paragraph holds all text, attributes and insets in a text paragraph
63 /// Note that this is 1 right now to avoid
64 /// crashes where getChar() is called wrongly
65 /// (returning 0) - if this was 0, then we'd
66 /// try getInset() and crash. We should fix
71 typedef char value_type;
73 typedef lyx::depth_type depth_type;
75 typedef std::vector<value_type> TextContainer;
80 Paragraph(Paragraph const &);
82 Paragraph & operator=(Paragraph const &);
90 Language const * getParLanguage(BufferParams const &) const;
92 bool isRightToLeftPar(BufferParams const &) const;
94 void changeLanguage(BufferParams const & bparams,
95 Language const * from, Language const * to);
97 bool isMultiLingual(BufferParams const &) const;
100 std::string const asString(Buffer const &,
101 OutputParams const & runparams,
104 std::string const asString(Buffer const &, bool label) const;
106 std::string const Paragraph::asString(Buffer const & buffer,
111 std::string const asString(Buffer const &,
112 OutputParams const & runparams,
118 void write(Buffer const &, std::ostream &, BufferParams const &,
119 depth_type & depth) const;
121 void validate(LaTeXFeatures &) const;
124 int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
127 int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
131 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
132 LyXFont const & outerfont, std::ostream &,
133 TexRow & texrow, OutputParams const &) const;
135 /// Writes to stream the content of the paragraph for linuxdoc
136 void simpleLinuxDocOnePar(Buffer const & buf,
138 LyXFont const & outerfont,
139 OutputParams const & runparams,
140 lyx::depth_type depth) const;
142 /// Can we drop the standard paragraph wrapper?
143 bool emptyTag() const;
145 /// Get the id of the paragraph, usefull for docbook and linuxdoc
146 std::string getID(Buffer const & buf,
147 OutputParams const & runparams) const;
149 // Get the first word of a paragraph, return the position where it left
150 lyx::pos_type getFirstWord(Buffer const & buf,
152 OutputParams const & runparams) const;
154 /// Checks if the paragraph contains only text and no inset or font change.
155 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
156 lyx::pos_type initial) const;
158 /// Writes to stream the docbook representation
159 void simpleDocBookOnePar(Buffer const & buf,
161 OutputParams const & runparams,
162 LyXFont const & outerfont,
163 lyx::pos_type initial = 0) const;
166 bool hasSameLayout(Paragraph const & par) const;
169 void makeSameLayout(Paragraph const & par);
172 void setInsetOwner(InsetBase * inset);
174 InsetBase * inInset() const;
176 InsetBase::Code ownerCode() const;
178 bool forceDefaultParagraphs() const;
181 lyx::pos_type size() const { return text_.size(); }
183 bool empty() const { return text_.empty(); }
185 void setContentsFromPar(Paragraph const & par);
187 void clearContents();
190 LyXLayout_ptr const & layout() const;
192 void layout(LyXLayout_ptr const & new_layout);
194 /// This is the item depth, only used by enumerate and itemize
195 signed char itemdepth;
198 InsetBibitem * bibitem() const; // ale970302
200 /// initialise tracking for this par
201 void trackChanges(Change::Type = Change::UNCHANGED);
204 void untrackChanges();
206 /// set entire paragraph to new text for change tracking
209 /// look up change type at given pos
210 Change::Type lookupChange(lyx::pos_type pos) const;
212 /// look up change at given pos
213 Change const lookupChangeFull(lyx::pos_type pos) const;
215 /// is there a change within the given range ?
216 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
218 /// is there a non-addition in this range ?
219 bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
221 /// set change at pos
222 void setChange(lyx::pos_type pos, Change::Type type);
225 void acceptChange(lyx::pos_type start, lyx::pos_type end);
228 void rejectChange(lyx::pos_type start, lyx::pos_type end);
230 /// mark whole par as erased
233 /// Paragraphs can contain "manual labels", for example, Description
234 /// environment. The text for this user-editable label is stored in
235 /// the paragraph alongside the text of the rest of the paragraph
236 /// (the body). This function returns the starting position of the
237 /// body of the text in the paragraph.
238 lyx::pos_type beginOfBody() const;
239 /// recompute this value
240 void setBeginOfBody();
243 std::string const & getLabelstring() const;
245 /// the next two functions are for the manual labels
246 std::string const getLabelWidthString() const;
248 void setLabelWidthString(std::string const & s);
250 char getAlign() const;
251 /// The nesting depth of a paragraph
252 depth_type getDepth() const;
253 /// The maximal possible depth of a paragraph after this one
254 depth_type getMaxDepthAfter() const;
256 void applyLayout(LyXLayout_ptr const & new_layout);
259 void eraseIntern(lyx::pos_type pos);
260 /// erase the char at the given position
261 bool erase(lyx::pos_type pos);
262 /// erase the given range. Returns the number of chars actually erased
263 int erase(lyx::pos_type start, lyx::pos_type end);
265 /** Get uninstantiated font setting. Returns the difference
266 between the characters font and the layoutfont.
267 This is what is stored in the fonttable
270 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
272 LyXFont const getFirstFontSettings() const;
274 /** Get fully instantiated font. If pos == -1, use the layout
275 font attached to this paragraph.
276 If pos == -2, use the label font of the layout attached here.
277 In all cases, the font is instantiated, i.e. does not have any
278 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
281 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
282 LyXFont const & outerfont) const;
283 LyXFont const getLayoutFont(BufferParams const &,
284 LyXFont const & outerfont) const;
285 LyXFont const getLabelFont(BufferParams const &,
286 LyXFont const & outerfont) const;
288 * The font returned by the above functions is the same in a
289 * span of characters. This method will return the first and
290 * the last positions in the paragraph for which that font is
291 * the same. This can be used to avoid unnecessary calls to
294 FontSpan fontSpan(lyx::pos_type pos) const;
296 /// this is a bottleneck.
297 value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
298 /// Get the char, but mirror all bracket characters if it is right-to-left
299 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
300 /// The position must already exist.
301 void setChar(lyx::pos_type pos, value_type c);
302 /// pos <= size() (there is a dummy font change at the end of each par)
303 void setFont(lyx::pos_type pos, LyXFont const & font);
304 /// Returns the height of the highest font in range
305 LyXFont_size highestFontInRange(lyx::pos_type startpos,
306 lyx::pos_type endpos,
307 LyXFont_size def_size) const;
309 void insert(lyx::pos_type pos, std::string const & str,
310 LyXFont const & font);
312 void insertChar(lyx::pos_type pos, value_type c,
313 Change change = Change::INSERTED);
315 void insertChar(lyx::pos_type pos, value_type c,
316 LyXFont const &, Change change = Change::INSERTED);
318 void insertInset(lyx::pos_type pos, InsetBase * inset,
319 Change change = Change::INSERTED);
321 void insertInset(lyx::pos_type pos, InsetBase * inset,
322 LyXFont const &, Change change = Change::INSERTED);
324 bool insetAllowed(InsetBase_code code);
326 InsetBase * getInset(lyx::pos_type pos) {
327 return insetlist.get(pos);
330 InsetBase const * getInset(lyx::pos_type pos) const {
331 return insetlist.get(pos);
335 bool isHfill(lyx::pos_type pos) const {
337 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
339 /// hinted by profiler
340 bool isInset(lyx::pos_type pos) const {
341 return getChar(pos) == static_cast<value_type>(META_INSET);
344 bool isNewline(lyx::pos_type pos) const;
345 /// return true if the char is a word separator
346 bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
348 bool isLineSeparator(lyx::pos_type pos) const;
349 /// True if the character/inset at this point can be part of a word
350 // Note that digits in particular are considered as letters
351 bool isLetter(lyx::pos_type pos) const;
353 /// returns -1 if inset not found
354 int getPositionOfInset(InsetBase const * inset) const;
356 /// Returns the number of line breaks and white-space stripped at the start
357 int stripLeadingSpaces();
359 /// return true if we allow multiple spaces
360 bool isFreeSpacing() const;
362 /// return true if we allow this par to stay empty
363 bool allowEmpty() const;
365 unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
367 ParagraphParameters & params();
369 ParagraphParameters const & params() const;
372 Row & getRow(lyx::pos_type pos, bool boundary);
374 Row const & getRow(lyx::pos_type pos, bool boundary) const;
376 size_t pos2row(lyx::pos_type pos) const;
378 /// total height of paragraph
379 unsigned int height() const { return dim_.height(); }
380 /// total width of paragraph, may differ from workwidth
381 unsigned int width() const { return dim_.width(); }
382 /// ascend of paragraph above baseline
383 unsigned int ascent() const { return dim_.ascent(); }
384 /// descend of paragraph below baseline
385 unsigned int descent() const { return dim_.descent(); }
386 /// LyXText updates the rows using this access point
387 RowList & rows() { return rows_; }
388 /// The painter and others use this
389 RowList const & rows() const { return rows_; }
391 /// LyXText::redoParagraph updates this
392 Dimension & dim() { return dim_; }
394 /// dump some information to lyxerr
402 /// cached dimensions of paragraph
406 mutable RowList rows_;
408 LyXLayout_ptr layout_;
410 * Keeping this here instead of in the pimpl makes LyX >10% faster
411 * for average tasks as buffer loading/switching etc.
415 lyx::pos_type begin_of_body_;
420 friend class Paragraph::Pimpl;
426 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
428 return par.lookupChange(pos) == Change::INSERTED;
432 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
434 return par.lookupChange(pos) == Change::DELETED;
437 #endif // PARAGRAPH_H