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 ParagraphMetrics(ParagraphMetrics const & pm);
69 ParagraphMetrics & operator=(ParagraphMetrics const & pm);
71 Row & getRow(pos_type pos, bool boundary);
73 Row const & getRow(pos_type pos, bool boundary) const;
75 size_t pos2row(pos_type pos) const;
77 /// LyXText::redoParagraph updates this
78 Dimension & dim() { return dim_; }
79 /// total height of paragraph
80 unsigned int height() const { return dim_.height(); }
81 /// total width of paragraph, may differ from workwidth
82 unsigned int width() const { return dim_.width(); }
83 /// ascend of paragraph above baseline
84 unsigned int ascent() const { return dim_.ascent(); }
85 /// descend of paragraph below baseline
86 unsigned int descent() const { return dim_.descent(); }
87 /// LyXText updates the rows using this access point
88 RowList & rows() { return rows_; }
89 /// The painter and others use this
90 RowList const & rows() const { return rows_; }
92 RowSignature & rowSignature() const { return rowSignature_; }
94 /// dump some information to lyxerr
99 mutable RowList rows_;
101 mutable RowSignature rowSignature_;
102 /// cached dimensions of paragraph
107 /// A Paragraph holds all text, attributes and insets in a text paragraph
108 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
109 /// should go in order to complete the Model/View separation of this class.
110 class Paragraph: public ParagraphMetrics {
114 /// Note that this is 1 right now to avoid
115 /// crashes where getChar() is called wrongly
116 /// (returning 0) - if this was 0, then we'd
117 /// try getInset() and crash. We should fix
118 /// all these places.
119 //META_INSET = 1 // as in trunk
120 META_INSET = 0x200001 // above 0x10ffff, for ucs-4
123 typedef char_type value_type;
125 typedef std::vector<value_type> TextContainer;
130 Paragraph(Paragraph const &);
132 Paragraph & operator=(Paragraph const &);
140 Language const * getParLanguage(BufferParams const &) const;
142 bool isRightToLeftPar(BufferParams const &) const;
144 void changeLanguage(BufferParams const & bparams,
145 Language const * from, Language const * to);
147 bool isMultiLingual(BufferParams const &) const;
150 docstring const asString(Buffer const &,
151 OutputParams const & runparams,
154 docstring const asString(Buffer const &, bool label) const;
156 docstring const asString(Buffer const & buffer,
161 docstring const asString(Buffer const &,
162 OutputParams const & runparams,
168 void write(Buffer const &, std::ostream &, BufferParams const &,
169 depth_type & depth) const;
171 void validate(LaTeXFeatures &) const;
174 int startTeXParParams(BufferParams const &, odocstream &, bool) const;
177 int endTeXParParams(BufferParams const &, odocstream &, bool) const;
181 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
182 LyXFont const & outerfont, odocstream &,
183 TexRow & texrow, OutputParams const &) const;
185 /// Can we drop the standard paragraph wrapper?
186 bool emptyTag() const;
188 /// Get the id of the paragraph, usefull for docbook
189 std::string getID(Buffer const & buf,
190 OutputParams const & runparams) const;
192 // Get the first word of a paragraph, return the position where it left
193 pos_type getFirstWord(Buffer const & buf,
195 OutputParams const & runparams) const;
197 /// Checks if the paragraph contains only text and no inset or font change.
198 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
199 pos_type initial) const;
201 /// Writes to stream the docbook representation
202 void simpleDocBookOnePar(Buffer const & buf,
204 OutputParams const & runparams,
205 LyXFont const & outerfont,
206 pos_type initial = 0) const;
209 bool hasSameLayout(Paragraph const & par) const;
212 void makeSameLayout(Paragraph const & par);
215 void setInsetOwner(InsetBase * inset);
217 InsetBase * inInset() const;
219 InsetBase::Code ownerCode() const;
221 bool forceDefaultParagraphs() const;
224 pos_type size() const { return text_.size(); }
226 bool empty() const { return text_.empty(); }
228 void clearContents();
231 LyXLayout_ptr const & layout() const;
233 void layout(LyXLayout_ptr const & new_layout);
235 /// This is the item depth, only used by enumerate and itemize
236 signed char itemdepth;
239 InsetBibitem * bibitem() const; // ale970302
241 /// look up change at given pos
242 Change const lookupChange(pos_type pos) const;
244 /// is there a change within the given range ?
245 bool isChanged(pos_type start, pos_type end) const;
246 /// is there an unchanged char at the given pos ?
247 bool isUnchanged(pos_type pos) const {
248 return lookupChange(pos).type == Change::UNCHANGED;
250 /// is there an insertion at the given pos ?
251 bool isInserted(pos_type pos) const {
252 return lookupChange(pos).type == Change::INSERTED;
254 /// is there a deletion at the given pos ?
255 bool isDeleted(pos_type pos) const {
256 return lookupChange(pos).type == Change::DELETED;
259 /// will the paragraph be physically merged with the next
260 /// one if the imaginary end-of-par character is logically deleted?
261 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
263 /// set change for the entire par
264 void setChange(Change const & change);
266 /// set change at given pos
267 void setChange(pos_type pos, Change const & change);
269 /// accept changes within the given range
270 void acceptChanges(pos_type start, pos_type end);
272 /// reject changes within the given range
273 void rejectChanges(pos_type start, pos_type end);
275 /// Paragraphs can contain "manual labels", for example, Description
276 /// environment. The text for this user-editable label is stored in
277 /// the paragraph alongside the text of the rest of the paragraph
278 /// (the body). This function returns the starting position of the
279 /// body of the text in the paragraph.
280 pos_type beginOfBody() const;
281 /// recompute this value
282 void setBeginOfBody();
285 docstring const & getLabelstring() const;
287 /// the next two functions are for the manual labels
288 docstring const getLabelWidthString() const;
290 void setLabelWidthString(docstring const & s);
291 /// Actual paragraph alignment used
292 char getAlign() const;
293 /// The nesting depth of a paragraph
294 depth_type getDepth() const;
295 /// The maximal possible depth of a paragraph after this one
296 depth_type getMaxDepthAfter() const;
298 void applyLayout(LyXLayout_ptr const & new_layout);
300 /// (logically) erase the char at pos; return true if it was actually erased
301 bool eraseChar(pos_type pos, bool trackChanges);
302 /// (logically) erase the given range; return the number of chars actually erased
303 int eraseChars(pos_type start, pos_type end, bool trackChanges);
305 /** Get uninstantiated font setting. Returns the difference
306 between the characters font and the layoutfont.
307 This is what is stored in the fonttable
310 getFontSettings(BufferParams const &, pos_type pos) const;
312 LyXFont const getFirstFontSettings(BufferParams const &) const;
314 /** Get fully instantiated font. If pos == -1, use the layout
315 font attached to this paragraph.
316 If pos == -2, use the label font of the layout attached here.
317 In all cases, the font is instantiated, i.e. does not have any
318 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
321 LyXFont const getFont(BufferParams const &, pos_type pos,
322 LyXFont const & outerfont) const;
323 LyXFont const getLayoutFont(BufferParams const &,
324 LyXFont const & outerfont) const;
325 LyXFont const getLabelFont(BufferParams const &,
326 LyXFont const & outerfont) const;
328 * The font returned by the above functions is the same in a
329 * span of characters. This method will return the first and
330 * the last positions in the paragraph for which that font is
331 * the same. This can be used to avoid unnecessary calls to
334 FontSpan fontSpan(pos_type pos) const;
336 /// this is a bottleneck.
337 value_type getChar(pos_type pos) const { return text_[pos]; }
338 /// Get the char, but mirror all bracket characters if it is right-to-left
339 value_type getUChar(BufferParams const &, pos_type pos) const;
340 /// pos <= size() (there is a dummy font change at the end of each par)
341 void setFont(pos_type pos, LyXFont const & font);
342 /// Returns the height of the highest font in range
343 LyXFont_size highestFontInRange(pos_type startpos,
344 pos_type endpos, LyXFont_size def_size) const;
346 void insert(pos_type pos, docstring const & str,
347 LyXFont const & font, Change const & change);
349 void insertChar(pos_type pos, value_type c, bool trackChanges);
351 void insertChar(pos_type pos, value_type c,
352 LyXFont const &, bool trackChanges);
354 void insertChar(pos_type pos, value_type c,
355 LyXFont const &, Change const & change);
357 void insertInset(pos_type pos, InsetBase * inset,
358 Change const & change);
360 void insertInset(pos_type pos, InsetBase * inset,
361 LyXFont const &, Change const & change);
363 bool insetAllowed(InsetBase_code code);
365 InsetBase * getInset(pos_type pos) {
366 return insetlist.get(pos);
369 InsetBase const * getInset(pos_type pos) const {
370 return insetlist.get(pos);
374 bool isHfill(pos_type pos) const {
376 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
378 /// hinted by profiler
379 bool isInset(pos_type pos) const {
380 return getChar(pos) == static_cast<value_type>(META_INSET);
383 bool isNewline(pos_type pos) const;
384 /// return true if the char is a word separator
385 bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
387 bool isLineSeparator(pos_type pos) const;
388 /// True if the character/inset at this point can be part of a word
389 // Note that digits in particular are considered as letters
390 bool isLetter(pos_type pos) const;
392 /// returns -1 if inset not found
393 int getPositionOfInset(InsetBase const * inset) const;
395 /// Returns the number of line breaks and white-space stripped at the start
396 int stripLeadingSpaces();
398 /// return true if we allow multiple spaces
399 bool isFreeSpacing() const;
401 /// return true if we allow this par to stay empty
402 bool allowEmpty() const;
404 char_type transformChar(char_type c, pos_type pos) const;
406 ParagraphParameters & params();
408 ParagraphParameters const & params() const;
410 bool hfillExpansion(Row const & row, pos_type pos) const;
419 LyXLayout_ptr layout_;
421 * Keeping this here instead of in the pimpl makes LyX >10% faster
422 * for average tasks as buffer loading/switching etc.
426 pos_type begin_of_body_;
431 friend class Paragraph::Pimpl;
438 #endif // PARAGRAPH_H