PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/TeXmacs-1.0.7.11-src/src/System/Language/hyphenate.cpp

#
C++ | 137 lines | 101 code | 14 blank | 22 comment | 49 complexity | 666ab960105fa34b525ea2e911e3fb40 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /******************************************************************************
  2. * MODULE : hyphenate.cpp
  3. * DESCRIPTION: hyphenation by Liang's algorithm
  4. * COPYRIGHT : (C) 1999 Joris van der Hoeven
  5. *******************************************************************************
  6. * This software falls under the GNU general public license version 3 or later.
  7. * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
  8. * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
  9. ******************************************************************************/
  10. #include "file.hpp"
  11. #include "hyphenate.hpp"
  12. #include "analyze.hpp"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. typedef int SI;
  17. #define MAX_SEARCH 10
  18. #define MAX_BUFFER_SIZE 256
  19. /*
  20. static bool
  21. my_strncmp (char* s1, char* s2, int len) {
  22. int i;
  23. for (i=0; i<len; i++) if (s1[i]!=s2[i]) return false;
  24. return true;
  25. }
  26. */
  27. static string
  28. unpattern (string s) {
  29. int i, n= N(s);
  30. string r;
  31. for (i=0; i<n; ) {
  32. while ((i<n) && (s[i]>='0') && (s[i]<='9')) i++;
  33. if (i<n) r << s[i++];
  34. }
  35. return r;
  36. }
  37. static string
  38. hyphen_normalize (string s) {
  39. int i;
  40. string r (0);
  41. for (i=0; i<N(s); i++)
  42. if ((i+3<N(s)) && (s[i]=='^') && (s[i+1]=='^')) {
  43. r << from_hexadecimal (s (i+2, i+4));
  44. i+=3;
  45. }
  46. else r << s[i];
  47. return r;
  48. }
  49. hashmap<string,string>
  50. load_hyphen_table (string file_name) {
  51. string s;
  52. file_name= string ("hyphen.") * file_name;
  53. load_string (url ("$TEXMACS_PATH/langs/natural/hyphen", file_name), s, true);
  54. if (DEBUG_VERBOSE) cout << "TeXmacs] Loading " << file_name << "\n";
  55. hashmap<string,string> H ("?");
  56. bool flag=false;
  57. int i=0, n= N(s);
  58. while (i<n) {
  59. string buffer;
  60. while ((i<n) && (s[i]!=' ') && (s[i]!='\t') && (s[i]!='\n')) {
  61. if (s[i] != '%') buffer << s[i++];
  62. else while ((i<n) && (s[i]!='\n')) i++;
  63. }
  64. if (i<n) i++;
  65. if (buffer == "}") flag=false;
  66. string norm= hyphen_normalize (buffer);
  67. //cout << norm << "\n";
  68. if (flag && (i!=0)) H (unpattern (norm))= norm;
  69. if (buffer == "\\patterns{") flag=true;
  70. }
  71. // cout << file_name << " done!\n";
  72. return H;
  73. }
  74. static string
  75. lower_case (string s) {
  76. int i;
  77. string r (N(s));
  78. for (i=0; i<N(s); i++) {
  79. if ((s[i]>='A') && (s[i]<='Z'))
  80. r[i]= (char) (((int) s[i])+ ((int) 'a')- ((int) 'A'));
  81. else r[i]=s[i];
  82. }
  83. return r;
  84. }
  85. array<int>
  86. get_hyphens (string s, hashmap<string,string> H) {
  87. ASSERT (N(s) != 0, "hyphenation of empty string");
  88. s= "." * lower_case (s) * ".";
  89. // cout << s << "\n";
  90. int i, j, k, m, len;
  91. array<int> T (N(s)+1);
  92. for (i=0; i<N(s)+1; i++) T[i]=0;
  93. for (len=1; len < MAX_SEARCH; len++)
  94. for (i=0; i<N(s)-len; i++) {
  95. string r= H [s (i, i+len)];
  96. if (!(r == "?")) {
  97. // cout << " " << s (i, i+len) << " => " << r << "\n";
  98. for (j=0, k=0; j<=len; j++, k++) {
  99. if ((k<N(r)) && (r[k]>='0') && (r[k]<='9')) {
  100. m=((int) r[k])-((int) '0');
  101. k++;
  102. }
  103. else m=0;
  104. if (m>T[i+j]) T[i+j]=m;
  105. }
  106. }
  107. }
  108. array<int> penalty (N(s)-3);
  109. for (i=2; i<N(s)-1; i++)
  110. penalty [i-2]= (((T[i]&1)==1)? HYPH_STD: HYPH_INVALID);
  111. if (N(penalty)>0) penalty[0] = penalty[N(penalty)-1] = HYPH_INVALID;
  112. if (N(penalty)>1) penalty[1] = penalty[N(penalty)-2] = HYPH_INVALID;
  113. if (N(penalty)>2) penalty[N(penalty)-3] = HYPH_INVALID;
  114. // cout << " -> " << penalty << "\n";
  115. return penalty;
  116. }
  117. void
  118. std_hyphenate (string s, int after, string& left, string& right, int penalty) {
  119. left = s (0, after+1);
  120. right= s (after+1, N(s));
  121. if (penalty >= HYPH_INVALID) left << string ("\\");
  122. else left << string ("-");
  123. }