2013-03-01 05:41:30 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-04 04:29:45 -07:00
|
|
|
|
|
|
|
#include "nsHyphenator.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
2012-02-24 02:15:46 -08:00
|
|
|
#include "nsUnicodeProperties.h"
|
2011-05-04 04:29:45 -07:00
|
|
|
#include "nsUnicharUtilCIID.h"
|
2011-10-06 08:06:32 -07:00
|
|
|
#include "nsIURI.h"
|
2011-05-04 04:29:45 -07:00
|
|
|
|
|
|
|
#include "hyphen.h"
|
|
|
|
|
2011-10-06 08:06:32 -07:00
|
|
|
nsHyphenator::nsHyphenator(nsIURI *aURI)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mDict(nullptr)
|
2011-05-04 04:29:45 -07:00
|
|
|
{
|
2011-10-06 08:06:32 -07:00
|
|
|
nsCString uriSpec;
|
|
|
|
nsresult rv = aURI->GetSpec(uriSpec);
|
2011-09-14 12:20:26 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-06 08:06:32 -07:00
|
|
|
mDict = hnj_hyphen_load(uriSpec.get());
|
2011-05-04 04:29:45 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (mDict) {
|
2011-10-06 08:06:32 -07:00
|
|
|
printf("loaded hyphenation patterns from %s\n", uriSpec.get());
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHyphenator::~nsHyphenator()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mDict != nullptr) {
|
2011-05-04 04:29:45 -07:00
|
|
|
hnj_hyphen_free((HyphenDict*)mDict);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDict = nullptr;
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-04 04:29:45 -07:00
|
|
|
nsHyphenator::IsValid()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return (mDict != nullptr);
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHyphenator::Hyphenate(const nsAString& aString,
|
2014-02-08 10:10:44 -08:00
|
|
|
FallibleTArray<bool>& aHyphens)
|
2011-05-04 04:29:45 -07:00
|
|
|
{
|
|
|
|
if (!aHyphens.SetLength(aString.Length())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-12-10 17:10:35 -08:00
|
|
|
memset(aHyphens.Elements(), false, aHyphens.Length() * sizeof(bool));
|
2011-05-04 04:29:45 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inWord = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t wordStart = 0, wordLimit = 0;
|
|
|
|
uint32_t chLen;
|
|
|
|
for (uint32_t i = 0; i < aString.Length(); i += chLen) {
|
|
|
|
uint32_t ch = aString[i];
|
2011-07-20 03:15:06 -07:00
|
|
|
chLen = 1;
|
|
|
|
|
|
|
|
if (NS_IS_HIGH_SURROGATE(ch)) {
|
|
|
|
if (i + 1 < aString.Length() && NS_IS_LOW_SURROGATE(aString[i+1])) {
|
|
|
|
ch = SURROGATE_TO_UCS4(ch, aString[i+1]);
|
|
|
|
chLen = 2;
|
|
|
|
} else {
|
|
|
|
NS_WARNING("unpaired surrogate found during hyphenation");
|
|
|
|
}
|
|
|
|
}
|
2011-05-04 04:29:45 -07:00
|
|
|
|
2012-02-24 02:15:46 -08:00
|
|
|
nsIUGenCategory::nsUGenCategory cat = mozilla::unicode::GetGenCategory(ch);
|
2011-05-04 04:29:45 -07:00
|
|
|
if (cat == nsIUGenCategory::kLetter || cat == nsIUGenCategory::kMark) {
|
|
|
|
if (!inWord) {
|
2011-10-17 07:59:28 -07:00
|
|
|
inWord = true;
|
2011-05-04 04:29:45 -07:00
|
|
|
wordStart = i;
|
|
|
|
}
|
2011-07-20 03:15:06 -07:00
|
|
|
wordLimit = i + chLen;
|
|
|
|
if (i + chLen < aString.Length()) {
|
2011-05-04 04:29:45 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inWord) {
|
2015-02-16 02:28:54 -08:00
|
|
|
// Convert the word to utf-8 for libhyphen, lowercasing it as we go
|
|
|
|
// so that it will match the (lowercased) patterns (bug 1105644).
|
|
|
|
nsAutoCString utf8;
|
|
|
|
const char16_t* const begin = aString.BeginReading();
|
|
|
|
const char16_t *cur = begin + wordStart;
|
|
|
|
const char16_t *end = begin + wordLimit;
|
|
|
|
while (cur < end) {
|
|
|
|
uint32_t ch = *cur++;
|
|
|
|
|
|
|
|
if (NS_IS_HIGH_SURROGATE(ch)) {
|
|
|
|
if (cur < end && NS_IS_LOW_SURROGATE(*cur)) {
|
|
|
|
ch = SURROGATE_TO_UCS4(ch, *cur++);
|
|
|
|
} else {
|
|
|
|
ch = 0xfffd; // unpaired surrogate, treat as REPLACEMENT CHAR
|
|
|
|
}
|
|
|
|
} else if (NS_IS_LOW_SURROGATE(ch)) {
|
|
|
|
ch = 0xfffd; // unpaired surrogate
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX What about language-specific casing? Consider Turkish I/i...
|
|
|
|
// In practice, it looks like the current patterns will not be
|
|
|
|
// affected by this, as they treat dotted and undotted i similarly.
|
|
|
|
ch = ToLowerCase(ch);
|
|
|
|
|
|
|
|
if (ch < 0x80) { // U+0000 - U+007F
|
|
|
|
utf8.Append(ch);
|
|
|
|
} else if (ch < 0x0800) { // U+0100 - U+07FF
|
|
|
|
utf8.Append(0xC0 | (ch >> 6));
|
|
|
|
utf8.Append(0x80 | (0x003F & ch));
|
|
|
|
} else if (ch < 0x10000) { // U+0800 - U+D7FF,U+E000 - U+FFFF
|
|
|
|
utf8.Append(0xE0 | (ch >> 12));
|
|
|
|
utf8.Append(0x80 | (0x003F & (ch >> 6)));
|
|
|
|
utf8.Append(0x80 | (0x003F & ch));
|
|
|
|
} else {
|
|
|
|
utf8.Append(0xF0 | (ch >> 18));
|
|
|
|
utf8.Append(0x80 | (0x003F & (ch >> 12)));
|
|
|
|
utf8.Append(0x80 | (0x003F & (ch >> 6)));
|
|
|
|
utf8.Append(0x80 | (0x003F & ch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-04 04:29:45 -07:00
|
|
|
nsAutoTArray<char,200> utf8hyphens;
|
|
|
|
utf8hyphens.SetLength(utf8.Length() + 5);
|
2012-07-30 07:20:58 -07:00
|
|
|
char **rep = nullptr;
|
|
|
|
int *pos = nullptr;
|
|
|
|
int *cut = nullptr;
|
2011-05-04 04:29:45 -07:00
|
|
|
int err = hnj_hyphen_hyphenate2((HyphenDict*)mDict,
|
|
|
|
utf8.BeginReading(), utf8.Length(),
|
2012-07-30 07:20:58 -07:00
|
|
|
utf8hyphens.Elements(), nullptr,
|
2011-05-04 04:29:45 -07:00
|
|
|
&rep, &pos, &cut);
|
|
|
|
if (!err) {
|
2011-07-20 03:15:06 -07:00
|
|
|
// Surprisingly, hnj_hyphen_hyphenate2 converts the 'hyphens' buffer
|
|
|
|
// from utf8 code unit indexing (which would match the utf8 input
|
|
|
|
// string directly) to Unicode character indexing.
|
|
|
|
// We then need to convert this to utf16 code unit offsets for Gecko.
|
|
|
|
const char *hyphPtr = utf8hyphens.Elements();
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *cur = begin + wordStart;
|
|
|
|
const char16_t *end = begin + wordLimit;
|
2011-07-20 03:15:06 -07:00
|
|
|
while (cur < end) {
|
|
|
|
if (*hyphPtr & 0x01) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aHyphens[cur - begin] = true;
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
2011-07-20 03:15:06 -07:00
|
|
|
cur++;
|
|
|
|
if (cur < end && NS_IS_LOW_SURROGATE(*cur) &&
|
|
|
|
NS_IS_HIGH_SURROGATE(*(cur-1)))
|
|
|
|
{
|
|
|
|
cur++;
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
2011-07-20 03:15:06 -07:00
|
|
|
hyphPtr++;
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
inWord = false;
|
2011-05-04 04:29:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|