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
44 class ParagraphParameters;
50 /// Invalid font span containing no character
51 FontSpan() : first(0), last(-1) {}
52 /// Span including first and last
53 FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
56 /// Range including first and last.
60 /// Helper class for Paragraph Metrics.
61 /// \todo FIXME: this class deserves its own .[Ch] files.
62 /// Then, the storage of such object should be done in \c BufferView
63 /// (most probably in the \c CoordCache class along \c Point objects).
64 class ParagraphMetrics {
67 Row & getRow(pos_type pos, bool boundary);
69 Row const & getRow(pos_type pos, bool boundary) const;
71 size_t pos2row(pos_type pos) const;
73 /// LyXText::redoParagraph updates this
74 Dimension & dim() { return dim_; }
75 /// total height of paragraph
76 unsigned int height() const { return dim_.height(); }
77 /// total width of paragraph, may differ from workwidth
78 unsigned int width() const { return dim_.width(); }
79 /// ascend of paragraph above baseline
80 unsigned int ascent() const { return dim_.ascent(); }
81 /// descend of paragraph below baseline
82 unsigned int descent() const { return dim_.descent(); }
83 /// LyXText updates the rows using this access point
84 RowList & rows() { return rows_; }
85 /// The painter and others use this
86 RowList const & rows() const { return rows_; }
88 RowSignature & rowSignature() const { return rowSignature_; }
90 /// dump some information to lyxerr
95 mutable RowList rows_;
97 mutable RowSignature rowSignature_;
98 /// cached dimensions of paragraph
103 /// A Paragraph holds all text, attributes and insets in a text paragraph
104 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
105 /// should go in order to complete the Model/View separation of this class.
106 class Paragraph: public ParagraphMetrics {
110 /// Note that this is 1 right now to avoid
111 /// crashes where getChar() is called wrongly
112 /// (returning 0) - if this was 0, then we'd
113 /// try getInset() and crash. We should fix
114 /// all these places.
115 //META_INSET = 1 // as in trunk
116 META_INSET = 0x200001 // above 0x10ffff, for ucs-4
119 typedef char_type value_type;
121 typedef std::vector<value_type> TextContainer;
126 Paragraph(Paragraph const &);
128 Paragraph & operator=(Paragraph const &);
136 Language const * getParLanguage(BufferParams const &) const;
138 bool isRightToLeftPar(BufferParams const &) const;
140 void changeLanguage(BufferParams const & bparams,
141 Language const * from, Language const * to);
143 bool isMultiLingual(BufferParams const &) const;
146 docstring const asString(Buffer const &,
147 OutputParams const & runparams,
150 docstring const asString(Buffer const &, bool label) const;
152 docstring const asString(Buffer const & buffer,
157 docstring const asString(Buffer const &,
158 OutputParams const & runparams,
164 void write(Buffer const &, std::ostream &, BufferParams const &,
165 depth_type & depth) const;
167 void validate(LaTeXFeatures &) const;
170 int startTeXParParams(BufferParams const &, odocstream &, bool) const;
173 int endTeXParParams(BufferParams const &, odocstream &, bool) const;
177 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
178 LyXFont const & outerfont, odocstream &,
179 TexRow & texrow, OutputParams const &) const;
181 /// Can we drop the standard paragraph wrapper?
182 bool emptyTag() const;
184 /// Get the id of the paragraph, usefull for docbook
185 std::string getID(Buffer const & buf,
186 OutputParams const & runparams) const;
188 /// Get the first word of a paragraph, return the position where it left
189 pos_type getFirstWord(Buffer const & buf,
191 OutputParams const & runparams) const;
193 /// Checks if the paragraph contains only text and no inset or font change.
194 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
195 pos_type initial) const;
197 /// Writes to stream the docbook representation
198 void simpleDocBookOnePar(Buffer const & buf,
200 OutputParams const & runparams,
201 LyXFont const & outerfont,
202 pos_type initial = 0) const;
205 bool hasSameLayout(Paragraph const & par) const;
208 void makeSameLayout(Paragraph const & par);
211 void setInsetOwner(InsetBase * inset);
213 InsetBase * inInset() const;
215 InsetBase::Code ownerCode() const;
217 bool forceDefaultParagraphs() const;
220 pos_type size() const { return text_.size(); }
222 bool empty() const { return text_.empty(); }
224 void clearContents();
227 LyXLayout_ptr const & layout() const;
229 void layout(LyXLayout_ptr const & new_layout);
231 /// This is the item depth, only used by enumerate and itemize
232 signed char itemdepth;
235 InsetBibitem * bibitem() const; // ale970302
237 /// look up change at given pos
238 Change const lookupChange(pos_type pos) const;
240 /// is there a change within the given range ?
241 bool isChanged(pos_type start, pos_type end) const;
242 /// is there an unchanged char at the given pos ?
243 bool isUnchanged(pos_type pos) const {
244 return lookupChange(pos).type == Change::UNCHANGED;
246 /// is there an insertion at the given pos ?
247 bool isInserted(pos_type pos) const {
248 return lookupChange(pos).type == Change::INSERTED;
250 /// is there a deletion at the given pos ?
251 bool isDeleted(pos_type pos) const {
252 return lookupChange(pos).type == Change::DELETED;
255 /// will the paragraph be physically merged with the next
256 /// one if the imaginary end-of-par character is logically deleted?
257 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
259 /// set change for the entire par
260 void setChange(Change const & change);
262 /// set change at given pos
263 void setChange(pos_type pos, Change const & change);
265 /// accept changes within the given range
266 void acceptChanges(pos_type start, pos_type end);
268 /// reject changes within the given range
269 void rejectChanges(pos_type start, pos_type end);
271 /// Paragraphs can contain "manual labels", for example, Description
272 /// environment. The text for this user-editable label is stored in
273 /// the paragraph alongside the text of the rest of the paragraph
274 /// (the body). This function returns the starting position of the
275 /// body of the text in the paragraph.
276 pos_type beginOfBody() const;
277 /// recompute this value
278 void setBeginOfBody();
281 docstring const & getLabelstring() const;
283 /// the next two functions are for the manual labels
284 docstring const getLabelWidthString() const;
286 void setLabelWidthString(docstring const & s);
287 /// Actual paragraph alignment used
288 char getAlign() const;
289 /// The nesting depth of a paragraph
290 depth_type getDepth() const;
291 /// The maximal possible depth of a paragraph after this one
292 depth_type getMaxDepthAfter() const;
294 void applyLayout(LyXLayout_ptr const & new_layout);
296 /// (logically) erase the char at pos; return true if it was actually erased
297 bool eraseChar(pos_type pos, bool trackChanges);
298 /// (logically) erase the given range; return the number of chars actually erased
299 int eraseChars(pos_type start, pos_type end, bool trackChanges);
301 /** Get uninstantiated font setting. Returns the difference
302 between the characters font and the layoutfont.
303 This is what is stored in the fonttable
306 getFontSettings(BufferParams const &, pos_type pos) const;
308 LyXFont const getFirstFontSettings(BufferParams const &) const;
310 /** Get fully instantiated font. If pos == -1, use the layout
311 font attached to this paragraph.
312 If pos == -2, use the label font of the layout attached here.
313 In all cases, the font is instantiated, i.e. does not have any
314 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
317 LyXFont const getFont(BufferParams const &, pos_type pos,
318 LyXFont const & outerfont) const;
319 LyXFont const getLayoutFont(BufferParams const &,
320 LyXFont const & outerfont) const;
321 LyXFont const getLabelFont(BufferParams const &,
322 LyXFont const & outerfont) const;
324 * The font returned by the above functions is the same in a
325 * span of characters. This method will return the first and
326 * the last positions in the paragraph for which that font is
327 * the same. This can be used to avoid unnecessary calls to getFont.
329 FontSpan fontSpan(pos_type pos) const;
331 /// this is a bottleneck.
332 value_type getChar(pos_type pos) const { return text_[pos]; }
333 /// Get the char, but mirror all bracket characters if it is right-to-left
334 value_type getUChar(BufferParams const &, pos_type pos) const;
335 /// pos <= size() (there is a dummy font change at the end of each par)
336 void setFont(pos_type pos, LyXFont const & font);
337 /// Returns the height of the highest font in range
338 LyXFont_size highestFontInRange(pos_type startpos,
339 pos_type endpos, LyXFont_size def_size) const;
341 void insert(pos_type pos, docstring const & str,
342 LyXFont const & font, Change const & change);
344 void insertChar(pos_type pos, value_type c, bool trackChanges);
346 void insertChar(pos_type pos, value_type c,
347 LyXFont const &, bool trackChanges);
349 void insertChar(pos_type pos, value_type c,
350 LyXFont const &, Change const & change);
352 void insertInset(pos_type pos, InsetBase * inset,
353 Change const & change);
355 void insertInset(pos_type pos, InsetBase * inset,
356 LyXFont const &, Change const & change);
358 bool insetAllowed(InsetBase_code code);
360 InsetBase * getInset(pos_type pos) {
361 return insetlist.get(pos);
364 InsetBase const * getInset(pos_type pos) const {
365 return insetlist.get(pos);
369 bool isHfill(pos_type pos) const {
371 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
373 /// hinted by profiler
374 bool isInset(pos_type pos) const {
375 return getChar(pos) == static_cast<value_type>(META_INSET);
378 bool isNewline(pos_type pos) const;
379 /// return true if the char is a word separator
380 bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
382 bool isLineSeparator(pos_type pos) const;
383 /// True if the character/inset at this point can be part of a word.
384 /// Note that digits in particular are considered as letters
385 bool isLetter(pos_type pos) const;
387 /// returns -1 if inset not found
388 int getPositionOfInset(InsetBase const * inset) const;
390 /// Returns the number of line breaks and white-space stripped at the start
391 int stripLeadingSpaces();
393 /// return true if we allow multiple spaces
394 bool isFreeSpacing() const;
396 /// return true if we allow this par to stay empty
397 bool allowEmpty() const;
399 char_type transformChar(char_type c, pos_type pos) const;
401 ParagraphParameters & params();
403 ParagraphParameters const & params() const;
405 bool hfillExpansion(Row const & row, pos_type pos) const;
414 LyXLayout_ptr layout_;
416 * Keeping this here instead of in the pimpl makes LyX >10% faster
417 * for average tasks as buffer loading/switching etc.
421 pos_type begin_of_body_;
426 friend class Paragraph::Pimpl;
433 #endif // PARAGRAPH_H