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