eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

numbers.c 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. /*
  2. * Copyright (C) 2005 to 2015 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2016, 2020 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <ctype.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <wctype.h>
  27. #include <espeak-ng/espeak_ng.h>
  28. #include <espeak-ng/speak_lib.h>
  29. #include <espeak-ng/encoding.h>
  30. #include "numbers.h"
  31. #include "dictionary.h" // for Lookup, TranslateRules, EncodePhonemes, Look...
  32. #include "phoneme.h" // for phonSWITCH, PHONEME_TAB, phonEND_WORD, phonP...
  33. #include "readclause.h" // for WordToString2, towlower2
  34. #include "synthdata.h" // for SelectPhonemeTable
  35. #include "synthesize.h" // for phoneme_tab
  36. #include "translate.h" // for Translator, LANGUAGE_OPTIONS, IsDigit09, WOR...
  37. #include "voice.h" // for voice, voice_t
  38. #define M_LIGATURE 0x8000
  39. #define M_NAME 0
  40. #define M_SMALLCAP 1
  41. #define M_TURNED 2
  42. #define M_REVERSED 3
  43. #define M_CURL 4
  44. #define M_ACUTE 5
  45. #define M_BREVE 6
  46. #define M_CARON 7
  47. #define M_CEDILLA 8
  48. #define M_CIRCUMFLEX 9
  49. #define M_DIAERESIS 10
  50. #define M_DOUBLE_ACUTE 11
  51. #define M_DOT_ABOVE 12
  52. #define M_GRAVE 13
  53. #define M_MACRON 14
  54. #define M_OGONEK 15
  55. #define M_RING 16
  56. #define M_STROKE 17
  57. #define M_TILDE 18
  58. #define M_BAR 19
  59. #define M_RETROFLEX 20
  60. #define M_HOOK 21
  61. #define M_MIDDLE_DOT M_DOT_ABOVE // duplicate of M_DOT_ABOVE
  62. #define M_IMPLOSIVE M_HOOK
  63. static int n_digit_lookup;
  64. static char *digit_lookup;
  65. static int speak_missing_thousands;
  66. static int number_control;
  67. typedef struct {
  68. const char *name;
  69. int accent_flags; // bit 0, say before the letter name
  70. } ACCENTS;
  71. // these are tokens to look up in the *_list file.
  72. static ACCENTS accents_tab[] = {
  73. { "_lig", 1 },
  74. { "_smc", 0 }, // smallcap
  75. { "_tur", 0 }, // turned
  76. { "_rev", 0 }, // reversed
  77. { "_crl", 0 }, // curl
  78. { "_acu", 0 }, // acute
  79. { "_brv", 0 }, // breve
  80. { "_hac", 0 }, // caron/hacek
  81. { "_ced", 0 }, // cedilla
  82. { "_cir", 0 }, // circumflex
  83. { "_dia", 0 }, // diaeresis
  84. { "_ac2", 0 }, // double acute
  85. { "_dot", 0 }, // dot
  86. { "_grv", 0 }, // grave
  87. { "_mcn", 0 }, // macron
  88. { "_ogo", 0 }, // ogonek
  89. { "_rng", 0 }, // ring
  90. { "_stk", 0 }, // stroke
  91. { "_tld", 0 }, // tilde
  92. { "_bar", 0 }, // bar
  93. { "_rfx", 0 }, // retroflex
  94. { "_hok", 0 }, // hook
  95. };
  96. #define CAPITAL 0
  97. #define LETTER(ch, mod1, mod2) (ch-59)+(mod1 << 6)+(mod2 << 11)
  98. #define LIGATURE(ch1, ch2, mod1) (ch1-59)+((ch2-59) << 6)+(mod1 << 12)+M_LIGATURE
  99. #define L_ALPHA 60 // U+3B1
  100. #define L_SCHWA 61 // U+259
  101. #define L_OPEN_E 62 // U+25B
  102. #define L_GAMMA 63 // U+3B3
  103. #define L_IOTA 64 // U+3B9
  104. #define L_PHI 67 // U+3C6
  105. #define L_ESH 68 // U+283
  106. #define L_UPSILON 69 // U+3C5
  107. #define L_EZH 70 // U+292
  108. #define L_GLOTTAL 71 // U+294
  109. #define L_RTAP 72 // U+27E
  110. #define L_RLONG 73 // U+27C
  111. static const short non_ascii_tab[] = {
  112. 0,
  113. 0x3b1, 0x259, 0x25b, 0x3b3, 0x3b9, 0x153, 0x3c9,
  114. 0x3c6, 0x283, 0x3c5, 0x292, 0x294, 0x27e, 0x27c
  115. };
  116. // characters U+00e0 to U+017f
  117. static const unsigned short letter_accents_0e0[] = {
  118. LETTER('a', M_GRAVE, 0), // U+00e0
  119. LETTER('a', M_ACUTE, 0),
  120. LETTER('a', M_CIRCUMFLEX, 0),
  121. LETTER('a', M_TILDE, 0),
  122. LETTER('a', M_DIAERESIS, 0),
  123. LETTER('a', M_RING, 0),
  124. LIGATURE('a', 'e', 0),
  125. LETTER('c', M_CEDILLA, 0),
  126. LETTER('e', M_GRAVE, 0),
  127. LETTER('e', M_ACUTE, 0),
  128. LETTER('e', M_CIRCUMFLEX, 0),
  129. LETTER('e', M_DIAERESIS, 0),
  130. LETTER('i', M_GRAVE, 0),
  131. LETTER('i', M_ACUTE, 0),
  132. LETTER('i', M_CIRCUMFLEX, 0),
  133. LETTER('i', M_DIAERESIS, 0),
  134. LETTER('d', M_NAME, 0), // eth U+00f0
  135. LETTER('n', M_TILDE, 0),
  136. LETTER('o', M_GRAVE, 0),
  137. LETTER('o', M_ACUTE, 0),
  138. LETTER('o', M_CIRCUMFLEX, 0),
  139. LETTER('o', M_TILDE, 0),
  140. LETTER('o', M_DIAERESIS, 0),
  141. 0, // division sign
  142. LETTER('o', M_STROKE, 0),
  143. LETTER('u', M_GRAVE, 0),
  144. LETTER('u', M_ACUTE, 0),
  145. LETTER('u', M_CIRCUMFLEX, 0),
  146. LETTER('u', M_DIAERESIS, 0),
  147. LETTER('y', M_ACUTE, 0),
  148. LETTER('t', M_NAME, 0), // thorn
  149. LETTER('y', M_DIAERESIS, 0),
  150. CAPITAL, // U+0100
  151. LETTER('a', M_MACRON, 0),
  152. CAPITAL,
  153. LETTER('a', M_BREVE, 0),
  154. CAPITAL,
  155. LETTER('a', M_OGONEK, 0),
  156. CAPITAL,
  157. LETTER('c', M_ACUTE, 0),
  158. CAPITAL,
  159. LETTER('c', M_CIRCUMFLEX, 0),
  160. CAPITAL,
  161. LETTER('c', M_DOT_ABOVE, 0),
  162. CAPITAL,
  163. LETTER('c', M_CARON, 0),
  164. CAPITAL,
  165. LETTER('d', M_CARON, 0),
  166. CAPITAL, // U+0110
  167. LETTER('d', M_STROKE, 0),
  168. CAPITAL,
  169. LETTER('e', M_MACRON, 0),
  170. CAPITAL,
  171. LETTER('e', M_BREVE, 0),
  172. CAPITAL,
  173. LETTER('e', M_DOT_ABOVE, 0),
  174. CAPITAL,
  175. LETTER('e', M_OGONEK, 0),
  176. CAPITAL,
  177. LETTER('e', M_CARON, 0),
  178. CAPITAL,
  179. LETTER('g', M_CIRCUMFLEX, 0),
  180. CAPITAL,
  181. LETTER('g', M_BREVE, 0),
  182. CAPITAL, // U+0120
  183. LETTER('g', M_DOT_ABOVE, 0),
  184. CAPITAL,
  185. LETTER('g', M_CEDILLA, 0),
  186. CAPITAL,
  187. LETTER('h', M_CIRCUMFLEX, 0),
  188. CAPITAL,
  189. LETTER('h', M_STROKE, 0),
  190. CAPITAL,
  191. LETTER('i', M_TILDE, 0),
  192. CAPITAL,
  193. LETTER('i', M_MACRON, 0),
  194. CAPITAL,
  195. LETTER('i', M_BREVE, 0),
  196. CAPITAL,
  197. LETTER('i', M_OGONEK, 0),
  198. CAPITAL, // U+0130
  199. LETTER('i', M_NAME, 0), // dotless i
  200. CAPITAL,
  201. LIGATURE('i', 'j', 0),
  202. CAPITAL,
  203. LETTER('j', M_CIRCUMFLEX, 0),
  204. CAPITAL,
  205. LETTER('k', M_CEDILLA, 0),
  206. LETTER('k', M_NAME, 0), // kra
  207. CAPITAL,
  208. LETTER('l', M_ACUTE, 0),
  209. CAPITAL,
  210. LETTER('l', M_CEDILLA, 0),
  211. CAPITAL,
  212. LETTER('l', M_CARON, 0),
  213. CAPITAL,
  214. LETTER('l', M_MIDDLE_DOT, 0), // U+0140
  215. CAPITAL,
  216. LETTER('l', M_STROKE, 0),
  217. CAPITAL,
  218. LETTER('n', M_ACUTE, 0),
  219. CAPITAL,
  220. LETTER('n', M_CEDILLA, 0),
  221. CAPITAL,
  222. LETTER('n', M_CARON, 0),
  223. LETTER('n', M_NAME, 0), // apostrophe n
  224. CAPITAL,
  225. LETTER('n', M_NAME, 0), // eng
  226. CAPITAL,
  227. LETTER('o', M_MACRON, 0),
  228. CAPITAL,
  229. LETTER('o', M_BREVE, 0),
  230. CAPITAL, // U+0150
  231. LETTER('o', M_DOUBLE_ACUTE, 0),
  232. CAPITAL,
  233. LIGATURE('o', 'e', 0),
  234. CAPITAL,
  235. LETTER('r', M_ACUTE, 0),
  236. CAPITAL,
  237. LETTER('r', M_CEDILLA, 0),
  238. CAPITAL,
  239. LETTER('r', M_CARON, 0),
  240. CAPITAL,
  241. LETTER('s', M_ACUTE, 0),
  242. CAPITAL,
  243. LETTER('s', M_CIRCUMFLEX, 0),
  244. CAPITAL,
  245. LETTER('s', M_CEDILLA, 0),
  246. CAPITAL, // U+0160
  247. LETTER('s', M_CARON, 0),
  248. CAPITAL,
  249. LETTER('t', M_CEDILLA, 0),
  250. CAPITAL,
  251. LETTER('t', M_CARON, 0),
  252. CAPITAL,
  253. LETTER('t', M_STROKE, 0),
  254. CAPITAL,
  255. LETTER('u', M_TILDE, 0),
  256. CAPITAL,
  257. LETTER('u', M_MACRON, 0),
  258. CAPITAL,
  259. LETTER('u', M_BREVE, 0),
  260. CAPITAL,
  261. LETTER('u', M_RING, 0),
  262. CAPITAL, // U+0170
  263. LETTER('u', M_DOUBLE_ACUTE, 0),
  264. CAPITAL,
  265. LETTER('u', M_OGONEK, 0),
  266. CAPITAL,
  267. LETTER('w', M_CIRCUMFLEX, 0),
  268. CAPITAL,
  269. LETTER('y', M_CIRCUMFLEX, 0),
  270. CAPITAL, // Y-DIAERESIS
  271. CAPITAL,
  272. LETTER('z', M_ACUTE, 0),
  273. CAPITAL,
  274. LETTER('z', M_DOT_ABOVE, 0),
  275. CAPITAL,
  276. LETTER('z', M_CARON, 0),
  277. LETTER('s', M_NAME, 0), // long-s U+17f
  278. };
  279. // characters U+0250 to U+029F
  280. static const unsigned short letter_accents_250[] = {
  281. LETTER('a', M_TURNED, 0), // U+250
  282. LETTER(L_ALPHA, 0, 0),
  283. LETTER(L_ALPHA, M_TURNED, 0),
  284. LETTER('b', M_IMPLOSIVE, 0),
  285. 0, // open-o
  286. LETTER('c', M_CURL, 0),
  287. LETTER('d', M_RETROFLEX, 0),
  288. LETTER('d', M_IMPLOSIVE, 0),
  289. LETTER('e', M_REVERSED, 0), // U+258
  290. 0, // schwa
  291. LETTER(L_SCHWA, M_HOOK, 0),
  292. 0, // open-e
  293. LETTER(L_OPEN_E, M_REVERSED, 0),
  294. LETTER(L_OPEN_E, M_HOOK, M_REVERSED),
  295. 0,
  296. LETTER('j', M_BAR, 0),
  297. LETTER('g', M_IMPLOSIVE, 0), // U+260
  298. LETTER('g', 0, 0),
  299. LETTER('g', M_SMALLCAP, 0),
  300. LETTER(L_GAMMA, 0, 0),
  301. 0, // ramshorn
  302. LETTER('h', M_TURNED, 0),
  303. LETTER('h', M_HOOK, 0),
  304. 0,
  305. LETTER('i', M_BAR, 0), // U+268
  306. LETTER(L_IOTA, 0, 0),
  307. LETTER('i', M_SMALLCAP, 0),
  308. LETTER('l', M_TILDE, 0),
  309. LETTER('l', M_BAR, 0),
  310. LETTER('l', M_RETROFLEX, 0),
  311. LIGATURE('l', 'z', 0),
  312. LETTER('m', M_TURNED, 0),
  313. 0,
  314. LETTER('m', M_HOOK, 0),
  315. 0,
  316. LETTER('n', M_RETROFLEX, 0),
  317. LETTER('n', M_SMALLCAP, 0),
  318. LETTER('o', M_BAR, 0),
  319. LIGATURE('o', 'e', M_SMALLCAP),
  320. 0,
  321. LETTER(L_PHI, 0, 0), // U+278
  322. LETTER('r', M_TURNED, 0),
  323. LETTER(L_RLONG, M_TURNED, 0),
  324. LETTER('r', M_RETROFLEX, M_TURNED),
  325. 0,
  326. LETTER('r', M_RETROFLEX, 0),
  327. 0, // r-tap
  328. LETTER(L_RTAP, M_REVERSED, 0),
  329. LETTER('r', M_SMALLCAP, 0), // U+280
  330. LETTER('r', M_TURNED, M_SMALLCAP),
  331. LETTER('s', M_RETROFLEX, 0),
  332. 0, // esh
  333. LETTER('j', M_HOOK, 0),
  334. LETTER(L_ESH, M_REVERSED, 0),
  335. LETTER(L_ESH, M_CURL, 0),
  336. LETTER('t', M_TURNED, 0),
  337. LETTER('t', M_RETROFLEX, 0), // U+288
  338. LETTER('u', M_BAR, 0),
  339. LETTER(L_UPSILON, 0, 0),
  340. LETTER('v', M_HOOK, 0),
  341. LETTER('v', M_TURNED, 0),
  342. LETTER('w', M_TURNED, 0),
  343. LETTER('y', M_TURNED, 0),
  344. LETTER('y', M_SMALLCAP, 0),
  345. LETTER('z', M_RETROFLEX, 0), // U+290
  346. LETTER('z', M_CURL, 0),
  347. 0, // ezh
  348. LETTER(L_EZH, M_CURL, 0),
  349. 0, // glottal stop
  350. LETTER(L_GLOTTAL, M_REVERSED, 0),
  351. LETTER(L_GLOTTAL, M_TURNED, 0),
  352. 0,
  353. 0, // bilabial click U+298
  354. LETTER('b', M_SMALLCAP, 0),
  355. 0,
  356. LETTER('g', M_IMPLOSIVE, M_SMALLCAP),
  357. LETTER('h', M_SMALLCAP, 0),
  358. LETTER('j', M_CURL, 0),
  359. LETTER('k', M_TURNED, 0),
  360. LETTER('l', M_SMALLCAP, 0),
  361. LETTER('q', M_HOOK, 0), // U+2a0
  362. LETTER(L_GLOTTAL, M_STROKE, 0),
  363. LETTER(L_GLOTTAL, M_STROKE, M_REVERSED),
  364. LIGATURE('d', 'z', 0),
  365. 0, // dezh
  366. LIGATURE('d', 'z', M_CURL),
  367. LIGATURE('t', 's', 0),
  368. 0, // tesh
  369. LIGATURE('t', 's', M_CURL),
  370. };
  371. static int LookupLetter2(Translator *tr, unsigned int letter, char *ph_buf)
  372. {
  373. int len;
  374. char single_letter[10];
  375. single_letter[0] = 0;
  376. single_letter[1] = '_';
  377. len = utf8_out(letter, &single_letter[2]);
  378. single_letter[len+2] = ' ';
  379. single_letter[len+3] = 0;
  380. if (Lookup(tr, &single_letter[1], ph_buf) == 0) {
  381. single_letter[1] = ' ';
  382. if (Lookup(tr, &single_letter[2], ph_buf) == 0)
  383. TranslateRules(tr, &single_letter[2], ph_buf, 20, NULL, 0, NULL);
  384. }
  385. return ph_buf[0];
  386. }
  387. void LookupAccentedLetter(Translator *tr, unsigned int letter, char *ph_buf)
  388. {
  389. // lookup the character in the accents table
  390. int accent_data = 0;
  391. int accent1 = 0;
  392. int accent2 = 0;
  393. int flags1, flags2;
  394. int basic_letter;
  395. int letter2 = 0;
  396. char ph_letter1[30];
  397. char ph_letter2[30];
  398. char ph_accent1[30];
  399. char ph_accent2[30];
  400. ph_accent2[0] = 0;
  401. if ((letter >= 0xe0) && (letter < 0x17f))
  402. accent_data = letter_accents_0e0[letter - 0xe0];
  403. else if ((letter >= 0x250) && (letter <= 0x2a8))
  404. accent_data = letter_accents_250[letter - 0x250];
  405. if (accent_data != 0) {
  406. basic_letter = (accent_data & 0x3f) + 59;
  407. if (basic_letter < 'a')
  408. basic_letter = non_ascii_tab[basic_letter-59];
  409. if (accent_data & M_LIGATURE) {
  410. letter2 = (accent_data >> 6) & 0x3f;
  411. letter2 += 59;
  412. accent2 = (accent_data >> 12) & 0x7;
  413. } else {
  414. accent1 = (accent_data >> 6) & 0x1f;
  415. accent2 = (accent_data >> 11) & 0xf;
  416. }
  417. if ((accent1 == 0) && !(accent_data & M_LIGATURE)) {
  418. // just a letter name, not an accented character or ligature
  419. return;
  420. }
  421. if ((flags1 = Lookup(tr, accents_tab[accent1].name, ph_accent1)) != 0) {
  422. if (LookupLetter2(tr, basic_letter, ph_letter1) != 0) {
  423. if (accent2 != 0) {
  424. flags2 = Lookup(tr, accents_tab[accent2].name, ph_accent2);
  425. if (flags2 & FLAG_ACCENT_BEFORE) {
  426. strcpy(ph_buf, ph_accent2);
  427. ph_buf += strlen(ph_buf);
  428. ph_accent2[0] = 0;
  429. }
  430. }
  431. if (letter2 != 0) {
  432. // ligature
  433. LookupLetter2(tr, letter2, ph_letter2);
  434. sprintf(ph_buf, "%s%c%s%c%s%s", ph_accent1, phonPAUSE_VSHORT, ph_letter1, phonSTRESS_P, ph_letter2, ph_accent2);
  435. } else {
  436. if (accent1 == 0)
  437. strcpy(ph_buf, ph_letter1);
  438. else if ((tr->langopts.accents & 1) || (flags1 & FLAG_ACCENT_BEFORE) || (accents_tab[accent1].accent_flags & 1))
  439. sprintf(ph_buf, "%s%c%c%s", ph_accent1, phonPAUSE_VSHORT, phonSTRESS_P, ph_letter1);
  440. else
  441. sprintf(ph_buf, "%c%s%c%s%c", phonSTRESS_2, ph_letter1, phonPAUSE_VSHORT, ph_accent1, phonPAUSE_VSHORT);
  442. }
  443. }
  444. }
  445. }
  446. }
  447. void LookupLetter(Translator *tr, unsigned int letter, int next_byte, char *ph_buf1, int control)
  448. {
  449. // control, bit 0: not the first letter of a word
  450. int len;
  451. char single_letter[10] = { 0, 0 };
  452. unsigned int dict_flags[2];
  453. char ph_buf3[40];
  454. ph_buf1[0] = 0;
  455. len = utf8_out(letter, &single_letter[2]);
  456. single_letter[len+2] = ' ';
  457. if (next_byte == -1) {
  458. // speaking normal text, not individual characters
  459. if (Lookup(tr, &single_letter[2], ph_buf1) != 0)
  460. return;
  461. single_letter[1] = '_';
  462. if (Lookup(tr, &single_letter[1], ph_buf3) != 0)
  463. return; // the character is specified as _* so ignore it when speaking normal text
  464. // check whether this character is specified for English
  465. if (tr->translator_name == L('e', 'n'))
  466. return; // we are already using English
  467. SetTranslator2(ESPEAKNG_DEFAULT_VOICE);
  468. if (Lookup(translator2, &single_letter[2], ph_buf3) != 0) {
  469. // yes, switch to English and re-translate the word
  470. sprintf(ph_buf1, "%c", phonSWITCH);
  471. }
  472. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  473. return;
  474. }
  475. if ((letter <= 32) || iswspace(letter)) {
  476. // lookup space as _&32 etc.
  477. sprintf(&single_letter[1], "_#%d ", letter);
  478. Lookup(tr, &single_letter[1], ph_buf1);
  479. return;
  480. }
  481. if (next_byte != ' ')
  482. next_byte = RULE_SPELLING;
  483. single_letter[3+len] = next_byte; // follow by space-space if the end of the word, or space-31
  484. single_letter[1] = '_';
  485. // if the $accent flag is set for this letter, use the accents table (below)
  486. dict_flags[1] = 0;
  487. if (Lookup(tr, &single_letter[1], ph_buf3) == 0) {
  488. single_letter[1] = ' ';
  489. if (Lookup(tr, &single_letter[2], ph_buf3) == 0)
  490. TranslateRules(tr, &single_letter[2], ph_buf3, sizeof(ph_buf3), NULL, FLAG_NO_TRACE, NULL);
  491. }
  492. if (ph_buf3[0] == 0)
  493. LookupAccentedLetter(tr, letter, ph_buf3);
  494. strcpy(ph_buf1, ph_buf3);
  495. if ((ph_buf1[0] == 0) || (ph_buf1[0] == phonSWITCH))
  496. return;
  497. dict_flags[0] = 0;
  498. dict_flags[1] = 0;
  499. SetWordStress(tr, ph_buf1, dict_flags, -1, control & 1);
  500. }
  501. // unicode ranges for non-ascii digits 0-9 (these must be in ascending order)
  502. static const int number_ranges[] = {
  503. 0x660, 0x6f0, // arabic
  504. 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xbe6, 0xc66, 0xce6, 0xd66, // indic
  505. 0xe50, 0xed0, 0xf20, 0x1040, 0x1090,
  506. 0
  507. };
  508. static int NonAsciiNumber(int letter)
  509. {
  510. // Change non-ascii digit into ascii digit '0' to '9', (or -1 if not)
  511. const int *p;
  512. int base;
  513. for (p = number_ranges; (base = *p) != 0; p++) {
  514. if (letter < base)
  515. break; // not found
  516. if (letter < (base+10))
  517. return letter-base+'0';
  518. }
  519. return -1;
  520. }
  521. #define L_SUB 0x4000 // subscript
  522. #define L_SUP 0x8000 // superscript
  523. static const char *modifiers[] = { NULL, "_sub", "_sup", NULL };
  524. // this list must be in ascending order
  525. static unsigned short derived_letters[] = {
  526. 0x00aa, 'a'+L_SUP,
  527. 0x00b2, '2'+L_SUP,
  528. 0x00b3, '3'+L_SUP,
  529. 0x00b9, '1'+L_SUP,
  530. 0x00ba, 'o'+L_SUP,
  531. 0x02b0, 'h'+L_SUP,
  532. 0x02b1, 0x266+L_SUP,
  533. 0x02b2, 'j'+L_SUP,
  534. 0x02b3, 'r'+L_SUP,
  535. 0x02b4, 0x279+L_SUP,
  536. 0x02b5, 0x27b+L_SUP,
  537. 0x02b6, 0x281+L_SUP,
  538. 0x02b7, 'w'+L_SUP,
  539. 0x02b8, 'y'+L_SUP,
  540. 0x02c0, 0x294+L_SUP,
  541. 0x02c1, 0x295+L_SUP,
  542. 0x02e0, 0x263+L_SUP,
  543. 0x02e1, 'l'+L_SUP,
  544. 0x02e2, 's'+L_SUP,
  545. 0x02e3, 'x'+L_SUP,
  546. 0x2070, '0'+L_SUP,
  547. 0x2071, 'i'+L_SUP,
  548. 0x2074, '4'+L_SUP,
  549. 0x2075, '5'+L_SUP,
  550. 0x2076, '6'+L_SUP,
  551. 0x2077, '7'+L_SUP,
  552. 0x2078, '8'+L_SUP,
  553. 0x2079, '9'+L_SUP,
  554. 0x207a, '+'+L_SUP,
  555. 0x207b, '-'+L_SUP,
  556. 0x207c, '='+L_SUP,
  557. 0x207d, '('+L_SUP,
  558. 0x207e, ')'+L_SUP,
  559. 0x207f, 'n'+L_SUP,
  560. 0x2080, '0'+L_SUB,
  561. 0x2081, '1'+L_SUB,
  562. 0x2082, '2'+L_SUB,
  563. 0x2083, '3'+L_SUB,
  564. 0x2084, '4'+L_SUB,
  565. 0x2085, '5'+L_SUB,
  566. 0x2086, '6'+L_SUB,
  567. 0x2087, '7'+L_SUB,
  568. 0x2088, '8'+L_SUB,
  569. 0x2089, '9'+L_SUB,
  570. 0x208a, '+'+L_SUB,
  571. 0x208b, '-'+L_SUB,
  572. 0x208c, '='+L_SUB,
  573. 0x208d, '('+L_SUB,
  574. 0x208e, ')'+L_SUB,
  575. 0x2090, 'a'+L_SUB,
  576. 0x2091, 'e'+L_SUB,
  577. 0x2092, 'o'+L_SUB,
  578. 0x2093, 'x'+L_SUB,
  579. 0x2094, 0x259+L_SUB,
  580. 0x2095, 'h'+L_SUB,
  581. 0x2096, 'k'+L_SUB,
  582. 0x2097, 'l'+L_SUB,
  583. 0x2098, 'm'+L_SUB,
  584. 0x2099, 'n'+L_SUB,
  585. 0x209a, 'p'+L_SUB,
  586. 0x209b, 's'+L_SUB,
  587. 0x209c, 't'+L_SUB,
  588. 0, 0
  589. };
  590. // names, using phonemes available to all languages
  591. static const char *hex_letters[] = {
  592. "'e:j",
  593. "b'i:",
  594. "s'i:",
  595. "d'i:",
  596. "'i:",
  597. "'ef"
  598. };
  599. int IsSuperscript(int letter)
  600. {
  601. // is this a subscript or superscript letter ?
  602. int ix;
  603. int c;
  604. for (ix = 0; (c = derived_letters[ix]) != 0; ix += 2) {
  605. if (c > letter)
  606. break;
  607. if (c == letter)
  608. return derived_letters[ix+1];
  609. }
  610. return 0;
  611. }
  612. int TranslateLetter(Translator *tr, char *word, char *phonemes, int control, ALPHABET *current_alphabet)
  613. {
  614. // get pronunciation for an isolated letter
  615. // return number of bytes used by the letter
  616. // control bit 0: a non-initial letter in a word
  617. // bit 1: say 'capital'
  618. // bit 2: say character code for unknown letters
  619. int n_bytes;
  620. int letter;
  621. int len;
  622. int ix;
  623. int c;
  624. char *p2;
  625. char *pbuf;
  626. const char *modifier;
  627. ALPHABET *alphabet;
  628. int al_offset;
  629. int al_flags;
  630. int language;
  631. int number;
  632. int phontab_1;
  633. int speak_letter_number;
  634. char capital[30];
  635. char ph_buf[80];
  636. char ph_buf2[80];
  637. char ph_alphabet[80];
  638. char hexbuf[12];
  639. static const char pause_string[] = { phonPAUSE, 0 };
  640. ph_buf[0] = 0;
  641. ph_alphabet[0] = 0;
  642. capital[0] = 0;
  643. phontab_1 = translator->phoneme_tab_ix;
  644. n_bytes = utf8_in(&letter, word);
  645. if ((letter & 0xfff00) == 0x0e000)
  646. letter &= 0xff; // uncode private usage area
  647. if (control & 2) {
  648. // include CAPITAL information
  649. if (iswupper(letter))
  650. Lookup(tr, "_cap", capital);
  651. }
  652. letter = towlower2(letter, tr);
  653. LookupLetter(tr, letter, word[n_bytes], ph_buf, control & 1);
  654. if (ph_buf[0] == 0) {
  655. // is this a subscript or superscript letter ?
  656. if ((c = IsSuperscript(letter)) != 0) {
  657. letter = c & 0x3fff;
  658. if ((control & 4 ) && ((modifier = modifiers[c >> 14]) != NULL)) {
  659. // don't say "superscript" during normal text reading
  660. Lookup(tr, modifier, capital);
  661. if (capital[0] == 0) {
  662. capital[2] = SetTranslator2(ESPEAKNG_DEFAULT_VOICE); // overwrites previous contents of translator2
  663. Lookup(translator2, modifier, &capital[3]);
  664. if (capital[3] != 0) {
  665. capital[0] = phonPAUSE;
  666. capital[1] = phonSWITCH;
  667. len = strlen(&capital[3]);
  668. capital[len+3] = phonSWITCH;
  669. capital[len+4] = phontab_1;
  670. capital[len+5] = 0;
  671. }
  672. }
  673. }
  674. }
  675. LookupLetter(tr, letter, word[n_bytes], ph_buf, control & 1);
  676. }
  677. if (ph_buf[0] == phonSWITCH) {
  678. strcpy(phonemes, ph_buf);
  679. return 0;
  680. }
  681. if ((ph_buf[0] == 0) && ((number = NonAsciiNumber(letter)) > 0)) {
  682. // convert a non-ascii number to 0-9
  683. LookupLetter(tr, number, 0, ph_buf, control & 1);
  684. }
  685. al_offset = 0;
  686. al_flags = 0;
  687. if ((alphabet = AlphabetFromChar(letter)) != NULL) {
  688. al_offset = alphabet->offset;
  689. al_flags = alphabet->flags;
  690. }
  691. if (alphabet != current_alphabet) {
  692. // speak the name of the alphabet
  693. current_alphabet = alphabet;
  694. if ((alphabet != NULL) && !(al_flags & AL_DONT_NAME) && (al_offset != translator->letter_bits_offset)) {
  695. if ((al_flags & AL_DONT_NAME) || (al_offset == translator->langopts.alt_alphabet) || (al_offset == translator->langopts.our_alphabet)) {
  696. // don't say the alphabet name
  697. } else {
  698. ph_buf2[0] = 0;
  699. if (Lookup(translator, alphabet->name, ph_alphabet) == 0) { // the original language for the current voice
  700. // Can't find the local name for this alphabet, use the English name
  701. ph_alphabet[2] = SetTranslator2(ESPEAKNG_DEFAULT_VOICE); // overwrites previous contents of translator2
  702. Lookup(translator2, alphabet->name, ph_buf2);
  703. } else if (translator != tr) {
  704. phontab_1 = tr->phoneme_tab_ix;
  705. strcpy(ph_buf2, ph_alphabet);
  706. ph_alphabet[2] = translator->phoneme_tab_ix;
  707. }
  708. if (ph_buf2[0] != 0) {
  709. // we used a different language for the alphabet name (now in ph_buf2)
  710. ph_alphabet[0] = phonPAUSE;
  711. ph_alphabet[1] = phonSWITCH;
  712. strcpy(&ph_alphabet[3], ph_buf2);
  713. len = strlen(ph_buf2) + 3;
  714. ph_alphabet[len] = phonSWITCH;
  715. ph_alphabet[len+1] = phontab_1;
  716. ph_alphabet[len+2] = 0;
  717. }
  718. }
  719. }
  720. }
  721. // caution: SetWordStress() etc don't expect phonSWITCH + phoneme table number
  722. if (ph_buf[0] == 0) {
  723. if ((al_offset != 0) && (al_offset == translator->langopts.alt_alphabet))
  724. language = translator->langopts.alt_alphabet_lang;
  725. else if ((alphabet != NULL) && (alphabet->language != 0) && !(al_flags & AL_NOT_LETTERS))
  726. language = alphabet->language;
  727. else
  728. language = L('e', 'n');
  729. if ((language != tr->translator_name) || (language == L('k', 'o'))) {
  730. char *p3;
  731. int initial, code;
  732. char hangul_buf[12];
  733. // speak in the language for this alphabet (or English)
  734. ph_buf[2] = SetTranslator2(WordToString2(language));
  735. if (translator2 != NULL) {
  736. if (((code = letter - 0xac00) >= 0) && (letter <= 0xd7af)) {
  737. // Special case for Korean letters.
  738. // break a syllable hangul into 2 or 3 individual jamo
  739. hangul_buf[0] = ' ';
  740. p3 = &hangul_buf[1];
  741. if ((initial = (code/28)/21) != 11) {
  742. p3 += utf8_out(initial + 0x1100, p3);
  743. }
  744. utf8_out(((code/28) % 21) + 0x1161, p3); // medial
  745. utf8_out((code % 28) + 0x11a7, &p3[3]); // final
  746. p3[6] = ' ';
  747. p3[7] = 0;
  748. ph_buf[3] = 0;
  749. TranslateRules(translator2, &hangul_buf[1], &ph_buf[3], sizeof(ph_buf)-3, NULL, 0, NULL);
  750. SetWordStress(translator2, &ph_buf[3], NULL, -1, 0);
  751. } else
  752. LookupLetter(translator2, letter, word[n_bytes], &ph_buf[3], control & 1);
  753. if (ph_buf[3] == phonSWITCH) {
  754. // another level of language change
  755. ph_buf[2] = SetTranslator2(&ph_buf[4]);
  756. LookupLetter(translator2, letter, word[n_bytes], &ph_buf[3], control & 1);
  757. }
  758. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  759. if (ph_buf[3] != 0) {
  760. ph_buf[0] = phonPAUSE;
  761. ph_buf[1] = phonSWITCH;
  762. len = strlen(&ph_buf[3]) + 3;
  763. ph_buf[len] = phonSWITCH; // switch back
  764. ph_buf[len+1] = tr->phoneme_tab_ix;
  765. ph_buf[len+2] = 0;
  766. }
  767. }
  768. }
  769. }
  770. if (ph_buf[0] == 0) {
  771. // character name not found
  772. if (ph_buf[0] == 0) {
  773. speak_letter_number = 1;
  774. if (!(al_flags & AL_NO_SYMBOL)) {
  775. if (iswalpha(letter))
  776. Lookup(translator, "_?A", ph_buf);
  777. if ((ph_buf[0] == 0) && !iswspace(letter))
  778. Lookup(translator, "_??", ph_buf);
  779. if (ph_buf[0] == 0)
  780. EncodePhonemes("l'et@", ph_buf, NULL);
  781. }
  782. if (!(control & 4) && (al_flags & AL_NOT_CODE)) {
  783. // don't speak the character code number, unless we want full details of this character
  784. speak_letter_number = 0;
  785. }
  786. if (speak_letter_number) {
  787. if (al_offset == 0x2800) {
  788. // braille dots symbol, list the numbered dots
  789. p2 = hexbuf;
  790. for (ix = 0; ix < 8; ix++) {
  791. if (letter & (1 << ix))
  792. *p2++ = '1'+ix;
  793. }
  794. *p2 = 0;
  795. } else {
  796. // speak the hexadecimal number of the character code
  797. sprintf(hexbuf, "%x", letter);
  798. }
  799. pbuf = ph_buf;
  800. for (p2 = hexbuf; *p2 != 0; p2++) {
  801. pbuf += strlen(pbuf);
  802. *pbuf++ = phonPAUSE_VSHORT;
  803. LookupLetter(translator, *p2, 0, pbuf, 1);
  804. if (((pbuf[0] == 0) || (pbuf[0] == phonSWITCH)) && (*p2 >= 'a')) {
  805. // This language has no translation for 'a' to 'f', speak English names using base phonemes
  806. EncodePhonemes(hex_letters[*p2 - 'a'], pbuf, NULL);
  807. }
  808. }
  809. strcat(pbuf, pause_string);
  810. }
  811. }
  812. }
  813. len = strlen(phonemes);
  814. if (tr->langopts.accents & 2) // 'capital' before or after the word ?
  815. sprintf(ph_buf2, "%c%s%s%s", 0xff, ph_alphabet, ph_buf, capital);
  816. else
  817. sprintf(ph_buf2, "%c%s%s%s", 0xff, ph_alphabet, capital, ph_buf); // the 0xff marker will be removed or replaced in SetSpellingStress()
  818. if ((len + strlen(ph_buf2)) < N_WORD_PHONEMES)
  819. strcpy(&phonemes[len], ph_buf2);
  820. return n_bytes;
  821. }
  822. void SetSpellingStress(Translator *tr, char *phonemes, int control, int n_chars)
  823. {
  824. // Individual letter names, reduce the stress of some.
  825. int ix;
  826. unsigned int c;
  827. int n_stress = 0;
  828. int prev = 0;
  829. int count;
  830. unsigned char buf[N_WORD_PHONEMES];
  831. for (ix = 0; (c = phonemes[ix]) != 0; ix++) {
  832. if ((c == phonSTRESS_P) && (prev != phonSWITCH))
  833. n_stress++;
  834. buf[ix] = prev = c;
  835. }
  836. buf[ix] = 0;
  837. count = 0;
  838. prev = 0;
  839. for (ix = 0; (c = buf[ix]) != 0; ix++) {
  840. if ((c == phonSTRESS_P) && (n_chars > 1) && (prev != phonSWITCH)) {
  841. count++;
  842. if (tr->langopts.spelling_stress == 1) {
  843. // stress on initial letter when spelling
  844. if (count > 1)
  845. c = phonSTRESS_3;
  846. } else {
  847. if (count != n_stress) {
  848. if (((count % 3) != 0) || (count == n_stress-1))
  849. c = phonSTRESS_3; // reduce to secondary stress
  850. }
  851. }
  852. } else if (c == 0xff) {
  853. if ((control < 2) || (ix == 0))
  854. continue; // don't insert pauses
  855. if (((count % 3) == 0) || (control > 2))
  856. c = phonPAUSE_NOLINK; // pause following a primary stress
  857. else
  858. c = phonPAUSE_VSHORT;
  859. }
  860. *phonemes++ = prev = c;
  861. }
  862. if (control >= 2)
  863. *phonemes++ = phonPAUSE_NOLINK;
  864. *phonemes = 0;
  865. }
  866. // Numbers
  867. static char ph_ordinal2[12];
  868. static char ph_ordinal2x[12];
  869. static int CheckDotOrdinal(Translator *tr, char *word, char *word_end, WORD_TAB *wtab, int roman)
  870. {
  871. int ordinal = 0;
  872. int c2;
  873. int nextflags;
  874. if ((tr->langopts.numbers & NUM_ORDINAL_DOT) && ((word_end[0] == '.') || (wtab[0].flags & FLAG_HAS_DOT)) && !(wtab[1].flags & FLAG_NOSPACE)) {
  875. if (roman || !(wtab[1].flags & FLAG_FIRST_UPPER)) {
  876. if (word_end[0] == '.')
  877. utf8_in(&c2, &word_end[2]);
  878. else
  879. utf8_in(&c2, &word_end[0]);
  880. if ((word_end[0] != 0) && (word_end[1] != 0) && ((c2 == 0) || (wtab[0].flags & FLAG_COMMA_AFTER) || IsAlpha(c2))) {
  881. // ordinal number is indicated by dot after the number
  882. // but not if the next word starts with an upper-case letter
  883. // (c2 == 0) is for cases such as, "2.,"
  884. ordinal = 2;
  885. if (word_end[0] == '.')
  886. word_end[0] = ' ';
  887. if ((roman == 0) && (tr->translator_name == L('h', 'u'))) {
  888. // lang=hu don't treat dot as ordinal indicator if the next word is a month name ($alt). It may have a suffix.
  889. nextflags = 0;
  890. if (IsAlpha(c2))
  891. nextflags = TranslateWord(tr, &word_end[2], NULL, NULL);
  892. if ((tr->prev_dict_flags[0] & FLAG_ALT_TRANS) && ((c2 == 0) || (wtab[0].flags & FLAG_COMMA_AFTER) || iswdigit(c2)))
  893. ordinal = 0; // TEST 09.02.10
  894. if (nextflags & FLAG_ALT_TRANS)
  895. ordinal = 0;
  896. if (nextflags & FLAG_ALT3_TRANS) {
  897. if (word[-2] == '-')
  898. ordinal = 0; // e.g. december 2-5. között
  899. if (tr->prev_dict_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT3_TRANS))
  900. ordinal = 0x22;
  901. }
  902. }
  903. }
  904. }
  905. }
  906. return ordinal;
  907. }
  908. static int hu_number_e(const char *word, int thousandplex, int value)
  909. {
  910. // lang-hu: variant form of numbers when followed by hyphen and a suffix starting with 'a' or 'e' (but not a, e, az, ez, azt, ezt, att. ett
  911. if ((word[0] == 'a') || (word[0] == 'e')) {
  912. if ((word[1] == ' ') || (word[1] == 'z') || ((word[1] == 't') && (word[2] == 't')))
  913. return 0;
  914. if (((thousandplex == 1) || ((value % 1000) == 0)) && (word[1] == 'l'))
  915. return 0; // 1000-el
  916. return 1;
  917. }
  918. return 0;
  919. }
  920. int TranslateRoman(Translator *tr, char *word, char *ph_out, WORD_TAB *wtab)
  921. {
  922. int c;
  923. char *p;
  924. const char *p2;
  925. int acc;
  926. int prev;
  927. int value;
  928. int subtract;
  929. int repeat = 0;
  930. int n_digits = 0;
  931. char *word_start;
  932. int num_control = 0;
  933. unsigned int flags[2];
  934. char ph_roman[30];
  935. char number_chars[N_WORD_BYTES];
  936. static const char *roman_numbers = "ixcmvld";
  937. static const int roman_values[] = { 1, 10, 100, 1000, 5, 50, 500 };
  938. acc = 0;
  939. prev = 0;
  940. subtract = 0x7fff;
  941. ph_out[0] = 0;
  942. flags[0] = 0;
  943. flags[1] = 0;
  944. if (((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && !(wtab[0].flags & FLAG_ALL_UPPER)) || IsDigit09(word[-2]))
  945. return 0; // not '2xx'
  946. if (word[1] == ' ') {
  947. if ((tr->langopts.numbers & (NUM_ROMAN_CAPITALS | NUM_ROMAN_ORDINAL | NUM_ORDINAL_DOT)) && (wtab[0].flags & FLAG_HAS_DOT)) {
  948. // allow single letter Roman ordinal followed by dot.
  949. } else
  950. return 0; // only one letter, don't speak as a Roman Number
  951. }
  952. word_start = word;
  953. while ((c = *word++) != ' ') {
  954. if ((p2 = strchr(roman_numbers, c)) == NULL)
  955. return 0;
  956. value = roman_values[p2 - roman_numbers];
  957. if (value == prev) {
  958. repeat++;
  959. if (repeat >= 3)
  960. return 0;
  961. } else
  962. repeat = 0;
  963. if ((prev > 1) && (prev != 10) && (prev != 100)) {
  964. if (value >= prev)
  965. return 0;
  966. }
  967. if ((prev != 0) && (prev < value)) {
  968. if (((acc % 10) != 0) || ((prev*10) < value))
  969. return 0;
  970. subtract = prev;
  971. value -= subtract;
  972. } else if (value >= subtract)
  973. return 0;
  974. else
  975. acc += prev;
  976. prev = value;
  977. n_digits++;
  978. }
  979. if (IsDigit09(word[0]))
  980. return 0; // e.g. 'xx2'
  981. acc += prev;
  982. if (acc < tr->langopts.min_roman)
  983. return 0;
  984. if (acc > tr->langopts.max_roman)
  985. return 0;
  986. Lookup(tr, "_roman", ph_roman); // precede by "roman" if _rom is defined in *_list
  987. p = &ph_out[0];
  988. if ((tr->langopts.numbers & NUM_ROMAN_AFTER) == 0) {
  989. strcpy(ph_out, ph_roman);
  990. p = &ph_out[strlen(ph_roman)];
  991. }
  992. sprintf(number_chars, " %d %s ", acc, tr->langopts.roman_suffix);
  993. if (word[0] == '.') {
  994. // dot has not been removed. This implies that there was no space after it
  995. return 0;
  996. }
  997. if (CheckDotOrdinal(tr, word_start, word, wtab, 1))
  998. wtab[0].flags |= FLAG_ORDINAL;
  999. if (tr->langopts.numbers & NUM_ROMAN_ORDINAL) {
  1000. if (tr->translator_name == L('h', 'u')) {
  1001. if (!(wtab[0].flags & FLAG_ORDINAL)) {
  1002. if ((wtab[0].flags & FLAG_HYPHEN_AFTER) && hu_number_e(word, 0, acc)) {
  1003. // should use the 'e' form of the number
  1004. num_control |= 1;
  1005. } else
  1006. return 0;
  1007. }
  1008. } else
  1009. wtab[0].flags |= FLAG_ORDINAL;
  1010. }
  1011. tr->prev_dict_flags[0] = 0;
  1012. tr->prev_dict_flags[1] = 0;
  1013. TranslateNumber(tr, &number_chars[2], p, flags, wtab, num_control);
  1014. if (tr->langopts.numbers & NUM_ROMAN_AFTER)
  1015. strcat(ph_out, ph_roman);
  1016. return 1;
  1017. }
  1018. static const char *M_Variant(int value)
  1019. {
  1020. // returns M, or perhaps MA or MB for some cases
  1021. bool teens = false;
  1022. if (((value % 100) > 10) && ((value % 100) < 20))
  1023. teens = true;
  1024. switch (translator->langopts.numbers2 & NUM2_THOUSANDS_VAR_BITS)
  1025. {
  1026. case NUM2_THOUSANDS_VAR1: // lang=ru use singular for xx1 except for x11
  1027. if ((teens == false) && ((value % 10) == 1))
  1028. return "1M";
  1029. break;
  1030. case NUM2_THOUSANDS_VAR2: // lang=cs,sk
  1031. if ((value >= 2) && (value <= 4))
  1032. return "0MA";
  1033. break;
  1034. case NUM2_THOUSANDS_VAR3: // lang=pl
  1035. if ((teens == false) && (((value % 10) >= 2) && ((value % 10) <= 4)))
  1036. return "0MA";
  1037. break;
  1038. case NUM2_THOUSANDS_VAR4: // lang=lt
  1039. if ((teens == true) || ((value % 10) == 0))
  1040. return "0MB";
  1041. if ((value % 10) == 1)
  1042. return "0MA";
  1043. break;
  1044. case NUM2_THOUSANDS_VAR5: // lang=bs,hr,sr
  1045. if (teens == false) {
  1046. if ((value % 10) == 1)
  1047. return "1M";
  1048. if (((value % 10) >= 2) && ((value % 10) <= 4))
  1049. return "0MA";
  1050. }
  1051. break;
  1052. }
  1053. return "0M";
  1054. }
  1055. static int LookupThousands(Translator *tr, int value, int thousandplex, int thousands_exact, char *ph_out)
  1056. {
  1057. // thousands_exact: bit 0 no hundreds,tens,or units, bit 1 ordinal numberr
  1058. int found;
  1059. int found_value = 0;
  1060. char string[12];
  1061. char ph_of[12];
  1062. char ph_thousands[40];
  1063. char ph_buf[40];
  1064. ph_of[0] = 0;
  1065. // first look for a match with the exact value of thousands
  1066. if (value > 0) {
  1067. if (thousands_exact & 1) {
  1068. if (thousands_exact & 2) {
  1069. // ordinal number
  1070. sprintf(string, "_%dM%do", value, thousandplex);
  1071. found_value = Lookup(tr, string, ph_thousands);
  1072. }
  1073. if (!found_value && (number_control & 1)) {
  1074. // look for the 'e' variant
  1075. sprintf(string, "_%dM%de", value, thousandplex);
  1076. found_value = Lookup(tr, string, ph_thousands);
  1077. }
  1078. if (!found_value) {
  1079. // is there a different pronunciation if there are no hundreds,tens,or units ? (LANG=ta)
  1080. sprintf(string, "_%dM%dx", value, thousandplex);
  1081. found_value = Lookup(tr, string, ph_thousands);
  1082. }
  1083. }
  1084. if (found_value == 0) {
  1085. sprintf(string, "_%dM%d", value, thousandplex);
  1086. found_value = Lookup(tr, string, ph_thousands);
  1087. }
  1088. }
  1089. if (found_value == 0) {
  1090. if ((value % 100) >= 20)
  1091. Lookup(tr, "_0of", ph_of);
  1092. found = 0;
  1093. if (thousands_exact & 1) {
  1094. if (thousands_exact & 2) {
  1095. // ordinal number
  1096. sprintf(string, "_%s%do", M_Variant(value), thousandplex);
  1097. found = Lookup(tr, string, ph_thousands);
  1098. }
  1099. if (!found && (number_control & 1)) {
  1100. // look for the 'e' variant
  1101. sprintf(string, "_%s%de", M_Variant(value), thousandplex);
  1102. found = Lookup(tr, string, ph_thousands);
  1103. }
  1104. if (!found) {
  1105. // is there a different pronunciation if there are no hundreds,tens,or units ?
  1106. sprintf(string, "_%s%dx", M_Variant(value), thousandplex);
  1107. found = Lookup(tr, string, ph_thousands);
  1108. }
  1109. }
  1110. if (found == 0) {
  1111. sprintf(string, "_%s%d", M_Variant(value), thousandplex);
  1112. if (Lookup(tr, string, ph_thousands) == 0) {
  1113. if (thousandplex > 3) {
  1114. sprintf(string, "_0M%d", thousandplex-1);
  1115. if (Lookup(tr, string, ph_buf) == 0) {
  1116. // say "millions" if this name is not available and neither is the next lower
  1117. Lookup(tr, "_0M2", ph_thousands);
  1118. speak_missing_thousands = 3;
  1119. }
  1120. }
  1121. if (ph_thousands[0] == 0) {
  1122. // repeat "thousand" if higher order names are not available
  1123. sprintf(string, "_%dM1", value);
  1124. if ((found_value = Lookup(tr, string, ph_thousands)) == 0)
  1125. Lookup(tr, "_0M1", ph_thousands);
  1126. speak_missing_thousands = 2;
  1127. }
  1128. }
  1129. }
  1130. }
  1131. sprintf(ph_out, "%s%s", ph_of, ph_thousands);
  1132. if ((value == 1) && (thousandplex == 1) && (tr->langopts.numbers & NUM_OMIT_1_THOUSAND))
  1133. return 1;
  1134. return found_value;
  1135. }
  1136. static int LookupNum2(Translator *tr, int value, int thousandplex, const int control, char *ph_out)
  1137. {
  1138. // Lookup a 2 digit number
  1139. // control bit 0: ordinal number
  1140. // control bit 1: final tens and units (not number of thousands) (use special form of '1', LANG=de "eins")
  1141. // control bit 2: tens and units only, no higher digits
  1142. // control bit 3: use feminine form of '2' (for thousands
  1143. // control bit 4: speak zero tens
  1144. // control bit 5: variant of ordinal number (lang=hu)
  1145. // bit 8 followed by decimal fraction
  1146. // bit 9: use #f form for both tens and units (lang=ml)
  1147. int found;
  1148. int ix;
  1149. int units;
  1150. int tens;
  1151. int is_ordinal;
  1152. int used_and = 0;
  1153. int found_ordinal = 0;
  1154. int next_phtype;
  1155. int ord_type = 'o';
  1156. char string[12]; // for looking up entries in *_list
  1157. char ph_ordinal[20];
  1158. char ph_tens[50];
  1159. char ph_digits[50];
  1160. char ph_and[12];
  1161. units = value % 10;
  1162. tens = value / 10;
  1163. found = 0;
  1164. ph_ordinal[0] = 0;
  1165. ph_tens[0] = 0;
  1166. ph_digits[0] = 0;
  1167. ph_and[0] = 0;
  1168. if (control & 0x20)
  1169. ord_type = 'q';
  1170. is_ordinal = control & 1;
  1171. if ((control & 2) && (n_digit_lookup == 2)) {
  1172. // pronunciation of the final 2 digits has already been found
  1173. strcpy(ph_out, digit_lookup);
  1174. } else {
  1175. if (digit_lookup[0] == 0) {
  1176. // is there a special pronunciation for this 2-digit number
  1177. if (control & 8) {
  1178. // is there a feminine or thousands-variant form?
  1179. sprintf(string, "_%dfx", value);
  1180. if ((found = Lookup(tr, string, ph_digits)) == 0) {
  1181. sprintf(string, "_%df", value);
  1182. found = Lookup(tr, string, ph_digits);
  1183. }
  1184. } else if (is_ordinal) {
  1185. strcpy(ph_ordinal, ph_ordinal2);
  1186. if (control & 4) {
  1187. sprintf(string, "_%d%cx", value, ord_type); // LANG=hu, special word for 1. 2. when there are no higher digits
  1188. if ((found = Lookup(tr, string, ph_digits)) != 0) {
  1189. if (ph_ordinal2x[0] != 0)
  1190. strcpy(ph_ordinal, ph_ordinal2x); // alternate pronunciation (lang=an)
  1191. }
  1192. }
  1193. if (found == 0) {
  1194. sprintf(string, "_%d%c", value, ord_type);
  1195. found = Lookup(tr, string, ph_digits);
  1196. }
  1197. found_ordinal = found;
  1198. }
  1199. if (found == 0) {
  1200. if (control & 2) {
  1201. // the final tens and units of a number
  1202. if (number_control & 1) {
  1203. // look for 'e' variant
  1204. sprintf(string, "_%de", value);
  1205. found = Lookup(tr, string, ph_digits);
  1206. }
  1207. } else {
  1208. // followed by hundreds or thousands etc
  1209. if ((tr->langopts.numbers2 & NUM2_ORDINAL_AND_THOUSANDS) && (thousandplex <= 1))
  1210. sprintf(string, "_%do", value); // LANG=TA
  1211. else
  1212. sprintf(string, "_%da", value);
  1213. found = Lookup(tr, string, ph_digits);
  1214. }
  1215. if (!found) {
  1216. if ((is_ordinal) && (tr->langopts.numbers2 & NUM2_NO_TEEN_ORDINALS)) {
  1217. // don't use numbers 10-99 to make ordinals, always use _1Xo etc (lang=pt)
  1218. } else {
  1219. sprintf(string, "_%d", value);
  1220. found = Lookup(tr, string, ph_digits);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. // no, speak as tens+units
  1226. if ((value < 10) && (control & 0x10)) {
  1227. // speak leading zero
  1228. Lookup(tr, "_0", ph_tens);
  1229. } else {
  1230. if (found)
  1231. ph_tens[0] = 0;
  1232. else {
  1233. if (is_ordinal) {
  1234. sprintf(string, "_%dX%c", tens, ord_type);
  1235. if (Lookup(tr, string, ph_tens) != 0) {
  1236. found_ordinal = 1;
  1237. if ((units != 0) && (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL)) {
  1238. // Use the ordinal form of tens as well as units. Add the ordinal ending
  1239. strcat(ph_tens, ph_ordinal2);
  1240. }
  1241. }
  1242. }
  1243. if (found_ordinal == 0) {
  1244. if (control & 0x200)
  1245. sprintf(string, "_%dXf", tens);
  1246. else
  1247. sprintf(string, "_%dX", tens);
  1248. Lookup(tr, string, ph_tens);
  1249. }
  1250. if ((ph_tens[0] == 0) && (tr->langopts.numbers & NUM_VIGESIMAL)) {
  1251. // tens not found, (for example) 73 is 60+13
  1252. units = (value % 20);
  1253. sprintf(string, "_%dX", tens & 0xfe);
  1254. Lookup(tr, string, ph_tens);
  1255. }
  1256. ph_digits[0] = 0;
  1257. if (units > 0) {
  1258. found = 0;
  1259. if ((control & 2) && (digit_lookup[0] != 0)) {
  1260. // we have an entry for this digit (possibly together with the next word)
  1261. strcpy(ph_digits, digit_lookup);
  1262. found_ordinal = 1;
  1263. ph_ordinal[0] = 0;
  1264. } else {
  1265. if (control & 8) {
  1266. // is there a variant form of this number?
  1267. sprintf(string, "_%df", units);
  1268. found = Lookup(tr, string, ph_digits);
  1269. }
  1270. if ((is_ordinal) && ((tr->langopts.numbers & NUM_SWAP_TENS) == 0)) {
  1271. // ordinal
  1272. sprintf(string, "_%d%c", units, ord_type);
  1273. if ((found = Lookup(tr, string, ph_digits)) != 0)
  1274. found_ordinal = 1;
  1275. }
  1276. if (found == 0) {
  1277. if ((number_control & 1) && (control & 2)) {
  1278. // look for 'e' variant
  1279. sprintf(string, "_%de", units);
  1280. found = Lookup(tr, string, ph_digits);
  1281. } else if (((control & 2) == 0) || ((tr->langopts.numbers & NUM_SWAP_TENS) != 0)) {
  1282. // followed by hundreds or thousands (or tens)
  1283. if ((tr->langopts.numbers2 & NUM2_ORDINAL_AND_THOUSANDS) && (thousandplex <= 1))
  1284. sprintf(string, "_%do", units); // LANG=TA, only for 100s, 1000s
  1285. else
  1286. sprintf(string, "_%da", units);
  1287. found = Lookup(tr, string, ph_digits);
  1288. }
  1289. }
  1290. if (found == 0) {
  1291. sprintf(string, "_%d", units);
  1292. Lookup(tr, string, ph_digits);
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. if ((is_ordinal) && (found_ordinal == 0) && (ph_ordinal[0] == 0)) {
  1299. if ((value >= 20) && (((value % 10) == 0) || (tr->langopts.numbers & NUM_SWAP_TENS)))
  1300. Lookup(tr, "_ord20", ph_ordinal);
  1301. if (ph_ordinal[0] == 0)
  1302. Lookup(tr, "_ord", ph_ordinal);
  1303. }
  1304. if ((tr->langopts.numbers & (NUM_SWAP_TENS | NUM_AND_UNITS)) && (ph_tens[0] != 0) && (ph_digits[0] != 0)) {
  1305. Lookup(tr, "_0and", ph_and);
  1306. if ((is_ordinal) && (tr->langopts.numbers2 & NUM2_ORDINAL_NO_AND))
  1307. ph_and[0] = 0;
  1308. if (tr->langopts.numbers & NUM_SWAP_TENS)
  1309. sprintf(ph_out, "%s%s%s%s", ph_digits, ph_and, ph_tens, ph_ordinal);
  1310. else
  1311. sprintf(ph_out, "%s%s%s%s", ph_tens, ph_and, ph_digits, ph_ordinal);
  1312. used_and = 1;
  1313. } else {
  1314. if (tr->langopts.numbers & NUM_SINGLE_VOWEL) {
  1315. // remove vowel from the end of tens if units starts with a vowel (LANG=Italian)
  1316. if (((ix = strlen(ph_tens)-1) >= 0) && (ph_digits[0] != 0)) {
  1317. if ((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS)
  1318. next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type;
  1319. if ((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL))
  1320. ph_tens[ix] = 0;
  1321. }
  1322. }
  1323. if ((tr->langopts.numbers2 & NUM2_ORDINAL_DROP_VOWEL) && (ph_ordinal[0] != 0)) {
  1324. ix = sprintf(ph_out, "%s%s", ph_tens, ph_digits);
  1325. if ((ix > 0) && (phoneme_tab[(unsigned char)(ph_out[ix-1])]->type == phVOWEL))
  1326. ix--;
  1327. sprintf(&ph_out[ix], "%s", ph_ordinal);
  1328. } else
  1329. sprintf(ph_out, "%s%s%s", ph_tens, ph_digits, ph_ordinal);
  1330. }
  1331. }
  1332. if (tr->langopts.numbers & NUM_SINGLE_STRESS_L) {
  1333. // only one primary stress, on the first part (tens)
  1334. found = 0;
  1335. for (ix = 0; ix < (signed)strlen(ph_out); ix++) {
  1336. if (ph_out[ix] == phonSTRESS_P) {
  1337. if (found)
  1338. ph_out[ix] = phonSTRESS_3;
  1339. else
  1340. found = 1;
  1341. }
  1342. }
  1343. } else if (tr->langopts.numbers & NUM_SINGLE_STRESS) {
  1344. // only one primary stress
  1345. found = 0;
  1346. for (ix = strlen(ph_out)-1; ix >= 0; ix--) {
  1347. if (ph_out[ix] == phonSTRESS_P) {
  1348. if (found)
  1349. ph_out[ix] = phonSTRESS_3;
  1350. else
  1351. found = 1;
  1352. }
  1353. }
  1354. }
  1355. return used_and;
  1356. }
  1357. static int LookupNum3(Translator *tr, int value, char *ph_out, bool suppress_null, int thousandplex, int control)
  1358. {
  1359. // Translate a 3 digit number
  1360. // control bit 0, previous thousands
  1361. // bit 1, ordinal number
  1362. // bit 5 variant form of ordinal number
  1363. // bit 8 followed by decimal fraction
  1364. int found;
  1365. int hundreds;
  1366. int tensunits;
  1367. int x;
  1368. int ix;
  1369. int exact;
  1370. int ordinal;
  1371. int tplex;
  1372. bool say_zero_hundred = false;
  1373. bool say_one_hundred;
  1374. char string[12]; // for looking up entries in **_list
  1375. char buf1[100];
  1376. char buf2[100];
  1377. char ph_100[20];
  1378. char ph_10T[20];
  1379. char ph_digits[50];
  1380. char ph_thousands[50];
  1381. char ph_hundred_and[12];
  1382. char ph_thousand_and[12];
  1383. ordinal = control & 0x22;
  1384. hundreds = value / 100;
  1385. tensunits = value % 100;
  1386. buf1[0] = 0;
  1387. ph_thousands[0] = 0;
  1388. ph_thousand_and[0] = 0;
  1389. if ((tr->langopts.numbers & NUM_ZERO_HUNDRED) && ((control & 1) || (hundreds >= 10)))
  1390. say_zero_hundred = true; // lang=vi
  1391. if ((hundreds > 0) || say_zero_hundred) {
  1392. found = 0;
  1393. if (ordinal && (tensunits == 0)) {
  1394. // ordinal number, with no tens or units
  1395. found = Lookup(tr, "_0Co", ph_100);
  1396. }
  1397. if (found == 0) {
  1398. if (tensunits == 0) {
  1399. // special form for exact hundreds?
  1400. found = Lookup(tr, "_0C0", ph_100);
  1401. }
  1402. if (!found)
  1403. Lookup(tr, "_0C", ph_100);
  1404. }
  1405. if (((tr->langopts.numbers & NUM_1900) != 0) && (hundreds == 19)) {
  1406. // speak numbers such as 1984 as years: nineteen-eighty-four
  1407. } else if (hundreds >= 10) {
  1408. ph_digits[0] = 0;
  1409. exact = 0;
  1410. if ((value % 1000) == 0)
  1411. exact = 1;
  1412. tplex = thousandplex+1;
  1413. if (tr->langopts.numbers2 & NUM2_MYRIADS)
  1414. tplex = 0;
  1415. if (LookupThousands(tr, hundreds / 10, tplex, exact | ordinal, ph_10T) == 0) {
  1416. x = 0;
  1417. if (tr->langopts.numbers2 & (1 << tplex))
  1418. x = 8; // use variant (feminine) for before thousands and millions
  1419. if (tr->translator_name == L('m', 'l'))
  1420. x = 0x208;
  1421. LookupNum2(tr, hundreds/10, thousandplex, x, ph_digits);
  1422. }
  1423. if (tr->langopts.numbers2 & NUM2_SWAP_THOUSANDS)
  1424. sprintf(ph_thousands, "%s%c%s%c", ph_10T, phonEND_WORD, ph_digits, phonEND_WORD);
  1425. else
  1426. sprintf(ph_thousands, "%s%c%s%c", ph_digits, phonEND_WORD, ph_10T, phonEND_WORD);
  1427. hundreds %= 10;
  1428. if ((hundreds == 0) && (say_zero_hundred == false))
  1429. ph_100[0] = 0;
  1430. suppress_null = true;
  1431. control |= 1;
  1432. }
  1433. ph_digits[0] = 0;
  1434. if ((hundreds > 0) || say_zero_hundred) {
  1435. if ((tr->langopts.numbers & NUM_AND_HUNDRED) && ((control & 1) || (ph_thousands[0] != 0)))
  1436. Lookup(tr, "_0and", ph_thousand_and);
  1437. suppress_null = true;
  1438. found = 0;
  1439. if ((ordinal)
  1440. && ((tensunits == 0) || (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL))) {
  1441. // ordinal number
  1442. sprintf(string, "_%dCo", hundreds);
  1443. found = Lookup(tr, string, ph_digits);
  1444. if ((tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL) && (tensunits > 0)) {
  1445. // Use ordinal form of hundreds, as well as for tens and units
  1446. // Add ordinal suffix to the hundreds
  1447. strcat(ph_digits, ph_ordinal2);
  1448. }
  1449. }
  1450. if ((hundreds == 0) && say_zero_hundred)
  1451. Lookup(tr, "_0", ph_digits);
  1452. else {
  1453. if ((hundreds == 1) && (tr->langopts.numbers2 & NUM2_OMIT_1_HUNDRED_ONLY) && ((control & 1) == 0)) {
  1454. // only look for special 100 if there are previous thousands
  1455. } else {
  1456. if ((!found) && (tensunits == 0)) {
  1457. // is there a special pronunciation for exactly n00 ?
  1458. sprintf(string, "_%dC0", hundreds);
  1459. found = Lookup(tr, string, ph_digits);
  1460. }
  1461. if (!found) {
  1462. sprintf(string, "_%dC", hundreds);
  1463. found = Lookup(tr, string, ph_digits); // is there a specific pronunciation for n-hundred ?
  1464. }
  1465. }
  1466. if (found)
  1467. ph_100[0] = 0;
  1468. else {
  1469. say_one_hundred = true;
  1470. if (hundreds == 1) {
  1471. if ((tr->langopts.numbers & NUM_OMIT_1_HUNDRED) != 0)
  1472. say_one_hundred = false;
  1473. }
  1474. if (say_one_hundred == true)
  1475. LookupNum2(tr, hundreds, thousandplex, 0, ph_digits);
  1476. }
  1477. }
  1478. }
  1479. sprintf(buf1, "%s%s%s%s", ph_thousands, ph_thousand_and, ph_digits, ph_100);
  1480. }
  1481. ph_hundred_and[0] = 0;
  1482. if (tensunits > 0) {
  1483. if ((control & 2) && (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL)) {
  1484. // Don't use "and" if we apply ordinal to both hundreds and units
  1485. } else {
  1486. if ((value > 100) || ((control & 1) && (thousandplex == 0))) {
  1487. if ((tr->langopts.numbers & NUM_HUNDRED_AND) || ((tr->langopts.numbers & NUM_HUNDRED_AND_DIGIT) && (tensunits < 10)))
  1488. Lookup(tr, "_0and", ph_hundred_and);
  1489. }
  1490. if ((tr->langopts.numbers & NUM_THOUSAND_AND) && (hundreds == 0) && ((control & 1) || (ph_thousands[0] != 0)))
  1491. Lookup(tr, "_0and", ph_hundred_and);
  1492. }
  1493. }
  1494. buf2[0] = 0;
  1495. if ((tensunits != 0) || (suppress_null == false)) {
  1496. x = 0;
  1497. if (thousandplex == 0) {
  1498. x = 2; // allow "eins" for 1 rather than "ein"
  1499. if (ordinal)
  1500. x = 3; // ordinal number
  1501. if ((value < 100) && !(control & 1))
  1502. x |= 4; // tens and units only, no higher digits
  1503. if (ordinal & 0x20)
  1504. x |= 0x20; // variant form of ordinal number
  1505. } else if (tr->langopts.numbers2 & (1 << thousandplex))
  1506. x = 8; // use variant (feminine) for before thousands and millions
  1507. if ((tr->translator_name == L('m', 'l')) && (thousandplex == 1))
  1508. x |= 0x208; // use #f form for both tens and units
  1509. if ((tr->langopts.numbers2 & NUM2_ZERO_TENS) && ((control & 1) || (hundreds > 0))) {
  1510. // LANG=zh,
  1511. x |= 0x10;
  1512. }
  1513. if (LookupNum2(tr, tensunits, thousandplex, x | (control & 0x100), buf2) != 0) {
  1514. if (tr->langopts.numbers & NUM_SINGLE_AND)
  1515. ph_hundred_and[0] = 0; // don't put 'and' after 'hundred' if there's 'and' between tens and units
  1516. }
  1517. } else {
  1518. if (ph_ordinal2[0] != 0) {
  1519. ix = strlen(buf1);
  1520. if ((ix > 0) && (buf1[ix-1] == phonPAUSE_SHORT))
  1521. buf1[ix-1] = 0; // remove pause before adding ordinal suffix
  1522. strcpy(buf2, ph_ordinal2);
  1523. }
  1524. }
  1525. sprintf(ph_out, "%s%s%c%s", buf1, ph_hundred_and, phonEND_WORD, buf2);
  1526. return 0;
  1527. }
  1528. static bool CheckThousandsGroup(char *word, int group_len)
  1529. {
  1530. // Is this a group of 3 digits which looks like a thousands group?
  1531. int ix;
  1532. for (ix = 0; ix < group_len; ix++) {
  1533. if (!IsDigit09(word[ix]))
  1534. return false;
  1535. }
  1536. if (IsDigit09(word[group_len]) || IsDigit09(word[-1]))
  1537. return false;
  1538. return true;
  1539. }
  1540. static int TranslateNumber_1(Translator *tr, char *word, char *ph_out, unsigned int *flags, WORD_TAB *wtab, int control)
  1541. {
  1542. // Number translation with various options
  1543. // the "word" may be up to 4 digits
  1544. // "words" of 3 digits may be preceded by another number "word" for thousands or millions
  1545. int n_digits;
  1546. int value;
  1547. int ix;
  1548. int digix;
  1549. unsigned char c;
  1550. bool suppress_null = false;
  1551. int decimal_point = 0;
  1552. int thousandplex = 0;
  1553. int thousands_exact = 1;
  1554. int thousands_inc = 0;
  1555. int prev_thousands = 0;
  1556. int ordinal = 0;
  1557. int this_value;
  1558. int decimal_count;
  1559. int max_decimal_count;
  1560. int decimal_mode;
  1561. int suffix_ix;
  1562. int skipwords = 0;
  1563. int group_len;
  1564. int len;
  1565. char *p;
  1566. char string[32]; // for looking up entries in **_list
  1567. char buf1[100];
  1568. char ph_append[50];
  1569. char ph_buf[200];
  1570. char ph_buf2[50];
  1571. char ph_zeros[50];
  1572. char suffix[30]; // string[] must be long enough for sizeof(suffix)+2
  1573. char buf_digit_lookup[50];
  1574. static const char str_pause[2] = { phonPAUSE_NOLINK, 0 };
  1575. *flags = 0;
  1576. n_digit_lookup = 0;
  1577. buf_digit_lookup[0] = 0;
  1578. digit_lookup = buf_digit_lookup;
  1579. number_control = control;
  1580. for (ix = 0; IsDigit09(word[ix]); ix++) ;
  1581. n_digits = ix;
  1582. value = this_value = atoi(word);
  1583. group_len = 3;
  1584. if (tr->langopts.numbers2 & NUM2_MYRIADS)
  1585. group_len = 4;
  1586. // is there a previous thousands part (as a previous "word") ?
  1587. if ((n_digits == group_len) && (word[-2] == tr->langopts.thousands_sep) && IsDigit09(word[-3]))
  1588. prev_thousands = 1;
  1589. else if ((tr->langopts.thousands_sep == ' ') || (tr->langopts.numbers & NUM_ALLOW_SPACE)) {
  1590. // thousands groups can be separated by spaces
  1591. if ((n_digits == 3) && !(wtab->flags & FLAG_MULTIPLE_SPACES) && IsDigit09(word[-2]))
  1592. prev_thousands = 1;
  1593. }
  1594. if (prev_thousands == 0)
  1595. speak_missing_thousands = 0;
  1596. ph_ordinal2[0] = 0;
  1597. ph_zeros[0] = 0;
  1598. if (prev_thousands || (word[0] != '0')) {
  1599. // don't check for ordinal if the number has a leading zero
  1600. ordinal = CheckDotOrdinal(tr, word, &word[ix], wtab, 0);
  1601. }
  1602. if ((word[ix] == '.') && !IsDigit09(word[ix+1]) && !IsDigit09(word[ix+2]) && !(wtab[1].flags & FLAG_NOSPACE)) {
  1603. // remove dot unless followed by another number
  1604. word[ix] = 0;
  1605. }
  1606. if ((ordinal == 0) || (tr->translator_name == L('h', 'u'))) {
  1607. // NOTE lang=hu, allow both dot and ordinal suffix, eg. "december 21.-én"
  1608. // look for an ordinal number suffix after the number
  1609. ix++;
  1610. p = suffix;
  1611. if (wtab[0].flags & FLAG_HYPHEN_AFTER) {
  1612. *p++ = '-';
  1613. ix++;
  1614. }
  1615. while ((word[ix] != 0) && (word[ix] != ' ') && (ix < (int)(sizeof(suffix)-1)))
  1616. *p++ = word[ix++];
  1617. *p = 0;
  1618. if (suffix[0] != 0) {
  1619. if ((tr->langopts.ordinal_indicator != NULL) && (strcmp(suffix, tr->langopts.ordinal_indicator) == 0))
  1620. ordinal = 2;
  1621. else if (!IsDigit09(suffix[0])) { // not _#9 (tab)
  1622. sprintf(string, "_#%s", suffix);
  1623. if (Lookup(tr, string, ph_ordinal2)) {
  1624. // this is an ordinal suffix
  1625. ordinal = 2;
  1626. flags[0] |= FLAG_SKIPWORDS;
  1627. skipwords = 1;
  1628. sprintf(string, "_x#%s", suffix);
  1629. Lookup(tr, string, ph_ordinal2x); // is there an alternate pronunciation?
  1630. }
  1631. }
  1632. }
  1633. }
  1634. if (wtab[0].flags & FLAG_ORDINAL)
  1635. ordinal = 2;
  1636. ph_append[0] = 0;
  1637. ph_buf2[0] = 0;
  1638. if ((word[0] == '0') && (prev_thousands == 0) && (word[1] != ' ') && (word[1] != tr->langopts.decimal_sep)) {
  1639. if ((n_digits == 2) && (word[3] == ':') && IsDigit09(word[5]) && isspace(word[7])) {
  1640. // looks like a time 02:30, omit the leading zero
  1641. } else {
  1642. if (n_digits > 3) {
  1643. flags[0] &= ~FLAG_SKIPWORDS;
  1644. return 0; // long number string with leading zero, speak as individual digits
  1645. }
  1646. // speak leading zeros
  1647. for (ix = 0; (word[ix] == '0') && (ix < (n_digits-1)); ix++)
  1648. Lookup(tr, "_0", &ph_zeros[strlen(ph_zeros)]);
  1649. }
  1650. }
  1651. if ((tr->langopts.numbers & NUM_ALLOW_SPACE) && (word[n_digits] == ' '))
  1652. thousands_inc = 1;
  1653. else if (word[n_digits] == tr->langopts.thousands_sep)
  1654. thousands_inc = 2;
  1655. suffix_ix = n_digits+2;
  1656. if (thousands_inc > 0) {
  1657. // if the following "words" are three-digit groups, count them and add
  1658. // a "thousand"/"million" suffix to this one
  1659. digix = n_digits + thousands_inc;
  1660. while (((wtab[thousandplex+1].flags & FLAG_MULTIPLE_SPACES) == 0) && CheckThousandsGroup(&word[digix], group_len)) {
  1661. for (ix = 0; ix < group_len; ix++) {
  1662. if (word[digix+ix] != '0') {
  1663. thousands_exact = 0;
  1664. break;
  1665. }
  1666. }
  1667. thousandplex++;
  1668. digix += group_len;
  1669. if ((word[digix] == tr->langopts.thousands_sep) || ((tr->langopts.numbers & NUM_ALLOW_SPACE) && (word[digix] == ' '))) {
  1670. suffix_ix = digix+2;
  1671. digix += thousands_inc;
  1672. } else
  1673. break;
  1674. }
  1675. }
  1676. if ((value == 0) && prev_thousands)
  1677. suppress_null = true;
  1678. if (tr->translator_name == L('h', 'u')) {
  1679. // variant form of numbers when followed by hyphen and a suffix starting with 'a' or 'e' (but not a, e, az, ez, azt, ezt
  1680. if ((wtab[thousandplex].flags & FLAG_HYPHEN_AFTER) && (thousands_exact == 1) && hu_number_e(&word[suffix_ix], thousandplex, value))
  1681. number_control |= 1; // use _1e variant of number
  1682. }
  1683. if ((word[n_digits] == tr->langopts.decimal_sep) && IsDigit09(word[n_digits+1])) {
  1684. // this "word" ends with a decimal point
  1685. Lookup(tr, "_dpt", ph_append);
  1686. decimal_point = 0x100;
  1687. } else if (suppress_null == false) {
  1688. if (thousands_inc > 0) {
  1689. if (thousandplex > 0) {
  1690. if ((suppress_null == false) && (LookupThousands(tr, value, thousandplex, thousands_exact, ph_append))) {
  1691. // found an exact match for N thousand
  1692. value = 0;
  1693. suppress_null = true;
  1694. }
  1695. }
  1696. }
  1697. } else if (speak_missing_thousands == 1) {
  1698. // speak this thousandplex if there was no word for the previous thousandplex
  1699. sprintf(string, "_0M%d", thousandplex+1);
  1700. if (Lookup(tr, string, buf1) == 0) {
  1701. sprintf(string, "_0M%d", thousandplex);
  1702. Lookup(tr, string, ph_append);
  1703. }
  1704. }
  1705. if ((ph_append[0] == 0) && (word[n_digits] == '.') && (thousandplex == 0))
  1706. Lookup(tr, "_.", ph_append);
  1707. if (thousandplex == 0) {
  1708. char *p2;
  1709. // look for combinations of the number with the next word
  1710. p = word;
  1711. while (IsDigit09(p[1])) p++; // just use the last digit
  1712. if (IsDigit09(p[-1])) {
  1713. p2 = p - 1;
  1714. if (LookupDictList(tr, &p2, buf_digit_lookup, flags, FLAG_SUFX, wtab)) // lookup 2 digits
  1715. n_digit_lookup = 2;
  1716. }
  1717. if ((buf_digit_lookup[0] == 0) && (*p != '0')) {
  1718. // LANG=hu ?
  1719. // not found, lookup only the last digit (?? but not if dot-ordinal has been found)
  1720. if (LookupDictList(tr, &p, buf_digit_lookup, flags, FLAG_SUFX, wtab)) // don't match '0', or entries with $only
  1721. n_digit_lookup = 1;
  1722. }
  1723. if (prev_thousands == 0) {
  1724. if ((decimal_point == 0) && (ordinal == 0)) {
  1725. // Look for special pronunciation for this number in isolation (LANG=kl)
  1726. sprintf(string, "_%dn", value);
  1727. if (Lookup(tr, string, ph_out))
  1728. return 1;
  1729. }
  1730. if (tr->langopts.numbers2 & NUM2_PERCENT_BEFORE) {
  1731. // LANG=si, say "percent" before the number
  1732. p2 = word;
  1733. while ((*p2 != ' ') && (*p2 != 0))
  1734. p2++;
  1735. if (p2[1] == '%') {
  1736. Lookup(tr, "%", ph_out);
  1737. ph_out += strlen(ph_out);
  1738. p2[1] = ' ';
  1739. }
  1740. }
  1741. }
  1742. }
  1743. LookupNum3(tr, value, ph_buf, suppress_null, thousandplex, prev_thousands | ordinal | decimal_point);
  1744. if ((thousandplex > 0) && (tr->langopts.numbers2 & NUM2_SWAP_THOUSANDS))
  1745. sprintf(ph_out, "%s%s%c%s%s", ph_zeros, ph_append, phonEND_WORD, ph_buf2, ph_buf);
  1746. else
  1747. sprintf(ph_out, "%s%s%s%c%s", ph_zeros, ph_buf2, ph_buf, phonEND_WORD, ph_append);
  1748. while (decimal_point) {
  1749. n_digits++;
  1750. decimal_count = 0;
  1751. while (IsDigit09(word[n_digits+decimal_count]))
  1752. decimal_count++;
  1753. max_decimal_count = 2;
  1754. switch (decimal_mode = (tr->langopts.numbers & NUM_DFRACTION_BITS))
  1755. {
  1756. case NUM_DFRACTION_4:
  1757. max_decimal_count = 5;
  1758. // fallthrough:
  1759. case NUM_DFRACTION_2:
  1760. // French/Polish decimal fraction
  1761. while (word[n_digits] == '0') {
  1762. Lookup(tr, "_0", buf1);
  1763. strcat(ph_out, buf1);
  1764. decimal_count--;
  1765. n_digits++;
  1766. }
  1767. if ((decimal_count <= max_decimal_count) && IsDigit09(word[n_digits])) {
  1768. LookupNum3(tr, atoi(&word[n_digits]), buf1, false, 0, 0);
  1769. strcat(ph_out, buf1);
  1770. n_digits += decimal_count;
  1771. }
  1772. break;
  1773. case NUM_DFRACTION_1: // italian, say "hundredths" if leading zero
  1774. case NUM_DFRACTION_5: // hungarian, always say "tenths" etc.
  1775. case NUM_DFRACTION_6: // kazakh, always say "tenths" etc, before the decimal fraction
  1776. LookupNum3(tr, atoi(&word[n_digits]), ph_buf, false, 0, 0);
  1777. if ((word[n_digits] == '0') || (decimal_mode != NUM_DFRACTION_1)) {
  1778. // decimal part has leading zeros, so add a "hundredths" or "thousandths" suffix
  1779. sprintf(string, "_0Z%d", decimal_count);
  1780. if (Lookup(tr, string, buf1) == 0)
  1781. break; // revert to speaking single digits
  1782. if (decimal_mode == NUM_DFRACTION_6)
  1783. strcat(ph_out, buf1);
  1784. else
  1785. strcat(ph_buf, buf1);
  1786. }
  1787. strcat(ph_out, ph_buf);
  1788. n_digits += decimal_count;
  1789. break;
  1790. case NUM_DFRACTION_3:
  1791. // Romanian decimal fractions
  1792. if ((decimal_count <= 4) && (word[n_digits] != '0')) {
  1793. LookupNum3(tr, atoi(&word[n_digits]), buf1, false, 0, 0);
  1794. strcat(ph_out, buf1);
  1795. n_digits += decimal_count;
  1796. }
  1797. break;
  1798. case NUM_DFRACTION_7:
  1799. // alternative form of decimal fraction digits, except the final digit
  1800. while (decimal_count-- > 1) {
  1801. sprintf(string, "_%cd", word[n_digits]);
  1802. if (Lookup(tr, string, buf1) == 0)
  1803. break;
  1804. n_digits++;
  1805. strcat(ph_out, buf1);
  1806. }
  1807. }
  1808. while (IsDigit09(c = word[n_digits]) && (strlen(ph_out) < (N_WORD_PHONEMES - 10))) {
  1809. // speak any remaining decimal fraction digits individually
  1810. value = word[n_digits++] - '0';
  1811. LookupNum2(tr, value, 0, 2, buf1);
  1812. len = strlen(ph_out);
  1813. sprintf(&ph_out[len], "%c%s", phonEND_WORD, buf1);
  1814. }
  1815. // something after the decimal part ?
  1816. if (Lookup(tr, "_dpt2", buf1))
  1817. strcat(ph_out, buf1);
  1818. if ((c == tr->langopts.decimal_sep) && IsDigit09(word[n_digits+1])) {
  1819. Lookup(tr, "_dpt", buf1);
  1820. strcat(ph_out, buf1);
  1821. } else
  1822. decimal_point = 0;
  1823. }
  1824. if ((ph_out[0] != 0) && (ph_out[0] != phonSWITCH)) {
  1825. int next_char;
  1826. char *p;
  1827. p = &word[n_digits+1];
  1828. p += utf8_in(&next_char, p);
  1829. if ((tr->langopts.numbers & NUM_NOPAUSE) && (next_char == ' '))
  1830. utf8_in(&next_char, p);
  1831. if (!iswalpha(next_char) && (thousands_exact == 0))
  1832. strcat(ph_out, str_pause); // don't add pause for 100s, 6th, etc.
  1833. }
  1834. *flags |= FLAG_FOUND;
  1835. speak_missing_thousands--;
  1836. if (skipwords)
  1837. dictionary_skipwords = skipwords;
  1838. return 1;
  1839. }
  1840. int TranslateNumber(Translator *tr, char *word1, char *ph_out, unsigned int *flags, WORD_TAB *wtab, int control)
  1841. {
  1842. if ((option_sayas == SAYAS_DIGITS1) || (wtab[0].flags & FLAG_INDIVIDUAL_DIGITS))
  1843. return 0; // speak digits individually
  1844. if (tr->langopts.numbers != 0)
  1845. return TranslateNumber_1(tr, word1, ph_out, flags, wtab, control);
  1846. return 0;
  1847. }