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 "Layout.h"
19 #include "LayoutEnums.h"
20 #include "LayoutModuleList.h"
21 
22 #include "insets/InsetLayout.h"
23 
24 #include "support/docstring.h"
25 #include "support/types.h"
26 
27 #include <list>
28 #include <map>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
33 #ifdef ERROR
34 #undef ERROR
35 #endif
36 
37 namespace lyx {
38 
39 namespace support { class FileName; }
40 
41 class Counters;
42 class FloatList;
43 class Layout;
44 class LayoutFile;
45 class Lexer;
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:
ProtectCopy()53 	ProtectCopy() {}
~ProtectCopy()54 	~ProtectCopy() {}
ProtectCopy(const 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 	///
~TextClass()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 	///
begin()112 	const_iterator begin() const { return layoutlist_.begin(); }
113 	///
end()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
plainLayout()130 	Layout const & plainLayout() const
131 			{ return operator[](plain_layout_); }
132 	/// the name of the plain layout
plainLayoutName()133 	docstring const & plainLayoutName() const
134 			{ return plain_layout_; }
135 	/// Enumerate the paragraph styles.
layoutCount()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
insetLayouts()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(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
loaded()189 	virtual bool loaded() const { return loaded_; }
190 
191 	///////////////////////////////////////////////////////////////////
192 	// accessors
193 	///////////////////////////////////////////////////////////////////
194 	///
name()195 	std::string const & name() const { return name_; }
196 	///
path()197 	std::string const & path() const { return path_; }
198 	///
category()199 	std::string const & category() const { return category_; }
200 	///
description()201 	std::string const & description() const { return description_; }
202 	///
latexname()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.
outputType()207 	OutputType outputType() const { return outputType_; }
208 	/// Can be latex, docbook ... (the name of a format)
outputFormat()209 	std::string outputFormat() const { return outputFormat_; }
210 	/// Does this class redefine the output format?
hasOutputFormat()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 &
outlinerNames()214 	outlinerNames() const { return outliner_names_; }
215 
216 protected:
217 	/// Protect construction
218 	TextClass();
219 	///
220 	Layout & operator[](docstring const & name);
221 	/** Create an new, very basic layout for this textclass. This is used for
222 	    the Plain Layout common to all TextClass objects and also, in
223 	    DocumentClass, for the creation of new layouts `on the fly' when
224 	    previously unknown layouts are encountered.
225 	    \param unknown Set to true if this layout is used to represent an
226 	    unknown layout
227 	 */
228 	Layout createBasicLayout(docstring const & name, bool unknown = false) const;
229 
230 	///////////////////////////////////////////////////////////////////
231 	// non-const iterators
232 	///////////////////////////////////////////////////////////////////
233 	///
234 	typedef LayoutList::iterator iterator;
235 	///
begin()236 	iterator begin() { return layoutlist_.begin(); }
237 	///
end()238 	iterator end() { return layoutlist_.end(); }
239 
240 	///////////////////////////////////////////////////////////////////
241 	// members
242 	///////////////////////////////////////////////////////////////////
243 	/// Paragraph styles used in this layout
244 	/// This variable is mutable because unknown layouts can be added
245 	/// to const textclass.
246 	mutable LayoutList layoutlist_;
247 	/// Layout file name
248 	std::string name_;
249 	/// Layout file path (empty for system layout files)
250 	std::string path_;
251 	/// Class category
252 	std::string category_;
253 	/// document class name
254 	std::string latexname_;
255 	/// document class description
256 	std::string description_;
257 	/// available types of float, eg. figure, algorithm.
258 	mutable FloatList floatlist_;
259 	/// Types of counters, eg. sections, eqns, figures, avail. in document class.
260 	mutable Counters counters_;
261 	/// Has this layout file been loaded yet?
262 	mutable bool loaded_;
263 	/// Is the TeX class available?
264 	bool tex_class_avail_;
265 	/// document class prerequisites
266 	mutable std::string prerequisites_;
267 	/// The possible cite engine types
268 	std::string opt_enginetype_;
269 	/// The cite framework (bibtex, biblatex)
270 	std::string citeframework_;
271 	///
272 	std::string opt_fontsize_;
273 	///
274 	std::string opt_pagestyle_;
275 	/// Specific class options
276 	std::string options_;
277 	///
278 	std::string pagestyle_;
279 	///
280 	std::string class_header_;
281 	///
282 	docstring defaultlayout_;
283 	/// name of plain layout
284 	static const docstring plain_layout_;
285 	/// preamble text to support layout styles
286 	docstring preamble_;
287 	/// same, but for HTML output
288 	/// this is output as is to the header
289 	docstring htmlpreamble_;
290 	/// same, but specifically for CSS information
291 	docstring htmlstyles_;
292 	/// the paragraph style to use for TOCs, Bibliography, etc
293 	mutable docstring html_toc_section_;
294 	/// latex packages loaded by document class.
295 	std::set<std::string> provides_;
296 	/// latex packages requested by document class.
297 	std::set<std::string> requires_;
298 	///
299 	std::map<std::string, std::string> package_options_;
300 	/// default modules wanted by document class
301 	LayoutModuleList default_modules_;
302 	/// modules provided by document class
303 	LayoutModuleList provided_modules_;
304 	/// modules excluded by document class
305 	LayoutModuleList excluded_modules_;
306 	///
307 	unsigned int columns_;
308 	///
309 	PageSides sides_;
310 	/// header depth to have numbering
311 	int secnumdepth_;
312 	/// header depth to appear in table of contents
313 	int tocdepth_;
314 	/// Can be LaTeX, DocBook, etc.
315 	OutputType outputType_;
316 	/// Can be latex, docbook ... (the name of a format)
317 	std::string outputFormat_;
318 	/// Does this class redefine the output format?
319 	bool has_output_format_;
320 	/** Base font. The paragraph and layout fonts are resolved against
321 	    this font. This has to be fully instantiated. Attributes
322 	    FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
323 	    extremely illegal.
324 	*/
325 	FontInfo defaultfont_;
326 	/// Text that dictates how wide the left margin is on the screen
327 	docstring leftmargin_;
328 	/// Text that dictates how wide the right margin is on the screen
329 	docstring rightmargin_;
330 	/// The type of command used to produce a title
331 	TitleLatexType titletype_;
332 	/// The name of the title command
333 	std::string titlename_;
334 	/// Input layouts available to this layout
335 	InsetLayouts insetlayoutlist_;
336 	/// The minimal TocLevel of sectioning layouts
337 	int min_toclevel_;
338 	/// The maximal TocLevel of sectioning layouts
339 	int max_toclevel_;
340 	/// Citation formatting information
341 	std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
342 	/// Citation macros
343 	std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
344 	/// The default BibTeX bibliography style file
345 	std::map<std::string, std::string> cite_default_biblio_style_;
346 	/// Citation command aliases
347 	std::map<std::string, std::string> cite_command_aliases_;
348 	/// The maximum number of citations before "et al."
349 	size_t maxcitenames_;
350 	/// Whether full author lists are supported
351 	bool cite_full_author_list_;
352 	/// The possible citation styles
353 	std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
354 	///
355 	std::map<std::string, docstring> outliner_names_;
356 private:
357 	///////////////////////////////////////////////////////////////////
358 	// helper routines for reading layout files
359 	///////////////////////////////////////////////////////////////////
360 	///
361 	bool deleteLayout(docstring const &);
362 	///
363 	bool deleteInsetLayout(docstring const &);
364 	///
365 	bool convertLayoutFormat(support::FileName const &, ReadType);
366 	/// Reads the layout file without running layout2layout.
367 	ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
368 	/// \return true for success.
369 	bool readStyle(Lexer &, Layout &) const;
370 	///
371 	void readOutputType(Lexer &);
372 	///
373 	void readTitleType(Lexer &);
374 	///
375 	void readMaxCounter(Lexer &);
376 	///
377 	void readClassOptions(Lexer &);
378 	///
379 	void readCharStyle(Lexer &, std::string const &);
380 	///
381 	bool readFloat(Lexer &);
382 	///
383 	bool readCiteEngine(Lexer &);
384 	///
385 	int readCiteEngineType(Lexer &) const;
386 	///
387 	bool readCiteFormat(Lexer &);
388 	///
389 	bool readOutlinerName(Lexer &);
390 };
391 
392 
393 /// A DocumentClass represents the layout information associated with a
394 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
395 /// various Modules.
396 ///
397 /// In that regard, DocumentClass objects are "dynamic". But this is really
398 /// an illusion, since DocumentClass objects are not (currently) changed
399 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
400 /// discarded---actually, it will be kept around if something on the cut
401 /// stack needs it---and a new one is created from scratch.
402 class DocumentClass : public TextClass {
403 public:
404 	///
~DocumentClass()405 	virtual ~DocumentClass() {}
406 
407 	///////////////////////////////////////////////////////////////////
408 	// Layout Info
409 	///////////////////////////////////////////////////////////////////
410 	/// \return true if there is a Layout with latexname lay
411 	bool hasLaTeXLayout(std::string const & lay) const;
412 	/// A DocumentClass nevers count as loaded, since it is dynamic
loaded()413 	virtual bool loaded() const { return false; }
414 	/// \return the layout object of an inset given by name. If the name
415 	/// is not found as such, the part after the ':' is stripped off, and
416 	/// searched again. In this way, an error fallback can be provided:
417 	/// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
418 	/// will invoke the layout object defined by name = 'CharStyle'.
419 	/// If that doesn't work either, an empty object returns (shouldn't
420 	/// happen).  -- Idea JMarc, comment MV
421 	InsetLayout const & insetLayout(docstring const & name) const;
422 	/// a plain inset layout for use as a default
423 	static InsetLayout const & plainInsetLayout();
424 	/// add a new layout \c name if it does not exist in layoutlist_
425 	/// \return whether we had to add one.
426 	bool addLayoutIfNeeded(docstring const & name) const;
427 	/// Forced layouts in layout file syntax
428 	std::string forcedLayouts() const;
429 
430 	///////////////////////////////////////////////////////////////////
431 	// accessors
432 	///////////////////////////////////////////////////////////////////
433 	/// the list of floats defined in the document class
floats()434 	FloatList const & floats() const { return floatlist_; }
435 	///
counters()436 	Counters & counters() const { return counters_; }
437 	///
opt_enginetype()438 	std::string const & opt_enginetype() const { return opt_enginetype_; }
439 	///
citeFramework()440 	std::string const & citeFramework() const { return citeframework_; }
441 	///
opt_fontsize()442 	std::string const & opt_fontsize() const { return opt_fontsize_; }
443 	///
opt_pagestyle()444 	std::string const & opt_pagestyle() const { return opt_pagestyle_; }
445 	///
options()446 	std::string const & options() const { return options_; }
447 	///
class_header()448 	std::string const & class_header() const { return class_header_; }
449 	///
pagestyle()450 	std::string const & pagestyle() const { return pagestyle_; }
451 	///
preamble()452 	docstring const & preamble() const { return preamble_; }
453 	///
htmlpreamble()454 	docstring const & htmlpreamble() const { return htmlpreamble_; }
455 	///
htmlstyles()456 	docstring const & htmlstyles() const { return htmlstyles_; }
457 	/// Looks for the layout of "highest level", other than Part (or other
458 	/// layouts with a negative toc number), for use in constructing TOCs and
459 	/// similar information.
460 	Layout const & getTOCLayout() const;
461 	/// the paragraph style to use for TOCs, Bibliography, etc
462 	/// we will attempt to calculate this if it was not given
463 	Layout const & htmlTOCLayout() const;
464 	/// is this feature already provided by the class?
465 	bool provides(std::string const & p) const;
466 	/// features required by the class?
requires()467 	std::set<std::string> const & requires() const { return requires_; }
468 	/// package options to write to LaTeX file
packageOptions()469 	std::map<std::string, std::string> const & packageOptions() const
470 		{ return package_options_; }
471 	///
columns()472 	unsigned int columns() const { return columns_; }
473 	///
sides()474 	PageSides sides() const { return sides_; }
475 	///
secnumdepth()476 	int secnumdepth() const { return secnumdepth_; }
477 	///
tocdepth()478 	int tocdepth() const { return tocdepth_; }
479 	///
defaultfont()480 	FontInfo const & defaultfont() const { return defaultfont_; }
481 	/// Text that dictates how wide the left margin is on the screen
leftmargin()482 	docstring const & leftmargin() const { return leftmargin_; }
483 	/// Text that dictates how wide the right margin is on the screen
rightmargin()484 	docstring const & rightmargin() const { return rightmargin_; }
485 	/// The type of command used to produce a title
titletype()486 	TitleLatexType titletype() const { return titletype_; }
487 	/// The name of the title command
titlename()488 	std::string const & titlename() const { return titlename_; }
489 	///
size()490 	int size() const { return layoutlist_.size(); }
491 	/// The minimal TocLevel of sectioning layouts
min_toclevel()492 	int min_toclevel() const { return min_toclevel_; }
493 	/// The maximal TocLevel of sectioning layouts
max_toclevel()494 	int max_toclevel() const { return max_toclevel_; }
495 	/// returns true if the class has a ToC structure
496 	bool hasTocLevels() const;
497 	///
498 	std::string const getCiteFormat(CiteEngineType const & type,
499 		std::string const & entry, bool const punct = true,
500 		std::string const & fallback = "") const;
501 	///
502 	std::string const & getCiteMacro(CiteEngineType const & type,
503 		std::string const & macro) const;
504 	///
505 	std::vector<std::string> const citeCommands(CiteEngineType const &) const;
506 	///
507 	std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
508 	///
defaultBiblioStyle()509 	std::map<std::string, std::string> const & defaultBiblioStyle() const
510 	{ return cite_default_biblio_style_; }
511 	///
citeCommandAliases()512 	std::map<std::string, std::string> const & citeCommandAliases() const
513 	{ return cite_command_aliases_; }
514 	/// The maximum number of citations before "et al."
max_citenames()515 	size_t max_citenames() const { return maxcitenames_; }
516 	///
fullAuthorList()517 	bool const & fullAuthorList() const { return cite_full_author_list_; }
518 protected:
519 	/// Constructs a DocumentClass based upon a LayoutFile.
520 	DocumentClass(LayoutFile const & tc);
521 	/// Needed in tex2lyx
DocumentClass()522 	DocumentClass() {}
523 private:
524 	/// The only way to make a DocumentClass is to call this function.
525 	friend DocumentClassPtr
526 		getDocumentClass(LayoutFile const &, LayoutModuleList const &,
527 				 LayoutModuleList const &,
528 				 bool const clone);
529 };
530 
531 
532 /// The only way to make a DocumentClass is to call this function.
533 /// The shared_ptr is needed because DocumentClass objects can be kept
534 /// in memory long after their associated Buffer is destroyed, mostly
535 /// on the CutStack.
536 DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
537 			LayoutModuleList const & modlist,
538 			LayoutModuleList const & celist,
539 			bool const clone = false);
540 
541 /// convert page sides option to text 1 or 2
542 std::ostream & operator<<(std::ostream & os, PageSides p);
543 
544 /// current format of layout files
545 extern int const LAYOUT_FORMAT;
546 /// layout format for the current lyx file format (usually equal to
547 /// LAYOUT_FORMAT)
548 extern int const LYXFILE_LAYOUT_FORMAT;
549 
550 
551 } // namespace lyx
552 
553 #endif
554