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