]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / TextClass.h
1 // -*- C++ -*-
2 /**
3  * \file TextClass.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * Full author contact details are available in file CREDITS.
8  */
9
10 #ifndef TEXTCLASS_H
11 #define TEXTCLASS_H
12
13 #include "Citation.h"
14 #include "Counters.h"
15 #include "DocumentClassPtr.h"
16 #include "FloatList.h"
17 #include "FontInfo.h"
18 #include "LayoutEnums.h"
19 #include "LayoutModuleList.h"
20
21 #include "insets/InsetLayout.h"
22
23 #include "support/docstring.h"
24 #include "support/types.h"
25
26 #include <list>
27 #include <map>
28 #include <set>
29 #include <string>
30 #include <vector>
31
32 #ifdef ERROR
33 #undef ERROR
34 #endif
35
36 namespace lyx {
37
38 namespace support {
39 class FileName;
40 class Lexer;
41 }
42
43 class FloatList;
44 class Layout;
45 class LayoutFile;
46
47 /// Based upon ideas in boost::noncopyable, inheriting from this
48 /// class effectively makes the copy constructor protected but the
49 /// assignment constructor private.
50 class ProtectCopy
51 {
52 protected:
53         ProtectCopy() {}
54         ~ProtectCopy() {}
55         ProtectCopy(const ProtectCopy &) {}
56 private:
57         const ProtectCopy & operator=(const ProtectCopy &);
58 };
59
60
61 /// A TextClass represents a collection of layout information: At the
62 /// moment, this includes Layout's and InsetLayout's.
63 ///
64 /// There are two major subclasses of TextClass: LayoutFile and
65 /// DocumentClass. These subclasses are what are actually used in LyX.
66 /// Simple TextClass objects are not directly constructed in the main
67 /// LyX code---the constructor is protected. (That said, in tex2lyx
68 /// there are what amount to simple TextClass objects.)
69 ///
70 /// A LayoutFile (see LayoutFile.{h,cpp}) represents a *.layout file.
71 /// These are generally static objects---though they can be reloaded
72 /// from disk via LFUN_LAYOUT_RELOAD, so one should not assume that
73 /// they will never change.
74 ///
75 /// A DocumentClass (see below) represents the layout information that
76 /// is associated with a given Buffer. These are static, in the sense
77 /// that they will not themselves change, but which DocumentClass is
78 /// associated with a Buffer can change, as modules are loaded and
79 /// unloaded, for example.
80 ///
81 class TextClass : protected ProtectCopy {
82 public:
83         ///
84         virtual ~TextClass() {}
85         ///////////////////////////////////////////////////////////////////
86         // typedefs
87         ///////////////////////////////////////////////////////////////////
88         // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
89         // std::list<Layout *>. This will lead to problems. The reason is
90         // that DocumentClass objects are generally created by copying a
91         // LayoutFile, which serves as a base for the DocumentClass. If the
92         // LayoutList is a container of pointers, then every DocumentClass
93         // that derives from a given LayoutFile (e.g., article) will SHARE
94         // a basic set of layouts. So if one Buffer were to modify a layout
95         // (say, Standard), that would modify that layout for EVERY Buffer
96         // that was based upon the same DocumentClass.
97         //
98         // NOTE: Layout pointers are directly assigned to paragraphs so a
99         // container that does not invalidate these pointers after insertion
100         // is needed.
101         /// The individual paragraph layouts comprising the document class
102         typedef std::list<Layout> LayoutList;
103         /// The inset layouts available to this class
104         typedef std::map<docstring, InsetLayout> InsetLayouts;
105         ///
106         typedef LayoutList::const_iterator const_iterator;
107
108         ///////////////////////////////////////////////////////////////////
109         // Iterators
110         ///////////////////////////////////////////////////////////////////
111         ///
112         const_iterator begin() const { return layoutlist_.begin(); }
113         ///
114         const_iterator end() const { return layoutlist_.end(); }
115
116
117         ///////////////////////////////////////////////////////////////////
118         // Layout Info
119         ///////////////////////////////////////////////////////////////////
120         ///
121         Layout const & defaultLayout() const;
122         ///
123         docstring const & defaultLayoutName() const;
124         ///
125         bool isDefaultLayout(Layout const &) const;
126         ///
127         bool isPlainLayout(Layout const &) const;
128         /// returns a special layout for use when we don't really want one,
129         /// e.g., in table cells
130         Layout const & plainLayout() const
131                         { return operator[](plain_layout_); }
132         /// the name of the plain layout
133         docstring const & plainLayoutName() const
134                         { return plain_layout_; }
135         /// Enumerate the paragraph styles.
136         size_t layoutCount() const { return layoutlist_.size(); }
137         ///
138         bool hasLayout(docstring const & name) const;
139         ///
140         bool hasInsetLayout(docstring const & name) const;
141         ///
142         Layout const & operator[](docstring const & vname) const;
143         /// Inset layouts of this doc class
144         InsetLayouts const & insetLayouts() const { return insetlayoutlist_; }
145
146         ///////////////////////////////////////////////////////////////////
147         // reading routines
148         ///////////////////////////////////////////////////////////////////
149         /// Enum used with TextClass::read
150         enum ReadType {
151                 BASECLASS, //>This is a base class, i.e., top-level layout file
152                 MERGE, //>This is a file included in a layout file
153                 MODULE, //>This is a layout module
154                 CITE_ENGINE, //>This is a cite engine
155                 VALIDATION //>We're just validating
156         };
157         /// return values for read()
158         enum ReturnValues {
159                 OK,
160                 OK_OLDFORMAT,
161                 ERROR,
162                 FORMAT_MISMATCH
163         };
164
165         /// Performs the read of the layout file.
166         /// \return true on success.
167         // FIXME Should return ReturnValues....
168         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
169         ///
170         ReturnValues read(std::string const & str, ReadType rt = MODULE);
171         ///
172         ReturnValues read(support::Lexer & lex, ReadType rt = BASECLASS);
173         /// validates the layout information passed in str
174         static ReturnValues validate(std::string const & str);
175         /// \return the conversion of \param str to the latest layout format
176         /// compatible with the lyx format.
177         static std::string convert(std::string const & str);
178
179         ///////////////////////////////////////////////////////////////////
180         // loading
181         ///////////////////////////////////////////////////////////////////
182         /// Sees to it the textclass structure has been loaded
183         /// This function will search for $classname.layout in default directories
184         /// and an optional path, but if path points to a file, it will be loaded
185         /// directly.
186         bool load(std::string const & path = std::string()) const;
187         /// Has this layout file been loaded yet?
188         /// Overridden by DocumentClass
189         virtual bool loaded() const { return loaded_; }
190
191         ///////////////////////////////////////////////////////////////////
192         // accessors
193         ///////////////////////////////////////////////////////////////////
194         ///
195         std::string const & name() const { return name_; }
196         ///
197         std::string const & path() const { return path_; }
198         ///
199         std::string const & category() const { return category_; }
200         ///
201         std::string const & description() const { return description_; }
202         ///
203         std::string const & latexname() const { return latexname_; }
204         ///
205         std::string const & prerequisites(std::string const & sep = "\n\t") const;
206         /// Can be LaTeX, DocBook, etc.
207         OutputType outputType() const { return outputType_; }
208         /// Can be latex, docbook ... (the name of a format)
209         std::string outputFormat() const { return outputFormat_; }
210         /// Does this class redefine the output format?
211         bool hasOutputFormat() const { return has_output_format_; }
212         /// Return the non-localised names for the toc types.
213         std::map<std::string, docstring> const &
214                 outlinerNames() const { return outliner_names_; }
215         /// \returns Layout named \p name if it exists, otherwise 0
216         Layout const * getLayout(docstring const & name) const;
217         /// \returns Layout named \p name if it exists, otherwise 0
218         Layout * getLayout(docstring const & name);
219
220 protected:
221         /// Protect construction
222         TextClass();
223         ///
224         Layout & operator[](docstring const & name);
225         /** Create an new, very basic layout for this textclass. This is used for
226             the Plain Layout common to all TextClass objects and also, in
227             DocumentClass, for the creation of new layouts `on the fly' when
228             previously unknown layouts are encountered.
229             \param unknown Set to true if this layout is used to represent an
230             unknown layout
231          */
232         Layout createBasicLayout(docstring const & name, bool unknown = false) const;
233
234         ///////////////////////////////////////////////////////////////////
235         // non-const iterators
236         ///////////////////////////////////////////////////////////////////
237         ///
238         typedef LayoutList::iterator iterator;
239         ///
240         iterator begin() { return layoutlist_.begin(); }
241         ///
242         iterator end() { return layoutlist_.end(); }
243
244         ///////////////////////////////////////////////////////////////////
245         // members
246         ///////////////////////////////////////////////////////////////////
247         /// Paragraph styles used in this layout
248         /// This variable is mutable because unknown layouts can be added
249         /// to const textclass.
250         mutable LayoutList layoutlist_;
251         /// Layout file name
252         std::string name_;
253         /// Layout file path (empty for system layout files)
254         std::string path_;
255         /// Class category
256         std::string category_;
257         /// document class name
258         std::string latexname_;
259         /// document class description
260         std::string description_;
261         /// available types of float, eg. figure, algorithm.
262         mutable FloatList floatlist_;
263         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
264         mutable Counters counters_;
265         /// Has this layout file been loaded yet?
266         mutable bool loaded_;
267         /// Is the TeX class available?
268         bool tex_class_avail_;
269         /// document class prerequisites
270         mutable std::string prerequisites_;
271         /// The possible cite engine types
272         std::string opt_enginetype_;
273         /// The cite framework (bibtex, biblatex)
274         std::string citeframework_;
275         ///
276         std::string opt_fontsize_;
277         ///
278         std::string opt_pagesize_;
279         ///
280         std::string opt_pagestyle_;
281         /// Specific class options
282         std::string options_;
283         /// Format of the fontsize option
284         std::string fontsize_format_;
285         /// Default page size
286         std::string pagesize_;
287         /// Format of the papersize option
288         std::string pagesize_format_;
289         ///
290         std::string pagestyle_;
291         ///
292         std::string tablestyle_;
293         ///
294         std::string class_header_;
295         ///
296         docstring defaultlayout_;
297         /// name of plain layout
298         static const docstring plain_layout_;
299         /// preamble text to support layout styles
300         docstring preamble_;
301         /// same, but for HTML output
302         /// this is output as is to the header
303         docstring htmlpreamble_;
304         /// same, but specifically for CSS information
305         docstring htmlstyles_;
306         /// the paragraph style to use for TOCs, bibliography, etc.
307         mutable docstring html_toc_section_;
308         /// root element when exporting as DocBook
309         std::string docbookroot_;
310         /// whether this root element does not accept text without a section (i.e. the first text that is met in LyX must
311         /// be considered as the abstract if this is true); this text must be output within <info> and <abstract>
312         bool docbookforceabstract_;
313         /// latex packages loaded by document class.
314         std::set<std::string> provides_;
315         /// latex packages requested by document class.
316         std::set<std::string> required_;
317         ///
318         std::map<std::string, std::string> package_options_;
319         /// default modules wanted by document class
320         LayoutModuleList default_modules_;
321         /// modules provided by document class
322         LayoutModuleList provided_modules_;
323         /// modules excluded by document class
324         LayoutModuleList excluded_modules_;
325         ///
326         unsigned int columns_;
327         ///
328         PageSides sides_;
329         /// header depth to have numbering
330         int secnumdepth_;
331         /// header depth to appear in table of contents
332         int tocdepth_;
333         /// Can be LaTeX, DocBook, etc.
334         OutputType outputType_;
335         /// Can be latex, docbook ... (the name of a format)
336         std::string outputFormat_;
337         /// Does this class redefine the output format?
338         bool has_output_format_;
339         /** Base font. The paragraph and layout fonts are resolved against
340             this font. This has to be fully instantiated. Attributes
341             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
342             extremely illegal.
343         */
344         FontInfo defaultfont_;
345         /// Text that dictates how wide the left margin is on the screen
346         docstring leftmargin_;
347         /// Text that dictates how wide the right margin is on the screen
348         docstring rightmargin_;
349         /// The type of command used to produce a title
350         TitleLatexType titletype_;
351         /// The name of the title command
352         std::string titlename_;
353         /// Input layouts available to this layout
354         InsetLayouts insetlayoutlist_;
355         /// The minimal TocLevel of sectioning layouts
356         int min_toclevel_;
357         /// The maximal TocLevel of sectioning layouts
358         int max_toclevel_;
359         /// Citation formatting information
360         std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
361         /// Citation macros
362         std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
363         /// The default BibTeX bibliography style file
364         std::map<std::string, std::string> cite_default_biblio_style_;
365         /// Citation command aliases
366         std::map<std::string, std::string> cite_command_aliases_;
367         /// The maximum number of citations before "et al."
368         size_t maxcitenames_;
369         /// Whether full author lists are supported
370         bool cite_full_author_list_;
371         /// The possible citation styles
372         std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
373         /// Class-added citation styles
374         std::map<CiteEngineType, std::vector<CitationStyle> > class_cite_styles_;
375         ///
376         std::map<std::string, docstring> outliner_names_;
377         /// Does this class put the bibliography to toc by itself?
378         bool bibintoc_;
379 private:
380         ///////////////////////////////////////////////////////////////////
381         // helper routines for reading layout files
382         ///////////////////////////////////////////////////////////////////
383         ///
384         bool deleteLayout(docstring const &);
385         ///
386         bool deleteInsetLayout(docstring const &);
387         ///
388         bool convertLayoutFormat(support::FileName const &, ReadType);
389         /// Reads the layout file without running layout2layout.
390         ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
391         /// \return true for success.
392         bool readStyle(support::Lexer &, Layout &, ReadType) const;
393         ///
394         void readOutputType(support::Lexer &);
395         ///
396         void readTitleType(support::Lexer &);
397         ///
398         void readMaxCounter(support::Lexer &);
399         ///
400         void readClassOptions(support::Lexer &);
401         ///
402         void readCharStyle(support::Lexer &, std::string const &);
403         ///
404         bool readFloat(support::Lexer &);
405         ///
406         std::vector<CitationStyle> const & getCiteStyles(CiteEngineType const &) const;
407         ///
408         bool readCiteEngine(support::Lexer &, ReadType, bool const add = false);
409         ///
410         int readCiteEngineType(support::Lexer &) const;
411         ///
412         bool readCiteFormat(support::Lexer &, ReadType);
413         ///
414         bool readOutlinerName(support::Lexer &);
415 };
416
417
418 /// A DocumentClass represents the layout information associated with a
419 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
420 /// various Modules.
421 ///
422 /// In that regard, DocumentClass objects are "dynamic". But this is really
423 /// an illusion, since DocumentClass objects are not (currently) changed
424 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
425 /// discarded---actually, it will be kept around if something on the cut
426 /// stack needs it---and a new one is created from scratch.
427 class DocumentClass : public TextClass {
428 public:
429         ///
430         virtual ~DocumentClass() {}
431
432         ///////////////////////////////////////////////////////////////////
433         // Layout Info
434         ///////////////////////////////////////////////////////////////////
435         /// \return true if there is a Layout with latexname lay
436         bool hasLaTeXLayout(std::string const & lay) const;
437         /// A DocumentClass nevers count as loaded, since it is dynamic
438         bool loaded() const override { return false; }
439         /// \return the layout object of an inset given by name. If the name
440         /// is not found as such, the part after the ':' is stripped off, and
441         /// searched again. In this way, an error fallback can be provided:
442         /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
443         /// will invoke the layout object defined by name = 'CharStyle'.
444         /// If that doesn't work either, an empty object returns (shouldn't
445         /// happen).  -- Idea JMarc, comment MV
446         InsetLayout const & insetLayout(docstring const & name) const;
447         /// a plain inset layout for use as a default
448         static InsetLayout const & plainInsetLayout();
449         /// add a new layout \c name if it does not exist in layoutlist_
450         /// \return whether we had to add one.
451         bool addLayoutIfNeeded(docstring const & name) const;
452         /// Forced layouts in layout file syntax
453         std::string forcedLayouts() const;
454
455         ///////////////////////////////////////////////////////////////////
456         // accessors
457         ///////////////////////////////////////////////////////////////////
458         /// the list of floats defined in the document class
459         FloatList const & floats() const { return floatlist_; }
460         ///
461         Counters & counters() const { return counters_; }
462         ///
463         std::string const & opt_enginetype() const { return opt_enginetype_; }
464         ///
465         std::string const & citeFramework() const { return citeframework_; }
466         ///
467         std::string const & opt_fontsize() const { return opt_fontsize_; }
468         ///
469         std::string const & opt_pagesize() const { return opt_pagesize_; }
470         ///
471         std::string const & opt_pagestyle() const { return opt_pagestyle_; }
472         ///
473         std::string const & options() const { return options_; }
474         ///
475         std::string const & class_header() const { return class_header_; }
476         ///
477         std::string const & fontsizeformat() const { return fontsize_format_; }
478         ///
479         std::string const & pagesize() const { return pagesize_; }
480         ///
481         std::string const & pagesizeformat() const { return pagesize_format_; }
482         ///
483         std::string const & pagestyle() const { return pagestyle_; }
484         ///
485         std::string const & tablestyle() const { return tablestyle_; }
486         ///
487         docstring const & preamble() const { return preamble_; }
488         ///
489         docstring const & htmlpreamble() const { return htmlpreamble_; }
490         ///
491         docstring const & htmlstyles() const { return htmlstyles_; }
492         ///
493         bool docbookforceabstract() const { return docbookforceabstract_; }
494         ///
495         std::string const & docbookroot() const { return docbookroot_; }
496         /// Looks for the layout of "highest level", other than Part (or other
497         /// layouts with a negative toc number), for use in constructing TOCs and
498         /// similar information.
499         Layout const & getTOCLayout() const;
500         /// the paragraph style to use for TOCs, Bibliography, etc
501         /// we will attempt to calculate this if it was not given
502         Layout const & htmlTOCLayout() const;
503         /// is this feature already provided by the class?
504         bool provides(std::string const & p) const;
505         /// features required by the class?
506         std::set<std::string> const & required() const { return required_; }
507         /// package options to write to LaTeX file
508         std::map<std::string, std::string> const & packageOptions() const
509                 { return package_options_; }
510         ///
511         unsigned int columns() const { return columns_; }
512         ///
513         PageSides sides() const { return sides_; }
514         ///
515         int secnumdepth() const { return secnumdepth_; }
516         ///
517         int tocdepth() const { return tocdepth_; }
518         ///
519         FontInfo const & defaultfont() const { return defaultfont_; }
520         /// Text that dictates how wide the left margin is on the screen
521         docstring const & leftmargin() const { return leftmargin_; }
522         /// Text that dictates how wide the right margin is on the screen
523         docstring const & rightmargin() const { return rightmargin_; }
524         /// The type of command used to produce a title
525         TitleLatexType titletype() const { return titletype_; }
526         /// The name of the title command
527         std::string const & titlename() const { return titlename_; }
528         ///
529         int size() const { return layoutlist_.size(); }
530         /// The minimal TocLevel of sectioning layouts
531         int min_toclevel() const { return min_toclevel_; }
532         /// The maximal TocLevel of sectioning layouts
533         int max_toclevel() const { return max_toclevel_; }
534         /// returns true if the class has a ToC structure
535         bool hasTocLevels() const;
536         ///
537         std::string const getCiteFormat(CiteEngineType const & type,
538                 std::string const & entry, bool const punct = true,
539                 std::string const & fallback = "") const;
540         ///
541         std::string const & getCiteMacro(CiteEngineType const & type,
542                 std::string const & macro) const;
543         ///
544         std::vector<std::string> const citeCommands(CiteEngineType const &) const;
545         ///
546         std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
547         ///
548         std::map<std::string, std::string> const & defaultBiblioStyle() const
549         { return cite_default_biblio_style_; }
550         ///
551         std::map<std::string, std::string> const & citeCommandAliases() const
552         { return cite_command_aliases_; }
553         /// The maximum number of citations before "et al."
554         size_t max_citenames() const { return maxcitenames_; }
555         ///
556         bool fullAuthorList() const { return cite_full_author_list_; }
557         ///
558         bool bibInToc() const { return bibintoc_; }
559 protected:
560         /// Constructs a DocumentClass based upon a LayoutFile.
561         DocumentClass(LayoutFile const & tc);
562         /// Needed in tex2lyx
563         DocumentClass() {}
564 private:
565         /// The only way to make a DocumentClass is to call this function.
566         friend DocumentClassPtr
567                 getDocumentClass(LayoutFile const &, LayoutModuleList const &,
568                                  std::string const &,
569                                  bool clone, bool internal);
570 };
571
572
573 /// The only way to make a DocumentClass is to call this function.
574 /// The shared_ptr is needed because DocumentClass objects can be kept
575 /// in memory long after their associated Buffer is destroyed, mostly
576 /// on the CutStack.
577 DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
578                         LayoutModuleList const & modlist,
579                         std::string const & cengine = std::string(),
580                         bool clone = false, bool internal = false);
581
582 /// convert page sides option to text 1 or 2
583 std::ostream & operator<<(std::ostream & os, PageSides p);
584
585 /// current format of layout files
586 extern int const LAYOUT_FORMAT;
587 /// layout format for the current lyx file format (usually equal to
588 /// LAYOUT_FORMAT)
589 extern int const LYXFILE_LAYOUT_FORMAT;
590
591
592 } // namespace lyx
593
594 #endif