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.
61 /// Helper class for Paragraph Metrics.
62 /// \todo FIXME: this class deserves its own .[Ch] files.
63 /// Then, the storage of such object should be done in \c BufferView
64 /// (most probably in the \c CoordCache class along \c Point objects).
65 class ParagraphMetrics {
68 Row & getRow(pos_type pos, bool boundary);
70 Row const & getRow(pos_type pos, bool boundary) const;
72 size_t pos2row(pos_type pos) const;
74 /// LyXText::redoParagraph updates this
75 Dimension & dim() { return dim_; }
76 /// total height of paragraph
77 unsigned int height() const { return dim_.height(); }
78 /// total width of paragraph, may differ from workwidth
79 unsigned int width() const { return dim_.width(); }
80 /// ascend of paragraph above baseline
81 unsigned int ascent() const { return dim_.ascent(); }
82 /// descend of paragraph below baseline
83 unsigned int descent() const { return dim_.descent(); }
84 /// LyXText updates the rows using this access point
85 RowList & rows() { return rows_; }
86 /// The painter and others use this
87 RowList const & rows() const { return rows_; }
89 RowSignature & rowSignature() const { return rowSignature_; }
91 /// dump some information to lyxerr
96 mutable RowList rows_;
98 mutable RowSignature rowSignature_;
99 /// cached dimensions of paragraph
104 /// A Paragraph holds all text, attributes and insets in a text paragraph
105 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
106 /// should go in order to complete the Model/View separation of this class.
107 class Paragraph: public ParagraphMetrics {
111 /// Note that this is 1 right now to avoid
112 /// crashes where getChar() is called wrongly
113 /// (returning 0) - if this was 0, then we'd
114 /// try getInset() and crash. We should fix
115 /// all these places.
116 //META_INSET = 1 // as in trunk
117 META_INSET = 0x200001 // above 0x10ffff, for ucs-4
120 typedef char_type value_type;
122 typedef std::vector<value_type> TextContainer;
127 Paragraph(Paragraph const &);
129 Paragraph & operator=(Paragraph const &);
137 Language const * getParLanguage(BufferParams const &) const;
139 bool isRightToLeftPar(BufferParams const &) const;
141 void changeLanguage(BufferParams const & bparams,
142 Language const * from, Language const * to);
144 bool isMultiLingual(BufferParams const &) const;
147 docstring const asString(Buffer const &,
148 OutputParams const & runparams,
151 docstring const asString(Buffer const &, bool label) const;
153 docstring const asString(Buffer const & buffer,
158 docstring const asString(Buffer const &,
159 OutputParams const & runparams,
165 void write(Buffer const &, std::ostream &, BufferParams const &,
166 depth_type & depth) const;
168 void validate(LaTeXFeatures &) const;
171 int startTeXParParams(BufferParams const &, odocstream &, bool) const;
174 int endTeXParParams(BufferParams const &, odocstream &, bool) const;
178 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
179 LyXFont const & outerfont, odocstream &,
180 TexRow & texrow, OutputParams const &) const;
182 /// Can we drop the standard paragraph wrapper?
183 bool emptyTag() const;
185 /// Get the id of the paragraph, usefull for docbook
186 std::string getID(Buffer const & buf,
187 OutputParams const & runparams) const;
189 // Get the first word of a paragraph, return the position where it left
190 pos_type getFirstWord(Buffer const & buf,
192 OutputParams const & runparams) const;
194 /// Checks if the paragraph contains only text and no inset or font change.
195 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
196 pos_type initial) const;
198 /// Writes to stream the docbook representation
199 void simpleDocBookOnePar(Buffer const & buf,
201 OutputParams const & runparams,
202 LyXFont const & outerfont,
203 pos_type initial = 0) const;
206 bool hasSameLayout(Paragraph const & par) const;
209 void makeSameLayout(Paragraph const & par);
212 void setInsetOwner(InsetBase * inset);
214 InsetBase * inInset() const;
216 InsetBase::Code ownerCode() const;
218 bool forceDefaultParagraphs() const;
221 pos_type size() const { return text_.size(); }
223 bool empty() const { return text_.empty(); }
225 void clearContents();
228 LyXLayout_ptr const & layout() const;
230 void layout(LyXLayout_ptr const & new_layout);
232 /// This is the item depth, only used by enumerate and itemize
233 signed char itemdepth;
236 InsetBibitem * bibitem() const; // ale970302
238 /// look up change at given pos
239 Change const lookupChange(pos_type pos) const;
241 /// is there a change within the given range ?
242 bool isChanged(pos_type start, pos_type end) const;
243 /// is there an unchanged char at the given pos ?
244 bool isUnchanged(pos_type pos) const {
245 return lookupChange(pos).type == Change::UNCHANGED;
247 /// is there an insertion at the given pos ?
248 bool isInserted(pos_type pos) const {
249 return lookupChange(pos).type == Change::INSERTED;
251 /// is there a deletion at the given pos ?
252 bool isDeleted(pos_type pos) const {
253 return lookupChange(pos).type == Change::DELETED;
256 /// will the paragraph be physically merged with the next
257 /// one if the imaginary end-of-par character is logically deleted?
258 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
260 /// set change for the entire par
261 void setChange(Change const & change);
263 /// set change at given pos
264 void setChange(pos_type pos, Change const & change);
266 /// accept changes within the given range
267 void acceptChanges(pos_type start, pos_type end);
269 /// reject changes within the given range
270 void rejectChanges(pos_type start, pos_type end);
272 /// Paragraphs can contain "manual labels", for example, Description
273 /// environment. The text for this user-editable label is stored in
274 /// the paragraph alongside the text of the rest of the paragraph
275 /// (the body). This function returns the starting position of the
276 /// body of the text in the paragraph.
277 pos_type beginOfBody() const;
278 /// recompute this value
279 void setBeginOfBody();
282 docstring const & getLabelstring() const;
284 /// the next two functions are for the manual labels
285 docstring const getLabelWidthString() const;
287 void setLabelWidthString(docstring const & s);
288 /// Actual paragraph alignment used
289 char getAlign() const;
290 /// The nesting depth of a paragraph
291 depth_type getDepth() const;
292 /// The maximal possible depth of a paragraph after this one
293 depth_type getMaxDepthAfter() const;
295 void applyLayout(LyXLayout_ptr const & new_layout);
297 /// (logically) erase the char at pos; return true if it was actually erased
298 bool eraseChar(pos_type pos, bool trackChanges);
299 /// (logically) erase the given range; return the number of chars actually erased
300 int eraseChars(pos_type start, pos_type end, bool trackChanges);
302 /** Get uninstantiated font setting. Returns the difference
303 between the characters font and the layoutfont.
304 This is what is stored in the fonttable
307 getFontSettings(BufferParams const &, pos_type pos) const;
309 LyXFont const getFirstFontSettings(BufferParams const &) const;
311 /** Get fully instantiated font. If pos == -1, use the layout
312 font attached to this paragraph.
313 If pos == -2, use the label font of the layout attached here.
314 In all cases, the font is instantiated, i.e. does not have any
315 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
318 LyXFont const getFont(BufferParams const &, pos_type pos,
319 LyXFont const & outerfont) const;
320 LyXFont const getLayoutFont(BufferParams const &,
321 LyXFont const & outerfont) const;
322 LyXFont const getLabelFont(BufferParams const &,
323 LyXFont const & outerfont) const;
325 * The font returned by the above functions is the same in a
326 * span of characters. This method will return the first and
327 * the last positions in the paragraph for which that font is
328 * the same. This can be used to avoid unnecessary calls to
331 FontSpan fontSpan(pos_type pos) const;
333 /// this is a bottleneck.
334 value_type getChar(pos_type pos) const { return text_[pos]; }
335 /// Get the char, but mirror all bracket characters if it is right-to-left
336 value_type getUChar(BufferParams const &, pos_type pos) const;
337 /// pos <= size() (there is a dummy font change at the end of each par)
338 void setFont(pos_type pos, LyXFont const & font);
339 /// Returns the height of the highest font in range
340 LyXFont_size highestFontInRange(pos_type startpos,
341 pos_type endpos, LyXFont_size def_size) const;
343 void insert(pos_type pos, docstring const & str,
344 LyXFont const & font, Change const & change);
346 void insertChar(pos_type pos, value_type c, bool trackChanges);
348 void insertChar(pos_type pos, value_type c,
349 LyXFont const &, bool trackChanges);
351 void insertChar(pos_type pos, value_type c,
352 LyXFont const &, Change const & change);
354 void insertInset(pos_type pos, InsetBase * inset,
355 Change const & change);
357 void insertInset(pos_type pos, InsetBase * inset,
358 LyXFont const &, Change const & change);
360 bool insetAllowed(InsetBase_code code);
362 InsetBase * getInset(pos_type pos) {
363 return insetlist.get(pos);
366 InsetBase const * getInset(pos_type pos) const {
367 return insetlist.get(pos);
371 bool isHfill(pos_type pos) const {
373 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
375 /// hinted by profiler
376 bool isInset(pos_type pos) const {
377 return getChar(pos) == static_cast<value_type>(META_INSET);
380 bool isNewline(pos_type pos) const;
381 /// return true if the char is a word separator
382 bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
384 bool isLineSeparator(pos_type pos) const;
385 /// True if the character/inset at this point can be part of a word
386 // Note that digits in particular are considered as letters
387 bool isLetter(pos_type pos) const;
389 /// returns -1 if inset not found
390 int getPositionOfInset(InsetBase const * inset) const;
392 /// Returns the number of line breaks and white-space stripped at the start
393 int stripLeadingSpaces();
395 /// return true if we allow multiple spaces
396 bool isFreeSpacing() const;
398 /// return true if we allow this par to stay empty
399 bool allowEmpty() const;
401 char_type transformChar(char_type c, pos_type pos) const;
403 ParagraphParameters & params();
405 ParagraphParameters const & params() const;
407 bool hfillExpansion(Row const & row, pos_type pos) const;
416 LyXLayout_ptr layout_;
418 * Keeping this here instead of in the pimpl makes LyX >10% faster
419 * for average tasks as buffer loading/switching etc.
423 pos_type begin_of_body_;
428 friend class Paragraph::Pimpl;
435 #endif // PARAGRAPH_H