pcollinsg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "dict.h"
  13. /*
  14. * Routines for handling dictionaries in the "Paperback Collins"
  15. * `German' format (with tags surrounded by \5⋯\6 and \xba⋯\xba)
  16. */
  17. /*
  18. * \5⋯\6 escapes (fonts, mostly)
  19. *
  20. * h headword (helvetica 7 pt)
  21. * c clause (helvetica 7 pt)
  22. * 3 helvetica 7 pt
  23. * 4 helvetica 6.5 pt
  24. * s helvetica 8 pt
  25. * x helvetica 8 pt
  26. * y helvetica 5 pt
  27. * m helvetica 30 pt
  28. * 1 roman 6 pt
  29. * 9 roman 4.5 pt
  30. * p roman 7 pt
  31. * q roman 4.5 pt
  32. * 2 italic 6 pt
  33. * 7 italic 4.5 pt
  34. * b bold 6 pt
  35. * a `indent 0:4 left'
  36. * k `keep 9'
  37. * l `size 12'
  38. */
  39. enum {
  40. IBASE=L'i', /* dotless i */
  41. Taglen=32,
  42. };
  43. static Rune intab[256] = {
  44. /*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/
  45. /*00*/ NONE, NONE, NONE, NONE, NONE, TAGS, TAGE, NONE,
  46. NONE, NONE, NONE, NONE, NONE, L' ', NONE, NONE,
  47. /*10*/ NONE, L'-', L' ', L' ', NONE, NONE, NONE, NONE,
  48. L' ', NONE, NONE, NONE, L' ', NONE, NONE, L'-',
  49. /*20*/ L' ', L'!', L'"', L'#', L'$', L'%', L'&', L'\'',
  50. L'(', L')', L'*', L'+', L',', L'-', L'.', L'/',
  51. /*30*/ L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',
  52. L'8', L'9', L':', L';', L'<', L'=', L'>', L'?',
  53. /*40*/ L'@', L'A', L'B', L'C', L'D', L'E', L'F', L'G',
  54. L'H', L'I', L'J', L'K', L'L', L'M', L'N', L'O',
  55. /*50*/ L'P', L'Q', L'R', L'S', L'T', L'U', L'V', L'W',
  56. L'X', L'Y', L'Z', L'[', L'\\', L']', L'^', L'_',
  57. /*60*/ L'`', L'a', L'b', L'c', L'd', L'e', L'f', L'g',
  58. L'h', L'i', L'j', L'k', L'l', L'm', L'n', L'o',
  59. /*70*/ L'p', L'q', L'r', L's', L't', L'u', L'v', L'w',
  60. L'x', L'y', L'z', L'{', L'|', L'}', L'~', NONE,
  61. /*80*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  62. NONE, NONE, L' ', NONE, NONE, NONE, NONE, NONE,
  63. /*90*/ L'ß', L'æ', NONE, MOE, NONE, NONE, NONE, L'ø',
  64. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  65. /*A0*/ NONE, NONE, L'"', L'£', NONE, NONE, NONE, NONE,
  66. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  67. /*B0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, L'~',
  68. NONE, IBASE, SPCS, NONE, NONE, NONE, NONE, NONE,
  69. /*C0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  70. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  71. /*D0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  72. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  73. /*E0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  74. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  75. /*F0*/ L' ', L' ', NONE, NONE, NONE, NONE, NONE, NONE,
  76. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  77. };
  78. static Nassoc numtab[] = {
  79. {1, L'+'},
  80. {4, L'='},
  81. {7, L'°'},
  82. {11, L'≈'},
  83. {69, L'♦'},
  84. {114, L'®'},
  85. {340, L'ɛ'},
  86. {341, L'ɔ'},
  87. {342, L'ʌ'},
  88. {343, L'ə'},
  89. {345, L'ʒ'},
  90. {346, L'ʃ'},
  91. {347, L'ɵ'},
  92. {348, L'ʊ'},
  93. {349, L'ˈ'},
  94. {351, L'ɪ'},
  95. {352, L'ɜ'},
  96. {354, L'ɑ'},
  97. {355, L'~'},
  98. {356, L'ɒ'},
  99. {384, L'ɳ'},
  100. {445, L'ð'}, /* BUG -- should be script eth */
  101. };
  102. static Nassoc overtab[] = {
  103. {L',', LCED},
  104. {L'/', LACU},
  105. {L':', LUML},
  106. {L'\\', LGRV},
  107. {L'^', LFRN},
  108. {L'~', LTIL},
  109. };
  110. static uint8_t *reach(uint8_t*, int);
  111. static Entry curentry;
  112. static char tag[Taglen];
  113. void
  114. pcollgprintentry(Entry e, int cmd)
  115. {
  116. uint8_t *p, *pe;
  117. int r, rprev = NONE, rx, over = 0, font;
  118. char buf[16];
  119. p = (uint8_t *)e.start;
  120. pe = (uint8_t *)e.end;
  121. curentry = e;
  122. if(cmd == 'h')
  123. outinhibit = 1;
  124. while(p < pe){
  125. if(cmd == 'r'){
  126. outchar(*p++);
  127. continue;
  128. }
  129. switch(r = intab[*p++]){ /* assign = */
  130. case TAGS:
  131. if(rprev != NONE){
  132. outrune(rprev);
  133. rprev = NONE;
  134. }
  135. p = reach(p, 0x06);
  136. font = tag[0];
  137. if(cmd == 'h')
  138. outinhibit = (font != 'h');
  139. break;
  140. case TAGE: /* an extra one */
  141. break;
  142. case SPCS:
  143. p = reach(p, 0xba);
  144. r = looknassoc(numtab, asize(numtab), strtol(tag,0,0));
  145. if(r < 0){
  146. if(rprev != NONE){
  147. outrune(rprev);
  148. rprev = NONE;
  149. }
  150. sprint(buf, "\\N'%s'", tag);
  151. outchars(buf);
  152. break;
  153. }
  154. /* else fall through */
  155. default:
  156. if(over){
  157. rx = looknassoc(overtab, asize(overtab), r);
  158. if(rx > 0)
  159. rx = liglookup(rx, rprev);
  160. if(rx > 0 && rx != NONE)
  161. outrune(rx);
  162. else{
  163. outrune(rprev);
  164. if(r == ':')
  165. outrune(L'¨');
  166. else{
  167. outrune(L'^');
  168. outrune(r);
  169. }
  170. }
  171. over = 0;
  172. rprev = NONE;
  173. }else if(r == '^'){
  174. over = 1;
  175. }else{
  176. if(rprev != NONE)
  177. outrune(rprev);
  178. rprev = r;
  179. }
  180. }
  181. }
  182. if(rprev != NONE)
  183. outrune(rprev);
  184. if(cmd == 'h')
  185. outinhibit = 0;
  186. outnl(0);
  187. }
  188. int32_t
  189. pcollgnextoff(int32_t fromoff)
  190. {
  191. int c, state = 0, defoff = -1;
  192. if(Bseek(bdict, fromoff, 0) < 0)
  193. return -1;
  194. while((c = Bgetc(bdict)) >= 0){
  195. if(c == '\r')
  196. defoff = Boffset(bdict);
  197. switch(state){
  198. case 0:
  199. if(c == 0x05)
  200. state = 1;
  201. break;
  202. case 1:
  203. if(c == 'h')
  204. state = 2;
  205. else
  206. state = 0;
  207. break;
  208. case 2:
  209. if(c == 0x06)
  210. return (Boffset(bdict)-3);
  211. else
  212. state = 0;
  213. break;
  214. }
  215. }
  216. return defoff;
  217. }
  218. void
  219. pcollgprintkey(void)
  220. {
  221. Bprint(bout, "No pronunciation key yet\n");
  222. }
  223. static uint8_t *
  224. reach(uint8_t *p, int tagchar)
  225. {
  226. int c; char *q=tag;
  227. while(p < (uint8_t *)curentry.end){
  228. c = *p++;
  229. if(c == tagchar)
  230. break;
  231. *q++ = c;
  232. if(q >= &tag[sizeof tag-1])
  233. break;
  234. }
  235. *q = 0;
  236. return p;
  237. }