11#include " cpp11tesseract_types.h"
22
3- #if TESSERACT_MAJOR_VERSION < 5
4- #include < tesseract/genericvector.h>
5- #else
63#define STRING std::string
74#define GenericVector std::vector
8- #endif
95
106[[cpp11::register ]] int tesseract_major_version () {
117 return TESSERACT_MAJOR_VERSION;
128}
139
1410using namespace cpp11 ;
1511
16- /* libtesseract 4.0 insisted that the engine is initiated in 'C' locale.
17- * We do this as exemplified in the example code in the libc manual:
18- * https://www.gnu.org/software/libc/manual/html_node/Setting-the-Locale.html
19- * Full discussion: https://github.com/tesseract-ocr/tesseract/issues/1670
20- */
21- #if TESSERACT_MAJOR_VERSION == 4 && TESSERACT_MINOR_VERSION == 0
22- #define TESSERACT40
23- #endif
24-
2512static tesseract::TessBaseAPI *make_analyze_api () {
26- #ifdef TESSERACT40
27- char *old_ctype = strdup (setlocale (LC_ALL, NULL ));
28- setlocale (LC_ALL, " C" );
29- #endif
3013 tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI ();
3114 api->InitForAnalysePage ();
32- #ifdef TESSERACT40
33- setlocale (LC_ALL, old_ctype);
34- free (old_ctype);
35- #endif
3615 return api;
3716}
3817
@@ -70,11 +49,6 @@ static tesseract::TessBaseAPI *make_analyze_api() {
7049 configs.push_back (&config_strings.back ()[0 ]);
7150 }
7251
73- #ifdef TESSERACT40
74- char *old_ctype = strdup (setlocale (LC_ALL, NULL ));
75- setlocale (LC_ALL, " C" );
76- #endif
77-
7852 tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI ();
7953
8054 GenericVector<STRING> params, values;
@@ -85,11 +59,6 @@ static tesseract::TessBaseAPI *make_analyze_api() {
8559 int err = api->Init (path, lang, tesseract::OEM_DEFAULT, configs.data (),
8660 configs.size (), ¶ms, &values, false );
8761
88- #ifdef TESSERACT40
89- setlocale (LC_ALL, old_ctype);
90- free (old_ctype);
91- #endif
92-
9362 if (err) {
9463 delete api;
9564 throw std::runtime_error (
@@ -124,13 +93,7 @@ tesseract::TessBaseAPI *get_engine(TessPtr engine) {
12493 writable::logicals out (params.size ());
12594 STRING str;
12695 for (int i = 0 ; i < params.size (); i++) {
127- #if TESSERACT_MAJOR_VERSION >= 5
12896 out[i] = api->GetVariableAsString (std::string (params.at (i)).c_str (), &str);
129- #else
130- const char *value =
131- api->GetStringVariable (std::string (params.at (i)).c_str ());
132- out[i] = (value != nullptr );
133- #endif
13497 }
13598 api->End ();
13699 delete api;
@@ -142,15 +105,9 @@ tesseract::TessBaseAPI *get_engine(TessPtr engine) {
142105 GenericVector<STRING> langs;
143106 api->GetAvailableLanguagesAsVector (&langs);
144107 writable::strings available;
145- #if TESSERACT_MAJOR_VERSION >= 5
146108 for (const auto &lang : langs) {
147109 available.push_back (lang);
148110 }
149- #else
150- for (int i = 0 ; i < langs.size (); i++) {
151- available.push_back (langs.get (i).c_str ());
152- }
153- #endif
154111 langs.clear ();
155112 api->GetLoadedLanguagesAsVector (&langs);
156113 writable::strings loaded;
@@ -175,15 +132,8 @@ tesseract::TessBaseAPI *get_engine(TessPtr engine) {
175132 std::vector<std::string> values;
176133 for (int i = 0 ; i < params.size (); ++i) {
177134 STRING str;
178- #if TESSERACT_MAJOR_VERSION >= 5
179135 if (api->GetVariableAsString (std::string (params.at (i)).c_str (), &str)) {
180136 values.push_back (str);
181- #else
182- const char *value =
183- api->GetStringVariable (std::string (params.at (i)).c_str ());
184- if (value) {
185- values.push_back (value);
186- #endif
187137 } else {
188138 values.push_back (" " );
189139 }
0 commit comments