| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "pinyinhelper.h" |
| 9 |
#include <algorithm> |
| 10 |
#include <clipboard_public.h> |
| 11 |
#include <fcitx-config/iniparser.h> |
| 12 |
#include <fcitx-utils/event.h> |
| 13 |
#include <fcitx-utils/i18n.h> |
| 14 |
#include <fcitx-utils/standardpaths.h> |
| 15 |
#include <fcitx-utils/utf8.h> |
| 16 |
#include <fcitx/addonfactory.h> |
| 17 |
#include <fcitx/inputcontext.h> |
| 18 |
#include <fcitx/inputmethodentry.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <set> |
| 21 |
|
| 22 |
namespace fcitx { |
| 23 |
|
| 24 |
PinyinHelper::PinyinHelper(Instance *instance) : instance_(instance) { |
| 25 |
// This is ok in the test. |
| 26 |
if (!instance_) { |
| 27 |
return; |
| 28 |
} |
| 29 |
deferEvent_ = instance_->eventLoop().addDeferEvent([this](EventSource *) { |
| 30 |
initQuickPhrase(); |
| 31 |
return true; |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
void PinyinHelper::initQuickPhrase() { |
| 36 |
if (!quickphrase()) { |
| 37 |
return; |
| 38 |
} |
| 39 |
handler_ = quickphrase()->call<IQuickPhrase::addProvider>( |
| 40 |
[this](InputContext *ic, const std::string &input, |
| 41 |
const QuickPhraseAddCandidateCallback &callback) { |
| 42 |
if (input != "duyin") { |
| 43 |
return true; |
| 44 |
} |
| 45 |
std::vector<std::string> s; |
| 46 |
if (ic->capabilityFlags().test(CapabilityFlag::SurroundingText)) { |
| 47 |
if (auto selected = ic->surroundingText().selectedText(); |
| 48 |
!selected.empty()) { |
| 49 |
s.push_back(std::move(selected)); |
| 50 |
} |
| 51 |
} |
| 52 |
if (clipboard()) { |
| 53 |
if (s.empty()) { |
| 54 |
auto primary = clipboard()->call<IClipboard::primary>(ic); |
| 55 |
if (std::find(s.begin(), s.end(), primary) == s.end()) { |
| 56 |
s.push_back(std::move(primary)); |
| 57 |
} |
| 58 |
} |
| 59 |
auto clip = clipboard()->call<IClipboard::clipboard>(ic); |
| 60 |
if (std::find(s.begin(), s.end(), clip) == s.end()) { |
| 61 |
s.push_back(std::move(clip)); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (s.empty()) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
for (const auto &str : s) { |
| 69 |
if (!utf8::validate(str)) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
// Hard limit to prevent do too much lookup. |
| 73 |
constexpr int limit = 20; |
| 74 |
int counter = 0; |
| 75 |
for (auto c : utf8::MakeUTF8CharRange(str)) { |
| 76 |
auto result = lookup(c); |
| 77 |
if (!result.empty()) { |
| 78 |
auto py = stringutils::join(result, ", "); |
| 79 |
auto display = _("{0} ({1})", utf8::UCS4ToUTF8(c), py); |
| 80 |
callback(display, display, |
| 81 |
QuickPhraseAction::DoNothing); |
| 82 |
} |
| 83 |
if (counter >= limit) { |
| 84 |
break; |
| 85 |
} |
| 86 |
counter += 1; |
| 87 |
} |
| 88 |
} |
| 89 |
return false; |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
std::vector<std::string> PinyinHelper::lookup(uint32_t chr) { |
| 94 |
if (lookup_.load()) { |
| 95 |
return lookup_.lookup(chr); |
| 96 |
} |
| 97 |
return {}; |
| 98 |
} |
| 99 |
|
| 100 |
std::vector<std::tuple<std::string, std::string, int>> |
| 101 |
PinyinHelper::fullLookup(uint32_t chr) { |
| 102 |
if (lookup_.load()) { |
| 103 |
return lookup_.fullLookup(chr); |
| 104 |
} |
| 105 |
return {}; |
| 106 |
} |
| 107 |
|
| 108 |
void PinyinHelper::loadStroke() { stroke_.loadAsync(); } |
| 109 |
|
| 110 |
std::vector<std::pair<std::string, std::string>> |
| 111 |
PinyinHelper::lookupStroke(const std::string &input, int limit) { |
| 112 |
static const std::set<char> num{'1', '2', '3', '4', '5'}; |
| 113 |
static const std::map<char, char> py{ |
| 114 |
{'h', '1'}, {'s', '2'}, {'p', '3'}, {'n', '4'}, {'z', '5'}}; |
| 115 |
if (input.empty()) { |
| 116 |
return {}; |
| 117 |
} |
| 118 |
if (!stroke_.load()) { |
| 119 |
return {}; |
| 120 |
} |
| 121 |
if (num.count(input[0])) { |
| 122 |
if (!std::all_of(input.begin(), input.end(), |
| 123 |
[&](char c) { return num.count(c); })) { |
| 124 |
return {}; |
| 125 |
} |
| 126 |
return stroke_.lookup(input, limit); |
| 127 |
} |
| 128 |
if (py.count(input[0])) { |
| 129 |
if (!std::all_of(input.begin(), input.end(), |
| 130 |
[&](char c) { return py.count(c); })) { |
| 131 |
return {}; |
| 132 |
} |
| 133 |
std::string converted; |
| 134 |
std::transform(input.begin(), input.end(), |
| 135 |
std::back_inserter(converted), |
| 136 |
[&](char c) { return py.find(c)->second; }); |
| 137 |
return stroke_.lookup(converted, limit); |
| 138 |
} |
| 139 |
return {}; |
| 140 |
} |
| 141 |
|
| 142 |
std::string PinyinHelper::reverseLookupStroke(const std::string &input) { |
| 143 |
if (!stroke_.load()) { |
| 144 |
return {}; |
| 145 |
} |
| 146 |
return stroke_.reverseLookup(input); |
| 147 |
} |
| 148 |
|
| 149 |
std::string PinyinHelper::prettyStrokeString(const std::string &input) { |
| 150 |
if (!stroke_.load()) { |
| 151 |
return {}; |
| 152 |
} |
| 153 |
return stroke_.prettyString(input); |
| 154 |
} |
| 155 |
|
| 156 |
class PinyinHelperModuleFactory : public AddonFactory { |
| 157 |
AddonInstance *create(AddonManager *manager) override { |
| 158 |
registerDomain("fcitx5-chinese-addons", FCITX_INSTALL_LOCALEDIR); |
| 159 |
return new PinyinHelper(manager->instance()); |
| 160 |
} |
| 161 |
}; |
| 162 |
} // namespace fcitx |
| 163 |
|
| 164 |
FCITX_ADDON_FACTORY_V2(pinyinhelper, fcitx::PinyinHelperModuleFactory) |