2
/******************************************************************************
3
* MODULE : hyphenate.cpp
4
* DESCRIPTION: hyphenation by Liang's algorithm
5
* COPYRIGHT : (C) 1999 Joris van der Hoeven
6
*******************************************************************************
7
* This software falls under the GNU general public license and comes WITHOUT
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
11
******************************************************************************/
14
#include "Languages/hyphenate.hpp"
15
#include "analyze.hpp"
23
#define MAX_BUFFER_SIZE 256
27
my_strncmp (char* s1, char* s2, int len) {
29
for (i=0; i<len; i++) if (s1[i]!=s2[i]) return false;
35
unpattern (string s) {
39
while ((i<n) && (s[i]>='0') && (s[i]<='9')) i++;
46
hyphen_normalize (string s) {
49
for (i=0; i<N(s); i++)
50
if ((i+3<N(s)) && (s[i]=='^') && (s[i+1]=='^')) {
51
r << from_hexadecimal (s (i+2, i+4));
58
hashmap<string,string>
59
load_hyphen_table (string file_name) {
61
file_name= string ("hyphen.") * file_name;
62
load_string (url ("$TEXMACS_PATH/langs/natural/hyphen", file_name), s, true);
63
if (DEBUG_AUTO) cout << "TeXmacs] Loading " << file_name << "\n";
65
hashmap<string,string> H ("?");
70
while ((i<n) && (s[i]!=' ') && (s[i]!='\t') && (s[i]!='\n')) {
71
if (s[i] != '%') buffer << s[i++];
72
else while ((i<n) && (s[i]!='\n')) i++;
75
if (buffer == "}") flag=false;
76
string norm= hyphen_normalize (buffer);
77
//cout << norm << "\n";
78
if (flag && (i!=0)) H (unpattern (norm))= norm;
79
if (buffer == "\\patterns{") flag=true;
82
// cout << file_name << " done!\n";
87
lower_case (string s) {
90
for (i=0; i<N(s); i++) {
91
if ((s[i]>='A') && (s[i]<='Z'))
92
r[i]= (char) (((int) s[i])+ ((int) 'a')- ((int) 'A'));
99
get_hyphens (string s, hashmap<string,string> H) {
100
if (N(s)==0) fatal_error ("hyphenation of empty string", "get_hyphens");
101
s= "." * lower_case (s) * ".";
102
// cout << s << "\n";
104
array<int> T (N(s)+1);
105
for (i=0; i<N(s)+1; i++) T[i]=0;
106
for (len=1; len < MAX_SEARCH; len++)
107
for (i=0; i<N(s)-len; i++) {
108
string r= H [s (i, i+len)];
110
// cout << " " << s (i, i+len) << " => " << r << "\n";
111
for (j=0, k=0; j<=len; j++, k++) {
112
if ((k<N(r)) && (r[k]>='0') && (r[k]<='9')) {
113
m=((int) r[k])-((int) '0');
117
if (m>T[i+j]) T[i+j]=m;
122
array<int> penalty (N(s)-3);
123
for (i=2; i<N(s)-1; i++)
124
penalty [i-2]= (((T[i]&1)==1)? HYPH_STD: HYPH_INVALID);
125
if (N(penalty)>0) penalty[0] = penalty[N(penalty)-1] = HYPH_INVALID;
126
if (N(penalty)>1) penalty[1] = penalty[N(penalty)-2] = HYPH_INVALID;
127
if (N(penalty)>2) penalty[N(penalty)-3] = HYPH_INVALID;
128
// cout << " -> " << penalty << "\n";
133
std_hyphenate (string s, int after, string& left, string& right, int penalty) {
134
left = s (0, after+1);
135
right= s (after+1, N(s));
136
if (penalty >= HYPH_INVALID) left << string ("\\");
137
else left << string ("-");