From 15868147bb5bec9ebfad4b55a97206c3a652ddf5 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sun, 20 Mar 2022 11:27:46 +0100 Subject: [PATCH] Add mathbb This resolves: https://github.com/ArthurSonzogni/Diagon/issues/33 --- CMakeLists.txt | 1 + src/translator/Factory.cpp | 5 + src/translator/graph_planar/CMakeLists.txt | 36 +++-- .../graph_planar/GraphPlanarEmpty.cpp | 5 + src/translator/math/Math.cpp | 130 +++++++++++++++++- test/Math/mathbb/input | 129 +++++++++++++++++ test/Math/mathbb/output | 129 +++++++++++++++++ test/Math_style=Latex/mathbb/input | 129 +++++++++++++++++ test/Math_style=Latex/mathbb/output | 129 +++++++++++++++++ 9 files changed, 675 insertions(+), 18 deletions(-) create mode 100644 src/translator/graph_planar/GraphPlanarEmpty.cpp create mode 100644 test/Math/mathbb/input create mode 100644 test/Math/mathbb/output create mode 100644 test/Math_style=Latex/mathbb/input create mode 100644 test/Math_style=Latex/mathbb/output diff --git a/CMakeLists.txt b/CMakeLists.txt index 62bdffb..1652863 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,7 @@ if (EMSCRIPTEN) string(APPEND CMAKE_CXX_FLAGS " -s WASM=1") string(APPEND CMAKE_CXX_FLAGS " --closure 1") string(APPEND CMAKE_CXX_FLAGS " -fexceptions") + string(APPEND CMAKE_CXX_FLAGS " -sUSE_BOOST_HEADERS") #string(APPEND CMAKE_CXX_FLAGS " -s STANDALONE_WASM") #string(APPEND CMAKE_CXX_FLAGS " -s WASM_ASYNC_COMPILATION=0") #string(APPEND CMAKE_CXX_FLAGS " -s SIDE_MODULE=1") diff --git a/src/translator/Factory.cpp b/src/translator/Factory.cpp index 20b4d29..05e21f6 100644 --- a/src/translator/Factory.cpp +++ b/src/translator/Factory.cpp @@ -1,4 +1,5 @@ #include "translator/Factory.h" +#include // List of exported translator. TranslatorPtr FrameTranslator(); @@ -25,6 +26,10 @@ std::vector& TranslatorList() { out.push_back(GraphDAGTranslator()); out.push_back(GraphPlanarTranslator()); out.push_back(FlowchartTranslator()); + + auto is_null = [](const TranslatorPtr& t) { return t == nullptr; }; + out.erase(std::remove_if(out.begin(), out.end(), is_null), out.end()); + return out; } diff --git a/src/translator/graph_planar/CMakeLists.txt b/src/translator/graph_planar/CMakeLists.txt index 52fdd36..ab61be6 100644 --- a/src/translator/graph_planar/CMakeLists.txt +++ b/src/translator/graph_planar/CMakeLists.txt @@ -1,19 +1,31 @@ -ANTLR(GraphPlanar.g4) - find_package(Boost COMPONENTS graph) -add_library(translator_graph_planar STATIC - GraphPlanarLexer.cpp - GraphPlanarParser.cpp - GraphPlanar.cpp -) -if (MSVC) +if (Boost_FOUND OR EMSCRIPTEN) + ANTLR(GraphPlanar.g4) + add_library(translator_graph_planar STATIC + GraphPlanarLexer.cpp + GraphPlanarParser.cpp + GraphPlanar.cpp + ) + + target_link_libraries(translator_graph_planar + PRIVATE diagon_base + PRIVATE antlr4_static + ) + + if (Boost_FOUND) + target_link_libraries(translator_graph_planar PRIVATE Boost::graph) + else() + message(WARNING "GraphPlanar: Boost::graph not found") + endif() else() + add_library(translator_graph_planar STATIC + GraphPlanarEmpty.cpp + ) +endif() + +if (NOT MSVC) target_compile_options(translator_graph_planar PRIVATE "-Wno-attributes") endif() -target_link_libraries(translator_graph_planar - PRIVATE diagon_base - PRIVATE antlr4_static - PRIVATE Boost::graph) target_set_common(translator_graph_planar) diff --git a/src/translator/graph_planar/GraphPlanarEmpty.cpp b/src/translator/graph_planar/GraphPlanarEmpty.cpp new file mode 100644 index 0000000..16cb3be --- /dev/null +++ b/src/translator/graph_planar/GraphPlanarEmpty.cpp @@ -0,0 +1,5 @@ +#include "translator/Translator.h" + +std::unique_ptr GraphPlanarTranslator() { + return nullptr; +} diff --git a/src/translator/math/Math.cpp b/src/translator/math/Math.cpp index d97e18c..7a270a3 100644 --- a/src/translator/math/Math.cpp +++ b/src/translator/math/Math.cpp @@ -599,6 +599,105 @@ std::wstring ParseFunctionMultLatex(MathParser::FunctionContext* context, return out + L" " + ParseLatex(context->equation(0), style); } +Draw ParseFunctionMathBB(MathParser::FunctionContext* context, Style* style) { + static const std::map known = { + {"0", "𝟘"}, // + {"1", "πŸ™"}, // + {"2", "𝟚"}, // + {"3", "πŸ›"}, // + {"4", "𝟜"}, // + {"5", "𝟝"}, // + {"6", "𝟞"}, // + {"7", "𝟟"}, // + {"8", "𝟠"}, // + {"9", "𝟑"}, // + {"A", "𝔸"}, // + {"B", "𝔹"}, // + {"C", "β„‚"}, // + {"D", "𝔻"}, // + {"E", "𝔼"}, // + {"F", "𝔽"}, // + {"G", "𝔾"}, // + {"H", "ℍ"}, // + {"I", "𝕀"}, // + {"J", "𝕁"}, // + {"K", "𝕂"}, // + {"L", "𝕃"}, // + {"M", "𝕄"}, // + {"N", "β„•"}, // + {"O", "𝕆"}, // + {"P", "β„™"}, // + {"PI", "β„Ώ"}, // + {"Q", "β„š"}, // + {"R", "ℝ"}, // + {"S", "π•Š"}, // + {"T", "𝕋"}, // + {"U", "π•Œ"}, // + {"V", "𝕍"}, // + {"W", "π•Ž"}, // + {"X", "𝕏"}, // + {"Y", "𝕐"}, // + {"Z", "β„€"}, // + {"a", "𝕒"}, // + {"b", "𝕓"}, // + {"c", "𝕔"}, // + {"d", "𝕕"}, // + {"e", "𝕖"}, // + {"f", "𝕗"}, // + {"g", "π•˜"}, // + {"h", "𝕙"}, // + {"i", "β…ˆ"}, // + {"i", "π•š"}, // + {"j", "𝕛"}, // + {"k", "π•œ"}, // + {"l", "𝕝"}, // + {"m", "π•ž"}, // + {"n", "π•Ÿ"}, // + {"o", "𝕠"}, // + {"p", "𝕑"}, // + {"pi", "β„Ό"}, // + {"q", "𝕒"}, // + {"r", "𝕣"}, // + {"s", "𝕀"}, // + {"t", "π•₯"}, // + {"u", "𝕦"}, // + {"v", "𝕧"}, // + {"w", "𝕨"}, // + {"x", "𝕩"}, // + {"y", "π•ͺ"}, // + {"z", "𝕫"}, // + }; + + std::string name; + for (int i = 0; i < context->equation().size(); ++i) + name += context->equation(i)->getText(); + Draw draw; + while (name.size() > 0) { + bool found = false; + for (const auto& it : known) { + if (name.rfind(it.first) == 0) { + name = name.substr(it.first.size()); + draw = ComposeHorizontal(draw, Draw(to_wstring(it.second)), 0); + found = true; + break; + } + } + if (!found) { + name = name.substr(1); + draw = ComposeHorizontal(draw, Draw(L"?"), 0); + } + } + return draw; +} + +std::wstring ParseFunctionMathBBLatex(MathParser::FunctionContext* context, + Style* style) { + std::string name = context->equation(0)->getText(); + for (int i = 1; i < context->equation().size(); ++i) + name += context->equation(i)->getText(); + return L"\\mathbb{" + to_wstring(name) + L"}"; +} + bool CheckFunctionIntegral(MathParser::FunctionContext* context) { int num_arguments = context->equation().size(); if (num_arguments > 3) { @@ -703,18 +802,32 @@ Draw Parse(MathParser::FunctionContext* context, Style* style) { return ParseFunctionIntegral(context, style); if (function_name == "mult") return ParseFunctionMult(context, style); + if (function_name == "mathbb" || function_name == "bb") + return ParseFunctionMathBB(context, style); return ParseFunctionCommon(context, style); } std::wstring ParseLatex(MathParser::FunctionContext* context, Style* style) { static const std::map known = { - {"sin", L"\\sin"}, {"cos", L"\\cos"}, {"tan", L"\\tan"}, - {"cot", L"\\cot"}, {"arcsin", L"\\arcsin"}, {"arccos", L"\\arccos"}, - {"arctan", L"\\arctan"}, {"sinh", L"\\sinh"}, {"cosh", L"\\cosh"}, - {"tanh", L"\\tanh"}, {"coth", L"\\coth"}, {"ln", L"\\ln"}, - {"log", L"\\log"}, {"exp ", L"\\exp "}, {"max", L"\\max"}, - {"min", L"\\min"}, {"ker", L"\\ker"}, + {"arccos", L"\\arccos"}, // + {"arcsin", L"\\arcsin"}, // + {"arctan", L"\\arctan"}, // + {"cos", L"\\cos"}, // + {"cosh", L"\\cosh"}, // + {"cot", L"\\cot"}, // + {"coth", L"\\coth"}, // + {"exp ", L"\\exp "}, // + {"ker", L"\\ker"}, // + {"ln", L"\\ln"}, // + {"log", L"\\log"}, // + {"max", L"\\max"}, // + {"min", L"\\min"}, // + {"sin", L"\\sin"}, // + {"sinh", L"\\sinh"}, // + {"tan", L"\\tan"}, // + {"tanh", L"\\tanh"}, // }; + std::string function_name = context->variable()->VARIABLE()->getText(); if (function_name == "sqrt") return ParseFunctionSqrtLatex(context, style); @@ -726,6 +839,8 @@ std::wstring ParseLatex(MathParser::FunctionContext* context, Style* style) { return ParseFunctionMultLatex(context, style); if (const auto it = known.find(function_name); it != known.end()) return ParseFunctionKnownLatex(context, style, it->second); + if (function_name == "mathbb" || function_name == "bb") + return ParseFunctionMathBBLatex(context, style); return ParseFunctionCommonLatex(context, style); } @@ -976,6 +1091,9 @@ class Math : public Translator { "Rho + rho + Chi + chi + Delta + delta + Theta + theta + Nu + nu \n" "Sigma + sigma + Psi + psi + Epsilon + epsilon + Iota + iota + Xi\n" "xi + Tau + tau + Omega + omega"}, + {"14-mathbb", + "mathbb(R)\n\nbb(R)\n\nbb(ABCDEFGHIJKLMNOPQRSTUVWXYZ)\n\nbb(" + "abcdefghijklmnopqrstuvwxyz)\n\nbb(0123456789)"}, {"100-continued-fraction", "psi = 1 + 1/(1+1/(1+1/(1+1/(1+...))))"}, }; } diff --git a/test/Math/mathbb/input b/test/Math/mathbb/input new file mode 100644 index 0000000..40f9387 --- /dev/null +++ b/test/Math/mathbb/input @@ -0,0 +1,129 @@ +bb(0) +bb(1) +bb(2) +bb(3) +bb(4) +bb(5) +bb(6) +bb(7) +bb(8) +bb(9) +bb(A) +bb(B) +bb(C) +bb(D) +bb(E) +bb(F) +bb(G) +bb(H) +bb(I) +bb(J) +bb(K) +bb(L) +bb(M) +bb(N) +bb(O) +bb(P) +bb(Q) +bb(R) +bb(S) +bb(T) +bb(U) +bb(V) +bb(W) +bb(X) +bb(Y) +bb(Z) +bb(a) +bb(b) +bb(c) +bb(d) +bb(e) +bb(f) +bb(g) +bb(h) +bb(i) +bb(j) +bb(k) +bb(l) +bb(m) +bb(n) +bb(o) +bb(p) +bb(q) +bb(r) +bb(s) +bb(t) +bb(u) +bb(v) +bb(w) +bb(x) +bb(y) +bb(z) + +mathbb(0) +mathbb(1) +mathbb(2) +mathbb(3) +mathbb(4) +mathbb(5) +mathbb(6) +mathbb(7) +mathbb(8) +mathbb(9) +mathbb(A) +mathbb(B) +mathbb(C) +mathbb(D) +mathbb(E) +mathbb(F) +mathbb(G) +mathbb(H) +mathbb(I) +mathbb(J) +mathbb(K) +mathbb(L) +mathbb(M) +mathbb(N) +mathbb(O) +mathbb(P) +mathbb(Q) +mathbb(R) +mathbb(S) +mathbb(T) +mathbb(U) +mathbb(V) +mathbb(W) +mathbb(X) +mathbb(Y) +mathbb(Z) +mathbb(a) +mathbb(b) +mathbb(c) +mathbb(d) +mathbb(e) +mathbb(f) +mathbb(g) +mathbb(h) +mathbb(i) +mathbb(j) +mathbb(k) +mathbb(l) +mathbb(m) +mathbb(n) +mathbb(o) +mathbb(p) +mathbb(q) +mathbb(r) +mathbb(s) +mathbb(t) +mathbb(u) +mathbb(v) +mathbb(w) +mathbb(x) +mathbb(y) +mathbb(z) + +bb(0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) + +bb(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz) diff --git a/test/Math/mathbb/output b/test/Math/mathbb/output new file mode 100644 index 0000000..6394a5b --- /dev/null +++ b/test/Math/mathbb/output @@ -0,0 +1,129 @@ +𝟘 +πŸ™ +𝟚 +πŸ› +𝟜 +𝟝 +𝟞 +𝟟 +𝟠 +𝟑 +𝔸 +𝔹 +β„‚ +𝔻 +𝔼 +𝔽 +𝔾 +ℍ +𝕀 +𝕁 +𝕂 +𝕃 +𝕄 +β„• +𝕆 +β„™ +β„š +ℝ +π•Š +𝕋 +π•Œ +𝕍 +π•Ž +𝕏 +𝕐 +β„€ +𝕒 +𝕓 +𝕔 +𝕕 +𝕖 +𝕗 +π•˜ +𝕙 +β…ˆ +𝕛 +π•œ +𝕝 +π•ž +π•Ÿ +𝕠 +𝕑 +𝕒 +𝕣 +𝕀 +π•₯ +𝕦 +𝕧 +𝕨 +𝕩 +π•ͺ +𝕫 + +𝟘 +πŸ™ +𝟚 +πŸ› +𝟜 +𝟝 +𝟞 +𝟟 +𝟠 +𝟑 +𝔸 +𝔹 +β„‚ +𝔻 +𝔼 +𝔽 +𝔾 +ℍ +𝕀 +𝕁 +𝕂 +𝕃 +𝕄 +β„• +𝕆 +β„™ +β„š +ℝ +π•Š +𝕋 +π•Œ +𝕍 +π•Ž +𝕏 +𝕐 +β„€ +𝕒 +𝕓 +𝕔 +𝕕 +𝕖 +𝕗 +π•˜ +𝕙 +β…ˆ +𝕛 +π•œ +𝕝 +π•ž +π•Ÿ +𝕠 +𝕑 +𝕒 +𝕣 +𝕀 +π•₯ +𝕦 +𝕧 +𝕨 +𝕩 +π•ͺ +𝕫 + +πŸ˜πŸ™πŸšπŸ›πŸœπŸπŸžπŸŸπŸ πŸ‘π”Έπ”Ήβ„‚π”»π”Όπ”½π”Ύβ„π•€π•π•‚π•ƒπ•„β„•π•†β„™β„šβ„π•Šπ•‹π•Œπ•π•Žπ•π•β„€π•’π•“π•”π••π•–π•—π•˜π•™β…ˆπ•›π•œπ•π•žπ•Ÿπ• π•‘π•’π•£π•€π•₯𝕦𝕧𝕨𝕩π•ͺ𝕫 + +πŸ˜πŸ™πŸšπŸ›πŸœπŸπŸžπŸŸπŸ πŸ‘π”Έπ”Ήβ„‚π”»π”Όπ”½π”Ύβ„π•€π•π•‚π•ƒπ•„β„•π•†β„™β„šβ„π•Šπ•‹π•Œπ•π•Žπ•π•β„€π•’π•“π•”π••π•–π•—π•˜π•™β…ˆπ•›π•œπ•π•žπ•Ÿπ• π•‘π•’π•£π•€π•₯𝕦𝕧𝕨𝕩π•ͺ𝕫 diff --git a/test/Math_style=Latex/mathbb/input b/test/Math_style=Latex/mathbb/input new file mode 100644 index 0000000..40f9387 --- /dev/null +++ b/test/Math_style=Latex/mathbb/input @@ -0,0 +1,129 @@ +bb(0) +bb(1) +bb(2) +bb(3) +bb(4) +bb(5) +bb(6) +bb(7) +bb(8) +bb(9) +bb(A) +bb(B) +bb(C) +bb(D) +bb(E) +bb(F) +bb(G) +bb(H) +bb(I) +bb(J) +bb(K) +bb(L) +bb(M) +bb(N) +bb(O) +bb(P) +bb(Q) +bb(R) +bb(S) +bb(T) +bb(U) +bb(V) +bb(W) +bb(X) +bb(Y) +bb(Z) +bb(a) +bb(b) +bb(c) +bb(d) +bb(e) +bb(f) +bb(g) +bb(h) +bb(i) +bb(j) +bb(k) +bb(l) +bb(m) +bb(n) +bb(o) +bb(p) +bb(q) +bb(r) +bb(s) +bb(t) +bb(u) +bb(v) +bb(w) +bb(x) +bb(y) +bb(z) + +mathbb(0) +mathbb(1) +mathbb(2) +mathbb(3) +mathbb(4) +mathbb(5) +mathbb(6) +mathbb(7) +mathbb(8) +mathbb(9) +mathbb(A) +mathbb(B) +mathbb(C) +mathbb(D) +mathbb(E) +mathbb(F) +mathbb(G) +mathbb(H) +mathbb(I) +mathbb(J) +mathbb(K) +mathbb(L) +mathbb(M) +mathbb(N) +mathbb(O) +mathbb(P) +mathbb(Q) +mathbb(R) +mathbb(S) +mathbb(T) +mathbb(U) +mathbb(V) +mathbb(W) +mathbb(X) +mathbb(Y) +mathbb(Z) +mathbb(a) +mathbb(b) +mathbb(c) +mathbb(d) +mathbb(e) +mathbb(f) +mathbb(g) +mathbb(h) +mathbb(i) +mathbb(j) +mathbb(k) +mathbb(l) +mathbb(m) +mathbb(n) +mathbb(o) +mathbb(p) +mathbb(q) +mathbb(r) +mathbb(s) +mathbb(t) +mathbb(u) +mathbb(v) +mathbb(w) +mathbb(x) +mathbb(y) +mathbb(z) + +bb(0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) + +bb(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz) diff --git a/test/Math_style=Latex/mathbb/output b/test/Math_style=Latex/mathbb/output new file mode 100644 index 0000000..89e54bb --- /dev/null +++ b/test/Math_style=Latex/mathbb/output @@ -0,0 +1,129 @@ +\mathbb{0} \\ +\mathbb{1} \\ +\mathbb{2} \\ +\mathbb{3} \\ +\mathbb{4} \\ +\mathbb{5} \\ +\mathbb{6} \\ +\mathbb{7} \\ +\mathbb{8} \\ +\mathbb{9} \\ +\mathbb{A} \\ +\mathbb{B} \\ +\mathbb{C} \\ +\mathbb{D} \\ +\mathbb{E} \\ +\mathbb{F} \\ +\mathbb{G} \\ +\mathbb{H} \\ +\mathbb{I} \\ +\mathbb{J} \\ +\mathbb{K} \\ +\mathbb{L} \\ +\mathbb{M} \\ +\mathbb{N} \\ +\mathbb{O} \\ +\mathbb{P} \\ +\mathbb{Q} \\ +\mathbb{R} \\ +\mathbb{S} \\ +\mathbb{T} \\ +\mathbb{U} \\ +\mathbb{V} \\ +\mathbb{W} \\ +\mathbb{X} \\ +\mathbb{Y} \\ +\mathbb{Z} \\ +\mathbb{a} \\ +\mathbb{b} \\ +\mathbb{c} \\ +\mathbb{d} \\ +\mathbb{e} \\ +\mathbb{f} \\ +\mathbb{g} \\ +\mathbb{h} \\ +\mathbb{i} \\ +\mathbb{j} \\ +\mathbb{k} \\ +\mathbb{l} \\ +\mathbb{m} \\ +\mathbb{n} \\ +\mathbb{o} \\ +\mathbb{p} \\ +\mathbb{q} \\ +\mathbb{r} \\ +\mathbb{s} \\ +\mathbb{t} \\ +\mathbb{u} \\ +\mathbb{v} \\ +\mathbb{w} \\ +\mathbb{x} \\ +\mathbb{y} \\ +\mathbb{z} \\ +\\ +\mathbb{0} \\ +\mathbb{1} \\ +\mathbb{2} \\ +\mathbb{3} \\ +\mathbb{4} \\ +\mathbb{5} \\ +\mathbb{6} \\ +\mathbb{7} \\ +\mathbb{8} \\ +\mathbb{9} \\ +\mathbb{A} \\ +\mathbb{B} \\ +\mathbb{C} \\ +\mathbb{D} \\ +\mathbb{E} \\ +\mathbb{F} \\ +\mathbb{G} \\ +\mathbb{H} \\ +\mathbb{I} \\ +\mathbb{J} \\ +\mathbb{K} \\ +\mathbb{L} \\ +\mathbb{M} \\ +\mathbb{N} \\ +\mathbb{O} \\ +\mathbb{P} \\ +\mathbb{Q} \\ +\mathbb{R} \\ +\mathbb{S} \\ +\mathbb{T} \\ +\mathbb{U} \\ +\mathbb{V} \\ +\mathbb{W} \\ +\mathbb{X} \\ +\mathbb{Y} \\ +\mathbb{Z} \\ +\mathbb{a} \\ +\mathbb{b} \\ +\mathbb{c} \\ +\mathbb{d} \\ +\mathbb{e} \\ +\mathbb{f} \\ +\mathbb{g} \\ +\mathbb{h} \\ +\mathbb{i} \\ +\mathbb{j} \\ +\mathbb{k} \\ +\mathbb{l} \\ +\mathbb{m} \\ +\mathbb{n} \\ +\mathbb{o} \\ +\mathbb{p} \\ +\mathbb{q} \\ +\mathbb{r} \\ +\mathbb{s} \\ +\mathbb{t} \\ +\mathbb{u} \\ +\mathbb{v} \\ +\mathbb{w} \\ +\mathbb{x} \\ +\mathbb{y} \\ +\mathbb{z} \\ +\\ +\mathbb{0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz} \\ +\\ +\mathbb{0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}