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
42 class ParagraphParameters;
48 /// Invalid font span containing no character
49 FontSpan() : first(0), last(-1) {}
50 /// Span including first and last
51 FontSpan(lyx::pos_type f, lyx::pos_type l) : first(f), last(l) {}
54 /// Range including first and last.
55 lyx::pos_type first, last;
59 /// A Paragraph holds all text, attributes and insets in a text paragraph
64 /// Note that this is 1 right now to avoid
65 /// crashes where getChar() is called wrongly
66 /// (returning 0) - if this was 0, then we'd
67 /// try getInset() and crash. We should fix
69 //META_INSET = 1 // as in trunk
70 META_INSET = 0x200001 // above 0x10ffff, for ucs-4
73 typedef lyx::char_type value_type;
75 typedef lyx::depth_type depth_type;
77 typedef std::vector<value_type> TextContainer;
82 Paragraph(Paragraph const &);
84 Paragraph & operator=(Paragraph const &);
92 Language const * getParLanguage(BufferParams const &) const;
94 bool isRightToLeftPar(BufferParams const &) const;
96 void changeLanguage(BufferParams const & bparams,
97 Language const * from, Language const * to);
99 bool isMultiLingual(BufferParams const &) const;
102 lyx::docstring const asString(Buffer const &,
103 OutputParams const & runparams,
106 lyx::docstring const asString(Buffer const &, bool label) const;
108 lyx::docstring const asString(Buffer const & buffer,
113 lyx::docstring const asString(Buffer const &,
114 OutputParams const & runparams,
120 void write(Buffer const &, std::ostream &, BufferParams const &,
121 depth_type & depth) const;
123 void validate(LaTeXFeatures &) const;
126 int startTeXParParams(BufferParams const &, lyx::odocstream &, bool) const;
129 int endTeXParParams(BufferParams const &, lyx::odocstream &, bool) const;
133 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
134 LyXFont const & outerfont, lyx::odocstream &,
135 TexRow & texrow, OutputParams const &) const;
137 /// Can we drop the standard paragraph wrapper?
138 bool emptyTag() const;
140 /// Get the id of the paragraph, usefull for docbook
141 std::string getID(Buffer const & buf,
142 OutputParams const & runparams) const;
144 // Get the first word of a paragraph, return the position where it left
145 lyx::pos_type getFirstWord(Buffer const & buf,
146 lyx::odocstream & os,
147 OutputParams const & runparams) const;
149 /// Checks if the paragraph contains only text and no inset or font change.
150 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
151 lyx::pos_type initial) const;
153 /// Writes to stream the docbook representation
154 void simpleDocBookOnePar(Buffer const & buf,
156 OutputParams const & runparams,
157 LyXFont const & outerfont,
158 lyx::pos_type initial = 0) const;
161 bool hasSameLayout(Paragraph const & par) const;
164 void makeSameLayout(Paragraph const & par);
167 void setInsetOwner(InsetBase * inset);
169 InsetBase * inInset() const;
171 InsetBase::Code ownerCode() const;
173 bool forceDefaultParagraphs() const;
176 lyx::pos_type size() const { return text_.size(); }
178 bool empty() const { return text_.empty(); }
180 void setContentsFromPar(Paragraph const & par);
182 void clearContents();
185 LyXLayout_ptr const & layout() const;
187 void layout(LyXLayout_ptr const & new_layout);
189 /// This is the item depth, only used by enumerate and itemize
190 signed char itemdepth;
193 InsetBibitem * bibitem() const; // ale970302
195 /// look up change at given pos
196 Change const lookupChange(lyx::pos_type pos) const;
198 /// is there a change within the given range ?
199 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
200 /// is there a deletion at the given pos ?
201 bool isDeleted(lyx::pos_type pos) const {
202 return lookupChange(pos).type == Change::DELETED;
205 /// set change for the entire par
206 void setChange(Change const & change);
208 /// set change at given pos
209 void setChange(lyx::pos_type pos, Change const & change);
212 void acceptChange(lyx::pos_type start, lyx::pos_type end);
215 void rejectChange(lyx::pos_type start, lyx::pos_type end);
217 /// Paragraphs can contain "manual labels", for example, Description
218 /// environment. The text for this user-editable label is stored in
219 /// the paragraph alongside the text of the rest of the paragraph
220 /// (the body). This function returns the starting position of the
221 /// body of the text in the paragraph.
222 lyx::pos_type beginOfBody() const;
223 /// recompute this value
224 void setBeginOfBody();
227 lyx::docstring const & getLabelstring() const;
229 /// the next two functions are for the manual labels
230 lyx::docstring const getLabelWidthString() const;
232 void setLabelWidthString(lyx::docstring const & s);
234 char getAlign() const;
235 /// The nesting depth of a paragraph
236 depth_type getDepth() const;
237 /// The maximal possible depth of a paragraph after this one
238 depth_type getMaxDepthAfter() const;
240 void applyLayout(LyXLayout_ptr const & new_layout);
242 /// erase the char at the given position
243 bool erase(lyx::pos_type pos, bool trackChanges);
244 /// erase the given range. Returns the number of chars actually erased
245 int erase(lyx::pos_type start, lyx::pos_type end, bool trackChanges);
247 /** Get uninstantiated font setting. Returns the difference
248 between the characters font and the layoutfont.
249 This is what is stored in the fonttable
252 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
254 LyXFont const getFirstFontSettings(BufferParams const &) const;
256 /** Get fully instantiated font. If pos == -1, use the layout
257 font attached to this paragraph.
258 If pos == -2, use the label font of the layout attached here.
259 In all cases, the font is instantiated, i.e. does not have any
260 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
263 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
264 LyXFont const & outerfont) const;
265 LyXFont const getLayoutFont(BufferParams const &,
266 LyXFont const & outerfont) const;
267 LyXFont const getLabelFont(BufferParams const &,
268 LyXFont const & outerfont) const;
270 * The font returned by the above functions is the same in a
271 * span of characters. This method will return the first and
272 * the last positions in the paragraph for which that font is
273 * the same. This can be used to avoid unnecessary calls to
276 FontSpan fontSpan(lyx::pos_type pos) const;
278 /// this is a bottleneck.
279 value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
280 /// Get the char, but mirror all bracket characters if it is right-to-left
281 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
282 /// The position must already exist.
283 void setChar(lyx::pos_type pos, value_type c);
284 /// pos <= size() (there is a dummy font change at the end of each par)
285 void setFont(lyx::pos_type pos, LyXFont const & font);
286 /// Returns the height of the highest font in range
287 LyXFont_size highestFontInRange(lyx::pos_type startpos,
288 lyx::pos_type endpos,
289 LyXFont_size def_size) const;
291 void insert(lyx::pos_type pos, std::string const & str,
292 LyXFont const & font, Change const & change);
294 void insertChar(lyx::pos_type pos, value_type c, bool trackChanges);
296 void insertChar(lyx::pos_type pos, value_type c,
297 LyXFont const &, bool trackChanges);
299 void insertChar(lyx::pos_type pos, value_type c,
300 LyXFont const &, Change const & change);
302 void insertInset(lyx::pos_type pos, InsetBase * inset,
303 Change const & change);
305 void insertInset(lyx::pos_type pos, InsetBase * inset,
306 LyXFont const &, Change const & change);
308 bool insetAllowed(InsetBase_code code);
310 InsetBase * getInset(lyx::pos_type pos) {
311 return insetlist.get(pos);
314 InsetBase const * getInset(lyx::pos_type pos) const {
315 return insetlist.get(pos);
319 bool isHfill(lyx::pos_type pos) const {
321 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
323 /// hinted by profiler
324 bool isInset(lyx::pos_type pos) const {
325 return getChar(pos) == static_cast<value_type>(META_INSET);
328 bool isNewline(lyx::pos_type pos) const;
329 /// return true if the char is a word separator
330 bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
332 bool isLineSeparator(lyx::pos_type pos) const;
333 /// True if the character/inset at this point can be part of a word
334 // Note that digits in particular are considered as letters
335 bool isLetter(lyx::pos_type pos) const;
337 /// returns -1 if inset not found
338 int getPositionOfInset(InsetBase const * inset) const;
340 /// Returns the number of line breaks and white-space stripped at the start
341 int stripLeadingSpaces();
343 /// return true if we allow multiple spaces
344 bool isFreeSpacing() const;
346 /// return true if we allow this par to stay empty
347 bool allowEmpty() const;
349 lyx::char_type transformChar(lyx::char_type c, lyx::pos_type pos) const;
351 ParagraphParameters & params();
353 ParagraphParameters const & params() const;
356 Row & getRow(lyx::pos_type pos, bool boundary);
358 Row const & getRow(lyx::pos_type pos, bool boundary) const;
360 size_t pos2row(lyx::pos_type pos) const;
362 /// total height of paragraph
363 unsigned int height() const { return dim_.height(); }
364 /// total width of paragraph, may differ from workwidth
365 unsigned int width() const { return dim_.width(); }
366 /// ascend of paragraph above baseline
367 unsigned int ascent() const { return dim_.ascent(); }
368 /// descend of paragraph below baseline
369 unsigned int descent() const { return dim_.descent(); }
370 /// LyXText updates the rows using this access point
371 RowList & rows() { return rows_; }
372 /// The painter and others use this
373 RowList const & rows() const { return rows_; }
375 RowSignature & rowSignature() const { return rowSignature_; }
377 /// LyXText::redoParagraph updates this
378 Dimension & dim() { return dim_; }
380 /// dump some information to lyxerr
388 /// cached dimensions of paragraph
392 mutable RowList rows_;
394 mutable RowSignature rowSignature_;
397 LyXLayout_ptr layout_;
399 * Keeping this here instead of in the pimpl makes LyX >10% faster
400 * for average tasks as buffer loading/switching etc.
404 lyx::pos_type begin_of_body_;
409 friend class Paragraph::Pimpl;
414 #endif // PARAGRAPH_H