From 34a2091dfc18177f18744c6f1563090937c761f8 Mon Sep 17 00:00:00 2001
From: Flexus <37239071+EnergyFlexus@users.noreply.github.com>
Date: Thu, 15 Sep 2022 15:14:01 +0300
Subject: [PATCH] Hyphen js+wasm test add
---
Common/3dParty/hyphen/.gitignore | 5 +-
Common/3dParty/hyphen/js/index.html | 16 +++++
Common/3dParty/hyphen/js/main.js | 72 +++++++++++++++++++
.../hyphen/js/src/exported_functions.cpp | 25 +++++++
.../hyphen/js/src/exported_functions.h | 16 +++++
Common/3dParty/hyphen/js/styles.css | 10 +++
Common/3dParty/hyphen/test/main.cpp | 34 ++++-----
7 files changed, 160 insertions(+), 18 deletions(-)
create mode 100644 Common/3dParty/hyphen/js/index.html
create mode 100644 Common/3dParty/hyphen/js/main.js
create mode 100644 Common/3dParty/hyphen/js/src/exported_functions.cpp
create mode 100644 Common/3dParty/hyphen/js/src/exported_functions.h
create mode 100644 Common/3dParty/hyphen/js/styles.css
diff --git a/Common/3dParty/hyphen/.gitignore b/Common/3dParty/hyphen/.gitignore
index 476026af48..7e727c1ce9 100644
--- a/Common/3dParty/hyphen/.gitignore
+++ b/Common/3dParty/hyphen/.gitignore
@@ -2,4 +2,7 @@ hyphen
test.pro.user
*.dic
msvc_make.bat
-build-test-*
\ No newline at end of file
+build-test-*
+*.wasm
+hyphen.js
+hyphen.data
\ No newline at end of file
diff --git a/Common/3dParty/hyphen/js/index.html b/Common/3dParty/hyphen/js/index.html
new file mode 100644
index 0000000000..898541c53e
--- /dev/null
+++ b/Common/3dParty/hyphen/js/index.html
@@ -0,0 +1,16 @@
+
+
+
+
+ test
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Common/3dParty/hyphen/js/main.js b/Common/3dParty/hyphen/js/main.js
new file mode 100644
index 0000000000..3fb8cf3360
--- /dev/null
+++ b/Common/3dParty/hyphen/js/main.js
@@ -0,0 +1,72 @@
+/**
+ * @param {Module} module
+ * @param {Number} length
+ * @return {Number}
+ * Allocate memory for wasm, returns pointer
+ */
+function allocateMemory(module, length) {
+ const ptr = module._malloc(length);
+ return ptr;
+}
+
+/**
+ * @param {Module} module
+ * @param {Number} ptr
+ * @return {void}
+ * Free memory
+ */
+function freeMemory(module, ptr) {
+ module._free(ptr);
+}
+
+/**
+ * @param {Module} module
+ * @param {Uint8Array} data
+ * @param {Number} ptr
+ * @return {void}
+ * Fill memory
+ */
+function setMemory(module, data, ptr) {
+ module.HEAP8.set(data, ptr);
+}
+
+/**
+ * @type {HTMLTextAreaElement}
+ */
+var textarea = document.getElementById("textarea");
+var form = document.querySelector("form");
+
+function hyphenate(word)
+{
+ var len = word.length;
+ var word_size = 4 * len + 1;
+ var hyphen_size = word_size + 5;
+ var hword_size = word_size * 2;
+
+ var pword = allocateMemory(Module, word_size);
+ var phyphens = allocateMemory(Module, hyphen_size);
+ var phword = allocateMemory(Module, hword_size);
+
+ Module.stringToUTF8(word, pword, word_size);
+ Module._hyphenate(pword, phyphens, phword)
+
+ var hword = Module.UTF8ToString(phword);
+
+ freeMemory(Module, pword);
+ freeMemory(Module, phyphens);
+ freeMemory(Module, phword);
+
+ return hword;
+}
+
+form.onsubmit = function(event) {
+ event.preventDefault();
+ var text = textarea.value.split("\n").join(" ").split(" ");
+ Module._load_dictionary();
+
+ for(var i = 0; i < text.length; i++) {
+ console.log(hyphenate(text[i]));
+ }
+ Module._free_dictionary();
+}
+
diff --git a/Common/3dParty/hyphen/js/src/exported_functions.cpp b/Common/3dParty/hyphen/js/src/exported_functions.cpp
new file mode 100644
index 0000000000..4dd1826c57
--- /dev/null
+++ b/Common/3dParty/hyphen/js/src/exported_functions.cpp
@@ -0,0 +1,25 @@
+#include "exported_functions.h"
+#include
+
+HyphenDict *dict;
+
+void load_dictionary()
+{
+ dict = hnj_hyphen_load("dictionaries/hyph_ru_RU.dic");
+}
+void free_dictionary()
+{
+ hnj_hyphen_free(dict);
+}
+
+void hyphenate(const char *word, char *hyphens, char *hword)
+{
+ int n = strlen(word);
+
+ char **rep = NULL;
+ int *pos = NULL;
+ int *cut = NULL;
+
+ hnj_hyphen_hyphenate2(dict, word, n, hyphens, hword, &rep, &pos, &cut);
+}
+// emcc src/exported_functions.cpp ../hyphen/hyphen.c ../hyphen/hnjalloc.c -o deploy/effects.js -sEXPORTED_FUNCTIONS='_malloc','_free','_hyphenate','_load_dictionary','_free_dictionary' -sEXPORTED_RUNTIME_METHODS='stringToUTF8','UTF8ToString' --preload-file dictionaries
\ No newline at end of file
diff --git a/Common/3dParty/hyphen/js/src/exported_functions.h b/Common/3dParty/hyphen/js/src/exported_functions.h
new file mode 100644
index 0000000000..6d5cb0b84d
--- /dev/null
+++ b/Common/3dParty/hyphen/js/src/exported_functions.h
@@ -0,0 +1,16 @@
+#include
+
+#include "./../../hyphen/hyphen.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+
+void load_dictionary();
+void free_dictionary();
+void hyphenate(const char *word, char *hyphens, char *hword);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
\ No newline at end of file
diff --git a/Common/3dParty/hyphen/js/styles.css b/Common/3dParty/hyphen/js/styles.css
new file mode 100644
index 0000000000..a521d17e8b
--- /dev/null
+++ b/Common/3dParty/hyphen/js/styles.css
@@ -0,0 +1,10 @@
+button {
+ width: 60px;
+ height: 30px;
+}
+
+#textarea{
+ display: block;
+ width: 300px;
+ height: 300px;
+}
\ No newline at end of file
diff --git a/Common/3dParty/hyphen/test/main.cpp b/Common/3dParty/hyphen/test/main.cpp
index 2819ac9c50..b7232ce4b1 100644
--- a/Common/3dParty/hyphen/test/main.cpp
+++ b/Common/3dParty/hyphen/test/main.cpp
@@ -12,7 +12,7 @@ int main(int argc, char *argv[])
std::string result_filename = PRO_DIR;
// set your filenames here
- dict_filename += "hyph_ru_RU.dic";
+ dict_filename += "hyph_en_US.dic";
words_filename += "words.txt";
result_filename += "result.txt";
@@ -50,27 +50,27 @@ int main(int argc, char *argv[])
* @brief
* input data:
*
- * word: input word
- * word_size: byte length of the input word
- * hyphens: allocated character buffer (size = word_size + 5)
- * hyphenated_word: allocated character buffer (size ~ word_size * 2) or NULL
- * rep, pos, cut: pointers (point to the allocated and _zeroed_ buffers
- * (size=word_size) or with NULL value) or NULL
+ * word: input word
+ * word_size: byte length of the input word
+ * hyphens: allocated character buffer (size = word_size + 5)
+ * hyphenated_word: allocated character buffer (size ~ word_size * 2) or NULL
+ * rep, pos, cut: pointers (point to the allocated and _zeroed_ buffers
+ * (size=word_size) or with NULL value) or NULL
*
* output data:
*
- * hyphens: hyphenation vector (hyphenation points signed with odd numbers).
- * hyphenated_word: hyphenated input word (hyphens signed with `=').
- * optional (NULL input).
- * rep: NULL (only standard hyph.), or replacements (hyphenation points
- * signed with `=' in replacements).
- * pos: NULL, or difference of the actual position and the beginning
- * positions of the change in input words.
- * cut: NULL, or counts of the removed characters of the original words
- * at hyphenation.
+ * hyphens: hyphenation vector (hyphenation points signed with odd numbers).
+ * hyphenated_word: hyphenated input word (hyphens signed with `=').
+ * optional (NULL input).
+ * rep: NULL (only standard hyph.), or replacements (hyphenation points
+ * signed with `=' in replacements).
+ * pos: NULL, or difference of the actual position and the beginning
+ * positions of the change in input words.
+ * cut: NULL, or counts of the removed characters of the original words
+ * at hyphenation.
*
* Note: rep, pos, cut are complementary arrays to the hyphens, indexed with the
- * character positions of the input word.
+ * character positions of the input word.
*/
hnj_hyphen_hyphenate2(dict, word.c_str(), n, hyphens, hword, &rep, &pos, &cut);