Source:NetHack 5.0.0/src/utf8map.c
Jump to navigation
Jump to search
Below is the full text to utf8map.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/utf8map.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 5.0 utf8map.c */ 2. /* Copyright (c) Michael Allison, 2021. */ 3. /* NetHack may be freely redistributed. See license for details. */ 4. 5. #include "hack.h" 6. 7. #ifdef ENHANCED_SYMBOLS 8. 9. extern const struct symparse loadsyms[]; 10. extern struct enum_dump monsdump[]; 11. extern struct enum_dump objdump[]; 12. extern glyph_map glyphmap[MAX_GLYPH]; 13. extern const char *const known_handling[]; /* symbols.c */ 14. 15. /* hexdd[] is defined in decl.c */ 16. 17. int 18. unicode_val(const char *cp) 19. { 20. const char *dp; 21. int cval = 0, dcount; 22. 23. if (cp && *cp) { 24. cval = dcount = 0; 25. if ((*cp == 'U' || *cp == 'u') 26. && cp[1] == '+' && cp[2] && (dp = strchr(hexdd, cp[2])) != 0) { 27. cp += 2; /* move past the 'U' and '+' */ 28. do { 29. cval = (cval * 16) + ((int) (dp - hexdd) / 2); 30. } while (*++cp && (dp = strchr(hexdd, *cp)) != 0 && ++dcount < 7); 31. } 32. } 33. return cval; 34. } 35. 36. int 37. set_map_u(glyph_map *gmap, uint32 utf32ch, const uint8 *utf8str) 38. { 39. glyph_map *tmpgm = gmap; 40. 41. if (!tmpgm || !utf32ch) 42. return 0; 43. 44. if (gmap->u == 0) { 45. gmap->u = 46. (struct unicode_representation *) alloc(sizeof *gmap->u); 47. gmap->u->utf8str = 0; 48. } 49. if (gmap->u->utf8str != 0) { 50. free(gmap->u->utf8str); 51. gmap->u->utf8str = 0; 52. } 53. gmap->u->utf8str = (uint8 *) dupstr((const char *) utf8str); 54. gmap->u->utf32ch = utf32ch; 55. return 1; 56. } 57. 58. void 59. free_all_glyphmap_u(void) 60. { 61. int glyph; 62. int x, y; 63. 64. for (glyph = 0; glyph < MAX_GLYPH; ++glyph) { 65. if (glyphmap[glyph].u) { 66. if (glyphmap[glyph].u->utf8str) { 67. free(glyphmap[glyph].u->utf8str); 68. glyphmap[glyph].u->utf8str = 0; 69. } 70. free(glyphmap[glyph].u); 71. glyphmap[glyph].u = 0; 72. } 73. } 74. /* Prevent use after free from gg.gbuf */ 75. for (y = 0; y < ROWNO; ++y) { 76. for (x = 0; x < COLNO; ++x) { 77. gg.gbuf[y][x].glyphinfo.gm.u = NULL; 78. } 79. } 80. } 81. 82. /* helper routine if a window port wants to embed any UTF-8 sequences 83. for the glyph representation in the string in place of the \GNNNNNNNN 84. reference */ 85. char * 86. mixed_to_utf8(char *buf, size_t bufsz, const char *str, int *retflags) 87. { 88. char *put = buf; 89. glyph_info glyphinfo = nul_glyphinfo; 90. 91. if (!str) 92. return strcpy(buf, ""); 93. 94. while (*str && put < (buf + bufsz) - 1) { 95. if (*str == '\\') { 96. int dcount, so, ggv; 97. const char *save_str; 98. 99. save_str = str++; 100. switch (*str) { 101. case 'G': /* glyph value \GXXXXNNNN*/ 102. if ((dcount = decode_glyph(str + 1, &ggv))) { 103. str += (dcount + 1); 104. map_glyphinfo(0, 0, ggv, 0, &glyphinfo); 105. if (glyphinfo.gm.u && glyphinfo.gm.u->utf8str) { 106. uint8 *ucp = glyphinfo.gm.u->utf8str; 107. 108. while (*ucp && put < (buf + bufsz) - 1) 109. *put++ = *ucp++; 110. if (retflags) 111. *retflags = 1; 112. } else { 113. so = glyphinfo.gm.sym.symidx; 114. *put++ = gs.showsyms[so]; 115. if (retflags) 116. *retflags = 0; 117. } 118. /* 'str' is ready for the next loop iteration and 119. '*str' should not be copied at the end of this 120. iteration */ 121. continue; 122. } else { 123. /* possible forgery - leave it the way it is */ 124. str = save_str; 125. } 126. break; 127. case '\\': 128. break; 129. case '\0': 130. /* String ended with '\\'. This can happen when someone 131. names an object with a name ending with '\\', drops the 132. named object on the floor nearby and does a look at all 133. nearby objects. */ 134. /* brh - should we perhaps not allow things to have names 135. that contain '\\' */ 136. str = save_str; 137. break; 138. } 139. } 140. if (put < (buf + bufsz) - 1) 141. *put++ = *str++; 142. } 143. *put = '\0'; 144. return buf; 145. } 146. 147. int 148. add_custom_urep_entry( 149. const char *customization_name, 150. int glyphidx, 151. uint32 utf32ch, 152. const uint8 *utf8str, 153. enum graphics_sets which_set) 154. { 155. struct symset_customization *gdc 156. = &gs.sym_customizations[which_set][custom_ureps]; 157. struct customization_detail *details, *newdetails = 0; 158. 159. 160. if (!gdc->details) { 161. gdc->customization_name = dupstr(customization_name); 162. gdc->custtype = custom_ureps; 163. gdc->details = 0; 164. gdc->details_end = 0; 165. } 166. details = find_matching_customization(customization_name, 167. custom_ureps, which_set); /* FIXME */ 168. if (details) { 169. while (details) { 170. if (details->content.urep.glyphidx == glyphidx) { 171. if (details->content.urep.u.utf8str) 172. free(details->content.urep.u.utf8str); 173. if (utf32ch) { 174. details->content.urep.u.utf8str = 175. (uint8 *) dupstr((const char *) utf8str); 176. details->content.urep.u.utf32ch = utf32ch; 177. } else { 178. details->content.urep.u.utf8str = (uint8 *) 0; 179. details->content.urep.u.utf32ch = 0; 180. } 181. return 1; 182. } 183. details = details->next; 184. } 185. } 186. /* create new details entry */ 187. newdetails = (struct customization_detail *) alloc( 188. sizeof (struct customization_detail)); 189. newdetails->content.urep.glyphidx = glyphidx; 190. if (utf8str && *utf8str) { 191. newdetails->content.urep.u.utf8str = 192. (uint8 *) dupstr((const char *) utf8str); 193. } else { 194. newdetails->content.urep.u.utf8str = 195. (uint8 *) 0; 196. } 197. newdetails->content.urep.u.utf32ch = utf32ch; 198. newdetails->next = (struct customization_detail *) 0; 199. if (gdc->details == NULL) { 200. gdc->details = newdetails; 201. } else { 202. gdc->details_end->next = newdetails; 203. } 204. gdc->details_end = newdetails; 205. gdc->count++; 206. return 1; 207. } 208. #endif /* ENHANCED_SYMBOLS */ 209. 210. void 211. reset_customsymbols(void) 212. { 213. #ifdef ENHANCED_SYMBOLS 214. free_all_glyphmap_u(); 215. apply_customizations(gc.currentgraphics, do_custom_symbols); 216. #endif 217. } 218. 219. /* utf8map.c */ 220. 221. 222.