Skip to content

Commit 07d5365

Browse files
committed
baseapi: Use std::stringstream to format float values
Using std::stringstream allows conversion of float to string independent of the current locale setting. Signed-off-by: Stefan Weil <[email protected]>
1 parent 47598fa commit 07d5365

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/api/baseapi.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
#include <cstring> // for strcmp, strcpy
4848
#include <fstream> // for size_t
4949
#include <iostream> // for std::cin
50+
#include <locale> // for std::locale::classic
5051
#include <memory> // for std::unique_ptr
5152
#include <set> // for std::pair
53+
#include <sstream> // for std::stringstream
5254
#include <vector> // for std::vector
5355
#include "allheaders.h" // for pixDestroy, boxCreate, boxaAddBox, box...
5456
#include "blobclass.h" // for ExtractFontName
@@ -1687,19 +1689,23 @@ char* TessBaseAPI::GetOsdText(int page_number) {
16871689
// clockwise rotation needed to make the page upright
16881690
int rotate = OrientationIdToValue(orient_deg / 90);
16891691

1690-
const int kOsdBufsize = 255;
1691-
char* osd_buf = new char[kOsdBufsize];
1692-
snprintf(osd_buf, kOsdBufsize,
1693-
"Page number: %d\n"
1694-
"Orientation in degrees: %d\n"
1695-
"Rotate: %d\n"
1696-
"Orientation confidence: %.2f\n"
1697-
"Script: %s\n"
1698-
"Script confidence: %.2f\n",
1699-
page_number, orient_deg, rotate, orient_conf, script_name,
1700-
script_conf);
1701-
1702-
return osd_buf;
1692+
std::stringstream stream;
1693+
// Use "C" locale (needed for float values orient_conf and script_conf).
1694+
stream.imbue(std::locale::classic());
1695+
// Use fixed notation with 2 digits after the decimal point for float values.
1696+
stream.precision(2);
1697+
stream
1698+
<< std::fixed
1699+
<< "Page number: " << page_number << "\n"
1700+
<< "Orientation in degrees: " << orient_deg << "\n"
1701+
<< "Rotate: " << rotate << "\n"
1702+
<< "Orientation confidence: " << orient_conf << "\n"
1703+
<< "Script: " << script_name << "\n"
1704+
<< "Script confidence: " << script_conf << "\n";
1705+
const std::string& text = stream.str();
1706+
char* result = new char[text.length() + 1];
1707+
strcpy(result, text.c_str());
1708+
return result;
17031709
}
17041710

17051711
#endif // ndef DISABLED_LEGACY_ENGINE

0 commit comments

Comments
 (0)