]> git.lyx.org Git - lyx.git/blob - src/Layout.cpp
cbac74886ac311b7f731ba938d72646b7d0e58b4
[lyx.git] / src / Layout.cpp
1 /**
2  * \file Layout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Layout.h"
16 #include "Language.h"
17 #include "TextClass.h"
18 #include "Lexer.h"
19 #include "FontInfo.h"
20
21 #include "support/Messages.h"
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
25
26 #include "support/regex.h"
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33 /// Special value of toclevel for layouts that to not belong in a TOC
34 const int Layout::NOT_IN_TOC = -1000;
35
36 //  The order of the LayoutTags enum is no more important. [asierra300396]
37 // Tags indexes.
38 enum LayoutTags {
39         LT_ALIGN = 1,
40         LT_ALIGNPOSSIBLE,
41         LT_MARGIN,
42         LT_BOTTOMSEP,
43         LT_CATEGORY,
44         LT_COMMANDDEPTH,
45         LT_COPYSTYLE,
46         LT_DEPENDSON,
47         LT_OBSOLETEDBY,
48         //LT_EMPTY,
49         LT_END,
50         //LT_ENVIRONMENT_DEFAULT,
51         //LT_FANCYHDR,
52         LT_FILL_BOTTOM,
53         LT_FILL_TOP,
54         //LT_FIRST_COUNTER,
55         LT_FONT,
56         LT_FREE_SPACING,
57         LT_PASS_THRU,
58         LT_PARBREAK_IS_NEWLINE,
59         //LT_HEADINGS,
60         LT_ITEMSEP,
61         LT_KEEPEMPTY,
62         LT_LABEL_BOTTOMSEP,
63         LT_LABELFONT,
64         LT_TEXTFONT,
65         LT_LABELINDENT,
66         LT_LABELSEP,
67         LT_LABELSTRING,
68         LT_LABELSTRING_APPENDIX,
69         LT_LABELCOUNTER,
70         LT_LABELTYPE,
71         LT_ENDLABELSTRING,
72         LT_ENDLABELTYPE,
73         LT_LATEXNAME,
74         LT_LATEXPARAM,
75         LT_OPTARGS,
76         LT_LATEXTYPE,
77         LT_LEFTMARGIN,
78         LT_NEED_PROTECT,
79         LT_NEWLINE,
80         LT_NEXTNOINDENT,
81         LT_PARINDENT,
82         LT_PARSEP,
83         LT_PARSKIP,
84         //LT_PLAIN,
85         LT_PREAMBLE,
86         LT_LANGPREAMBLE,
87         LT_BABELPREAMBLE,
88         LT_REQUIRES,
89         LT_RIGHTMARGIN,
90         LT_SPACING,
91         LT_TOPSEP,
92         LT_TOCLEVEL,
93         LT_INNERTAG,
94         LT_LABELTAG,
95         LT_ITEMTAG,
96         LT_HTMLTAG,
97         LT_HTMLATTR,
98         LT_HTMLITEM,
99         LT_HTMLITEMATTR,
100         LT_HTMLLABEL,
101         LT_HTMLLABELATTR, 
102         LT_HTMLLABELFIRST,
103         LT_HTMLPREAMBLE,
104         LT_HTMLSTYLE,
105         LT_HTMLFORCECSS,
106         LT_INPREAMBLE,
107         LT_HTMLTITLE,
108         LT_SPELLCHECK,
109         LT_REFPREFIX,
110         LT_REQARGS,
111         LT_INTITLE // keep this last!
112 };
113
114 /////////////////////
115
116 Layout::Layout()
117 {
118         unknown_ = false;
119         margintype = MARGIN_STATIC;
120         latextype = LATEX_PARAGRAPH;
121         intitle = false;
122         inpreamble = false;
123         needprotect = false;
124         keepempty = false;
125         font = inherit_font;
126         labelfont = inherit_font;
127         resfont = sane_font;
128         reslabelfont = sane_font;
129         nextnoindent = false;
130         parskip = 0.0;
131         itemsep = 0;
132         topsep = 0.0;
133         bottomsep = 0.0;
134         labelbottomsep = 0.0;
135         parsep = 0;
136         align = LYX_ALIGN_BLOCK;
137         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
138         labeltype = LABEL_NO_LABEL;
139         endlabeltype = END_LABEL_NO_LABEL;
140         // Should or should not. That is the question.
141         // spacing.set(Spacing::OneHalf);
142         fill_top = false;
143         fill_bottom = false;
144         newline_allowed = true;
145         free_spacing = false;
146         pass_thru = false;
147         parbreak_is_newline = false;
148         toclevel = NOT_IN_TOC;
149         commanddepth = 0;
150         htmllabelfirst_ = false;
151         htmlforcecss_ = false;
152         htmltitle_ = false;
153         spellcheck = true;
154         optargs = 0;
155         reqargs = 0;
156 }
157
158
159 bool Layout::read(Lexer & lex, TextClass const & tclass)
160 {
161         // This table is sorted alphabetically [asierra 30March96]
162         LexerKeyword layoutTags[] = {
163                 { "align",          LT_ALIGN },
164                 { "alignpossible",  LT_ALIGNPOSSIBLE },
165                 { "babelpreamble",  LT_BABELPREAMBLE },
166                 { "bottomsep",      LT_BOTTOMSEP },
167                 { "category",       LT_CATEGORY },
168                 { "commanddepth",   LT_COMMANDDEPTH },
169                 { "copystyle",      LT_COPYSTYLE },
170                 { "dependson",      LT_DEPENDSON },
171                 { "end",            LT_END },
172                 { "endlabelstring", LT_ENDLABELSTRING },
173                 { "endlabeltype",   LT_ENDLABELTYPE },
174                 { "fill_bottom",    LT_FILL_BOTTOM },
175                 { "fill_top",       LT_FILL_TOP },
176                 { "font",           LT_FONT },
177                 { "freespacing",    LT_FREE_SPACING },
178                 { "htmlattr",       LT_HTMLATTR },
179                 { "htmlforcecss",   LT_HTMLFORCECSS },
180                 { "htmlitem",       LT_HTMLITEM },
181                 { "htmlitemattr",   LT_HTMLITEMATTR },
182                 { "htmllabel",      LT_HTMLLABEL },
183                 { "htmllabelattr",  LT_HTMLLABELATTR },
184                 { "htmllabelfirst", LT_HTMLLABELFIRST },
185                 { "htmlpremable",   LT_HTMLPREAMBLE },
186                 { "htmlstyle",      LT_HTMLSTYLE },
187                 { "htmltag",        LT_HTMLTAG },
188                 { "htmltitle",      LT_HTMLTITLE },
189                 { "innertag",       LT_INNERTAG },
190                 { "inpreamble",     LT_INPREAMBLE },
191                 { "intitle",        LT_INTITLE },
192                 { "itemsep",        LT_ITEMSEP },
193                 { "itemtag",        LT_ITEMTAG },
194                 { "keepempty",      LT_KEEPEMPTY },
195                 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
196                 { "labelcounter",   LT_LABELCOUNTER },
197                 { "labelfont",      LT_LABELFONT },
198                 { "labelindent",    LT_LABELINDENT },
199                 { "labelsep",       LT_LABELSEP },
200                 { "labelstring",    LT_LABELSTRING },
201                 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
202                 { "labeltag",       LT_LABELTAG },
203                 { "labeltype",      LT_LABELTYPE },
204                 { "langpreamble",   LT_LANGPREAMBLE },
205                 { "latexname",      LT_LATEXNAME },
206                 { "latexparam",     LT_LATEXPARAM },
207                 { "latextype",      LT_LATEXTYPE },
208                 { "leftmargin",     LT_LEFTMARGIN },
209                 { "margin",         LT_MARGIN },
210                 { "needprotect",    LT_NEED_PROTECT },
211                 { "newline",        LT_NEWLINE },
212                 { "nextnoindent",   LT_NEXTNOINDENT },
213                 { "obsoletedby",    LT_OBSOLETEDBY },
214                 { "optionalargs",   LT_OPTARGS },
215                 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
216                 { "parindent",      LT_PARINDENT },
217                 { "parsep",         LT_PARSEP },
218                 { "parskip",        LT_PARSKIP },
219                 { "passthru",       LT_PASS_THRU },
220                 { "preamble",       LT_PREAMBLE },
221                 { "refprefix",      LT_REFPREFIX },
222                 { "requiredargs",   LT_REQARGS },
223                 { "requires",       LT_REQUIRES },
224                 { "rightmargin",    LT_RIGHTMARGIN },
225                 { "spacing",        LT_SPACING },
226                 { "spellcheck",     LT_SPELLCHECK },
227                 { "textfont",       LT_TEXTFONT },
228                 { "toclevel",       LT_TOCLEVEL },
229                 { "topsep",         LT_TOPSEP }
230         };
231
232         bool error = false;
233         bool finished = false;
234         lex.pushTable(layoutTags);
235         // parse style section
236         while (!finished && lex.isOK() && !error) {
237                 int le = lex.lex();
238                 // See comment in LyXRC.cpp.
239                 switch (le) {
240                 case Lexer::LEX_FEOF:
241                         continue;
242
243                 case Lexer::LEX_UNDEF:          // parse error
244                         lex.printError("Unknown layout tag `$$Token'");
245                         error = true;
246                         continue;
247
248                 default: 
249                         break;
250                 }
251                 switch (static_cast<LayoutTags>(le)) {
252                 case LT_END:            // end of structure
253                         finished = true;
254                         break;
255
256                 case LT_CATEGORY:
257                         lex >> category_;
258                         break;
259
260                 case LT_COPYSTYLE: {     // initialize with a known style
261                         docstring style;
262                         lex >> style;
263                         style = subst(style, '_', ' ');
264
265                         if (tclass.hasLayout(style)) {
266                                 docstring const tmpname = name_;
267                                 this->operator=(tclass[style]);
268                                 name_ = tmpname;
269                         } else {
270                                 LYXERR0("Cannot copy unknown style `"
271                                         << style << "'\n"
272                                         << "All layouts so far:");
273                                 DocumentClass::const_iterator lit = tclass.begin();
274                                 DocumentClass::const_iterator len = tclass.end();
275                                 for (; lit != len; ++lit)
276                                         LYXERR0(lit->name());
277                         }
278                         break;
279                         }
280
281                 case LT_OBSOLETEDBY: {   // replace with a known style
282                         docstring style;
283                         lex >> style;
284                         style = subst(style, '_', ' ');
285
286                         if (tclass.hasLayout(style)) {
287                                 docstring const tmpname = name_;
288                                 this->operator=(tclass[style]);
289                                 name_ = tmpname;
290                                 if (obsoleted_by().empty())
291                                         obsoleted_by_ = style;
292                         } else {
293                                 LYXERR0("Cannot replace with unknown style `" 
294                                         << style << '\'');
295
296                                 //lex.printError("Cannot replace with"
297                                 //               " unknown style "
298                                 //               "`$$Token'");
299                         }
300                         break;
301                 }
302
303                 case LT_DEPENDSON:
304                         lex >> depends_on_;
305                         depends_on_ = subst(depends_on_, '_', ' ');
306                         break;
307
308                 case LT_MARGIN:         // margin style definition.
309                         readMargin(lex);
310                         break;
311
312                 case LT_LATEXTYPE:      // LaTeX style definition.
313                         readLatexType(lex);
314                         break;
315
316                 case LT_INTITLE:
317                         lex >> intitle;
318                         break;
319
320                 case LT_INPREAMBLE:
321                         lex >> inpreamble;
322                         break;
323
324                 case LT_TOCLEVEL:
325                         lex >> toclevel;
326                         break;
327
328                 case LT_OPTARGS:
329                         lex >> optargs;
330                         break;
331
332                 case LT_REQARGS:
333                         lex >> reqargs;
334                         break;
335
336                 case LT_NEED_PROTECT:
337                         lex >> needprotect;
338                         break;
339
340                 case LT_KEEPEMPTY:
341                         lex >> keepempty;
342                         break;
343
344                 case LT_FONT:
345                         font = lyxRead(lex, font);
346                         labelfont = font;
347                         break;
348
349                 case LT_TEXTFONT:
350                         font = lyxRead(lex, font);
351                         break;
352
353                 case LT_LABELFONT:
354                         labelfont = lyxRead(lex, labelfont);
355                         break;
356
357                 case LT_NEXTNOINDENT:   // Indent next paragraph?
358                         lex >> nextnoindent;
359                         break;
360
361                 case LT_COMMANDDEPTH:
362                         lex >> commanddepth;
363                         break;
364
365                 case LT_LATEXNAME:
366                         lex >> latexname_;
367                         break;
368
369                 case LT_LATEXPARAM:
370                         lex >> latexparam_;
371                         latexparam_ = subst(latexparam_, "&quot;", "\"");
372                         break;
373
374                 case LT_INNERTAG:
375                         lex >> innertag_;
376                         break;
377
378                 case LT_LABELTAG:
379                         lex >> labeltag_;
380                         break;
381
382                 case LT_ITEMTAG:
383                         lex >> itemtag_;
384                         break;
385
386                 case LT_PREAMBLE:
387                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
388                         break;
389
390                 case LT_LANGPREAMBLE:
391                         langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
392                         break;
393
394                 case LT_BABELPREAMBLE:
395                         babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
396                         break;
397
398                 case LT_LABELTYPE:
399                         readLabelType(lex);
400                         break;
401
402                 case LT_ENDLABELTYPE:
403                         readEndLabelType(lex);
404                         break;
405
406                 case LT_LEFTMARGIN:     // left margin type
407                         lex >> leftmargin;
408                         break;
409
410                 case LT_RIGHTMARGIN:    // right margin type
411                         lex >> rightmargin;
412                         break;
413
414                 case LT_LABELINDENT:    // label indenting flag
415                         lex >> labelindent;
416                         break;
417
418                 case LT_PARINDENT:      // paragraph indent. flag
419                         lex >> parindent;
420                         break;
421
422                 case LT_PARSKIP:        // paragraph skip size
423                         lex >> parskip;
424                         break;
425
426                 case LT_ITEMSEP:        // item separation size
427                         lex >> itemsep;
428                         break;
429
430                 case LT_TOPSEP:         // top separation size
431                         lex >> topsep;
432                         break;
433
434                 case LT_BOTTOMSEP:      // bottom separation size
435                         lex >> bottomsep;
436                         break;
437
438                 case LT_LABEL_BOTTOMSEP: // label bottom separation size
439                         lex >> labelbottomsep;
440                         break;
441
442                 case LT_LABELSEP:       // label separator
443                         lex >> labelsep;
444                         labelsep = subst(labelsep, 'x', ' ');
445                         break;
446
447                 case LT_PARSEP:         // par. separation size
448                         lex >> parsep;
449                         break;
450
451                 case LT_FILL_TOP:       // fill top flag
452                         lex >> fill_top;
453                         break;
454
455                 case LT_FILL_BOTTOM:    // fill bottom flag
456                         lex >> fill_bottom;
457                         break;
458
459                 case LT_NEWLINE:        // newlines allowed?
460                         lex >> newline_allowed;
461                         break;
462
463                 case LT_ALIGN:          // paragraph align
464                         readAlign(lex);
465                         break;
466                 case LT_ALIGNPOSSIBLE:  // paragraph allowed align
467                         readAlignPossible(lex);
468                         break;
469
470                 case LT_LABELSTRING:    // label string definition
471                         // FIXME: this means LT_ENDLABELSTRING may only
472                         // occur after LT_LABELSTRING
473                         lex >> labelstring_;
474                         labelstring_ = trim(labelstring_);
475                         labelstring_appendix_ = labelstring_;
476                         break;
477
478                 case LT_ENDLABELSTRING: // endlabel string definition
479                         lex >> endlabelstring_; 
480                         endlabelstring_ = trim(endlabelstring_);
481                         break;
482
483                 case LT_LABELSTRING_APPENDIX: // label string appendix definition
484                         lex >> labelstring_appendix_;   
485                         labelstring_appendix_ = trim(labelstring_appendix_);
486                         break;
487
488                 case LT_LABELCOUNTER: // name of counter to use
489                         lex >> counter; 
490                         counter = trim(counter);
491                         break;
492
493                 case LT_FREE_SPACING:   // Allow for free spacing.
494                         lex >> free_spacing;
495                         break;
496
497                 case LT_PASS_THRU:      // Allow for pass thru.
498                         lex >> pass_thru;
499                         break;
500
501                 case LT_PARBREAK_IS_NEWLINE:
502                         lex >> parbreak_is_newline;
503                         break;
504
505                 case LT_SPACING: // setspace.sty
506                         readSpacing(lex);
507                         break;
508
509                 case LT_REQUIRES: {
510                         lex.eatLine();
511                         vector<string> const req = 
512                                 getVectorFromString(lex.getString());
513                         requires_.insert(req.begin(), req.end());
514                         break;
515                 }
516                         
517                 case LT_REFPREFIX: {
518                         docstring arg;
519                         lex >> arg;
520                         if (arg == "OFF")
521                                 refprefix.clear();
522                         else
523                                 refprefix = arg;
524                         break;
525                 }
526
527                 case LT_HTMLTAG:
528                         lex >> htmltag_;
529                         break;
530         
531                 case LT_HTMLATTR:
532                         lex >> htmlattr_;
533                         break;
534
535                 case LT_HTMLITEM:
536                         lex >> htmlitemtag_;
537                         break;
538         
539                 case LT_HTMLITEMATTR:
540                         lex >> htmlitemattr_;
541                         break;
542         
543                 case LT_HTMLLABEL:
544                         lex >> htmllabeltag_;
545                         break;
546
547                 case LT_HTMLLABELATTR: 
548                         lex >> htmllabelattr_;
549                         break;
550
551                 case LT_HTMLLABELFIRST:
552                         lex >> htmllabelfirst_;
553                         break;
554                         
555                 case LT_HTMLSTYLE:
556                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
557                         break;
558
559                 case LT_HTMLFORCECSS:
560                         lex >> htmlforcecss_;
561                         break;
562
563                 case LT_HTMLPREAMBLE:
564                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
565                         break;
566                 
567                 case LT_HTMLTITLE:
568                         lex >> htmltitle_;
569                         break;
570
571                 case LT_SPELLCHECK:
572                         lex >> spellcheck;
573                         break;
574                 }
575         }
576         lex.popTable();
577         // make sure we only have inpreamble = true for commands
578         if (inpreamble && latextype != LATEX_COMMAND) {
579                 LYXERR0("InPreamble not permitted except with Command-type layouts.");
580                 LYXERR0("Layout name: " << name());
581                 inpreamble = false;
582         }
583
584         return finished && !error;
585 }
586
587
588 enum {
589         AT_BLOCK = 1,
590         AT_LEFT,
591         AT_RIGHT,
592         AT_CENTER,
593         AT_LAYOUT
594 };
595
596
597 LexerKeyword alignTags[] = {
598         { "block",  AT_BLOCK },
599         { "center", AT_CENTER },
600         { "layout", AT_LAYOUT },
601         { "left",   AT_LEFT },
602         { "right",  AT_RIGHT }
603 };
604
605
606 void Layout::readAlign(Lexer & lex)
607 {
608         PushPopHelper pph(lex, alignTags);
609         int le = lex.lex();
610         switch (le) {
611         case Lexer::LEX_UNDEF:
612                 lex.printError("Unknown alignment `$$Token'");
613                 return;
614         default: break;
615         };
616         switch (le) {
617         case AT_BLOCK:
618                 align = LYX_ALIGN_BLOCK;
619                 break;
620         case AT_LEFT:
621                 align = LYX_ALIGN_LEFT;
622                 break;
623         case AT_RIGHT:
624                 align = LYX_ALIGN_RIGHT;
625                 break;
626         case AT_CENTER:
627                 align = LYX_ALIGN_CENTER;
628                 break;
629         case AT_LAYOUT:
630                 align = LYX_ALIGN_LAYOUT;
631                 break;
632         }
633 }
634
635
636 void Layout::readAlignPossible(Lexer & lex)
637 {
638         lex.pushTable(alignTags);
639         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
640         int lineno = lex.lineNumber();
641         do {
642                 int le = lex.lex();
643                 switch (le) {
644                 case Lexer::LEX_UNDEF:
645                         lex.printError("Unknown alignment `$$Token'");
646                         continue;
647                 default: break;
648                 };
649                 switch (le) {
650                 case AT_BLOCK:
651                         alignpossible |= LYX_ALIGN_BLOCK;
652                         break;
653                 case AT_LEFT:
654                         alignpossible |= LYX_ALIGN_LEFT;
655                         break;
656                 case AT_RIGHT:
657                         alignpossible |= LYX_ALIGN_RIGHT;
658                         break;
659                 case AT_CENTER:
660                         alignpossible |= LYX_ALIGN_CENTER;
661                         break;
662                 case AT_LAYOUT:
663                         alignpossible |= LYX_ALIGN_LAYOUT;
664                         break;
665                 }
666         } while (lineno == lex.lineNumber());
667         lex.popTable();
668 }
669
670
671 void Layout::readLabelType(Lexer & lex)
672 {
673         enum {
674                 LA_NO_LABEL = 1,
675                 LA_MANUAL,
676                 LA_TOP_ENVIRONMENT,
677                 LA_CENTERED_TOP_ENVIRONMENT,
678                 LA_STATIC,
679                 LA_SENSITIVE,
680                 LA_COUNTER,
681                 LA_ENUMERATE,
682                 LA_ITEMIZE,
683                 LA_BIBLIO
684         };
685
686
687         LexerKeyword labelTypeTags[] = {
688                 { "bibliography",             LA_BIBLIO },
689                 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
690                 { "counter",                  LA_COUNTER },
691                 { "enumerate",                LA_ENUMERATE },
692                 { "itemize",                  LA_ITEMIZE },
693                 { "manual",                   LA_MANUAL },
694                 { "no_label",                 LA_NO_LABEL },
695                 { "sensitive",                LA_SENSITIVE },
696                 { "static",                   LA_STATIC },
697                 { "top_environment",          LA_TOP_ENVIRONMENT }
698         };
699
700         PushPopHelper pph(lex, labelTypeTags);
701         int le = lex.lex();
702         switch (le) {
703         case Lexer::LEX_UNDEF:
704                 lex.printError("Unknown labeltype tag `$$Token'");
705                 return;
706         default: break;
707         }
708         switch (le) {
709         case LA_NO_LABEL:
710                 labeltype = LABEL_NO_LABEL;
711                 break;
712         case LA_MANUAL:
713                 labeltype = LABEL_MANUAL;
714                 break;
715         case LA_TOP_ENVIRONMENT:
716                 labeltype = LABEL_TOP_ENVIRONMENT;
717                 break;
718         case LA_CENTERED_TOP_ENVIRONMENT:
719                 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
720                 break;
721         case LA_STATIC:
722                 labeltype = LABEL_STATIC;
723                 break;
724         case LA_SENSITIVE:
725                 labeltype = LABEL_SENSITIVE;
726                 break;
727         case LA_COUNTER:
728                 labeltype = LABEL_COUNTER;
729                 break;
730         case LA_ENUMERATE:
731                 labeltype = LABEL_ENUMERATE;
732                 break;
733         case LA_ITEMIZE:
734                 labeltype = LABEL_ITEMIZE;
735                 break;
736         case LA_BIBLIO:
737                 labeltype = LABEL_BIBLIO;
738                 break;
739         }
740 }
741
742
743 void Layout::readEndLabelType(Lexer & lex)
744 {
745         static LexerKeyword endlabelTypeTags[] = {
746                 { "box",              END_LABEL_BOX },
747                 { "filled_box", END_LABEL_FILLED_BOX },
748                 { "no_label",     END_LABEL_NO_LABEL },
749                 { "static",     END_LABEL_STATIC }
750         };
751
752         PushPopHelper pph(lex, endlabelTypeTags);
753         int le = lex.lex();
754         switch (le) {
755         case Lexer::LEX_UNDEF:
756                 lex.printError("Unknown labeltype tag `$$Token'");
757                 break;
758         case END_LABEL_STATIC:
759         case END_LABEL_BOX:
760         case END_LABEL_FILLED_BOX:
761         case END_LABEL_NO_LABEL:
762                 endlabeltype = static_cast<EndLabelType>(le);
763                 break;
764         default:
765                 LYXERR0("Unhandled value " << le);
766                 break;
767         }
768 }
769
770
771 void Layout::readMargin(Lexer & lex)
772 {
773         LexerKeyword marginTags[] = {
774                 { "dynamic",           MARGIN_DYNAMIC },
775                 { "first_dynamic",     MARGIN_FIRST_DYNAMIC },
776                 { "manual",            MARGIN_MANUAL },
777                 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
778                 { "static",            MARGIN_STATIC }
779         };
780
781         PushPopHelper pph(lex, marginTags);
782
783         int le = lex.lex();
784         switch (le) {
785         case Lexer::LEX_UNDEF:
786                 lex.printError("Unknown margin type tag `$$Token'");
787                 return;
788         case MARGIN_STATIC:
789         case MARGIN_MANUAL:
790         case MARGIN_DYNAMIC:
791         case MARGIN_FIRST_DYNAMIC:
792         case MARGIN_RIGHT_ADDRESS_BOX:
793                 margintype = static_cast<MarginType>(le);
794                 break;
795         default:
796                 LYXERR0("Unhandled value " << le);
797                 break;
798         }
799 }
800
801
802 void Layout::readLatexType(Lexer & lex)
803 {
804         LexerKeyword latexTypeTags[] = {
805                 { "bib_environment",  LATEX_BIB_ENVIRONMENT },
806                 { "command",          LATEX_COMMAND },
807                 { "environment",      LATEX_ENVIRONMENT },
808                 { "item_environment", LATEX_ITEM_ENVIRONMENT },
809                 { "list_environment", LATEX_LIST_ENVIRONMENT },
810                 { "paragraph",        LATEX_PARAGRAPH }
811         };
812
813         PushPopHelper pph(lex, latexTypeTags);
814         int le = lex.lex();
815         switch (le) {
816         case Lexer::LEX_UNDEF:
817                 lex.printError("Unknown latextype tag `$$Token'");
818                 return;
819         case LATEX_PARAGRAPH:
820         case LATEX_COMMAND:
821         case LATEX_ENVIRONMENT:
822         case LATEX_ITEM_ENVIRONMENT:
823         case LATEX_BIB_ENVIRONMENT:
824         case LATEX_LIST_ENVIRONMENT:
825                 latextype = static_cast<LatexType>(le);
826                 break;
827         default:
828                 LYXERR0("Unhandled value " << le);
829                 break;
830         }
831 }
832
833
834 void Layout::readSpacing(Lexer & lex)
835 {
836         enum {
837                 ST_SPACING_SINGLE = 1,
838                 ST_SPACING_ONEHALF,
839                 ST_SPACING_DOUBLE,
840                 ST_OTHER
841         };
842
843         LexerKeyword spacingTags[] = {
844                 {"double",  ST_SPACING_DOUBLE },
845                 {"onehalf", ST_SPACING_ONEHALF },
846                 {"other",   ST_OTHER },
847                 {"single",  ST_SPACING_SINGLE }
848         };
849
850         PushPopHelper pph(lex, spacingTags);
851         int le = lex.lex();
852         switch (le) {
853         case Lexer::LEX_UNDEF:
854                 lex.printError("Unknown spacing token `$$Token'");
855                 return;
856         default: break;
857         }
858         switch (le) {
859         case ST_SPACING_SINGLE:
860                 spacing.set(Spacing::Single);
861                 break;
862         case ST_SPACING_ONEHALF:
863                 spacing.set(Spacing::Onehalf);
864                 break;
865         case ST_SPACING_DOUBLE:
866                 spacing.set(Spacing::Double);
867                 break;
868         case ST_OTHER:
869                 lex.next();
870                 spacing.set(Spacing::Other, lex.getString());
871                 break;
872         }
873 }
874
875
876 namespace {
877
878 docstring const i18npreamble(Language const * lang, docstring const & templ)
879 {
880         if (templ.empty())
881                 return templ;
882
883         string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
884
885 #ifdef TEX2LYX
886         // tex2lyx does not have getMessages()
887         LASSERT(false, /**/);
888 #else
889         // FIXME UNICODE
890         // lyx::regex is not unicode-safe.
891         // Should use QRegExp or (boost::u32regex, but that requires ICU)
892         static regex const reg("_\\(([^\\)]+)\\)");
893         smatch sub;
894         while (regex_search(preamble, sub, reg)) {
895                 string const key = sub.str(1);
896                 string translated;
897                 if (isAscii(key))
898                         translated = to_utf8(getMessages(lang->code()).get(key));
899                 else {
900                         lyxerr << "Warning: not translating `" << key
901                                << "' because it is not pure ASCII." << endl;
902                         translated = key;
903                 }
904                 preamble = subst(preamble, sub.str(), translated);
905         }
906 #endif
907         return from_utf8(preamble);
908 }
909
910 }
911
912
913 docstring const Layout::langpreamble(Language const * lang) const
914 {
915         return i18npreamble(lang, langpreamble_);
916 }
917
918
919 docstring const Layout::babelpreamble(Language const * lang) const
920 {
921         return i18npreamble(lang, babelpreamble_);
922 }
923
924
925 string const & Layout::htmltag() const 
926
927         if (htmltag_.empty())
928                 htmltag_ =  "div";
929         return htmltag_;
930 }
931
932
933 string const & Layout::htmlattr() const 
934
935         if (htmlattr_.empty())
936                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
937         return htmlattr_; 
938 }
939
940
941 string const & Layout::htmlitemtag() const 
942
943         if (htmlitemtag_.empty())
944                 htmlitemtag_ = "div";
945         return htmlitemtag_; 
946 }
947
948
949 string const & Layout::htmlitemattr() const 
950
951         if (htmlitemattr_.empty())
952                 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
953         return htmlitemattr_; 
954 }
955
956
957 string const & Layout::htmllabeltag() const 
958
959         if (htmllabeltag_.empty())
960                 htmllabeltag_ = "span";
961         return htmllabeltag_; 
962 }
963
964
965 string const & Layout::htmllabelattr() const 
966
967         if (htmllabelattr_.empty())
968                 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
969         return htmllabelattr_; 
970 }
971
972
973 docstring Layout::htmlstyle() const {
974         if (!htmlstyle_.empty() && !htmlforcecss_)
975                 return htmlstyle_;
976         if (htmldefaultstyle_.empty()) 
977                 makeDefaultCSS();
978         docstring retval = htmldefaultstyle_;
979         if (!htmlstyle_.empty())
980                 retval += '\n' + htmlstyle_;
981         return retval;
982 }
983
984
985 string Layout::defaultCSSClass() const
986
987         if (!defaultcssclass_.empty())
988                 return defaultcssclass_;
989         docstring d;
990         docstring::const_iterator it = name().begin();
991         docstring::const_iterator en = name().end();
992         for (; it != en; ++it) {
993                 if (!isalpha(*it))
994                         continue;
995                 if (islower(*it))
996                         d += *it;
997                 else 
998                         d += lowercase(*it);
999         }
1000         // are there other characters we need to remove?
1001         defaultcssclass_ = to_utf8(d);
1002         return defaultcssclass_;
1003 }
1004
1005
1006 // NOTE There is a whole ton of stuff that could go into this.
1007 // Things like bottomsep, topsep, and parsep could become various
1008 // sorts of margins or padding, for example. But for now we are
1009 // going to keep it simple.
1010 void Layout::makeDefaultCSS() const {
1011         // this never needs to be redone, since reloading layouts will
1012         // wipe out what we did before.
1013         if (!htmldefaultstyle_.empty()) 
1014                 return;
1015         docstring const mainfontCSS = font.asCSS();
1016         if (!mainfontCSS.empty())
1017                 htmldefaultstyle_ = 
1018                         from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1019                         mainfontCSS + from_ascii("\n}\n");
1020         if (labelfont == font || htmllabeltag() == "NONE")
1021                 return;
1022         docstring const labelfontCSS = labelfont.asCSS();
1023         if (!labelfontCSS.empty())
1024                 htmldefaultstyle_ +=
1025                         from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1026                         labelfontCSS + from_ascii("\n}\n");
1027 }
1028
1029
1030 bool Layout::operator==(Layout const & rhs) const
1031 {
1032         // This is enough for the applications we actually make,
1033         // at least at the moment. But we could check more.
1034         return name() == rhs.name()
1035                 && latexname() == rhs.latexname()
1036                 && latextype == rhs.latextype;
1037 }
1038
1039
1040 } // namespace lyx