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 &, bool label) const;
106 docstring const asString(Buffer const & buffer,
112 void write(Buffer const &, std::ostream &, BufferParams const &,
113 depth_type & depth) const;
115 void validate(LaTeXFeatures &) const;
118 int startTeXParParams(BufferParams const &, odocstream &, bool) const;
121 int endTeXParParams(BufferParams const &, odocstream &, bool) const;
125 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
126 LyXFont const & outerfont, odocstream &,
127 TexRow & texrow, OutputParams const &) const;
129 /// Can we drop the standard paragraph wrapper?
130 bool emptyTag() const;
132 /// Get the id of the paragraph, usefull for docbook
133 std::string getID(Buffer const & buf,
134 OutputParams const & runparams) const;
136 /// Get the first word of a paragraph, return the position where it left
137 pos_type getFirstWord(Buffer const & buf,
139 OutputParams const & runparams) const;
141 /// Checks if the paragraph contains only text and no inset or font change.
142 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
143 pos_type initial) const;
145 /// Writes to stream the docbook representation
146 void simpleDocBookOnePar(Buffer const & buf,
148 OutputParams const & runparams,
149 LyXFont const & outerfont,
150 pos_type initial = 0) const;
153 bool hasSameLayout(Paragraph const & par) const;
156 void makeSameLayout(Paragraph const & par);
159 void setInsetOwner(InsetBase * inset);
161 InsetBase * inInset() const;
163 InsetBase::Code ownerCode() const;
165 bool forceDefaultParagraphs() const;
168 pos_type size() const { return text_.size(); }
170 bool empty() const { return text_.empty(); }
173 LyXLayout_ptr const & layout() const;
175 void layout(LyXLayout_ptr const & new_layout);
177 /// This is the item depth, only used by enumerate and itemize
178 signed char itemdepth;
181 InsetBibitem * bibitem() const; // ale970302
183 /// look up change at given pos
184 Change const & lookupChange(pos_type pos) const;
186 /// is there a change within the given range ?
187 bool isChanged(pos_type start, pos_type end) const;
188 /// is there an unchanged char at the given pos ?
189 bool isUnchanged(pos_type pos) const {
190 return lookupChange(pos).type == Change::UNCHANGED;
192 /// is there an insertion at the given pos ?
193 bool isInserted(pos_type pos) const {
194 return lookupChange(pos).type == Change::INSERTED;
196 /// is there a deletion at the given pos ?
197 bool isDeleted(pos_type pos) const {
198 return lookupChange(pos).type == Change::DELETED;
201 /// will the paragraph be physically merged with the next
202 /// one if the imaginary end-of-par character is logically deleted?
203 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
205 /// set change for the entire par
206 void setChange(Change const & change);
208 /// set change at given pos
209 void setChange(pos_type pos, Change const & change);
211 /// accept changes within the given range
212 void acceptChanges(BufferParams const & bparams, pos_type start, pos_type end);
214 /// reject changes within the given range
215 void rejectChanges(BufferParams const & bparams, pos_type start, pos_type end);
217 /// Paragraphs can contain "manual labels", for example, Description
218 /// environment. The text for this user-editable label is stored in
219 /// the paragraph alongside the text of the rest of the paragraph
220 /// (the body). This function returns the starting position of the
221 /// body of the text in the paragraph.
222 pos_type beginOfBody() const;
223 /// recompute this value
224 void setBeginOfBody();
227 docstring const & getLabelstring() const;
229 /// the next two functions are for the manual labels
230 docstring const getLabelWidthString() const;
231 /// Set label width string.
232 void setLabelWidthString(docstring const & s);
233 /// translate \p label to the paragraph language if possible.
234 docstring const translateIfPossible(docstring const & label,
235 BufferParams const & bparams) const;
236 /// Expand the counters for the labelstring of \c layout
237 docstring expandLabel(LyXLayout_ptr const &, BufferParams const &,
238 bool process_appendix = true) const;
239 /// Actual paragraph alignment used
240 char getAlign() const;
241 /// The nesting depth of a paragraph
242 depth_type getDepth() const;
243 /// The maximal possible depth of a paragraph after this one
244 depth_type getMaxDepthAfter() const;
246 void applyLayout(LyXLayout_ptr const & new_layout);
248 /// (logically) erase the char at pos; return true if it was actually erased
249 bool eraseChar(pos_type pos, bool trackChanges);
250 /// (logically) erase the given range; return the number of chars actually erased
251 int eraseChars(pos_type start, pos_type end, bool trackChanges);
253 /** Get uninstantiated font setting. Returns the difference
254 between the characters font and the layoutfont.
255 This is what is stored in the fonttable
258 getFontSettings(BufferParams const &, pos_type pos) const;
260 LyXFont const getFirstFontSettings(BufferParams const &) const;
262 /** Get fully instantiated font. If pos == -1, use the layout
263 font attached to this paragraph.
264 If pos == -2, use the label font of the layout attached here.
265 In all cases, the font is instantiated, i.e. does not have any
266 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
269 LyXFont const getFont(BufferParams const &, pos_type pos,
270 LyXFont const & outerfont) const;
271 LyXFont const getLayoutFont(BufferParams const &,
272 LyXFont const & outerfont) const;
273 LyXFont const getLabelFont(BufferParams const &,
274 LyXFont const & outerfont) const;
276 * The font returned by the above functions is the same in a
277 * span of characters. This method will return the first and
278 * the last positions in the paragraph for which that font is
279 * the same. This can be used to avoid unnecessary calls to getFont.
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 true if at least one line break or line separator has been deleted
343 /// at the beginning of the paragraph (either physically or logically)
344 bool stripLeadingSpaces(bool trackChanges);
346 /// return true if we allow multiple spaces
347 bool isFreeSpacing() const;
349 /// return true if we allow this par to stay empty
350 bool allowEmpty() const;
352 char_type transformChar(char_type c, pos_type pos) const;
354 ParagraphParameters & params();
356 ParagraphParameters const & params() const;
358 bool hfillExpansion(Row const & row, pos_type pos) const;
360 /// Check if we are in a Biblio environment.
361 /// \retval true if the cursor needs to be moved right.
362 bool checkBiblio(bool track_changes);
371 LyXLayout_ptr layout_;
373 * Keeping this here instead of in the pimpl makes LyX >10% faster
374 * for average tasks as buffer loading/switching etc.
378 pos_type begin_of_body_;
383 friend class Paragraph::Pimpl;
390 #endif // PARAGRAPH_H