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 class Lexer;
30 class TextClass;
31 
32 /* Fixed labels are printed flushright, manual labels flushleft.
33  * MARGIN_MANUAL and MARGIN_FIRST_DYNAMIC are *only* for LABEL_MANUAL,
34  * MARGIN_DYNAMIC and MARGIN_STATIC are *not* for LABEL_MANUAL.
35  * This seems a funny restriction, but I think other combinations are
36  * not needed, so I will not change it yet.
37  * Correction: MARGIN_FIRST_DYNAMIC also usable with LABEL_STATIC.
38  */
39 
40 
41 /* There is a parindent and a parskip. Which one is used depends on the
42  * paragraph_separation-flag of the text-object.
43  * BUT: parindent is only thrown away, if a parskip is defined! So if you
44  * want a space between the paragraphs and a parindent at the same time,
45  * you should set parskip to zero and use topsep, parsep and bottomsep.
46  *
47  * The standard layout is an exception: its parindent is only set, if the
48  * previous paragraph is standard too. Well, this is LateX and it is good!
49  */
50 
51 ///
52 class Layout {
53 public:
54 	///
55 	Layout();
56 	/// is this layout a default layout created for an unknown layout
isUnknown()57 	bool isUnknown() const { return unknown_; }
setUnknown(bool unknown)58 	void setUnknown(bool unknown) { unknown_ = unknown; }
59 	/// Reads a layout definition from file
60 	/// \return true on success.
61 	bool read(Lexer &, TextClass const &);
62 	///
63 	void readAlign(Lexer &);
64 	///
65 	void readAlignPossible(Lexer &);
66 	///
67 	void readLabelType(Lexer &);
68 	///
69 	void readEndLabelType(Lexer &);
70 	///
71 	void readMargin(Lexer &);
72 	///
73 	void readLatexType(Lexer &);
74 	///
75 	void readSpacing(Lexer &);
76 	///
77 	void readArgument(Lexer &);
78 	/// Write a layout definition in utf8 encoding
79 	void write(std::ostream &) const;
80 	///
name()81 	docstring const & name() const { return name_; }
82 	///
setName(docstring const & n)83 	void setName(docstring const & n) { name_ = n; }
84 	///
obsoleted_by()85 	docstring const & obsoleted_by() const { return obsoleted_by_; }
86 	///
depends_on()87 	docstring const & depends_on() const { return depends_on_; }
88 	///
latexname()89 	std::string const & latexname() const { return latexname_; }
90 	///
itemcommand()91 	std::string const & itemcommand() const { return itemcommand_; }
92 	/// The arguments of this layout
93 	struct latexarg {
94 		docstring labelstring;
95 		docstring menustring;
96 		bool mandatory;
97 		docstring ldelim;
98 		docstring rdelim;
99 		docstring defaultarg;
100 		docstring presetarg;
101 		docstring tooltip;
102 		std::string requires;
103 		std::string decoration;
104 		FontInfo font;
105 		FontInfo labelfont;
106 		bool autoinsert;
107 		bool insertcotext;
108 		ArgPassThru passthru;
109 		docstring pass_thru_chars;
110 		bool is_toc_caption;
111 	};
112 	///
113 	typedef std::map<std::string, latexarg> LaTeXArgMap;
114 	///
latexargs()115 	LaTeXArgMap const & latexargs() const { return latexargs_; }
116 	///
postcommandargs()117 	LaTeXArgMap const & postcommandargs() const { return postcommandargs_; }
118 	///
itemargs()119 	LaTeXArgMap const & itemargs() const { return itemargs_; }
120 	/// Returns true is the layout has arguments. If false, then an
121 	/// InsetArgument in this layout stands for the parent InsetText.
122 	bool hasArgs() const;
123 	/// Returns latexargs() + postcommandargs() + itemargs().
124 	/// But note that it returns a *copy*, not a reference, so do not do
125 	/// anything like:
126 	///   Layout::LaTeXArgMap::iterator it = args().begin();
127 	///   Layout::LaTeXArgMap::iterator en = args().end();
128 	/// Those are iterators for different containers.
129 	LaTeXArgMap args() const;
130 	///
131 	int optArgs() const;
132 	///
133 	int requiredArgs() const;
134 	///
labelstring(bool in_appendix)135 	docstring const & labelstring(bool in_appendix) const
136 	{ return in_appendix ? labelstring_appendix_ : labelstring_; }
137 	///
endlabelstring()138 	docstring const & endlabelstring() const { return endlabelstring_; }
139 	///
category()140 	docstring const & category() const { return category_; }
141 	///
preamble()142 	docstring const & preamble() const { return preamble_; }
143 	/// Get language dependent macro definitions needed for this layout
144 	/// for language \p lang
langpreamble()145 	docstring const langpreamble() const { return langpreamble_; }
146 	/// Get language and babel dependent macro definitions needed for
147 	/// this layout for language \p lang
babelpreamble()148 	docstring const babelpreamble() const { return babelpreamble_; }
149 	///
requires()150 	std::set<std::string> const & requires() const { return requires_; }
151 	///
autonests()152 	std::set<docstring> const & autonests() const { return autonests_; }
153 	///
isAutonestedBy()154 	std::set<docstring> const & isAutonestedBy() const { return autonested_by_; }
155 	///
latexparam()156 	std::string const & latexparam() const { return latexparam_; }
157 	///
leftdelim()158 	docstring leftdelim() const { return leftdelim_; }
159 	///
rightdelim()160 	docstring rightdelim() const { return rightdelim_; }
161 	///
innertag()162 	std::string const & innertag() const { return innertag_; }
163 	///
labeltag()164 	std::string const & labeltag() const { return labeltag_; }
165 	///
itemtag()166 	std::string const & itemtag() const { return itemtag_; }
167 	///
168 	std::string const & htmltag() const;
169 	///
170 	std::string const & htmlattr() const;
171 	///
172 	std::string const & htmlitemtag() const;
173 	///
174 	std::string const & htmlitemattr() const;
175 	///
176 	std::string const & htmllabeltag() const;
177 	///
178 	std::string const & htmllabelattr() const;
179 	///
180 	std::string defaultCSSClass() const;
181 	///
htmllabelfirst()182 	bool htmllabelfirst() const { return htmllabelfirst_; }
183 	///
184 	docstring htmlstyle() const;
185 	///
htmlpreamble()186 	docstring const & htmlpreamble() const { return htmlpreamble_; }
187 	///
htmltitle()188 	bool htmltitle() const { return htmltitle_; }
189 	///
isParagraph()190 	bool isParagraph() const { return latextype == LATEX_PARAGRAPH; }
191 	///
isCommand()192 	bool isCommand() const { return latextype == LATEX_COMMAND; }
193 	///
isEnvironment()194 	bool isEnvironment() const {
195 		return latextype == LATEX_ENVIRONMENT
196 			|| latextype == LATEX_BIB_ENVIRONMENT
197 			|| latextype == LATEX_ITEM_ENVIRONMENT
198 			|| latextype == LATEX_LIST_ENVIRONMENT;
199 	}
200 	/// Is this the kind of layout in which adjacent paragraphs
201 	/// are handled as one group?
isParagraphGroup()202 	bool isParagraphGroup() const {	return par_group_; }
203 	///
labelIsInline()204 	bool labelIsInline() const {
205 		return labeltype == LABEL_STATIC
206 			|| labeltype == LABEL_SENSITIVE
207 		  || labeltype == LABEL_ENUMERATE
208 			|| labeltype == LABEL_ITEMIZE;
209 	}
labelIsAbove()210 	bool labelIsAbove() const {
211 		return labeltype == LABEL_ABOVE
212 			|| labeltype == LABEL_CENTERED
213 		  || labeltype == LABEL_BIBLIO;
214 	}
215 	///
addToToc()216 	bool addToToc() const { return add_to_toc_; }
217 	///
tocType()218 	std::string tocType() const { return toc_type_; }
219 	///
isTocCaption()220 	bool isTocCaption() const { return is_toc_caption_; }
221 
222 	///
223 	bool operator==(Layout const &) const;
224 	///
225 	bool operator!=(Layout const & rhs) const
226 		{ return !(*this == rhs); }
227 
228 	////////////////////////////////////////////////////////////////
229 	// members
230 	////////////////////////////////////////////////////////////////
231 	/** Default font for this layout/environment.
232 	    The main font for this kind of environment. If an attribute has
233 	    INHERITED_*, it means that the value is specified by
234 	    the defaultfont for the entire layout. If we are nested, the
235 	    font is inherited from the font in the environment one level
236 	    up until the font is resolved. The values :IGNORE_*
237 	    and FONT_TOGGLE are illegal here.
238 	*/
239 	FontInfo font;
240 
241 	/** Default font for labels.
242 	    Interpretation the same as for font above
243 	*/
244 	FontInfo labelfont;
245 
246 	/** Resolved version of the font for this layout/environment.
247 	    This is a resolved version the default font. The font is resolved
248 	    against the defaultfont of the entire layout.
249 	*/
250 	FontInfo resfont;
251 
252 	/** Resolved version of the font used for labels.
253 	    This is a resolved version the label font. The font is resolved
254 	    against the defaultfont of the entire layout.
255 	*/
256 	FontInfo reslabelfont;
257 
258 	/// Text that dictates how wide the left margin is on the screen
259 	docstring leftmargin;
260 	/// Text that dictates how wide the right margin is on the screen
261 	docstring rightmargin;
262 	/// Text that dictates how much space to leave after a potential label
263 	docstring labelsep;
264 	/// Text that dictates how much space to leave before a potential label
265 	docstring labelindent;
266 	/// Text that dictates the width of the indentation of indented pars
267 	docstring parindent;
268 	///
269 	double parskip;
270 	///
271 	double itemsep;
272 	///
273 	double topsep;
274 	///
275 	double bottomsep;
276 	///
277 	double labelbottomsep;
278 	///
279 	double parsep;
280 	///
281 	Spacing spacing;
282 	///
283 	LyXAlignment align;
284 	///
285 	LyXAlignment alignpossible;
286 	///
287 	LabelType labeltype;
288 	///
289 	EndLabelType endlabeltype;
290 	///
291 	MarginType margintype;
292 	///
293 	bool newline_allowed;
294 	///
295 	bool nextnoindent;
296 	///
297 	ToggleIndentation toggle_indent;
298 	///
299 	bool free_spacing;
300 	///
301 	bool pass_thru;
302 	/// Individual chars to be passed verbatim
303 	docstring pass_thru_chars;
304 	///
305 	bool parbreak_is_newline;
306 	/// show this in toc
307 	int toclevel;
308 	/// special value of toclevel for non-section layouts
309 	static const int NOT_IN_TOC;
310 
311 	/** true when the fragile commands in the paragraph need to be
312 	    \protect'ed. */
313 	bool needprotect;
314 	/// true when empty paragraphs should be kept.
315 	bool keepempty;
316 	/// Type of LaTeX object
317 	LatexType latextype;
318 	/// Does this object belong in the title part of the document?
319 	bool intitle;
320 	/// Is the content to go in the preamble rather than the body?
321 	bool inpreamble;
322 	/// Which counter to step
323 	docstring counter;
324 	/// Resume counter?
325 	bool resumecounter;
326 	/// Step master counter?
327 	bool stepmastercounter;
328 	/// Prefix to use when creating labels
329 	docstring refprefix;
330 	/// Depth of XML command
331 	int commanddepth;
332 
333 	/// Return a pointer on a new layout suitable to describe a caption.
334 	/// FIXME: remove this eventually. This is only for tex2lyx
335 	/// until it has proper support for the caption inset (JMarc)
336 	static Layout * forCaption();
337 
338 	/// Is this spellchecked?
339 	bool spellcheck;
340 	/**
341 	 * Should this layout definition always be written to the document preamble?
342 	 * Possible values are:
343 	 *   0: Do not enforce local layout
344 	 * >=1: Enforce local layout with version forcelocal
345 	 *  -1: Enforce local layout with infinite version
346 	 * On reading, the forced local layout is only used if its version
347 	 * number is greater than the version number of the same layout in the
348 	 * document class. Otherwise, it is ignored.
349 	 */
350 	int forcelocal;
351 
352 
353 private:
354 	/// Reads a layout definition from file
355 	/// \return true on success.
356 	bool readIgnoreForcelocal(Lexer &, TextClass const &);
357 	/// generates the default CSS for this layout
358 	void makeDefaultCSS() const;
359 	///
defaultCSSItemClass()360 	std::string defaultCSSItemClass() const { return defaultCSSClass() + "_item"; }
361 	///
defaultCSSLabelClass()362 	std::string defaultCSSLabelClass() const { return defaultCSSClass() + "_label"; }
363 
364 	/// Name of the layout/paragraph environment
365 	docstring name_;
366 
367 	/// LaTeX name for environment
368 	std::string latexname_;
369 
370 	/** Is this layout the default layout for an unknown layout? If
371 	 * so, its name will be displayed as xxx (unknown).
372 	 */
373 	bool unknown_;
374 
375 	/** Name of an layout that has replaced this layout.
376 	    This is used to rename a layout, while keeping backward
377 	    compatibility
378 	*/
379 	docstring obsoleted_by_;
380 
381 	/** Name of an layout which preamble must come before this one
382 	    This is used when the preamble snippet uses macros defined in
383 	    another preamble
384 	 */
385 	docstring depends_on_;
386 
387 	/// Label string. "Abstract", "Reference", "Caption"...
388 	docstring labelstring_;
389 	///
390 	docstring endlabelstring_;
391 	/// Label string inside appendix. "Appendix", ...
392 	docstring labelstring_appendix_;
393 	/// LaTeX parameter for environment
394 	std::string latexparam_;
395 	/// Item command in lists
396 	std::string itemcommand_;
397 	/// Left delimiter of the content
398 	docstring leftdelim_;
399 	/// Right delimiter of the content
400 	docstring rightdelim_;
401 	/// Internal tag to use (e.g., <title></title> for sect header)
402 	std::string innertag_;
403 	/// Internal tag to use (e.g. to surround varentrylist label)
404 	std::string labeltag_;
405 	/// Internal tag to surround the item text in a list.
406 	std::string itemtag_;
407 	/// The interpretation of this tag varies depending upon the latextype.
408 	/// In an environment, it is the tag enclosing all content for this set of
409 	/// paragraphs. So for quote, e.g,. it would be: blockquote. For itemize,
410 	/// it would be: ul. (You get the idea.)
411 	///
412 	/// For a command, it is the tag enclosing the content of the command.
413 	/// So, for section, it might be: h2.
414 	///
415 	/// For the paragraph type, it is the tag that will enclose each paragraph.
416 	///
417 	/// Defaults to "div".
418 	mutable std::string htmltag_;
419 	/// Additional attributes for inclusion with the start tag. Defaults
420 	/// to: class="layoutname".
421 	mutable std::string htmlattr_;
422 	/// Tag for individual paragraphs in an environment. In lists, this
423 	/// would be something like "li". But it also needs to be set for
424 	/// quotation, e.g., since the paragraphs in a quote need to be
425 	/// in "p" tags. Default is "div".
426 	/// Note that when I said "environment", I meant it: This has no
427 	/// effect for LATEX_PARAGRAPH type layouts.
428 	mutable std::string htmlitemtag_;
429 	/// Attributes for htmlitemtag_. Default is: class="layoutname_item".
430 	mutable std::string htmlitemattr_;
431 	/// Tag for labels, of whatever sort. One use for this is in setting
432 	/// descriptions, in which case it would be: dt. Another use is to
433 	/// customize the display of, say, the auto-generated label for
434 	/// sections. Defaults to "span".
435 	/// If set to "NONE", this suppresses the printing of the label.
436 	mutable std::string htmllabeltag_;
437 	/// Attributes for the label. Defaults to: class="layoutname_label".
438 	mutable std::string htmllabelattr_;
439 	/// Whether to put the label before the item, or within the item.
440 	/// I.e., do we have (true):
441 	///    <label>...</label><item>...</item>
442 	/// or instead (false):
443 	///    <item><label>...</label>...</item>
444 	/// The latter is the default.
445 	bool htmllabelfirst_;
446 	/// CSS information needed by this layout.
447 	docstring htmlstyle_;
448 	/// Should we generate the default CSS for this layout, even if HTMLStyle
449 	/// has been given? Default is false.
450 	/// Note that the default CSS is output first, then the user CSS, so it is
451 	/// possible to override what one does not want.
452 	bool htmlforcecss_;
453 	/// A cache for the default style info so generated.
454 	mutable docstring htmldefaultstyle_;
455 	/// Any other info for the HTML header.
456 	docstring htmlpreamble_;
457 	/// Whether this is the <title> paragraph.
458 	bool htmltitle_;
459 	/// calculating this is expensive, so we cache it.
460 	mutable std::string defaultcssclass_;
461 	/// This is the `category' for this layout. The following are
462 	/// recommended basic categories: FrontMatter, BackMatter, MainText,
463 	/// Sectioning, Starred, List, Reasoning.
464 	docstring category_;
465 	/// Macro definitions needed for this layout
466 	docstring preamble_;
467 	/// Language dependent macro definitions needed for this layout
468 	docstring langpreamble_;
469 	/// Language and babel dependent macro definitions needed for this layout
470 	docstring babelpreamble_;
471 	/// Are adjacent paragraphs handled as one group?
472 	bool par_group_;
473 	/// Packages needed for this layout
474 	std::set<std::string> requires_;
475 	/// Layouts that are by default nested after this one
476 	std::set<docstring> autonests_;
477 	/// Layouts that by auto-nest this one
478 	std::set<docstring> autonested_by_;
479 	///
480 	LaTeXArgMap latexargs_;
481 	///
482 	LaTeXArgMap postcommandargs_;
483 	///
484 	LaTeXArgMap itemargs_;
485 	///
486 	bool add_to_toc_;
487 	///
488 	std::string toc_type_;
489 	///
490 	bool is_toc_caption_;
491 };
492 
493 
494 } // namespace lyx
495 
496 #endif
497