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 "InsetList.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "RowList_fwd.h"
24 #include "insets/insetbase.h" // only for InsetBase::Code
26 #include "support/types.h"
28 #include <boost/assert.hpp>
44 class ParagraphParameters;
48 /// A Paragraph holds all text, attributes and insets in a text paragraph
53 /// Note that this is 1 right now to avoid
54 /// crashes where getChar() is called wrongly
55 /// (returning 0) - if this was 0, then we'd
56 /// try getInset() and crash. We should fix
61 typedef char value_type;
63 typedef lyx::depth_type depth_type;
65 typedef std::vector<value_type> TextContainer;
70 Paragraph(Paragraph const &);
72 Paragraph & operator=(Paragraph const &);
80 Language const * getParLanguage(BufferParams const &) const;
82 bool isRightToLeftPar(BufferParams const &) const;
84 void changeLanguage(BufferParams const & bparams,
85 Language const * from, Language const * to);
87 bool isMultiLingual(BufferParams const &) const;
90 std::string const asString(Buffer const &,
91 OutputParams const & runparams,
94 std::string const asString(Buffer const &, bool label) const;
96 std::string const Paragraph::asString(Buffer const & buffer,
101 std::string const asString(Buffer const &,
102 OutputParams const & runparams,
108 void write(Buffer const &, std::ostream &, BufferParams const &,
109 depth_type & depth) const;
111 void validate(LaTeXFeatures &) const;
114 int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
117 int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
121 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
122 LyXFont const & outerfont, std::ostream &,
123 TexRow & texrow, OutputParams const &) const;
126 void simpleLinuxDocOnePar(Buffer const & buf,
128 LyXFont const & outerfont,
129 OutputParams const & runparams,
130 lyx::depth_type depth) const;
132 /// Get the id of the paragraph, usefull for docbook
133 std::string getDocbookId() const;
136 void simpleDocBookOnePar(Buffer const & buf,
138 LyXFont const & outerfont,
139 OutputParams const & runparams,
140 lyx::depth_type depth) const;
143 bool hasSameLayout(Paragraph const & par) const;
146 void makeSameLayout(Paragraph const & par);
149 void setInsetOwner(UpdatableInset * inset);
151 UpdatableInset * inInset() const;
153 InsetBase::Code ownerCode() const;
155 bool autoBreakRows() const;
157 bool forceDefaultParagraphs() const;
160 lyx::pos_type size() const { return text_.size(); }
162 bool empty() const { return text_.empty(); }
164 void setContentsFromPar(Paragraph const & par);
166 void clearContents();
169 LyXLayout_ptr const & layout() const;
171 void layout(LyXLayout_ptr const & new_layout);
173 /// This is the item depth, only used by enumerate and itemize
174 signed char itemdepth;
177 InsetBibitem * bibitem() const; // ale970302
179 /// initialise tracking for this par
180 void trackChanges(Change::Type = Change::UNCHANGED);
183 void untrackChanges();
185 /// set entire paragraph to new text for change tracking
188 /// look up change type at given pos
189 Change::Type lookupChange(lyx::pos_type pos) const;
191 /// look up change at given pos
192 Change const lookupChangeFull(lyx::pos_type pos) const;
194 /// is there a change within the given range ?
195 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
197 /// is there a non-addition in this range ?
198 bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
200 /// set change at pos
201 void setChange(lyx::pos_type pos, Change::Type type);
204 void acceptChange(lyx::pos_type start, lyx::pos_type end);
207 void rejectChange(lyx::pos_type start, lyx::pos_type end);
209 /// mark whole par as erased
212 /// Paragraphs can contain "manual labels", for example, Description
213 /// environment. The text for this user-editable label is stored in
214 /// the paragraph alongside the text of the rest of the paragraph
215 /// (the body). This function returns the starting position of the
216 /// body of the text in the paragraph.
217 lyx::pos_type beginOfBody() const;
218 /// recompute this value
219 void setBeginOfBody();
222 std::string const & getLabelstring() const;
224 /// the next two functions are for the manual labels
225 std::string const getLabelWidthString() const;
227 void setLabelWidthString(std::string const & s);
229 char getAlign() const;
230 /// The nesting depth of a paragraph
231 depth_type getDepth() const;
232 /// The maximal possible depth of a paragraph after this one
233 depth_type getMaxDepthAfter() const;
235 void applyLayout(LyXLayout_ptr const & new_layout);
238 void eraseIntern(lyx::pos_type pos);
239 /// erase the char at the given position
240 bool erase(lyx::pos_type pos);
241 /// erase the given range. Returns the number of chars actually erased
242 int erase(lyx::pos_type start, lyx::pos_type end);
244 /** Get uninstantiated font setting. Returns the difference
245 between the characters font and the layoutfont.
246 This is what is stored in the fonttable
249 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
251 LyXFont const getFirstFontSettings() const;
253 /** Get fully instantiated font. If pos == -1, use the layout
254 font attached to this paragraph.
255 If pos == -2, use the label font of the layout attached here.
256 In all cases, the font is instantiated, i.e. does not have any
257 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
260 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
261 LyXFont const & outerfont) const;
262 LyXFont const getLayoutFont(BufferParams const &,
263 LyXFont const & outerfont) const;
264 LyXFont const getLabelFont(BufferParams const &,
265 LyXFont const & outerfont) const;
267 * The font returned by the above functions is the same in a
268 * span of characters. This method will return the last position
269 * in the paragraph for which that font is the same.
270 * This can be used to avoid unnecessary calls to getFont.
272 lyx::pos_type getEndPosOfFontSpan(lyx::pos_type pos) const;
274 /// this is a bottleneck.
275 value_type getChar(lyx::pos_type pos) const
277 BOOST_ASSERT(pos >= 0);
278 BOOST_ASSERT(pos < int(text_.size()));
282 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
283 /// The position must already exist.
284 void setChar(lyx::pos_type pos, value_type c);
285 /// pos <= size() (there is a dummy font change at the end of each par)
286 void setFont(lyx::pos_type pos, LyXFont const & font);
287 /// Returns the height of the highest font in range
288 LyXFont_size highestFontInRange(lyx::pos_type startpos,
289 lyx::pos_type endpos,
290 LyXFont_size def_size) const;
292 void insert(lyx::pos_type pos, std::string const & str,
293 LyXFont const & font);
295 void insertChar(lyx::pos_type pos, value_type c,
296 Change change = Change(Change::INSERTED));
298 void insertChar(lyx::pos_type pos, value_type c,
299 LyXFont const &, Change change = Change(Change::INSERTED));
301 bool checkInsertChar(LyXFont &);
303 void insertInset(lyx::pos_type pos, InsetBase * inset,
304 Change change = Change(Change::INSERTED));
306 void insertInset(lyx::pos_type pos, InsetBase * inset,
307 LyXFont const &, Change change = Change(Change::INSERTED));
309 bool insetAllowed(InsetOld_code code);
311 InsetBase * getInset(lyx::pos_type pos);
313 InsetBase const * getInset(lyx::pos_type pos) const;
316 bool isHfill(lyx::pos_type pos) const;
317 /// hinted by profiler
318 bool isInset(lyx::pos_type pos) const { return getChar(pos) == META_INSET; }
320 bool isNewline(lyx::pos_type pos) const;
322 bool isSeparator(lyx::pos_type pos) const;
324 bool isLineSeparator(lyx::pos_type pos) const;
326 bool isKomma(lyx::pos_type pos) const;
327 /// Used by the spellchecker
328 bool isLetter(lyx::pos_type pos) const;
330 bool isWord(lyx::pos_type pos) const;
332 /// returns -1 if inset not found
333 int getPositionOfInset(InsetBase const * inset) const;
336 int stripLeadingSpaces();
338 /// return true if we allow multiple spaces
339 bool isFreeSpacing() const;
341 /// return true if we allow this par to stay empty
342 bool allowEmpty() const;
344 unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
346 ParagraphParameters & params();
348 ParagraphParameters const & params() const;
351 RowList::iterator getRow(lyx::pos_type pos);
353 RowList::const_iterator getRow(lyx::pos_type pos) const;
355 size_t row(lyx::pos_type pos) const;
361 mutable RowList rows;
362 /// last draw y position (baseline of top row)
364 /// total height of paragraph
366 /// total width of paragraph, may differ from workwidth
371 LyXLayout_ptr layout_;
372 /// keeping this here instead of in the pimpl makes LyX >10% faster
373 // for average tasks as buffer loading/switching etc.
376 lyx::pos_type begin_of_body_;
380 friend struct Paragraph::Pimpl;
387 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
389 return par.lookupChange(pos) == Change::INSERTED;
393 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
395 return par.lookupChange(pos) == Change::DELETED;
398 #endif // PARAGRAPH_H