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
45 class ParagraphParameters;
51 /// Invalid font span containing no character
52 FontSpan() : first(0), last(-1) {}
53 /// Span including first and last
54 FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
57 /// Range including first and 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 char_type value_type;
78 typedef std::vector<value_type> TextContainer;
83 Paragraph(Paragraph const &);
85 Paragraph & operator=(Paragraph const &);
93 Language const * getParLanguage(BufferParams const &) const;
95 bool isRightToLeftPar(BufferParams const &) const;
97 void changeLanguage(BufferParams const & bparams,
98 Language const * from, Language const * to);
100 bool isMultiLingual(BufferParams const &) const;
103 docstring const asString(Buffer const &,
104 OutputParams const & runparams,
107 docstring const asString(Buffer const &, bool label) const;
109 docstring const asString(Buffer const & buffer,
114 docstring const asString(Buffer const &,
115 OutputParams const & runparams,
121 void write(Buffer const &, std::ostream &, BufferParams const &,
122 depth_type & depth) const;
124 void validate(LaTeXFeatures &) const;
127 int startTeXParParams(BufferParams const &, odocstream &, bool) const;
130 int endTeXParParams(BufferParams const &, odocstream &, bool) const;
134 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
135 LyXFont const & outerfont, odocstream &,
136 TexRow & texrow, OutputParams const &) const;
138 /// Can we drop the standard paragraph wrapper?
139 bool emptyTag() const;
141 /// Get the id of the paragraph, usefull for docbook
142 std::string getID(Buffer const & buf,
143 OutputParams const & runparams) const;
145 // Get the first word of a paragraph, return the position where it left
146 pos_type getFirstWord(Buffer const & buf,
148 OutputParams const & runparams) const;
150 /// Checks if the paragraph contains only text and no inset or font change.
151 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
152 pos_type initial) const;
154 /// Writes to stream the docbook representation
155 void simpleDocBookOnePar(Buffer const & buf,
157 OutputParams const & runparams,
158 LyXFont const & outerfont,
159 pos_type initial = 0) const;
162 bool hasSameLayout(Paragraph const & par) const;
165 void makeSameLayout(Paragraph const & par);
168 void setInsetOwner(InsetBase * inset);
170 InsetBase * inInset() const;
172 InsetBase::Code ownerCode() const;
174 bool forceDefaultParagraphs() const;
177 pos_type size() const { return text_.size(); }
179 bool empty() const { return text_.empty(); }
181 void setContentsFromPar(Paragraph const & par);
183 void clearContents();
186 LyXLayout_ptr const & layout() const;
188 void layout(LyXLayout_ptr const & new_layout);
190 /// This is the item depth, only used by enumerate and itemize
191 signed char itemdepth;
194 InsetBibitem * bibitem() const; // ale970302
196 /// look up change at given pos
197 Change const lookupChange(pos_type pos) const;
199 /// is there a change within the given range ?
200 bool isChanged(pos_type start, pos_type end) const;
201 /// is there an insertion at the given pos ?
202 bool isInserted(pos_type pos) const {
203 return lookupChange(pos).type == Change::INSERTED;
205 /// is there a deletion at the given pos ?
206 bool isDeleted(pos_type pos) const {
207 return lookupChange(pos).type == Change::DELETED;
210 /// set change for the entire par
211 void setChange(Change const & change);
213 /// set change at given pos
214 void setChange(pos_type pos, Change const & change);
217 void acceptChange(pos_type start, pos_type end);
220 void rejectChange(pos_type start, pos_type end);
222 /// Paragraphs can contain "manual labels", for example, Description
223 /// environment. The text for this user-editable label is stored in
224 /// the paragraph alongside the text of the rest of the paragraph
225 /// (the body). This function returns the starting position of the
226 /// body of the text in the paragraph.
227 pos_type beginOfBody() const;
228 /// recompute this value
229 void setBeginOfBody();
232 docstring const & getLabelstring() const;
234 /// the next two functions are for the manual labels
235 docstring const getLabelWidthString() const;
237 void setLabelWidthString(docstring const & s);
239 char getAlign() const;
240 /// The nesting depth of a paragraph
241 depth_type getDepth() const;
242 /// The maximal possible depth of a paragraph after this one
243 depth_type getMaxDepthAfter() const;
245 void applyLayout(LyXLayout_ptr const & new_layout);
247 /// (logically) erase the char at pos; return true if it was actually erased
248 bool eraseChar(pos_type pos, bool trackChanges);
249 /// (logically) erase the given range; return the number of chars actually erased
250 int eraseChars(pos_type start, pos_type end, bool trackChanges);
252 /** Get uninstantiated font setting. Returns the difference
253 between the characters font and the layoutfont.
254 This is what is stored in the fonttable
257 getFontSettings(BufferParams const &, pos_type pos) const;
259 LyXFont const getFirstFontSettings(BufferParams const &) const;
261 /** Get fully instantiated font. If pos == -1, use the layout
262 font attached to this paragraph.
263 If pos == -2, use the label font of the layout attached here.
264 In all cases, the font is instantiated, i.e. does not have any
265 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
268 LyXFont const getFont(BufferParams const &, pos_type pos,
269 LyXFont const & outerfont) const;
270 LyXFont const getLayoutFont(BufferParams const &,
271 LyXFont const & outerfont) const;
272 LyXFont const getLabelFont(BufferParams const &,
273 LyXFont const & outerfont) const;
275 * The font returned by the above functions is the same in a
276 * span of characters. This method will return the first and
277 * the last positions in the paragraph for which that font is
278 * the same. This can be used to avoid unnecessary calls to
281 FontSpan fontSpan(pos_type pos) const;
283 /// this is a bottleneck.
284 value_type getChar(pos_type pos) const { return text_[pos]; }
285 /// Get the char, but mirror all bracket characters if it is right-to-left
286 value_type getUChar(BufferParams const &, pos_type pos) const;
287 /// pos <= size() (there is a dummy font change at the end of each par)
288 void setFont(pos_type pos, LyXFont const & font);
289 /// Returns the height of the highest font in range
290 LyXFont_size highestFontInRange(pos_type startpos,
291 pos_type endpos, LyXFont_size def_size) const;
293 void insert(pos_type pos, docstring const & str,
294 LyXFont const & font, Change const & change);
296 void insertChar(pos_type pos, value_type c, bool trackChanges);
298 void insertChar(pos_type pos, value_type c,
299 LyXFont const &, bool trackChanges);
301 void insertChar(pos_type pos, value_type c,
302 LyXFont const &, Change const & change);
304 void insertInset(pos_type pos, InsetBase * inset,
305 Change const & change);
307 void insertInset(pos_type pos, InsetBase * inset,
308 LyXFont const &, Change const & change);
310 bool insetAllowed(InsetBase_code code);
312 InsetBase * getInset(pos_type pos) {
313 return insetlist.get(pos);
316 InsetBase const * getInset(pos_type pos) const {
317 return insetlist.get(pos);
321 bool isHfill(pos_type pos) const {
323 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
325 /// hinted by profiler
326 bool isInset(pos_type pos) const {
327 return getChar(pos) == static_cast<value_type>(META_INSET);
330 bool isNewline(pos_type pos) const;
331 /// return true if the char is a word separator
332 bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
334 bool isLineSeparator(pos_type pos) const;
335 /// True if the character/inset at this point can be part of a word
336 // Note that digits in particular are considered as letters
337 bool isLetter(pos_type pos) const;
339 /// returns -1 if inset not found
340 int getPositionOfInset(InsetBase const * inset) const;
342 /// Returns the number of line breaks and white-space stripped at the start
343 int stripLeadingSpaces();
345 /// return true if we allow multiple spaces
346 bool isFreeSpacing() const;
348 /// return true if we allow this par to stay empty
349 bool allowEmpty() const;
351 char_type transformChar(char_type c, pos_type pos) const;
353 ParagraphParameters & params();
355 ParagraphParameters const & params() const;
358 Row & getRow(pos_type pos, bool boundary);
360 Row const & getRow(pos_type pos, bool boundary) const;
362 size_t pos2row(pos_type pos) const;
364 /// total height of paragraph
365 unsigned int height() const { return dim_.height(); }
366 /// total width of paragraph, may differ from workwidth
367 unsigned int width() const { return dim_.width(); }
368 /// ascend of paragraph above baseline
369 unsigned int ascent() const { return dim_.ascent(); }
370 /// descend of paragraph below baseline
371 unsigned int descent() const { return dim_.descent(); }
372 /// LyXText updates the rows using this access point
373 RowList & rows() { return rows_; }
374 /// The painter and others use this
375 RowList const & rows() const { return rows_; }
377 RowSignature & rowSignature() const { return rowSignature_; }
379 bool hfillExpansion(Row const & row, pos_type pos) const;
381 /// LyXText::redoParagraph updates this
382 Dimension & dim() { return dim_; }
384 /// dump some information to lyxerr
392 /// cached dimensions of paragraph
396 mutable RowList rows_;
398 mutable RowSignature rowSignature_;
401 LyXLayout_ptr layout_;
403 * Keeping this here instead of in the pimpl makes LyX >10% faster
404 * for average tasks as buffer loading/switching etc.
408 pos_type begin_of_body_;
413 friend class Paragraph::Pimpl;
421 #endif // PARAGRAPH_H