PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/aterm-1.0.1/src/thai.c

#
C | 295 lines | 153 code | 51 blank | 91 comment | 44 complexity | 183092a9b75b55779017aa27511e2f42 MD5 | raw file
  1. /*--------------------------------*-C-*---------------------------------*
  2. * File: thai.c
  3. *----------------------------------------------------------------------*
  4. *
  5. * All portions of code are copyright by their respective author/s.
  6. * Copyright (C) 2000,2001 Teepanis Chachiyo <teepanis@physics.purdue.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *---------------------------------------------------------------------*/
  22. #include "rxvt.h"
  23. #include "thai.h"
  24. #ifdef THAI
  25. #if defined (NO_BRIGHTCOLOR) || defined (VERYBOLD)
  26. # define MONO_BOLD(x) ((x) & (RS_Bold|RS_Blink))
  27. #else
  28. # define MONO_BOLD(x) (((x) & RS_Bold) && fore == Color_fg)
  29. #endif
  30. /*
  31. * ThaiIsMiddleLineCh returns TRUE if character 'ch'
  32. * is to be displayed in the middle line.
  33. */
  34. /* PROTO */
  35. int
  36. ThaiIsMiddleLineCh(char ch)
  37. {
  38. if(thai_map[(unsigned char)ch]==0)
  39. return 1;
  40. else
  41. return 0;
  42. }
  43. /*
  44. * ThaiPixel2Col converts x-y coordiates in pixel
  45. * into byte offset that store character data.
  46. * This function replaces Pixel2Col in orinal
  47. * ATERM.
  48. */
  49. /* PROTO */
  50. int
  51. ThaiPixel2Col(int x, int y)
  52. {
  53. int row; /* row storing data */
  54. int i; /* byte offset index */
  55. int xpixel; /* dummy x pixel */
  56. char *str; /* drawn_text pointer */
  57. XFontStruct *wf; /* font */
  58. /* locate buffer storing data */
  59. row = Pixel2Row(y);
  60. MAX_IT(row, 0);
  61. MIN_IT(row, TermWin.nrow - 1);
  62. str = drawn_text[row];
  63. /* access to font structure */
  64. wf = TermWin.font;
  65. /* increase byte offset until we get to target x */
  66. i = 0;
  67. xpixel = CharWidth(wf,str[0]);
  68. x -= TermWin_internalBorder;
  69. while(xpixel <= x && i < TermWin.ncol){
  70. i++;
  71. xpixel += CharWidth(wf,str[i]); /* wf->per_char[char_num].width; */
  72. }
  73. MAX_IT(i, 0);
  74. MIN_IT(i, TermWin.ncol);
  75. return i;
  76. }
  77. /*
  78. * ThaiCol2Pixel returns x cooridate of Thai strings.
  79. * It also adjusts the to the upper/lowwer Thai character.
  80. * This replaces Col2Pixel and is called repeatedly so
  81. * I use 'static' variable to speed it up.
  82. */
  83. /* PROTO */
  84. int
  85. ThaiCol2Pixel(int col, char *start)
  86. /*
  87. * TODO: Still have some problem with scaled font
  88. */
  89. {
  90. static int i=0; /* index */
  91. static char *laststart=NULL; /* strings from last call */
  92. static unsigned int x=0; /* x coordinate for proportional font */
  93. static XFontStruct *wf; /* font */
  94. /* new string, so restart from the beginning */
  95. if(laststart!=start || col < i){
  96. i=0;
  97. x=TermWin_internalBorder;
  98. laststart=start;
  99. wf = TermWin.font;
  100. }
  101. /* old string, continue counting */
  102. for(; i < col; i++){
  103. x+= CharWidth(wf,start[i]);
  104. }
  105. return x;
  106. }
  107. /* PROTO */
  108. int
  109. ThaiUpdateMap2(text_t *stp, text_t *dtp, rend_t *srp, rend_t *drp, char *map, int len)
  110. {
  111. int stpx, dtpx; /* xpixel position of screen & drawntext */
  112. int stpi, dtpi;
  113. int char_num;
  114. XFontStruct *wf;
  115. wf = TermWin.font;
  116. stpx = dtpx = 0;
  117. stpi = dtpi = 0;
  118. for(stpi = 0; stpi < len;stpi++,
  119. char_num = (unsigned char)stp[stpi]-wf->min_char_or_byte2,
  120. stpx += wf->per_char[char_num].width){
  121. while(dtpx < stpx && dtpi < len-1){
  122. dtpi++;
  123. char_num = (unsigned char)dtp[dtpi]-wf->min_char_or_byte2;
  124. dtpx+= wf->per_char[char_num].width;
  125. }
  126. map[stpi] = (stp[stpi]==dtp[dtpi] && srp[stpi]==drp[dtpi]) ? 0 : 1;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * ThaiUpdateMap decides which character should be redraw on
  132. * screen. It returns the number of characters to update.
  133. * The algorithm is follow:
  134. * (1) At current Thai column, check if all characters at
  135. * this column is the same
  136. * (2) If yes, no need to update
  137. * (3) If no, update entire Thai column
  138. * (4) go to next Thai column, --> (1)
  139. */
  140. /* PROTO */
  141. int
  142. ThaiUpdateMap(text_t *stp, text_t *dtp, rend_t *srp, rend_t *drp, char *map, int len)
  143. {
  144. int stp_col=0, dtp_col=0; /* index */
  145. int stp_lev=0, dtp_lev=0; /* # of characters at the column */
  146. int i=0,match=0; /* matching flag */
  147. int lastbold=0; /* BOLD_OVERSTRIKE feature */
  148. int nupdate; /* # chars to update */
  149. nupdate = len;
  150. /* update by default */
  151. for(i=0;i<len;i++) map[i]=1;
  152. /* Bug Fixed: 12/27/2000 */
  153. /* sync the first column for both stp and dtp */
  154. for(stp_col=0; stp_col < len && thai_map[(unsigned char)stp[stp_col]]!=0; stp_col++);
  155. for(dtp_col=0; dtp_col < len && thai_map[(unsigned char)dtp[dtp_col]]!=0; dtp_col++);
  156. while(stp_col < len && dtp_col < len){
  157. /* check if identical column */
  158. match = 0;
  159. if(stp_lev==dtp_lev){
  160. for(i=0; i < stp_lev
  161. && stp[stp_col+i]==dtp[dtp_col+i] /* same chars */
  162. && (srp[stp_col+i]==drp[dtp_col+i] /* either same ATTR */
  163. || (stp[stp_col+i]==' ' /* or space with same background */
  164. && GET_BGATTR(srp[stp_col+i])==GET_BGATTR(drp[dtp_col+i]))); i++);
  165. /* set matching flag */
  166. if(i==stp_lev) match = 1;
  167. }
  168. /* if match don't update */
  169. if(match){
  170. /*
  171. * previous Bold character can leave excess dot on screen
  172. * so we must update char to clean up the mess
  173. */
  174. if(!lastbold)
  175. for(i=0;i < stp_lev; i++){
  176. map[stp_col+i] = 0;
  177. nupdate--;
  178. }
  179. lastbold = 0;
  180. }else /* set bold flag */
  181. lastbold = MONO_BOLD(srp[stp_col]);
  182. stp_col += stp_lev;
  183. dtp_col += dtp_lev;
  184. /* go to next Thai column and also fing # of levels too */
  185. for(stp_lev=1; thai_map[(unsigned char)stp[stp_col+stp_lev]]!=0
  186. && (stp_col+stp_lev)<len; stp_lev++);
  187. for(dtp_lev=1; thai_map[(unsigned char)dtp[dtp_col+dtp_lev]]!=0
  188. && (dtp_col+dtp_lev)<len; dtp_lev++);
  189. }
  190. /* in drawn_text buffer is full, won't update */
  191. while(stp_col < dtp_col){
  192. /*
  193. * Bug fixed: only skip update only its a blankspace.
  194. * (12/27/2000)
  195. */
  196. if(stp[stp_col] == ' '){
  197. map[stp_col]=0;
  198. nupdate--;
  199. }
  200. stp_col++;
  201. }
  202. return nupdate;
  203. }
  204. /*
  205. * Thai_CursorArea returns map of area around cursor.
  206. * We need this to take care of tone sign updating in
  207. * Thai language.
  208. */
  209. /* PROTO */
  210. int
  211. Thai_CursorArea(char *stp, char *map, int len, int col)
  212. {
  213. int i,j;
  214. /* not update by default */
  215. for(i=0;i<len;i++) map[i]=0;
  216. return 0;
  217. /* forward */
  218. i=col;
  219. for(j=0;j<3;j++)
  220. for(map[i++]=1; i<len && thai_map[(unsigned char)stp[i]]!=0;i++)
  221. map[i]=1;
  222. /* backward */
  223. i=col;
  224. for(j=0;j<3;j++)
  225. for(map[i--]=1; i >=0 && thai_map[(unsigned char)stp[i]]!=0;i--)
  226. map[i]=1;
  227. return 0;
  228. }
  229. /*
  230. * Thai_ColMaxPaint finds the truncated spaces needed to
  231. * paint strings str on screen.
  232. */
  233. /* PROTO */
  234. int
  235. Thai_ColMaxPaint(text_t *str, int len)
  236. {
  237. int i, col=0;
  238. /* truncate end space */
  239. for(i = len-1; i>=0 && str[i]==' '; i--);
  240. for(; i>=0; i--)
  241. if(ThaiIsMiddleLineCh(str[i])) col++;
  242. return col;
  243. }
  244. #endif /* #ifdef THAI */