]> git.lyx.org Git - lyx.git/blob - src/Layout.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / Layout.h
1 // -*- C++ -*-
2 /**
3  * \file Layout.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author André Pönitz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef LAYOUT_H
15 #define LAYOUT_H
16
17 #include "FontInfo.h"
18 #include "LayoutEnums.h"
19 #include "Spacing.h"
20 #include "support/debug.h"
21 #include "support/docstring.h"
22
23 #include <map>
24 #include <set>
25 #include <string>
26
27 namespace lyx {
28
29 namespace support { class Lexer; }
30
31 class TextClass;
32
33 /* Fixed labels are printed flushright, manual labels flushleft.
34  * MARGIN_MANUAL and MARGIN_FIRST_DYNAMIC are *only* for LABEL_MANUAL,
35  * MARGIN_DYNAMIC and MARGIN_STATIC are *not* for LABEL_MANUAL.
36  * This seems a funny restriction, but I think other combinations are
37  * not needed, so I will not change it yet.
38  * Correction: MARGIN_FIRST_DYNAMIC also usable with LABEL_STATIC.
39  */
40
41
42 /* There is a parindent and a parskip. Which one is used depends on the
43  * paragraph_separation-flag of the text-object.
44  * BUT: parindent is only thrown away, if a parskip is defined! So if you
45  * want a space between the paragraphs and a parindent at the same time,
46  * you should set parskip to zero and use topsep, parsep and bottomsep.
47  *
48  * The standard layout is an exception: its parindent is only set, if the
49  * previous paragraph is standard too. Well, this is LateX and it is good!
50  */
51
52 ///
53 class Layout {
54 public:
55         ///
56         Layout();
57         /// is this layout a default layout created for an unknown layout
58         bool isUnknown() const { return unknown_; }
59         void setUnknown(bool unknown) { unknown_ = unknown; }
60         /// Reads a layout definition from file
61         /// \return true on success.
62         bool read(support::Lexer &, TextClass const &, bool validating = false);
63         ///
64         void readAlign(support::Lexer &);
65         ///
66         void readAlignPossible(support::Lexer &);
67         ///
68         void readLabelType(support::Lexer &);
69         ///
70         void readEndLabelType(support::Lexer &);
71         ///
72         void readMargin(support::Lexer &);
73         ///
74         void readLatexType(support::Lexer &);
75         ///
76         void readSpacing(support::Lexer &);
77         ///
78         void readArgument(support::Lexer &, bool);
79         /// Write a layout definition in utf8 encoding
80         void write(std::ostream &) const;
81         ///
82         docstring const & name() const { return name_; }
83         ///
84         void setName(docstring const & n) { name_ = n; }
85         ///
86         docstring const & obsoleted_by() const { return obsoleted_by_; }
87         ///
88         docstring const & depends_on() const { return depends_on_; }
89         ///
90         std::string const & latexname() const { return latexname_; }
91         ///
92         std::string const & itemcommand() const { return itemcommand_; }
93         /// One argument of this layout
94         struct latexarg {
95                 docstring labelstring;
96                 docstring menustring;
97                 bool mandatory = false;
98                 bool nodelims = false;
99                 docstring ldelim;
100                 docstring rdelim;
101                 docstring defaultarg;
102                 docstring presetarg;
103                 docstring tooltip;
104                 std::string required;
105                 std::string decoration;
106                 FontInfo font = inherit_font;
107                 FontInfo labelfont = inherit_font;
108                 bool autoinsert = false;
109                 bool insertcotext = false;
110                 bool insertonnewline = false;
111                 ArgPassThru passthru = PT_INHERITED;
112                 docstring pass_thru_chars;
113                 bool is_toc_caption = false;
114                 bool free_spacing = false;
115                 std::string newlinecmd;
116                 /// The DocBook tag corresponding to this argument.
117                 docstring docbooktag;
118                 docstring docbooktagtype;
119                 docstring docbookattr;
120                 /// Whether this argument should be output after the main tag (default: inside). The result if the argument
121                 /// should be output both before and after the main tag is undefined.
122                 bool docbookargumentbeforemaintag = false;
123                 /// Whether this argument should be output before the main tag (default: inside). The result if the argument
124                 /// should be output both before and after the main tag is undefined.
125                 bool docbookargumentaftermaintag = false;
126         };
127         ///
128         typedef std::map<std::string, latexarg> LaTeXArgMap;
129         ///
130         LaTeXArgMap const & latexargs() const { return latexargs_; }
131         ///
132         LaTeXArgMap const & postcommandargs() const { return postcommandargs_; }
133         ///
134         LaTeXArgMap const & listpreamble() const { return listpreamble_; }
135         ///
136         LaTeXArgMap const & itemargs() const { return itemargs_; }
137         /// Returns true is the layout has arguments. If false, then an
138         /// InsetArgument in this layout stands for the parent InsetText.
139         bool hasArgs() const;
140         /// Returns latexargs() + postcommandargs() + itemargs().
141         /// But note that it returns a *copy*, not a reference, so do not do
142         /// anything like:
143         ///   Layout::LaTeXArgMap::iterator it = args().begin();
144         ///   Layout::LaTeXArgMap::iterator en = args().end();
145         /// Those are iterators for different containers.
146         LaTeXArgMap args() const;
147         ///
148         int optArgs() const;
149         ///
150         int requiredArgs() const;
151         ///
152         docstring const & labelstring(bool in_appendix) const
153         { return in_appendix ? labelstring_appendix_ : labelstring_; }
154         ///
155         docstring const & endlabelstring() const { return endlabelstring_; }
156         ///
157         docstring const & category() const { return category_; }
158         ///
159         docstring const & preamble() const { return preamble_; }
160         /// Get language dependent macro definitions needed for this layout
161         /// for language \p lang
162         docstring const langpreamble() const { return langpreamble_; }
163         /// Get language and babel dependent macro definitions needed for
164         /// this layout for language \p lang
165         docstring const babelpreamble() const { return babelpreamble_; }
166         ///
167         std::set<std::string> const & required() const { return required_; }
168         ///
169         std::set<docstring> const & autonests() const { return autonests_; }
170         ///
171         std::set<docstring> const & isAutonestedBy() const { return autonested_by_; }
172         ///
173         std::string const & latexparam() const { return latexparam_; }
174         ///
175         docstring leftdelim() const { return leftdelim_; }
176         ///
177         docstring rightdelim() const { return rightdelim_; }
178         ///
179         std::string const & innertag() const { return innertag_; }
180         ///
181         std::string const & labeltag() const { return labeltag_; }
182         ///
183         std::string const & itemtag() const { return itemtag_; }
184         ///
185         std::string const & htmltag() const;
186         ///
187         std::string const & htmlattr() const { return htmlattr_; }
188         ///
189         std::string const & htmlclass() const;
190         /// Returns a complete attribute string, including class, etc.
191         std::string const & htmlGetAttrString() const;
192         ///
193         std::string const & htmlitemtag() const;
194         ///
195         std::string const & htmlitemattr() const;
196         ///
197         std::string const & htmllabeltag() const;
198         ///
199         std::string const & htmllabelattr() const;
200         ///
201         bool htmlintoc() const { return htmlintoc_; }
202         ///
203         std::string defaultCSSClass() const;
204         ///
205         bool htmllabelfirst() const { return htmllabelfirst_; }
206         ///
207         docstring htmlstyle() const;
208         ///
209         docstring const & htmlpreamble() const { return htmlpreamble_; }
210         ///
211         bool htmltitle() const { return htmltitle_; }
212         ///
213         std::string const & docbooktag() const;
214         ///
215         std::string const & docbookattr() const;
216         ///
217         std::string const & docbooktagtype() const;
218         ///
219         std::string const & docbookinnertag() const;
220         ///
221         std::string const & docbookinnerattr() const;
222         ///
223         std::string const & docbookinnertagtype() const;
224         ///
225         std::string const & docbookininfo() const;
226         ///
227         bool docbookabstract() const { return docbookabstract_; }
228         ///
229         std::string const & docbookwrappertag() const;
230         ///
231         std::string const & docbookwrapperattr() const;
232         ///
233         std::string const & docbookwrappertagtype() const;
234         ///
235         bool docbookwrappermergewithprevious() const { return docbookwrappermergewithprevious_; }
236         ///
237         std::string const & docbooksectiontag() const;
238         ///
239         bool docbooksection() const { return docbooksection_; }
240         ///
241         std::string const & docbookitemwrappertag() const;
242         ///
243         std::string const & docbookitemwrapperattr() const;
244         ///
245         std::string const & docbookitemwrappertagtype() const;
246         ///
247         std::string const & docbookitemlabeltag() const;
248         ///
249         std::string const & docbookitemlabelattr() const;
250         ///
251         std::string const & docbookitemlabeltagtype() const;
252         ///
253         std::string const & docbookiteminnertag() const;
254         ///
255         std::string const & docbookiteminnerattr() const;
256         ///
257         std::string const & docbookiteminnertagtype() const;
258         ///
259         std::string const & docbookitemtag() const;
260         ///
261         std::string const & docbookitemattr() const;
262         ///
263         std::string const & docbookitemtagtype() const;
264         ///
265         std::string const & docbookforceabstracttag() const;
266         ///
267         bool docbooknofontinside() const { return docbooknofontinside_; }
268         ///
269         bool docbookgeneratetitle() const { return docbookgeneratetitle_; }
270         ///
271         bool isParagraph() const { return latextype == LATEX_PARAGRAPH; }
272         ///
273         bool isCommand() const { return latextype == LATEX_COMMAND; }
274         ///
275         bool isEnvironment() const {
276                 return latextype == LATEX_ENVIRONMENT
277                         || latextype == LATEX_BIB_ENVIRONMENT
278                         || latextype == LATEX_ITEM_ENVIRONMENT
279                         || latextype == LATEX_LIST_ENVIRONMENT;
280         }
281         /// Is this the kind of layout in which adjacent paragraphs
282         /// are handled as one group?
283         bool isParagraphGroup() const { return par_group_; }
284         ///
285         bool labelIsInline() const {
286                 return labeltype == LABEL_STATIC
287                         || labeltype == LABEL_SENSITIVE
288                         || labeltype == LABEL_ENUMERATE
289                         || labeltype == LABEL_ITEMIZE;
290         }
291         bool labelIsAbove() const {
292                 return labeltype == LABEL_ABOVE
293                         || labeltype == LABEL_CENTERED
294                         || labeltype == LABEL_BIBLIO;
295         }
296         bool isNumHeadingLabelType() const {
297                 return labeltype == LABEL_ABOVE
298                         || labeltype == LABEL_CENTERED
299                         || labeltype == LABEL_STATIC;
300         }
301         ///
302         bool addToToc() const { return add_to_toc_; }
303         ///
304         std::string tocType() const { return toc_type_; }
305         ///
306         bool isTocCaption() const { return is_toc_caption_; }
307
308         ///
309         bool operator==(Layout const &) const;
310         ///
311         bool operator!=(Layout const & rhs) const
312                 { return !(*this == rhs); }
313
314         ////////////////////////////////////////////////////////////////
315         // members
316         ////////////////////////////////////////////////////////////////
317         /** Default font for this layout/environment.
318             The main font for this kind of environment. If an attribute has
319             INHERITED_*, it means that the value is specified by
320             the defaultfont for the entire layout. If we are nested, the
321             font is inherited from the font in the environment one level
322             up until the font is resolved. The values :IGNORE_*
323             and FONT_TOGGLE are illegal here.
324         */
325         FontInfo font;
326
327         /** Default font for labels.
328             Interpretation the same as for font above
329         */
330         FontInfo labelfont;
331
332         /** Resolved version of the font for this layout/environment.
333             This is a resolved version the default font. The font is resolved
334             against the defaultfont of the entire layout.
335         */
336         FontInfo resfont;
337
338         /** Resolved version of the font used for labels.
339             This is a resolved version the label font. The font is resolved
340             against the defaultfont of the entire layout.
341         */
342         FontInfo reslabelfont;
343
344         /// Text that dictates how wide the left margin is on the screen
345         docstring leftmargin;
346         /// Text that dictates how wide the right margin is on the screen
347         docstring rightmargin;
348         /// Text that dictates how much space to leave after a potential label
349         docstring labelsep;
350         /// Text that dictates how much space to leave before a potential label
351         docstring labelindent;
352         /// Text that dictates the width of the indentation of indented pars
353         docstring parindent;
354         ///
355         double parskip;
356         ///
357         double itemsep;
358         ///
359         double topsep;
360         ///
361         double bottomsep;
362         ///
363         double labelbottomsep;
364         ///
365         double parsep;
366         ///
367         Spacing spacing;
368         ///
369         LyXAlignment align;
370         ///
371         LyXAlignment alignpossible;
372         ///
373         LabelType labeltype;
374         ///
375         EndLabelType endlabeltype;
376         ///
377         MarginType margintype;
378         ///
379         bool newline_allowed;
380         ///
381         bool nextnoindent;
382         ///
383         ToggleIndentation toggle_indent;
384         ///
385         bool free_spacing;
386         ///
387         bool pass_thru;
388         /// Individual chars to be passed verbatim
389         docstring pass_thru_chars;
390         ///
391         bool parbreak_is_newline;
392         /// show this in toc
393         int toclevel;
394         /// special value of toclevel for non-section layouts
395         static const int NOT_IN_TOC;
396
397         /** true when the fragile commands in the paragraph need to be
398             \protect'ed. */
399         bool needprotect;
400         /** true when the verbatim stuff of this layout needs to be
401             \cprotect'ed. */
402         bool needcprotect;
403         /** true when specific commands in this paragraph need to be
404             protected in an \mbox. */
405         bool needmboxprotect;
406         /// true when empty paragraphs should be kept.
407         bool keepempty;
408         /// Type of LaTeX object
409         LatexType latextype;
410         /// Does this object belong in the title part of the document?
411         bool intitle;
412         /// Is the content to go in the preamble rather than the body?
413         bool inpreamble;
414         /// Which counter to step
415         docstring counter;
416         /// Resume counter?
417         bool resumecounter;
418         /// Step parent counter?
419         bool stepparentcounter;
420         /// Prefix to use when creating labels
421         docstring refprefix;
422         /// Depth of XML command
423         int commanddepth;
424
425         /// Is this spellchecked?
426         bool spellcheck;
427         /**
428          * Should this layout definition always be written to the document preamble?
429          * Possible values are:
430          *   0: Do not enforce local layout
431          * >=1: Enforce local layout with version forcelocal
432          *  -1: Enforce local layout with infinite version
433          * On reading, the forced local layout is only used if its version
434          * number is greater than the version number of the same layout in the
435          * document class. Otherwise, it is ignored.
436          */
437         int forcelocal;
438
439
440 private:
441         /// Reads a layout definition from file
442         /// \return true on success.
443         bool readIgnoreForcelocal(support::Lexer &, TextClass const &, bool validating);
444         /// generates the default CSS for this layout
445         void makeDefaultCSS() const;
446         ///
447         std::string defaultCSSItemClass() const { return defaultCSSClass() + "_item"; }
448         ///
449         std::string defaultCSSLabelClass() const { return defaultCSSClass() + "_label"; }
450
451         /// Name of the layout/paragraph environment
452         docstring name_;
453
454         /// LaTeX name for environment
455         std::string latexname_;
456
457         /** Is this layout the default layout for an unknown layout? If
458          * so, its name will be displayed as xxx (unknown).
459          */
460         bool unknown_;
461
462         /** Name of an layout that has replaced this layout.
463             This is used to rename a layout, while keeping backward
464             compatibility
465         */
466         docstring obsoleted_by_;
467
468         /** Name of an layout which preamble must come before this one
469             This is used when the preamble snippet uses macros defined in
470             another preamble
471          */
472         docstring depends_on_;
473
474         /// Label string. "Abstract", "Reference", "Caption"...
475         docstring labelstring_;
476         ///
477         docstring endlabelstring_;
478         /// Label string inside appendix. "Appendix", ...
479         docstring labelstring_appendix_;
480         /// LaTeX parameter for environment
481         std::string latexparam_;
482         /// Item command in lists
483         std::string itemcommand_;
484         /// Left delimiter of the content
485         docstring leftdelim_;
486         /// Right delimiter of the content
487         docstring rightdelim_;
488         /// Internal tag to use (e.g., <title></title> for sect header)
489         std::string innertag_;
490         /// Internal tag to use (e.g. to surround varentrylist label)
491         std::string labeltag_;
492         /// Internal tag to surround the item text in a list.
493         std::string itemtag_;
494         /// The interpretation of this tag varies depending upon the latextype.
495         /// In an environment, it is the tag enclosing all content for this set of
496         /// paragraphs. So for quote, e.g,. it would be: blockquote. For itemize,
497         /// it would be: ul. (You get the idea.)
498         ///
499         /// For a command, it is the tag enclosing the content of the command.
500         /// So, for section, it might be: h2.
501         ///
502         /// For the paragraph type, it is the tag that will enclose each paragraph.
503         ///
504         /// Defaults to "div".
505         mutable std::string htmltag_;
506         /// Additional attributes for inclusion with the start tag.
507         /// Note that the CSS class is handled separately.
508         std::string htmlattr_;
509         /// The CSS class to use. Calculated from the layout name if not given.
510         mutable std::string htmlclass_;
511         /// cached
512         mutable std::string htmlfullattrs_;
513         /// Tag for individual paragraphs in an environment. In lists, this
514         /// would be something like "li". But it also needs to be set for
515         /// quotation, e.g., since the paragraphs in a quote need to be
516         /// in "p" tags. Default is "div".
517         /// Note that when I said "environment", I meant it: This has no
518         /// effect for LATEX_PARAGRAPH type layouts.
519         mutable std::string htmlitemtag_;
520         /// Attributes for htmlitemtag_. Default is: class="layoutname_item".
521         mutable std::string htmlitemattr_;
522         /// Tag for labels, of whatever sort. One use for this is in setting
523         /// descriptions, in which case it would be: dt. Another use is to
524         /// customize the display of, say, the auto-generated label for
525         /// sections. Defaults to "span".
526         /// If set to "NONE", this suppresses the printing of the label.
527         mutable std::string htmllabeltag_;
528         /// Attributes for the label. Defaults to: class="layoutname_label".
529         mutable std::string htmllabelattr_;
530         /// Whether to put the label before the item, or within the item.
531         /// I.e., do we have (true):
532         ///    <label>...</label><item>...</item>
533         /// or instead (false):
534         ///    <item><label>...</label>...</item>
535         /// The latter is the default.
536         bool htmllabelfirst_;
537         /// Is this to be output with the toc?
538         bool htmlintoc_;
539         /// CSS information needed by this layout.
540         docstring htmlstyle_;
541         /// DocBook tag corresponding to this layout.
542         mutable std::string docbooktag_;
543         /// Roles to add to docbooktag_, if any (default: none).
544         mutable std::string docbookattr_;
545         /// DocBook tag type corresponding to this layout (block, paragraph, or inline; default: block).
546         mutable std::string docbooktagtype_;
547         /// DocBook inner tag corresponding to this layout.
548         mutable std::string docbookinnertag_;
549         /// Roles to add to docbookinnertag_, if any (default: none).
550         mutable std::string docbookinnerattr_;
551         /// DocBook inner-tag type corresponding to this layout (block, paragraph, or inline; default: block).
552         mutable std::string docbookinnertagtype_;
553         /// DocBook tag corresponding to this item (mainly for lists).
554         mutable std::string docbookitemtag_;
555         /// Roles to add to docbookitemtag_, if any (default: none).
556         mutable std::string docbookitemattr_;
557         /// DocBook tag type corresponding to this item (block, paragraph, or inline; default: block).
558         mutable std::string docbookitemtagtype_;
559         /// DocBook tag corresponding to the wrapper around an item (mainly for lists).
560         mutable std::string docbookitemwrappertag_;
561         /// Roles to add to docbookitemwrappertag_, if any (default: none).
562         mutable std::string docbookitemwrapperattr_;
563         /// DocBook tag type corresponding to the wrapper around an item (block, paragraph, or inline; default: block).
564         mutable std::string docbookitemwrappertagtype_;
565         /// DocBook tag corresponding to this label (mostly for description lists;
566         /// labels in the common sense do not exist with DocBook).
567         mutable std::string docbookitemlabeltag_;
568         /// Roles to add to docbooklabeltag_, if any (default: none).
569         mutable std::string docbookitemlabelattr_;
570         /// DocBook tag corresponding to this label (block, paragraph, or inline; default: block).
571         mutable std::string docbookitemlabeltagtype_;
572         /// DocBook tag to add within the item, around its direct content (mainly for lists).
573         mutable std::string docbookiteminnertag_;
574         /// Roles to add to docbookiteminnertag_, if any (default: none).
575         mutable std::string docbookiteminnerattr_;
576         /// DocBook tag to add within the item, around its direct content (block, paragraph, or inline; default: block).
577         mutable std::string docbookiteminnertagtype_;
578         /// DocBook tag corresponding to this wrapper around the main tag.
579         mutable std::string docbookwrappertag_;
580         /// Roles to add to docbookwrappertag_, if any (default: none).
581         mutable std::string docbookwrapperattr_;
582         /// DocBook tag corresponding to this wrapper around the main tag (block, paragraph, or inline; default: block).
583         mutable std::string docbookwrappertagtype_;
584         /// Whether this wrapper tag may be merged with the previously opened wrapper tag.
585         bool docbookwrappermergewithprevious_;
586         /// Outer tag for this section, only if this layout represent a sectionning item, including chapters
587         /// (default: section).
588         mutable std::string docbooksectiontag_;
589         /// Whether this element should be considered as a section-level element in DocBook.
590         bool docbooksection_;
591         /// Whether this tag must/can/can't go into an <info> tag (default: never, as it only makes sense for metadata).
592         mutable std::string docbookininfo_;
593         /// Whether this paragraph should be considered as abstract. Such paragraphs are output as a part of the document
594         /// abstract element (corresponding to the root tag).
595         bool docbookabstract_;
596         /// Whether this element (root or not) does not accept text without a section (i.e. the first text that is met
597         /// in LyX must be considered as the abstract if this is true); this text must be output with the specific tag
598         /// held by this attribute.
599         mutable std::string docbookforceabstracttag_;
600         /// Whether font tags are allowed inside this tag.
601         bool docbooknofontinside_ = false;
602         /// Whether LyX should create a title on its own, just after the wrapper tag. Typically, this parameter is required
603         /// because the wrapper tag requires a title (like a figure). The generated title will be similar to a LyXHTML label
604         /// (environment type and a number).
605         bool docbookgeneratetitle_ = false;
606         /// Should we generate the default CSS for this layout, even if HTMLStyle
607         /// has been given? Default is false.
608         /// Note that the default CSS is output first, then the user CSS, so it is
609         /// possible to override what one does not want.
610         bool htmlforcecss_;
611         /// A cache for the default style info so generated.
612         mutable docstring htmldefaultstyle_;
613         /// Any other info for the HTML header.
614         docstring htmlpreamble_;
615         /// Whether this is the <title> paragraph.
616         bool htmltitle_;
617         /// calculating this is expensive, so we cache it.
618         mutable std::string defaultcssclass_;
619         /// This is the `category' for this layout. The following are
620         /// recommended basic categories: FrontMatter, BackMatter, MainText,
621         /// Sectioning, Starred, List, Reasoning.
622         docstring category_;
623         /// Macro definitions needed for this layout
624         docstring preamble_;
625         /// Language dependent macro definitions needed for this layout
626         docstring langpreamble_;
627         /// Language and babel dependent macro definitions needed for this layout
628         docstring babelpreamble_;
629         /// Are adjacent paragraphs handled as one group?
630         bool par_group_;
631         /// Packages needed for this layout
632         std::set<std::string> required_;
633         /// Layouts that are by default nested after this one
634         std::set<docstring> autonests_;
635         /// Layouts that by auto-nest this one
636         std::set<docstring> autonested_by_;
637         ///
638         LaTeXArgMap latexargs_;
639         ///
640         LaTeXArgMap postcommandargs_;
641         ///
642         LaTeXArgMap listpreamble_;
643         ///
644         LaTeXArgMap itemargs_;
645         ///
646         bool add_to_toc_;
647         ///
648         std::string toc_type_;
649         ///
650         bool is_toc_caption_;
651 };
652
653
654 } // namespace lyx
655
656 #endif