~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Resource/Languages/hyphenate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
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
******************************************************************************/
 
12
 
 
13
#include "file.hpp"
 
14
#include "Languages/hyphenate.hpp"
 
15
#include "analyze.hpp"
 
16
 
 
17
#include <stdio.h>
 
18
#include <stdlib.h>
 
19
#include <string.h>
 
20
 
 
21
typedef int SI;
 
22
#define MAX_SEARCH 10
 
23
#define MAX_BUFFER_SIZE 256
 
24
 
 
25
/*
 
26
static bool
 
27
my_strncmp (char* s1, char* s2, int len) {
 
28
  int i;
 
29
  for (i=0; i<len; i++) if (s1[i]!=s2[i]) return false;
 
30
  return true;
 
31
}
 
32
*/
 
33
 
 
34
static string
 
35
unpattern (string s) {
 
36
  int i, n= N(s);
 
37
  string r;
 
38
  for (i=0; i<n; ) {
 
39
    while ((i<n) && (s[i]>='0') && (s[i]<='9')) i++;
 
40
    if (i<n) r << s[i++];
 
41
  }
 
42
  return r;
 
43
}
 
44
 
 
45
static string
 
46
hyphen_normalize (string s) {
 
47
  int i;
 
48
  string r (0);
 
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));
 
52
      i+=3;
 
53
    }
 
54
    else r << s[i];
 
55
  return r;
 
56
}
 
57
 
 
58
hashmap<string,string>
 
59
load_hyphen_table (string file_name) {
 
60
  string s;
 
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";
 
64
 
 
65
  hashmap<string,string> H ("?");
 
66
  bool flag=false;
 
67
  int i=0, n= N(s);
 
68
  while (i<n) {
 
69
    string buffer;
 
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++;
 
73
    }
 
74
    if (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;
 
80
  }
 
81
 
 
82
  // cout << file_name << " done!\n";
 
83
  return H;
 
84
}
 
85
 
 
86
static string
 
87
lower_case (string s) {
 
88
  int i;
 
89
  string r (N(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'));
 
93
    else r[i]=s[i];
 
94
  }
 
95
  return r;
 
96
}
 
97
 
 
98
array<int>
 
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";
 
103
  int i, j, k, m, len;
 
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)];
 
109
      if (!(r == "?")) {
 
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');
 
114
            k++;
 
115
          }
 
116
          else m=0;
 
117
          if (m>T[i+j]) T[i+j]=m;
 
118
        }
 
119
      }
 
120
    }
 
121
 
 
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";
 
129
  return penalty;
 
130
}
 
131
 
 
132
void
 
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 ("-");
 
138
}