summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp')
-rw-r--r--Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp56
1 files changed, 25 insertions, 31 deletions
diff --git a/Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp b/Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp
index 6a55fa2e246..efb6122e2a7 100644
--- a/Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp
+++ b/Build/source/texk/dvisvgm/dvisvgm-src/src/Unicode.cpp
@@ -2,7 +2,7 @@
** Unicode.cpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
-** Copyright (C) 2005-2016 Martin Gieseking <martin.gieseking@uos.de> **
+** Copyright (C) 2005-2017 Martin Gieseking <martin.gieseking@uos.de> **
** **
** This program is free software; you can redistribute it and/or **
** modify it under the terms of the GNU General Public License as **
@@ -18,12 +18,13 @@
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/
-#include <xxhash.h>
+#include <algorithm>
#include <cctype>
#include <cstddef>
#include <iomanip>
#include <sstream>
-#include "Unicode.h"
+#include <xxhash.h>
+#include "Unicode.hpp"
using namespace std;
@@ -31,18 +32,18 @@ using namespace std;
/** Returns true if c is a valid Unicode point in XML documents.
* XML version 1.0 doesn't allow various Unicode character references
* (&#1; for example). */
-bool Unicode::isValidCodepoint (UInt32 c) {
+bool Unicode::isValidCodepoint (uint32_t c) {
if ((c & 0xffff) == 0xfffe || (c & 0xffff) == 0xffff)
return false;
- UInt32 ranges[] = {
+ uint32_t ranges[] = {
0x0000, 0x0020, // basic control characters + space
0x007f, 0x009f, // use of control characters is discouraged by the XML standard
0x202a, 0x202e, // bidi control characters
0xd800, 0xdfff, // High Surrogates are not allowed in XML
0xfdd0, 0xfdef, // non-characters for internal use by applications
};
- for (size_t i=0; i < sizeof(ranges)/sizeof(UInt32) && c >= ranges[i]; i+=2)
+ for (size_t i=0; i < sizeof(ranges)/sizeof(uint32_t) && c >= ranges[i]; i+=2)
if (c <= ranges[i+1])
return false;
return true;
@@ -52,8 +53,8 @@ bool Unicode::isValidCodepoint (UInt32 c) {
/** Returns a valid Unicode point for the given character code. Character codes
* that are invalid code points because the XML standard forbids or discourages
* their usage, are mapped to the Private Use Zone U+E000-U+F8FF. */
-UInt32 Unicode::charToCodepoint (UInt32 c) {
- UInt32 ranges[] = {
+uint32_t Unicode::charToCodepoint (uint32_t c) {
+ uint32_t ranges[] = {
0x0000, 0x0020, 0xe000, // basic control characters + space
0x007f, 0x009f, 0xe021, // use of control characters is discouraged by the XML standard
0x202a, 0x202e, 0xe042, // bidi control characters
@@ -87,7 +88,7 @@ UInt32 Unicode::charToCodepoint (UInt32 c) {
/** Converts a Unicode point to a UTF-8 byte sequence.
* @param[in] cp code point
* @return utf8 sequence consisting of 1-4 bytes */
-string Unicode::utf8 (Int32 cp) {
+string Unicode::utf8 (int32_t cp) {
string utf8;
if (cp >= 0) {
if (cp < 0x80)
@@ -112,20 +113,16 @@ string Unicode::utf8 (Int32 cp) {
return utf8;
}
-#include "AGLTable.h"
-
-static inline bool is_hex_digit (char c) {
- return isdigit(c) || (c >= 'A' && c <= 'F');
-}
-
+#include "AGLTable.hpp"
/** Tries to extract the codepoint from AGL character names like "uni1234" or "u1234".
* Returns 0 if the given name doesn't satisfy the constraints.
* https://github.com/adobe-type-tools/agl-specification
* @param[in] name AGL character name
* @return the extracted codepoint or 0 on failure */
-static Int32 extract_codepoint_from_name (const string &name) {
+static int32_t extract_codepoint_from_name (const string &name) {
size_t offset=1;
+ auto is_hex_digit = [](char c) {return isdigit(c) || (c >= 'A' && c <= 'F');};
if (name.substr(0, 3) == "uni" && is_hex_digit(name[4]) && name.length() >= 7)
offset = 3;
else if (name[0] != 'u' || !is_hex_digit(name[1]) || name.length() < 5)
@@ -142,7 +139,7 @@ static Int32 extract_codepoint_from_name (const string &name) {
return 0;
if (offset == 3)
hexstr = hexstr.substr(0, 4);
- Int32 codepoint;
+ int32_t codepoint;
istringstream iss(hexstr);
iss >> hex >> codepoint;
if (!iss.fail() && (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0x10FFFF)))
@@ -172,21 +169,18 @@ static const char* get_suffix (const string &name) {
/** Returns the Unicode point for a given AGL character name.
* @param name AGL name of the character to look up
* @return codepoint of the character */
-Int32 Unicode::aglNameToCodepoint (const string &name) {
- if (Int32 cp = extract_codepoint_from_name(name))
+int32_t Unicode::aglNameToCodepoint (const string &name) {
+ if (int32_t cp = extract_codepoint_from_name(name))
return cp;
- UInt32 hash = XXH32(&name[0], name.length(), 0);
- int left=0;
- int right=sizeof(hash2unicode)/sizeof(Hash2Unicode)-1;
- while (left <= right) {
- int mid = left+(right-left)/2;
- if (hash == hash2unicode[mid].hash)
- return hash2unicode[mid].codepoint;
- if (hash < hash2unicode[mid].hash)
- right = mid-1;
- else
- left = mid+1;
- }
+ uint32_t hash = XXH32(&name[0], name.length(), 0);
+ const HashCodepointPair cmppair = {hash, 0};
+ auto it = lower_bound(hash2unicode.begin(), hash2unicode.end(), cmppair,
+ [](const HashCodepointPair &p1, const HashCodepointPair &p2) {
+ return p1.hash < p2.hash;
+ }
+ );
+ if (it != hash2unicode.end() && it->hash == hash)
+ return it->codepoint;
return 0;
}