| 1 |
// |
| 2 |
// Copyright RIME Developers |
| 3 |
// Distributed under the BSD License |
| 4 |
// |
| 5 |
// 2016-09-08 osfans <[email protected]> |
| 6 |
// |
| 7 |
|
| 8 |
#include <boost/asio.hpp> |
| 9 |
#include <boost/algorithm/string.hpp> |
| 10 |
#include <boost/locale.hpp> |
| 11 |
|
| 12 |
#include <rime/candidate.h> |
| 13 |
#include <rime/engine.h> |
| 14 |
#include <rime/schema.h> |
| 15 |
#include <rime/segmentation.h> |
| 16 |
#include <rime/ticket.h> |
| 17 |
#include <rime/translation.h> |
| 18 |
#include <rime/gear/translator_commons.h> |
| 19 |
|
| 20 |
#include "codepoint_translator.h" |
| 21 |
|
| 22 |
namespace rime { |
| 23 |
|
| 24 |
CodepointTranslator::CodepointTranslator(const Ticket& ticket) |
| 25 |
: Translator(ticket), tag_("charset") { |
| 26 |
if (!ticket.schema) |
| 27 |
return; |
| 28 |
Config* config = ticket.schema->config(); |
| 29 |
config->GetString(name_space_ + "/tag", &tag_); |
| 30 |
} |
| 31 |
|
| 32 |
void CodepointTranslator::Initialize() { |
| 33 |
initialized_ = true; // no retry |
| 34 |
if (!engine_) |
| 35 |
return; |
| 36 |
Ticket ticket(engine_, name_space_); |
| 37 |
Config* config = engine_->schema()->config(); |
| 38 |
if (!config) |
| 39 |
return; |
| 40 |
config->GetString(name_space_ + "/prefix", &prefix_); |
| 41 |
config->GetString(name_space_ + "/suffix", &suffix_); |
| 42 |
config->GetString(name_space_ + "/tips", &tips_); |
| 43 |
config->GetString(name_space_ + "/charset", &charset_); |
| 44 |
converters_["utf"] = [](const string& code) { |
| 45 |
string s; |
| 46 |
uint32_t i = 0; |
| 47 |
sscanf(code.c_str(), "%x", &i); |
| 48 |
if (i > 0) { |
| 49 |
s = boost::locale::conv::utf_to_utf<char>(&i, &i+1); |
| 50 |
} |
| 51 |
return s; |
| 52 |
}; |
| 53 |
converters_["xml"] = [](const string& code) { |
| 54 |
string s; |
| 55 |
uint32_t i = 0; |
| 56 |
sscanf(code.c_str(), "%u", &i); |
| 57 |
if (i > 0) { |
| 58 |
s = boost::locale::conv::utf_to_utf<char>(&i, &i+1); |
| 59 |
} |
| 60 |
return s; |
| 61 |
}; |
| 62 |
converters_["quwei"] = [](const string& code) { |
| 63 |
string s; |
| 64 |
uint16_t i = 0; |
| 65 |
sscanf(code.c_str(), "%hu", &i); |
| 66 |
if (i >= 1601 && i <= 9494) { |
| 67 |
i = (i/100 + 0xa0)<<8 | (i%100 + 0xa0); |
| 68 |
i = ntohs(i); |
| 69 |
s = boost::locale::conv::to_utf<char>((const char*)&i, "gb2312"); |
| 70 |
} |
| 71 |
return s; |
| 72 |
}; |
| 73 |
converters_[""] = converters_["codepoint"] = converters_["utf"]; // aliases |
| 74 |
converters_["dec"] = converters_["xml"]; |
| 75 |
} |
| 76 |
|
| 77 |
static string conv_to_utf(const string& input, const string& charset) { |
| 78 |
string s; |
| 79 |
uint32_t codepoint = 0; |
| 80 |
string code = input; |
| 81 |
size_t n = code.length(); |
| 82 |
size_t padding = 8 - (n % 8); |
| 83 |
if (padding < 8) code.append(padding, '0'); |
| 84 |
sscanf(code.c_str(), "%x", &codepoint); |
| 85 |
if (codepoint > 0) { |
| 86 |
codepoint = ntohl(codepoint); |
| 87 |
const char* c = (const char*)&codepoint; |
| 88 |
s += boost::locale::conv::to_utf<char>(c, c + 4, charset); |
| 89 |
} |
| 90 |
return s; |
| 91 |
} |
| 92 |
|
| 93 |
an<Translation> CodepointTranslator::Query(const string& input, |
| 94 |
const Segment& segment) { |
| 95 |
if (!segment.HasTag(tag_)) |
| 96 |
return nullptr; |
| 97 |
if (!initialized_) |
| 98 |
Initialize(); |
| 99 |
DLOG(INFO) << "input = '" << input |
| 100 |
<< "', [" << segment.start << ", " << segment.end << ")"; |
| 101 |
|
| 102 |
size_t start = 0; |
| 103 |
if (!prefix_.empty() && boost::starts_with(input, prefix_)) { |
| 104 |
start = prefix_.length(); |
| 105 |
} |
| 106 |
string code = input.substr(start); |
| 107 |
if (!suffix_.empty() && boost::ends_with(code, suffix_)) { |
| 108 |
code.resize(code.length() - suffix_.length()); |
| 109 |
} |
| 110 |
|
| 111 |
if (start > 0) { |
| 112 |
// usually translators do not modify the segment directly; |
| 113 |
// prompt text is best set by a processor or a segmentor. |
| 114 |
const_cast<Segment*>(&segment)->prompt = tips_; |
| 115 |
} |
| 116 |
|
| 117 |
if (code.length() == 0) return nullptr; |
| 118 |
try { |
| 119 |
string converted; |
| 120 |
if (converters_.find(charset_) != converters_.end()) { |
| 121 |
converted = converters_[charset_](code); |
| 122 |
} else { |
| 123 |
converted = conv_to_utf(code, charset_); |
| 124 |
} |
| 125 |
if (converted.empty()) return nullptr; |
| 126 |
auto candidate = New<SimpleCandidate>("raw", |
| 127 |
segment.start, |
| 128 |
segment.end, |
| 129 |
converted); |
| 130 |
return New<UniqueTranslation>(candidate); |
| 131 |
} catch (...) { |
| 132 |
return nullptr; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
} // namespace rime |