Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mathbb #34

Merged
merged 1 commit into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/translator/Factory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "translator/Factory.h"
#include <algorithm>

// List of exported translator.
TranslatorPtr FrameTranslator();
Expand All @@ -25,6 +26,10 @@ std::vector<TranslatorPtr>& 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;
}

Expand Down
36 changes: 24 additions & 12 deletions src/translator/graph_planar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions src/translator/graph_planar/GraphPlanarEmpty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "translator/Translator.h"

std::unique_ptr<Translator> GraphPlanarTranslator() {
return nullptr;
}
130 changes: 124 additions & 6 deletions src/translator/math/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> 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) {
Expand Down Expand Up @@ -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<std::string, std::wstring> 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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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+...))))"},
};
}
Expand Down
129 changes: 129 additions & 0 deletions test/Math/mathbb/input
Original file line number Diff line number Diff line change
@@ -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)
Loading