Browse Source

itex to mathml working, can generate svg files

master
alistair 3 years ago
parent
commit
928089022d
  1. 9
      CMakeLists.txt
  2. 83
      main.cpp

9
CMakeLists.txt

@ -52,8 +52,11 @@ if(NOT TARGET lexbor) @@ -52,8 +52,11 @@ if(NOT TARGET lexbor)
find_package(lexbor REQUIRED)
endif()
link_directories(${GLIB_LIBRARY_DIRS})
ADD_EXECUTABLE(stgen3 util.cpp darkhttpd.c duktape/src/duktape.c markdown.cpp main.cpp)
target_include_directories(stgen3 PRIVATE include duktape/src itexToMML/itex-src
lib/lasem/src )
@ -67,11 +70,15 @@ add_custom_command( @@ -67,11 +70,15 @@ add_custom_command(
message("defaults-h")
)
add_definitions(${GLIB_CFLAGS_OTHER})
target_link_libraries(stgen3 PRIVATE ${CMAKE_SOURCE_DIR}/MultiMarkdown/build/libMultiMarkdown.a)
target_link_libraries(stgen3 PRIVATE ${CMAKE_SOURCE_DIR}/itexToMML/itex-src/libitex2MML.a)
target_link_libraries(stgen3 PRIVATE tinyxml2)
target_link_libraries(stgen3 PRIVATE lexbor )
target_link_libraries(stgen3 PRIVATE source-highlight)
target_link_libraries(stgen3 PRIVATE ${CMAKE_SOURCE_DIR}/lib/lasem/src/.libs/liblasem-0.6.a)
target_link_libraries(stgen3 PRIVATE
${CMAKE_SOURCE_DIR}/lib/lasem/src/.libs/liblasem-0.6.so ${GLIB_LIBRARIES} m
pthread /usr/lib64/libgobject-2.0.so.0 /usr/lib64/libcairo.so.2)
install(TARGETS stgen3)

83
main.cpp

@ -1,3 +1,6 @@ @@ -1,3 +1,6 @@
#include <ctime>
#include <ios>
#include <fstream>
@ -31,7 +34,16 @@ They are provided by the lyx-fonts package in fedora, and the ttf-lyx package in @@ -31,7 +34,16 @@ They are provided by the lyx-fonts package in fedora, and the ttf-lyx package in
#include <lsm.h>
#include <lsmmathml.h>
#include <lsmsvg.h>
#include <lsm.h>
#include <lsmmathml.h>
#include <lsmsvg.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <cairo-pdf.h>
#include <cairo-svg.h>
#include <cairo-ps.h>
}
#include <json.hpp>
@ -146,6 +158,7 @@ class Imtex { @@ -146,6 +158,7 @@ class Imtex {
double option_zoom = 1.0;
double width_pt = 2.0;
double height_pt = 2.0;
public:
Imtex () {
@ -166,24 +179,23 @@ class Imtex { @@ -166,24 +179,23 @@ class Imtex {
static cairo_status_t cairo_stream_to_buffer_func (void *closure,
const unsigned char *data, unsigned int datalen)
{
std::vector<char> *cairobuffer = (std::vector<char> *)closure;
std::string *cairobuffer = (std::string *)closure;
for (int i = 0 ; i < datalen ; i ++) {
cairobuffer->push_back(data[i]);
}
return (cairo_status_t)0;
}
return CAIRO_STATUS_SUCCESS;
}
public:
std::string get_svg_from_itex(const std::string& itex) {
auto doc =
LSM_DOM_DOCUMENT(lsm_mathml_document_new_from_itex (itex.c_str(), itex.size(), NULL));
if (doc == NULL) {
spdlog::error("Failed to parse itex");
spdlog::error("Failed to parse itex {}", itex) ;
return itex;
}
@ -195,10 +207,17 @@ class Imtex { @@ -195,10 +207,17 @@ class Imtex {
*/
if (doc == NULL) {
spdlog::error("Failed to parse itex");
return itex;
}
char *tbuffer;
gsize size;
lsm_dom_document_save_to_memory (doc, &tbuffer, &size, NULL);
std::string mathml {tbuffer, tbuffer + size};
spdlog::info("Mathml: {}", mathml);
g_free (tbuffer);
auto view = lsm_dom_document_create_view (doc);
lsm_dom_view_set_resolution (view, option_ppi);
@ -210,21 +229,41 @@ class Imtex { @@ -210,21 +229,41 @@ class Imtex {
//auto surface = cairo_svg_surface_create (output_filename,
// width_pt, height_pt);
auto surface = cairo_svg_surface_create_for_stream(cairo_stream_to_buffer_func, NULL, width_pt, height_pt);
// cairo_surface_t * surface = cairo_svg_surface_create_for_stream(cairo_stream_to_buffer_func, (void *)&bufer, width_pt, height_pt);
cairo_surface_t * surface = cairo_svg_surface_create("testfil.svg", width_pt, height_pt);
cairo_t * cairo = cairo_create(surface);
//cairo_surface_destory(surface);
//cairo_scale (cairo, option_zoom, option_zoom);
cairo_scale (cairo, option_zoom, option_zoom);
lsm_dom_view_render(view, cairo, -offset_x_pt, -offset_y_pt);
/*
void
cairo_surface_get_mime_data (cairo_surface_t *surface,
const char *mime_type,
const unsigned char **data,
unsigned long *length);
*/
unsigned long sd_len;
const unsigned char * data;
cairo_surface_get_mime_data(surface, CAIRO_MIME_TYPE_URI, &data, &sd_len);
std::string bufer {data, data + sd_len};
spdlog::info("{} svg? \n\n {}", itex, bufer);
cairo_surface_destroy(surface);
cairo_destroy (cairo);
g_object_unref (view);
g_object_unref (doc);
spdlog::info("{} svg again? \n\n {}", itex, bufer);
return mathml;
}
@ -262,7 +301,7 @@ struct runtime_config { @@ -262,7 +301,7 @@ struct runtime_config {
runtime_config settings;
#ifdef katex_support
namespace duk {
extern "C" {
#include <duk_config.h>
@ -270,6 +309,7 @@ extern "C" { @@ -270,6 +309,7 @@ extern "C" {
}
class duktape {
duk_context *ctx = nullptr;
@ -415,6 +455,7 @@ class duktape { @@ -415,6 +455,7 @@ class duktape {
};
#endif // katex_support
/* Fork and exec a command given in args taking input from in and sending
@ -458,12 +499,16 @@ class builder { @@ -458,12 +499,16 @@ class builder {
templater *s2_templr;
std::unordered_map<fs::path, time_t, pathHash> last_build;
duk::duktape dukengine {};
gnu_highlighter shl {};
imtex::Imtex imtx {};
const std::set<std::string> apply_templates_exts {"md","html", "txt", "markdown", "xml", "atom", "rss"};
#ifdef katex_support
duk::duktape dukengine {};
#endif
std::string
postprocess_math(const std::string &html_text)
{
@ -503,7 +548,12 @@ class builder { @@ -503,7 +548,12 @@ class builder {
lxb_char_t *text = lxb_dom_node_text_content(&element->node, &len);
const std::string tt {(const char *)text, (const char *)text + len};
#ifdef katex_support
std::string mathtext = dukengine.eval_katex(tt);
#endif
// ok itex only understands display math?
std::string mathtext = imtx.get_svg_from_itex("$$" + tt.substr(2, tt.size() - 4) + "$$");
const unsigned char local_name[] = "math";
@ -984,9 +1034,10 @@ class builder { @@ -984,9 +1034,10 @@ class builder {
s2_templr = new templater({}, {new rss_feed_plugin{}, new microblog_plugin{}});
add_default_templates();
#ifdef katex_support
if (settings.comp_katex)
dukengine.prepare_katex();
#endif
}
~builder() {delete templr; delete textsub_templr; delete s2_templr;}

Loading…
Cancel
Save