使用连字符
有时需要使用连字符在文档中更紧凑地排列文本。 与此同时,重要的是要了解每种语言的单词断字的具体细节可能不同。
目前,连字符的使用不像以前那样频繁,特别是在英文文本中。 尽管如此,使用此功能可能会对用户文档产生严重影响–连字符会影响布局,因此会影响输出文件的外观,例如PDF格式。
为了正确分割单词,使用特定于语言的断字字典。 Aspose.Words使用高级算法来处理此类字典,并允许您获得与Microsoft Word中相同的连字符。
连字符字典
由于不同的语言对单词断字使用不同的规范和规则,正确断字的最佳解决方案是使用特殊的字典。 Aspose.Words使用OpenOffice字典。
对于拼写检查,OpenOffice使用 Hunspell图书馆,这是TeX的断字算法的泛化。 此算法允许使用竞争标准和自定义断字模式自动非标准断字。 Hunspell使用 连字符,连字符 为断字。
断字算法
Aspose.Words实现 TeX断字算法 并且可以重用OpenOffice连字符字典。
应该考虑Aspose.Words算法的以下特性:
- 断字距离参数(LEFTHYPHENMIN, RIGHTHYPHENMIN, COMPOUNDLEFTHYPHENMIN, COMPOUNDRIGHTHYPHENMIN) 断字字典中指定将被忽略。 Aspose.Words根据文档兼容模式使用自己的一组距离参数。
- Aspose.Words中的断字算法支持 复合连字符. 但是,Aspose.Words将包含混合字母和非字母字符的字符序列拆分为仅字母部分(单词)并分别用连字符连接。 请注意,复合词断字的Microsoft Word逻辑取决于文档兼容模式。
- Aspose.Words中的连字符算法不实现 非标准连字符. 非标准模式将被忽略。
加载连字符字典
要使用断字功能,请先注册断字字典。下面的代码示例演示如何从文件中加载指定语言的断字字典:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(MyDir + u"German text.docx"); | |
Hyphenation::RegisterDictionary(u"en-US", MyDir + u"hyph_en_US.dic"); | |
Hyphenation::RegisterDictionary(u"de-CH", MyDir + u"hyph_de_CH.dic"); | |
doc->Save(ArtifactsDir + u"WorkingWithHyphenation.HyphenateWordsOfLanguages.pdf"); |
下面的代码示例演示如何从流中加载指定语言的断字字典:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(MyDir + u"German text.docx"); | |
SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(MyDir + u"hyph_de_CH.dic"); | |
Hyphenation::RegisterDictionary(u"de-CH", stream); | |
doc->Save(ArtifactsDir + u"WorkingWithHyphenation.LoadHyphenationDictionaryForLanguage.pdf"); |
作为预先注册连字符字典的替代方法,可以"按请求"仅注册所需的连字符字典。 为此,请实现IHyphenationCallback接口并使用静态回调Callback。
下面的代码示例演示如何实现IHyphenationCallback接口:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class CustomHyphenationCallback : public IHyphenationCallback | |
{ | |
public: | |
void RequestDictionary(String language) override | |
{ | |
String dictionaryFolder = MyDir; | |
String dictionaryFullFileName; | |
if (language == u"en-US") | |
{ | |
dictionaryFullFileName = System::IO::Path::Combine(dictionaryFolder, u"hyph_en_US.dic"); | |
} | |
else if (language == u"de-CH") | |
{ | |
dictionaryFullFileName = System::IO::Path::Combine(dictionaryFolder, u"hyph_de_CH.dic"); | |
} | |
else | |
{ | |
throw System::Exception(String::Format(u"Missing hyphenation dictionary for {0}.", language)); | |
} | |
// Register dictionary for requested language. | |
Hyphenation::RegisterDictionary(language, dictionaryFullFileName); | |
} | |
}; | |
void HyphenationCallback() | |
{ | |
{ | |
auto __finally_guard_0 = ::System::MakeScopeGuard([]() { Hyphenation::set_Callback(nullptr); }); | |
try | |
{ | |
// Register hyphenation callback. | |
Hyphenation::set_Callback(MakeObject<WorkingWithHyphenation::CustomHyphenationCallback>()); | |
auto document = MakeObject<Document>(MyDir + u"German text.docx"); | |
document->Save(ArtifactsDir + u"WorkingWithHyphenation.HyphenationCallback.pdf"); | |
} | |
catch (System::Exception& e) | |
{ | |
ASSERT_TRUE(e->get_Message().StartsWith(u"Missing hyphenation dictionary")); | |
std::cout << e->get_Message() << std::endl; | |
} | |
} | |
} |
断字对布局的影响
将文本分成几行时,Aspose.Words会检查每个单词是否完全适合当前行。 如果另一个单词太长而不适合行尾,默认情况下Aspose.Words将其移动到下一行的开头,而不是用连字符。
但是,可以在Aspose.Words中使用连字符功能将连字符插入到单词中,以消除对齐文本中的空白或在窄列中保持偶数行长度。 这显然会影响行数,从而影响页数。 换句话说,使用断字功能会影响文档布局。
连字符和理由(H&J)
Microsoft Word有复杂的逻辑来选择断点,如果文本是合理的,并且启用了连字符。 简而言之,Microsoft Word可能更喜欢收缩或拉伸空格以避免行断字。 很可能这个逻辑是基于 Knuth的文章.
Aspose.Words实现自己的H&J算法,该算法给出与Microsoft Word相同的结果,并在输出文档中提供相同的换行。