bug 253317 - part 4 - implement nsHyphenationManager and nsHyphenator classes. r=smontagu sr=roc

This commit is contained in:
Jonathan Kew 2011-05-04 12:29:45 +01:00
parent b1dd91b239
commit b9795adddd
11 changed files with 757 additions and 0 deletions

View File

@ -43,6 +43,7 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
PARALLEL_DIRS = \
hyphenation \
locale \
locales \
lwbrk \

View File

@ -79,6 +79,7 @@ SHARED_LIBRARY_LIBS = \
../strres/src/$(LIB_PREFIX)strres_s.$(LIB_SUFFIX) \
../locale/src/$(LIB_PREFIX)nslocale_s.$(LIB_SUFFIX) \
../locale/src/$(LOCALE_DIR)/$(LIB_PREFIX)platlocale_s.$(LIB_SUFFIX) \
../hyphenation/src/$(HYPHENATION_DIR)/$(LIB_PREFIX)hyphenation_s.$(LIB_SUFFIX) \
$(NULL)
EXTRA_DSO_LDOPTS = \

View File

@ -0,0 +1,48 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Mozilla Hyphenation Service.
#
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Jonathan Kew <jfkthame@gmail.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = hyphenation
DIRS = public src
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,53 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Mozilla Hyphenation Service.
#
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Jonathan Kew <jfkthame@gmail.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = hyphenation
EXPORTS = \
nsHyphenationManager.h \
nsHyphenator.h \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,74 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Hyphenation Service.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonathan Kew <jfkthame@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsHyphenationManager_h__
#define nsHyphenationManager_h__
#include "nsInterfaceHashtable.h"
#include "nsRefPtrHashtable.h"
#include "nsHashKeys.h"
class nsHyphenator;
class nsIAtom;
class nsHyphenationManager
{
public:
nsHyphenationManager();
already_AddRefed<nsHyphenator> GetHyphenator(nsIAtom *aLocale);
static nsHyphenationManager *Instance();
static void Shutdown();
private:
~nsHyphenationManager();
protected:
void LoadPatternList();
void LoadPatternListFromDir(nsIFile *aDir);
void LoadAliases();
nsInterfaceHashtable<nsISupportsHashKey,nsIAtom> mHyphAliases;
nsInterfaceHashtable<nsISupportsHashKey,nsIFile> mPatternFiles;
nsRefPtrHashtable<nsISupportsHashKey,nsHyphenator> mHyphenators;
static nsHyphenationManager *sInstance;
};
#endif // nsHyphenationManager_h__

View File

@ -0,0 +1,66 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Hyphenation Service.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonathan Kew <jfkthame@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsHyphenator_h__
#define nsHyphenator_h__
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsTArray.h"
class nsIUGenCategory;
class nsHyphenator
{
public:
nsHyphenator(nsIFile *aFile);
NS_INLINE_DECL_REFCOUNTING(nsHyphenator)
PRBool IsValid();
nsresult Hyphenate(const nsAString& aText, nsTArray<PRPackedBool>& aHyphens);
private:
~nsHyphenator();
protected:
void *mDict;
nsCOMPtr<nsIUGenCategory> mCategories;
};
#endif // nsHyphenator_h__

View File

@ -0,0 +1,56 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Mozilla Hyphenation Service.
#
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Jonathan Kew <jfkthame@gmail.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = hyphenation
LIBRARY_NAME = hyphenation_s
LIBXUL_LIBRARY = 1
CSRCS = hyphen.c \
$(NULL)
CPPSRCS = nsHyphenator.cpp \
nsHyphenationManager.cpp \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,50 @@
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Foundation code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2005-2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonathan Kew <jfkthame@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Simple replacement for hnjalloc.h from libhyphen-2.x, to use moz_x* memory
* allocation functions. Note that the hyphen.c code does *NOT* check for
* NULL from memory (re)allocation, so it is essential that we use the
* "infallible" moz_x* variants here.
*/
#include "mozilla/mozalloc.h"
#define hnj_malloc(size) moz_xmalloc(size)
#define hnj_realloc(p, size) moz_xrealloc(p, size)
#define hnj_free(p) moz_free(p)

View File

@ -0,0 +1,256 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Hyphenation Service.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonathan Kew <jfkthame@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsHyphenationManager.h"
#include "nsHyphenator.h"
#include "nsIAtom.h"
#include "nsIFile.h"
#include "nsIProperties.h"
#include "nsISimpleEnumerator.h"
#include "nsIDirectoryEnumerator.h"
#include "nsDirectoryServiceDefs.h"
#include "nsUnicharUtils.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#define INTL_HYPHENATIONALIAS_PREFIX "intl.hyphenation-alias."
nsHyphenationManager *nsHyphenationManager::sInstance = nsnull;
nsHyphenationManager*
nsHyphenationManager::Instance()
{
if (sInstance == nsnull) {
sInstance = new nsHyphenationManager();
}
return sInstance;
}
void
nsHyphenationManager::Shutdown()
{
delete sInstance;
}
nsHyphenationManager::nsHyphenationManager()
{
mHyphAliases.Init();
mPatternFiles.Init();
mHyphenators.Init();
LoadPatternList();
LoadAliases();
}
nsHyphenationManager::~nsHyphenationManager()
{
sInstance = nsnull;
}
already_AddRefed<nsHyphenator>
nsHyphenationManager::GetHyphenator(nsIAtom *aLocale)
{
nsRefPtr<nsHyphenator> hyph;
mHyphenators.Get(aLocale, getter_AddRefs(hyph));
if (hyph) {
return hyph.forget();
}
nsCOMPtr<nsIFile> file = mPatternFiles.Get(aLocale);
if (!file) {
nsCOMPtr<nsIAtom> alias = mHyphAliases.Get(aLocale);
if (alias) {
mHyphenators.Get(alias, getter_AddRefs(hyph));
if (hyph) {
return hyph.forget();
}
file = mPatternFiles.Get(alias);
if (file) {
aLocale = alias;
}
}
if (!file) {
// In the case of a locale such as "de-DE-1996", we try replacing
// successive trailing subtags with "-*" to find fallback patterns,
// so "de-DE-1996" -> "de-DE-*" (and then recursively -> "de-*")
nsAtomCString localeStr(aLocale);
if (StringEndsWith(localeStr, NS_LITERAL_CSTRING("-*"))) {
localeStr.Truncate(localeStr.Length() - 2);
}
PRInt32 i = localeStr.RFindChar('-');
if (i > 1) {
localeStr.Replace(i, localeStr.Length() - i, "-*");
nsCOMPtr<nsIAtom> fuzzyLocale = do_GetAtom(localeStr);
return GetHyphenator(fuzzyLocale);
} else {
return nsnull;
}
}
}
hyph = new nsHyphenator(file);
if (hyph->IsValid()) {
mHyphenators.Put(aLocale, hyph);
return hyph.forget();
}
#ifdef DEBUG
nsCString msg;
file->GetNativePath(msg);
msg.Insert("failed to load patterns from ", 0);
NS_WARNING(msg.get());
#endif
mPatternFiles.Remove(aLocale);
return nsnull;
}
void
nsHyphenationManager::LoadPatternList()
{
mPatternFiles.Clear();
mHyphenators.Clear();
nsresult rv;
nsCOMPtr<nsIProperties> dirSvc =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
if (!dirSvc) {
return;
}
nsCOMPtr<nsIFile> greDir;
rv = dirSvc->Get(NS_GRE_DIR,
NS_GET_IID(nsIFile), getter_AddRefs(greDir));
if (NS_SUCCEEDED(rv)) {
greDir->AppendNative(NS_LITERAL_CSTRING("hyphenation"));
LoadPatternListFromDir(greDir);
}
nsCOMPtr<nsIFile> appDir;
rv = dirSvc->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
NS_GET_IID(nsIFile), getter_AddRefs(appDir));
if (NS_SUCCEEDED(rv)) {
appDir->AppendNative(NS_LITERAL_CSTRING("hyphenation"));
PRBool equals;
if (NS_SUCCEEDED(appDir->Equals(greDir, &equals)) && !equals) {
LoadPatternListFromDir(appDir);
}
}
}
void
nsHyphenationManager::LoadPatternListFromDir(nsIFile *aDir)
{
nsresult rv;
PRBool check = PR_FALSE;
rv = aDir->Exists(&check);
if (NS_FAILED(rv) || !check) {
return;
}
rv = aDir->IsDirectory(&check);
if (NS_FAILED(rv) || !check) {
return;
}
nsCOMPtr<nsISimpleEnumerator> e;
rv = aDir->GetDirectoryEntries(getter_AddRefs(e));
if (NS_FAILED(rv)) {
return;
}
nsCOMPtr<nsIDirectoryEnumerator> files(do_QueryInterface(e));
if (!files) {
return;
}
nsCOMPtr<nsIFile> file;
while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(file))) && file){
nsAutoString dictName;
file->GetLeafName(dictName);
NS_ConvertUTF16toUTF8 locale(dictName);
ToLowerCase(locale);
if (!StringEndsWith(locale, NS_LITERAL_CSTRING(".dic"))) {
continue;
}
if (StringBeginsWith(locale, NS_LITERAL_CSTRING("hyph_"))) {
locale.Cut(0, 5);
}
locale.SetLength(locale.Length() - 4); // strip ".dic"
for (PRUint32 i = 0; i < locale.Length(); ++i) {
if (locale[i] == '_') {
locale.Replace(i, 1, '-');
}
}
#ifdef DEBUG
printf("adding hyphenation patterns for %s: %s\n", locale.get(),
NS_ConvertUTF16toUTF8(dictName).get());
#endif
nsCOMPtr<nsIAtom> localeAtom = do_GetAtom(locale);
mPatternFiles.Put(localeAtom, file);
}
}
void
nsHyphenationManager::LoadAliases()
{
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefBranch =
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return;
}
PRUint32 prefCount;
char **prefNames;
rv = prefBranch->GetChildList(INTL_HYPHENATIONALIAS_PREFIX,
&prefCount, &prefNames);
if (NS_SUCCEEDED(rv) && prefCount > 0) {
for (PRUint32 i = 0; i < prefCount; ++i) {
char *prefValue;
rv = prefBranch->GetCharPref(prefNames[i], &prefValue);
if (NS_SUCCEEDED(rv)) {
nsCAutoString alias(prefNames[i]);
alias.Cut(0, strlen(INTL_HYPHENATIONALIAS_PREFIX));
ToLowerCase(alias);
nsCAutoString value(prefValue);
ToLowerCase(value);
nsCOMPtr<nsIAtom> aliasAtom = do_GetAtom(alias);
nsCOMPtr<nsIAtom> valueAtom = do_GetAtom(value);
mHyphAliases.Put(aliasAtom, valueAtom);
NS_Free(prefValue);
}
}
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(prefCount, prefNames);
}
}

View File

@ -0,0 +1,147 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Hyphenation Service.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonathan Kew <jfkthame@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsHyphenator.h"
#include "nsIFile.h"
#include "nsUTF8Utils.h"
#include "nsIUGenCategory.h"
#include "nsUnicharUtilCIID.h"
#include "hyphen.h"
nsHyphenator::nsHyphenator(nsIFile *aFile)
: mDict(nsnull)
{
nsCString path;
aFile->GetNativePath(path);
mDict = hnj_hyphen_load(path.get());
#ifdef DEBUG
if (mDict) {
printf("loaded hyphenation patterns from %s\n", path.get());
}
#endif
nsresult rv;
mCategories =
do_GetService(NS_UNICHARCATEGORY_CONTRACTID, &rv);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get category service");
}
nsHyphenator::~nsHyphenator()
{
if (mDict != nsnull) {
hnj_hyphen_free((HyphenDict*)mDict);
mDict = nsnull;
}
}
PRBool
nsHyphenator::IsValid()
{
return (mDict != nsnull) && (mCategories != nsnull);
}
nsresult
nsHyphenator::Hyphenate(const nsAString& aString,
nsTArray<PRPackedBool>& aHyphens)
{
if (!aHyphens.SetLength(aString.Length())) {
return NS_ERROR_OUT_OF_MEMORY;
}
memset(aHyphens.Elements(), PR_FALSE, aHyphens.Length());
PRBool inWord = PR_FALSE;
PRUint32 wordStart = 0, wordLimit = 0;
for (PRUint32 i = 0; i < aString.Length(); i++) {
PRUnichar ch = aString[i];
nsIUGenCategory::nsUGenCategory cat = mCategories->Get(ch);
if (cat == nsIUGenCategory::kLetter || cat == nsIUGenCategory::kMark) {
if (!inWord) {
inWord = PR_TRUE;
wordStart = i;
}
wordLimit = i + 1;
if (i < aString.Length() - 1) {
continue;
}
}
if (inWord) {
NS_ConvertUTF16toUTF8 utf8(aString.BeginReading() + wordStart,
wordLimit - wordStart);
nsAutoTArray<char,200> utf8hyphens;
utf8hyphens.SetLength(utf8.Length() + 5);
char **rep = nsnull;
int *pos = nsnull;
int *cut = nsnull;
int err = hnj_hyphen_hyphenate2((HyphenDict*)mDict,
utf8.BeginReading(), utf8.Length(),
utf8hyphens.Elements(), nsnull,
&rep, &pos, &cut);
if (!err) {
PRUint32 utf16offset = wordStart;
const char *cp = utf8.BeginReading();
while (cp < utf8.EndReading()) {
if (UTF8traits::isASCII(*cp)) { // single-byte utf8 char
cp++;
utf16offset++;
} else if (UTF8traits::is2byte(*cp)) { // 2-byte sequence
cp += 2;
utf16offset++;
} else if (UTF8traits::is3byte(*cp)) { // 3-byte sequence
cp += 3;
utf16offset++;
} else { // must be a 4-byte sequence (no need to check validity,
// as this was just created with NS_ConvertUTF16toUTF8)
NS_ASSERTION(UTF8traits::is4byte(*cp), "unexpected utf8 byte");
cp += 4;
utf16offset += 2;
}
NS_ASSERTION(cp <= utf8.EndReading(), "incomplete utf8 string?");
NS_ASSERTION(utf16offset <= aString.Length(), "length mismatch?");
if (utf8hyphens[cp - utf8.BeginReading() - 1] & 0x01) {
aHyphens[utf16offset - 1] = PR_TRUE;
}
}
}
}
inWord = PR_FALSE;
}
return NS_OK;
}

View File

@ -1048,6 +1048,11 @@ pref("intl.uidirection.ar", "rtl");
pref("intl.uidirection.he", "rtl");
pref("intl.uidirection.fa", "rtl");
// use en-US hyphenation by default for content tagged with plain lang="en"
pref("intl.hyphenation-alias.en", "en-us");
// and for other subtags of en-*, if no specific patterns are available
pref("intl.hyphenation-alias.en-*", "en-us");
pref("font.mathfont-family", "STIXNonUnicode, STIXSizeOneSym, STIXSize1, STIXGeneral, Standard Symbols L, DejaVu Sans, Cambria Math");
// Some CJK fonts have bad underline offset, their CJK character glyphs are overlapped (or adjoined) to its underline.