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.
61 /// A Paragraph holds all text, attributes and insets in a text paragraph
62 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
63 /// should go in order to complete the Model/View separation of this class.
68 /// Note that this is 1 right now to avoid
69 /// crashes where getChar() is called wrongly
70 /// (returning 0) - if this was 0, then we'd
71 /// try getInset() and crash. We should fix
73 //META_INSET = 1 // as in trunk
74 META_INSET = 0x200001 // above 0x10ffff, for ucs-4
77 typedef char_type value_type;
79 typedef std::vector<value_type> TextContainer;
84 Paragraph(Paragraph const &);
86 Paragraph & operator=(Paragraph const &);
94 Language const * getParLanguage(BufferParams const &) const;
96 bool isRightToLeftPar(BufferParams const &) const;
98 void changeLanguage(BufferParams const & bparams,
99 Language const * from, Language const * to);
101 bool isMultiLingual(BufferParams const &) const;
104 docstring const asString(Buffer const &,
105 OutputParams const & runparams,
108 docstring const asString(Buffer const &, bool label) const;
110 docstring const asString(Buffer const & buffer,
115 docstring const asString(Buffer const &,
116 OutputParams const & runparams,
122 void write(Buffer const &, std::ostream &, BufferParams const &,
123 depth_type & depth) const;
125 void validate(LaTeXFeatures &) const;
128 int startTeXParParams(BufferParams const &, odocstream &, bool) const;
131 int endTeXParParams(BufferParams const &, odocstream &, bool) const;
135 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
136 LyXFont const & outerfont, odocstream &,
137 TexRow & texrow, OutputParams const &) const;
139 /// Can we drop the standard paragraph wrapper?
140 bool emptyTag() const;
142 /// Get the id of the paragraph, usefull for docbook
143 std::string getID(Buffer const & buf,
144 OutputParams const & runparams) const;
146 /// Get the first word of a paragraph, return the position where it left
147 pos_type getFirstWord(Buffer const & buf,
149 OutputParams const & runparams) const;
151 /// Checks if the paragraph contains only text and no inset or font change.
152 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
153 pos_type initial) const;
155 /// Writes to stream the docbook representation
156 void simpleDocBookOnePar(Buffer const & buf,
158 OutputParams const & runparams,
159 LyXFont const & outerfont,
160 pos_type initial = 0) const;
163 bool hasSameLayout(Paragraph const & par) const;
166 void makeSameLayout(Paragraph const & par);
169 void setInsetOwner(InsetBase * inset);
171 InsetBase * inInset() const;
173 InsetBase::Code ownerCode() const;
175 bool forceDefaultParagraphs() const;
178 pos_type size() const { return text_.size(); }
180 bool empty() const { return text_.empty(); }
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(pos_type pos) const;
198 /// is there a change within the given range ?
199 bool isChanged(pos_type start, pos_type end) const;
200 /// is there an unchanged char at the given pos ?
201 bool isUnchanged(pos_type pos) const {
202 return lookupChange(pos).type == Change::UNCHANGED;
204 /// is there an insertion at the given pos ?
205 bool isInserted(pos_type pos) const {
206 return lookupChange(pos).type == Change::INSERTED;
208 /// is there a deletion at the given pos ?
209 bool isDeleted(pos_type pos) const {
210 return lookupChange(pos).type == Change::DELETED;
213 /// will the paragraph be physically merged with the next
214 /// one if the imaginary end-of-par character is logically deleted?
215 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
217 /// set change for the entire par
218 void setChange(Change const & change);
220 /// set change at given pos
221 void setChange(pos_type pos, Change const & change);
223 /// accept changes within the given range
224 void acceptChanges(pos_type start, pos_type end);
226 /// reject changes within the given range
227 void rejectChanges(pos_type start, pos_type end);
229 /// Paragraphs can contain "manual labels", for example, Description
230 /// environment. The text for this user-editable label is stored in
231 /// the paragraph alongside the text of the rest of the paragraph
232 /// (the body). This function returns the starting position of the
233 /// body of the text in the paragraph.
234 pos_type beginOfBody() const;
235 /// recompute this value
236 void setBeginOfBody();
239 docstring const & getLabelstring() const;
241 /// the next two functions are for the manual labels
242 docstring const getLabelWidthString() const;
244 void setLabelWidthString(docstring const & s);
245 /// Actual paragraph alignment used
246 char getAlign() const;
247 /// The nesting depth of a paragraph
248 depth_type getDepth() const;
249 /// The maximal possible depth of a paragraph after this one
250 depth_type getMaxDepthAfter() const;
252 void applyLayout(LyXLayout_ptr const & new_layout);
254 /// (logically) erase the char at pos; return true if it was actually erased
255 bool eraseChar(pos_type pos, bool trackChanges);
256 /// (logically) erase the given range; return the number of chars actually erased
257 int eraseChars(pos_type start, pos_type end, bool trackChanges);
259 /** Get uninstantiated font setting. Returns the difference
260 between the characters font and the layoutfont.
261 This is what is stored in the fonttable
264 getFontSettings(BufferParams const &, pos_type pos) const;
266 LyXFont const getFirstFontSettings(BufferParams const &) const;
268 /** Get fully instantiated font. If pos == -1, use the layout
269 font attached to this paragraph.
270 If pos == -2, use the label font of the layout attached here.
271 In all cases, the font is instantiated, i.e. does not have any
272 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
275 LyXFont const getFont(BufferParams const &, pos_type pos,
276 LyXFont const & outerfont) const;
277 LyXFont const getLayoutFont(BufferParams const &,
278 LyXFont const & outerfont) const;
279 LyXFont const getLabelFont(BufferParams const &,
280 LyXFont const & outerfont) const;
282 * The font returned by the above functions is the same in a
283 * span of characters. This method will return the first and
284 * the last positions in the paragraph for which that font is
285 * the same. This can be used to avoid unnecessary calls to getFont.
287 FontSpan fontSpan(pos_type pos) const;
289 /// this is a bottleneck.
290 value_type getChar(pos_type pos) const { return text_[pos]; }
291 /// Get the char, but mirror all bracket characters if it is right-to-left
292 value_type getUChar(BufferParams const &, pos_type pos) const;
293 /// pos <= size() (there is a dummy font change at the end of each par)
294 void setFont(pos_type pos, LyXFont const & font);
295 /// Returns the height of the highest font in range
296 LyXFont_size highestFontInRange(pos_type startpos,
297 pos_type endpos, LyXFont_size def_size) const;
299 void insert(pos_type pos, docstring const & str,
300 LyXFont const & font, Change const & change);
302 void insertChar(pos_type pos, value_type c, bool trackChanges);
304 void insertChar(pos_type pos, value_type c,
305 LyXFont const &, bool trackChanges);
307 void insertChar(pos_type pos, value_type c,
308 LyXFont const &, Change const & change);
310 void insertInset(pos_type pos, InsetBase * inset,
311 Change const & change);
313 void insertInset(pos_type pos, InsetBase * inset,
314 LyXFont const &, Change const & change);
316 bool insetAllowed(InsetBase_code code);
318 InsetBase * getInset(pos_type pos) {
319 return insetlist.get(pos);
322 InsetBase const * getInset(pos_type pos) const {
323 return insetlist.get(pos);
327 bool isHfill(pos_type pos) const {
329 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
331 /// hinted by profiler
332 bool isInset(pos_type pos) const {
333 return getChar(pos) == static_cast<value_type>(META_INSET);
336 bool isNewline(pos_type pos) const;
337 /// return true if the char is a word separator
338 bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
340 bool isLineSeparator(pos_type pos) const;
341 /// True if the character/inset at this point can be part of a word.
342 /// Note that digits in particular are considered as letters
343 bool isLetter(pos_type pos) const;
345 /// returns -1 if inset not found
346 int getPositionOfInset(InsetBase const * inset) const;
348 /// Returns the number of line breaks and white-space stripped at the start
349 int stripLeadingSpaces();
351 /// return true if we allow multiple spaces
352 bool isFreeSpacing() const;
354 /// return true if we allow this par to stay empty
355 bool allowEmpty() const;
357 char_type transformChar(char_type c, pos_type pos) const;
359 ParagraphParameters & params();
361 ParagraphParameters const & params() const;
363 bool hfillExpansion(Row const & row, pos_type pos) const;
365 /// Check if we are in a Biblio environment.
366 /// \retval true if the cursor needs to be moved right.
367 bool checkBiblio(bool track_changes);
376 LyXLayout_ptr layout_;
378 * Keeping this here instead of in the pimpl makes LyX >10% faster
379 * for average tasks as buffer loading/switching etc.
383 pos_type begin_of_body_;
388 friend class Paragraph::Pimpl;
395 #endif // PARAGRAPH_H