symbol.c (4978B)
1 2 #include "fm.h" 3 4 #include "Symbols/alt.sym" 5 #include "Symbols/graph.sym" 6 #ifdef USE_M17N 7 #include "Symbols/eucjp.sym" 8 #include "Symbols/euckr.sym" 9 #include "Symbols/euccn.sym" 10 #include "Symbols/euctw.sym" 11 #include "Symbols/big5.sym" 12 #ifdef USE_UNICODE 13 #include "Symbols/utf8.sym" 14 #endif 15 #include "Symbols/cp850.sym" 16 17 typedef struct { 18 wc_ces ces; 19 char width; 20 char **item; 21 char **conved_item; 22 } symbol_set; 23 24 typedef struct { 25 wc_ces charset; 26 symbol_set *symbol; 27 } charset_symbol_set; 28 29 /* *INDENT-OFF* */ 30 static symbol_set alt_symbol_set = { WC_CES_US_ASCII, 1, alt_symbol, alt_symbol }; 31 static symbol_set alt2_symbol_set = { WC_CES_US_ASCII, 2, alt2_symbol, alt2_symbol }; 32 static symbol_set eucjp_symbol_set = { WC_CES_EUC_JP, 2, eucjp_symbol, NULL }; 33 static symbol_set euckr_symbol_set = { WC_CES_EUC_KR, 2, euckr_symbol, NULL }; 34 static symbol_set euccn_symbol_set = { WC_CES_EUC_CN, 2, euccn_symbol, NULL }; 35 static symbol_set euctw_symbol_set = { WC_CES_EUC_TW, 2, euctw_symbol, NULL }; 36 static symbol_set big5_symbol_set = { WC_CES_BIG5, 2, big5_symbol, NULL }; 37 #ifdef USE_UNICODE 38 static symbol_set utf8_symbol_set = { WC_CES_UTF_8, 1, utf8_symbol, NULL }; 39 #endif 40 static symbol_set cp850_symbol_set = { WC_CES_CP850, 1, cp850_symbol, NULL }; 41 42 static charset_symbol_set charset_symbol_list[] = { 43 { WC_CES_EUC_JP, &eucjp_symbol_set }, 44 { WC_CES_SHIFT_JIS, &eucjp_symbol_set }, 45 { WC_CES_ISO_2022_JP, &eucjp_symbol_set }, 46 { WC_CES_ISO_2022_JP_2, &eucjp_symbol_set }, 47 { WC_CES_ISO_2022_JP_3, &eucjp_symbol_set }, 48 { WC_CES_EUC_KR, &euckr_symbol_set }, 49 { WC_CES_ISO_2022_KR, &euckr_symbol_set }, 50 { WC_CES_JOHAB, &euckr_symbol_set }, 51 { WC_CES_UHC, &euckr_symbol_set }, 52 { WC_CES_EUC_CN, &euccn_symbol_set }, 53 { WC_CES_GBK, &euccn_symbol_set }, 54 { WC_CES_GB18030, &euccn_symbol_set }, 55 { WC_CES_HZ_GB_2312, &euccn_symbol_set }, 56 { WC_CES_ISO_2022_CN, &euccn_symbol_set }, 57 { WC_CES_EUC_TW, &euctw_symbol_set }, 58 { WC_CES_BIG5, &big5_symbol_set }, 59 { WC_CES_HKSCS, &big5_symbol_set }, 60 #ifdef USE_UNICODE 61 { WC_CES_UTF_8, &utf8_symbol_set }, 62 #endif 63 { WC_CES_CP850, &cp850_symbol_set }, 64 { 0, NULL }, 65 }; 66 /* *INDENT-ON* */ 67 68 static wc_ces save_charset = 0; 69 static symbol_set *save_symbol = NULL; 70 71 static void 72 encode_symbol(symbol_set * s) 73 { 74 int i; 75 76 for (i = 0; s->item[i]; i++) ; 77 s->conved_item = New_N(char *, i); 78 for (i = 0; s->item[i]; i++) { 79 if (*(s->item[i])) 80 s->conved_item[i] = wc_conv(s->item[i], s->ces, InnerCharset)->ptr; 81 } 82 } 83 84 char ** 85 get_symbol(wc_ces charset, int *width) 86 { 87 charset_symbol_set *p; 88 symbol_set *s = NULL; 89 90 if (UseGraphicChar != GRAPHIC_CHAR_ASCII) { 91 if (charset == save_charset && save_symbol != NULL && 92 *width == save_symbol->width) { 93 return save_symbol->conved_item; 94 } 95 save_charset = charset; 96 for (p = charset_symbol_list; p->charset; p++) { 97 if (charset == p->charset && 98 (*width == 0 || *width == p->symbol->width)) { 99 s = p->symbol; 100 break; 101 } 102 } 103 if (s == NULL) 104 s = (*width == 2) ? &alt2_symbol_set : &alt_symbol_set; 105 if (s != save_symbol) { 106 if (!s->conved_item) 107 encode_symbol(s); 108 save_symbol = s; 109 } 110 } else { 111 if (save_symbol != NULL && *width == save_symbol->width) 112 return save_symbol->conved_item; 113 s = (*width == 2) ? &alt2_symbol_set : &alt_symbol_set; 114 save_symbol = s; 115 } 116 *width = s->width; 117 return s->conved_item; 118 } 119 120 char ** 121 set_symbol(int width) 122 { 123 static char **symbol_buf = NULL; 124 static int save_width = -1; 125 symbol_set *s = &alt_symbol_set; 126 int i; 127 Str tmp; 128 129 if (width == save_width) 130 return symbol_buf; 131 if (symbol_buf == NULL) { 132 for (i = 0; s->item[i]; i++) ; 133 symbol_buf = New_N(char *, i); 134 } 135 for (i = 0; s->item[i]; i++) { 136 tmp = Strnew_size(4); 137 if (width == 2) 138 wtf_push(tmp, WC_CCS_SPECIAL_W, (wc_uint32) (SYMBOL_BASE + i)); 139 else 140 wtf_push(tmp, WC_CCS_SPECIAL, (wc_uint32) (SYMBOL_BASE + i)); 141 symbol_buf[i] = tmp->ptr; 142 } 143 save_width = width; 144 return symbol_buf; 145 } 146 147 #ifdef USE_UNICODE 148 void 149 update_utf8_symbol(void) 150 { 151 charset_symbol_set *p; 152 utf8_symbol_set.width = WcOption.east_asian_width ? 2 : 1; 153 for (p = charset_symbol_list; p->charset; p++) { 154 if (p->charset == WC_CES_UTF_8) { 155 encode_symbol(p->symbol); 156 break; 157 } 158 } 159 } 160 #endif 161 162 #else 163 164 char ** 165 get_symbol(void) 166 { 167 return alt_symbol; 168 } 169 #endif 170 171 void 172 push_symbol(Str str, char symbol, int width, int n) 173 { 174 char buf[2], *p; 175 int i; 176 177 #ifdef USE_M17N 178 if (width == 2) 179 p = alt2_symbol[(int)symbol]; 180 else 181 #endif 182 p = alt_symbol[(int)symbol]; 183 for (i = 0; i < 2 && *p; i++, p++) 184 buf[i] = (*p == ' ') ? NBSP_CODE : *p; 185 186 Strcat(str, Sprintf("<_SYMBOL TYPE=%d>", symbol)); 187 for (; n > 0; n--) 188 Strcat_charp_n(str, buf, i); 189 Strcat_charp(str, "</_SYMBOL>"); 190 }