]> git.lyx.org Git - lyx.git/blob - src/BufferEncodings.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / BufferEncodings.cpp
1 /**
2  * \file BufferEncodings.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 Dekel Tsur
9  * \author Stephan Witt
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "BufferEncodings.h"
17
18 #include "Buffer.h"
19 #include "InsetIterator.h"
20 #include "LaTeXFeatures.h"
21
22 #include "support/lstrings.h"
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 void BufferEncodings::initUnicodeMath(Buffer const & buffer, bool for_master)
30 {
31         if (for_master) {
32                 mathcmd.clear();
33                 textcmd.clear();
34                 mathsym.clear();
35         }
36
37         // Check this buffer
38         for (Inset const & it : buffer.inset())
39                 it.initUnicodeMath();
40
41         if (!for_master)
42                 return;
43
44         // Check children
45         for (Buffer * buf : buffer.getDescendants())
46                 initUnicodeMath(*buf, false);
47 }
48
49
50 void BufferEncodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
51 {
52         CharInfo const & ci = Encodings::unicodeCharInfo(c);
53         if (ci.isUnicodeSymbol()) {
54                 // In mathed, c could be used both in textmode and mathmode
55                 docstring const textcommand = ci.textCommand();
56                 bool const math_mode = for_mathed && isMathCmd(c);
57                 bool const use_math = math_mode ||
58                                       (!for_mathed && textcommand.empty());
59                 bool const use_text = (for_mathed && isTextCmd(c)) ||
60                                       (!for_mathed && !textcommand.empty());
61                 bool const plain_utf8 = (features.runparams().encoding->name() == "utf8-plain");
62                 bool const unicode_math = (features.isRequired("unicode-math")
63                         && features.isAvailable("unicode-math"));
64                 // with utf8-plain, we only load packages when in mathed (see #7766)
65                 // and if we do not use unicode-math
66                 if ((math_mode && !unicode_math)
67                      || (use_math && !plain_utf8)) {
68                         string const mathpreamble = ci.mathPreamble();
69                         if (!mathpreamble.empty()) {
70                                 if (ci.mathFeature()) {
71                                         string feats = mathpreamble;
72                                         while (!feats.empty()) {
73                                                 string feat;
74                                                 feats = split(feats, feat, ',');
75                                                 features.require(feat);
76                                         }
77                                 } else
78                                         features.addPreambleSnippet(from_utf8(mathpreamble));
79                         }
80                 }
81                 // with utf8-plain, we do not load packages (see #7766)
82                 if (use_text && !plain_utf8) {
83                         string const textpreamble = ci.textPreamble();
84                         if (!textpreamble.empty()) {
85                                 if (ci.textFeature()) {
86                                         string feats = textpreamble;
87                                         while (!feats.empty()) {
88                                                 string feat;
89                                                 feats = split(feats, feat, ',');
90                                                 // context-dependent features are handled
91                                                 // in Paragraph::Private::validate()
92                                                 if (!contains(feat, '='))
93                                                         features.require(feat);
94                                         }
95                                 } else
96                                         features.addPreambleSnippet(from_utf8(textpreamble));
97                         }
98                 }
99         }
100         if (for_mathed && isMathSym(c)) {
101                 features.require("amstext");
102                 features.require("lyxmathsym");
103         }
104 }
105
106 } // namespace lyx