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
72 //META_INSET = 1 // as in trunk
73 META_INSET = 0x200001 // above 0x10ffff, for ucs-4
76 typedef lyx::char_type value_type;
78 typedef lyx::depth_type depth_type;
80 typedef std::vector<value_type> TextContainer;
85 Paragraph(Paragraph const &);
87 Paragraph & operator=(Paragraph const &);
95 Language const * getParLanguage(BufferParams const &) const;
97 bool isRightToLeftPar(BufferParams const &) const;
99 void changeLanguage(BufferParams const & bparams,
100 Language const * from, Language const * to);
102 bool isMultiLingual(BufferParams const &) const;
105 lyx::docstring const asString(Buffer const &,
106 OutputParams const & runparams,
109 lyx::docstring const asString(Buffer const &, bool label) const;
111 lyx::docstring const asString(Buffer const & buffer,
116 lyx::docstring const asString(Buffer const &,
117 OutputParams const & runparams,
123 void write(Buffer const &, std::ostream &, BufferParams const &,
124 depth_type & depth) const;
126 void validate(LaTeXFeatures &) const;
129 int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
132 int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
136 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
137 LyXFont const & outerfont, std::ostream &,
138 TexRow & texrow, OutputParams const &) const;
140 /// Can we drop the standard paragraph wrapper?
141 bool emptyTag() const;
143 /// Get the id of the paragraph, usefull for docbook
144 std::string getID(Buffer const & buf,
145 OutputParams const & runparams) const;
147 // Get the first word of a paragraph, return the position where it left
148 lyx::pos_type getFirstWord(Buffer const & buf,
150 OutputParams const & runparams) const;
152 /// Checks if the paragraph contains only text and no inset or font change.
153 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
154 lyx::pos_type initial) const;
156 /// Writes to stream the docbook representation
157 void simpleDocBookOnePar(Buffer const & buf,
159 OutputParams const & runparams,
160 LyXFont const & outerfont,
161 lyx::pos_type initial = 0) const;
164 bool hasSameLayout(Paragraph const & par) const;
167 void makeSameLayout(Paragraph const & par);
170 void setInsetOwner(InsetBase * inset);
172 InsetBase * inInset() const;
174 InsetBase::Code ownerCode() const;
176 bool forceDefaultParagraphs() const;
179 lyx::pos_type size() const { return text_.size(); }
181 bool empty() const { return text_.empty(); }
183 void setContentsFromPar(Paragraph const & par);
185 void clearContents();
188 LyXLayout_ptr const & layout() const;
190 void layout(LyXLayout_ptr const & new_layout);
192 /// This is the item depth, only used by enumerate and itemize
193 signed char itemdepth;
196 InsetBibitem * bibitem() const; // ale970302
198 /// look up change at given pos
199 Change const lookupChange(lyx::pos_type pos) const;
201 /// is there a change within the given range ?
202 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
203 /// is there a deletion at the given pos ?
204 bool isDeleted(lyx::pos_type pos) const {
205 return lookupChange(pos).type == Change::DELETED;
208 /// set change type at given pos
209 void setChangeType(lyx::pos_type pos, Change::Type type);
211 /// set change for the entire par
212 void setChange(Change const & change);
214 /// set change at given pos
215 void setChange(lyx::pos_type pos, Change const & change);
218 void acceptChange(lyx::pos_type start, lyx::pos_type end);
221 void rejectChange(lyx::pos_type start, lyx::pos_type end);
223 /// Paragraphs can contain "manual labels", for example, Description
224 /// environment. The text for this user-editable label is stored in
225 /// the paragraph alongside the text of the rest of the paragraph
226 /// (the body). This function returns the starting position of the
227 /// body of the text in the paragraph.
228 lyx::pos_type beginOfBody() const;
229 /// recompute this value
230 void setBeginOfBody();
233 std::string const & getLabelstring() const;
235 /// the next two functions are for the manual labels
236 std::string const getLabelWidthString() const;
238 void setLabelWidthString(std::string const & s);
240 char getAlign() const;
241 /// The nesting depth of a paragraph
242 depth_type getDepth() const;
243 /// The maximal possible depth of a paragraph after this one
244 depth_type getMaxDepthAfter() const;
246 void applyLayout(LyXLayout_ptr const & new_layout);
249 void eraseIntern(lyx::pos_type pos);
250 /// erase the char at the given position
251 bool erase(lyx::pos_type pos);
252 /// erase the given range. Returns the number of chars actually erased
253 int erase(lyx::pos_type start, lyx::pos_type end);
255 /** Get uninstantiated font setting. Returns the difference
256 between the characters font and the layoutfont.
257 This is what is stored in the fonttable
260 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
262 LyXFont const getFirstFontSettings(BufferParams const &) const;
264 /** Get fully instantiated font. If pos == -1, use the layout
265 font attached to this paragraph.
266 If pos == -2, use the label font of the layout attached here.
267 In all cases, the font is instantiated, i.e. does not have any
268 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
271 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
272 LyXFont const & outerfont) const;
273 LyXFont const getLayoutFont(BufferParams const &,
274 LyXFont const & outerfont) const;
275 LyXFont const getLabelFont(BufferParams const &,
276 LyXFont const & outerfont) const;
278 * The font returned by the above functions is the same in a
279 * span of characters. This method will return the first and
280 * the last positions in the paragraph for which that font is
281 * the same. This can be used to avoid unnecessary calls to
284 FontSpan fontSpan(lyx::pos_type pos) const;
286 /// this is a bottleneck.
287 value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
288 /// Get the char, but mirror all bracket characters if it is right-to-left
289 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
290 /// The position must already exist.
291 void setChar(lyx::pos_type pos, value_type c);
292 /// pos <= size() (there is a dummy font change at the end of each par)
293 void setFont(lyx::pos_type pos, LyXFont const & font);
294 /// Returns the height of the highest font in range
295 LyXFont_size highestFontInRange(lyx::pos_type startpos,
296 lyx::pos_type endpos,
297 LyXFont_size def_size) const;
299 void insert(lyx::pos_type pos, std::string const & str,
300 LyXFont const & font);
302 void insertChar(lyx::pos_type pos, value_type c, Change const & change);
304 void insertChar(lyx::pos_type pos, value_type c,
305 LyXFont const &, Change const & change);
307 void insertInset(lyx::pos_type pos, InsetBase * inset,
308 Change const & change);
310 void insertInset(lyx::pos_type pos, InsetBase * inset,
311 LyXFont const &, Change const & change);
313 bool insetAllowed(InsetBase_code code);
315 InsetBase * getInset(lyx::pos_type pos) {
316 return insetlist.get(pos);
319 InsetBase const * getInset(lyx::pos_type pos) const {
320 return insetlist.get(pos);
324 bool isHfill(lyx::pos_type pos) const {
326 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
328 /// hinted by profiler
329 bool isInset(lyx::pos_type pos) const {
330 return getChar(pos) == static_cast<value_type>(META_INSET);
333 bool isNewline(lyx::pos_type pos) const;
334 /// return true if the char is a word separator
335 bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
337 bool isLineSeparator(lyx::pos_type pos) const;
338 /// True if the character/inset at this point can be part of a word
339 // Note that digits in particular are considered as letters
340 bool isLetter(lyx::pos_type pos) const;
342 /// returns -1 if inset not found
343 int getPositionOfInset(InsetBase const * inset) const;
345 /// Returns the number of line breaks and white-space stripped at the start
346 int stripLeadingSpaces();
348 /// return true if we allow multiple spaces
349 bool isFreeSpacing() const;
351 /// return true if we allow this par to stay empty
352 bool allowEmpty() const;
354 lyx::char_type transformChar(lyx::char_type c, lyx::pos_type pos) const;
356 ParagraphParameters & params();
358 ParagraphParameters const & params() const;
361 Row & getRow(lyx::pos_type pos, bool boundary);
363 Row const & getRow(lyx::pos_type pos, bool boundary) const;
365 size_t pos2row(lyx::pos_type pos) const;
367 /// total height of paragraph
368 unsigned int height() const { return dim_.height(); }
369 /// total width of paragraph, may differ from workwidth
370 unsigned int width() const { return dim_.width(); }
371 /// ascend of paragraph above baseline
372 unsigned int ascent() const { return dim_.ascent(); }
373 /// descend of paragraph below baseline
374 unsigned int descent() const { return dim_.descent(); }
375 /// LyXText updates the rows using this access point
376 RowList & rows() { return rows_; }
377 /// The painter and others use this
378 RowList const & rows() const { return rows_; }
380 RowSignature & rowSignature() const { return rowSignature_; }
382 /// LyXText::redoParagraph updates this
383 Dimension & dim() { return dim_; }
385 /// dump some information to lyxerr
393 /// cached dimensions of paragraph
397 mutable RowList rows_;
399 mutable RowSignature rowSignature_;
402 LyXLayout_ptr layout_;
404 * Keeping this here instead of in the pimpl makes LyX >10% faster
405 * for average tasks as buffer loading/switching etc.
409 lyx::pos_type begin_of_body_;
414 friend class Paragraph::Pimpl;
419 #endif // PARAGRAPH_H