FontData.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements the FontData class for character bbox data
  19. * and stretchy delimiters.
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. import {OptionList, defaultOptions, userOptions} from '../../util/Options.js';
  24. import {StyleList} from '../../util/StyleList.js';
  25. /****************************************************************************/
  26. /**
  27. * The extra options allowed in a CharData array
  28. */
  29. export interface CharOptions {
  30. ic?: number; // italic correction value
  31. sk?: number; // skew value
  32. dx?: number; // offset for combining characters
  33. unknown?: boolean; // true if not found in the given variant
  34. smp?: number; // Math Alphanumeric codepoint this char is mapped to
  35. }
  36. /****************************************************************************/
  37. /**
  38. * Data about a character
  39. * [height, depth, width, {italic-correction, skew, options}]
  40. *
  41. * @template C The CharOptions type
  42. */
  43. export type CharData<C extends CharOptions> =
  44. [number, number, number] |
  45. [number, number, number, C];
  46. /**
  47. * An object making character positions to character data
  48. *
  49. * @template C The CharOptions type
  50. */
  51. export type CharMap<C extends CharOptions> = {
  52. [n: number]: CharData<C>;
  53. };
  54. /**
  55. * An object making variants to character maps
  56. *
  57. * @template C The CharOptions type
  58. */
  59. export type CharMapMap<C extends CharOptions> = {
  60. [name: string]: CharMap<C>;
  61. };
  62. /****************************************************************************/
  63. /**
  64. * Data for a variant
  65. *
  66. * @template C The CharOptions type
  67. */
  68. export interface VariantData<C extends CharOptions> {
  69. /**
  70. * A list of CharMaps that must be updated when characters are
  71. * added to this variant
  72. */
  73. linked: CharMap<C>[];
  74. /**
  75. * The character data for this variant
  76. */
  77. chars: CharMap<C>;
  78. }
  79. /**
  80. * An object making variants names to variant data
  81. *
  82. * @template C The CharOptions type
  83. * @template V The VariantData type
  84. */
  85. export type VariantMap<C extends CharOptions, V extends VariantData<C>> = {
  86. [name: string]: V;
  87. };
  88. /**
  89. * Data to use to map unknown characters in a variant to a
  90. * generic CSS font:
  91. *
  92. * [fontname, italic, bold]
  93. */
  94. export type CssFontData = [string, boolean, boolean];
  95. /**
  96. * An object mapping a variant name to the CSS data needed for it
  97. */
  98. export type CssFontMap = {
  99. [name: string]: CssFontData;
  100. };
  101. /****************************************************************************/
  102. /**
  103. * Stretchy delimiter data
  104. */
  105. export const enum DIRECTION {None, Vertical, Horizontal}
  106. export const V = DIRECTION.Vertical;
  107. export const H = DIRECTION.Horizontal;
  108. /****************************************************************************/
  109. /**
  110. * Data needed for stretchy vertical and horizontal characters
  111. */
  112. export type DelimiterData = {
  113. dir: DIRECTION; // vertical or horizontal direction
  114. sizes?: number[]; // Array of fixed sizes for this character
  115. variants?: number[]; // The variants in which the different sizes can be found (if not the default)
  116. schar?: number[]; // The character number to use for each size (if different from the default)
  117. stretch?: number[]; // The unicode code points for the parts of multi-character versions [beg, ext, end, mid?]
  118. stretchv?: number[]; // the variants to use for the stretchy characters (index into variant name array)
  119. HDW?: number[]; // [h, d, w] (for vertical, h and d are the normal size, w is the multi-character width,
  120. // for horizontal, h and d are the multi-character ones, w is for the normal size).
  121. min?: number; // The minimum size a multi-character version can be
  122. c?: number; // The character number (for aliased delimiters)
  123. fullExt?: [number, number] // When present, extenders must be full sized, and the first number is
  124. // the size of the extender, while the second is the total size of the ends
  125. };
  126. /**
  127. * An object mapping character numbers to delimiter data
  128. *
  129. * @template D The DelimiterData type
  130. */
  131. export type DelimiterMap<D extends DelimiterData> = {
  132. [n: number]: D;
  133. };
  134. /**
  135. * Delimiter data for a non-stretchy character
  136. */
  137. export const NOSTRETCH: DelimiterData = {dir: DIRECTION.None};
  138. /****************************************************************************/
  139. /**
  140. * Data for remapping characters
  141. */
  142. export type RemapData = string;
  143. export type RemapMap = {
  144. [key: number]: RemapData;
  145. };
  146. export type RemapMapMap = {
  147. [key: string]: RemapMap;
  148. };
  149. /**
  150. * Character remapping data for Math Alphanumerics
  151. */
  152. export type SmpMap = {
  153. [c: number]: number;
  154. };
  155. /**
  156. * Data for Math Alphanumeric conversion: starting positions for
  157. * [Alpha, alpha, Greek, greek, Numbers]
  158. */
  159. export type SmpData = [number, number, number?, number?, number?];
  160. /****************************************************************************/
  161. /**
  162. * Font parameters (for TeX typesetting rules)
  163. */
  164. export type FontParameters = {
  165. x_height: number,
  166. quad: number,
  167. num1: number,
  168. num2: number,
  169. num3: number,
  170. denom1: number,
  171. denom2: number,
  172. sup1: number,
  173. sup2: number,
  174. sup3: number,
  175. sub1: number,
  176. sub2: number,
  177. sup_drop: number,
  178. sub_drop: number,
  179. delim1: number,
  180. delim2: number,
  181. axis_height: number,
  182. rule_thickness: number,
  183. big_op_spacing1: number,
  184. big_op_spacing2: number,
  185. big_op_spacing3: number,
  186. big_op_spacing4: number,
  187. big_op_spacing5: number,
  188. surd_height: number,
  189. scriptspace: number,
  190. nulldelimiterspace: number,
  191. delimiterfactor: number,
  192. delimitershortfall: number,
  193. min_rule_thickness: number,
  194. separation_factor: number,
  195. extra_ic: number
  196. };
  197. /****************************************************************************/
  198. /**
  199. * The FontData class (for storing character bounding box data by variant,
  200. * and the stretchy delimiter data).
  201. *
  202. * @template C The CharOptions type
  203. * @template V The VariantData type
  204. * @template D The DelimiterData type
  205. */
  206. export class FontData<C extends CharOptions, V extends VariantData<C>, D extends DelimiterData> {
  207. /**
  208. * Options for the font
  209. */
  210. public static OPTIONS: OptionList = {
  211. unknownFamily: 'serif' // Should use 'monospace' with LiteAdaptor
  212. };
  213. /**
  214. * The name of the output jax this font data is for (used by extensions)
  215. */
  216. public static JAX: string = 'common';
  217. /**
  218. * The name of the font that is being defined (used by extensions)
  219. */
  220. public static NAME: string = '';
  221. /**
  222. * The standard variants to define
  223. */
  224. public static defaultVariants = [
  225. ['normal'],
  226. ['bold', 'normal'],
  227. ['italic', 'normal'],
  228. ['bold-italic', 'italic', 'bold'],
  229. ['double-struck', 'bold'],
  230. ['fraktur', 'normal'],
  231. ['bold-fraktur', 'bold', 'fraktur'],
  232. ['script', 'italic'],
  233. ['bold-script', 'bold-italic', 'script'],
  234. ['sans-serif', 'normal'],
  235. ['bold-sans-serif', 'bold', 'sans-serif'],
  236. ['sans-serif-italic', 'italic', 'sans-serif'],
  237. ['sans-serif-bold-italic', 'bold-italic', 'bold-sans-serif'],
  238. ['monospace', 'normal']
  239. ];
  240. /**
  241. * The family, style, and weight to use for each variant (for unknown characters)
  242. * The 'unknown' family is replaced by options.unknownFamily
  243. */
  244. public static defaultCssFonts: CssFontMap = {
  245. normal: ['unknown', false, false],
  246. bold: ['unknown', false, true],
  247. italic: ['unknown', true, false],
  248. 'bold-italic': ['unknown', true, true],
  249. 'double-struck': ['unknown', false, true],
  250. fraktur: ['unknown', false, false],
  251. 'bold-fraktur': ['unknown', false, true],
  252. script: ['cursive', false, false],
  253. 'bold-script': ['cursive', false, true],
  254. 'sans-serif': ['sans-serif', false, false],
  255. 'bold-sans-serif': ['sans-serif', false, true],
  256. 'sans-serif-italic': ['sans-serif', true, false],
  257. 'sans-serif-bold-italic': ['sans-serif', true, true],
  258. monospace: ['monospace', false, false]
  259. };
  260. /**
  261. * The default prefix for explicit font-family settings
  262. */
  263. protected static defaultCssFamilyPrefix = '';
  264. /**
  265. * Variant locations in the Math Alphabnumerics block:
  266. * [upper-alpha, lower-alpha, upper-Greek, lower-Greek, numbers]
  267. */
  268. public static VariantSmp: {[name: string]: SmpData} = {
  269. bold: [0x1D400, 0x1D41A, 0x1D6A8, 0x1D6C2, 0x1D7CE],
  270. italic: [0x1D434, 0x1D44E, 0x1D6E2, 0x1D6FC],
  271. 'bold-italic': [0x1D468, 0x1D482, 0x1D71C, 0x1D736],
  272. script: [0x1D49C, 0x1D4B6],
  273. 'bold-script': [0x1D4D0, 0x1D4EA],
  274. fraktur: [0x1D504, 0x1D51E],
  275. 'double-struck': [0x1D538, 0x1D552, , , 0x1D7D8],
  276. 'bold-fraktur': [0x1D56C, 0x1D586],
  277. 'sans-serif': [0x1D5A0, 0x1D5BA, , , 0x1D7E2],
  278. 'bold-sans-serif': [0x1D5D4, 0x1D5EE, 0x1D756, 0x1D770, 0x1D7EC],
  279. 'sans-serif-italic': [0x1D608, 0x1D622],
  280. 'sans-serif-bold-italic': [0x1D63C, 0x1D656, 0x1D790, 0x1D7AA],
  281. 'monospace': [0x1D670, 0x1D68A, , , 0x1D7F6]
  282. };
  283. /**
  284. * Character ranges to remap into Math Alphanumerics
  285. */
  286. public static SmpRanges = [
  287. [0, 0x41, 0x5A], // Upper-case alpha
  288. [1, 0x61, 0x7A], // Lower-case alpha
  289. [2, 0x391, 0x3A9], // Upper-case Greek
  290. [3, 0x3B1, 0x3C9], // Lower-case Greek
  291. [4, 0x30, 0x39] // Numbers
  292. ];
  293. /**
  294. * Characters to map back top other Unicode positions
  295. * (holes in the Math Alphanumeric ranges)
  296. */
  297. public static SmpRemap: SmpMap = {
  298. 0x1D455: 0x210E, // PLANCK CONSTANT
  299. 0x1D49D: 0x212C, // SCRIPT CAPITAL B
  300. 0x1D4A0: 0x2130, // SCRIPT CAPITAL E
  301. 0x1D4A1: 0x2131, // SCRIPT CAPITAL F
  302. 0x1D4A3: 0x210B, // SCRIPT CAPITAL H
  303. 0x1D4A4: 0x2110, // SCRIPT CAPITAL I
  304. 0x1D4A7: 0x2112, // SCRIPT CAPITAL L
  305. 0x1D4A8: 0x2133, // SCRIPT CAPITAL M
  306. 0x1D4AD: 0x211B, // SCRIPT CAPITAL R
  307. 0x1D4BA: 0x212F, // SCRIPT SMALL E
  308. 0x1D4BC: 0x210A, // SCRIPT SMALL G
  309. 0x1D4C4: 0x2134, // SCRIPT SMALL O
  310. 0x1D506: 0x212D, // BLACK-LETTER CAPITAL C
  311. 0x1D50B: 0x210C, // BLACK-LETTER CAPITAL H
  312. 0x1D50C: 0x2111, // BLACK-LETTER CAPITAL I
  313. 0x1D515: 0x211C, // BLACK-LETTER CAPITAL R
  314. 0x1D51D: 0x2128, // BLACK-LETTER CAPITAL Z
  315. 0x1D53A: 0x2102, // DOUBLE-STRUCK CAPITAL C
  316. 0x1D53F: 0x210D, // DOUBLE-STRUCK CAPITAL H
  317. 0x1D545: 0x2115, // DOUBLE-STRUCK CAPITAL N
  318. 0x1D547: 0x2119, // DOUBLE-STRUCK CAPITAL P
  319. 0x1D548: 0x211A, // DOUBLE-STRUCK CAPITAL Q
  320. 0x1D549: 0x211D, // DOUBLE-STRUCK CAPITAL R
  321. 0x1D551: 0x2124, // DOUBLE-STRUCK CAPITAL Z
  322. };
  323. /**
  324. * Greek upper-case variants
  325. */
  326. public static SmpRemapGreekU: SmpMap = {
  327. 0x2207: 0x19, // nabla
  328. 0x03F4: 0x11 // theta symbol
  329. };
  330. /**
  331. * Greek lower-case variants
  332. */
  333. public static SmpRemapGreekL: SmpMap = {
  334. 0x3D1: 0x1B, // theta symbol
  335. 0x3D5: 0x1D, // phi symbol
  336. 0x3D6: 0x1F, // omega symbol
  337. 0x3F0: 0x1C, // kappa symbol
  338. 0x3F1: 0x1E, // rho symbol
  339. 0x3F5: 0x1A, // lunate epsilon symbol
  340. 0x2202: 0x19 // partial differential
  341. };
  342. /**
  343. * The default remappings
  344. */
  345. protected static defaultAccentMap: RemapMap = {
  346. 0x0300: '\u02CB', // grave accent
  347. 0x0301: '\u02CA', // acute accent
  348. 0x0302: '\u02C6', // curcumflex
  349. 0x0303: '\u02DC', // tilde accent
  350. 0x0304: '\u02C9', // macron
  351. 0x0306: '\u02D8', // breve
  352. 0x0307: '\u02D9', // dot
  353. 0x0308: '\u00A8', // diaresis
  354. 0x030A: '\u02DA', // ring above
  355. 0x030C: '\u02C7', // caron
  356. 0x2192: '\u20D7',
  357. 0x2032: '\'',
  358. 0x2033: '\'\'',
  359. 0x2034: '\'\'\'',
  360. 0x2035: '`',
  361. 0x2036: '``',
  362. 0x2037: '```',
  363. 0x2057: '\'\'\'\'',
  364. 0x20D0: '\u21BC', // combining left harpoon
  365. 0x20D1: '\u21C0', // combining right harpoon
  366. 0x20D6: '\u2190', // combining left arrow
  367. 0x20E1: '\u2194', // combining left-right arrow
  368. 0x20F0: '*', // combining asterisk
  369. 0x20DB: '...', // combining three dots above
  370. 0x20DC: '....', // combining four dots above
  371. 0x20EC: '\u21C1', // combining low left harpoon
  372. 0x20ED: '\u21BD', // combining low right harpoon
  373. 0x20EE: '\u2190', // combining low left arrows
  374. 0x20EF: '\u2192' // combining low right arrows
  375. };
  376. /**
  377. * Default map for characters inside <mo>
  378. */
  379. protected static defaultMoMap: RemapMap = {
  380. 0x002D: '\u2212' // hyphen
  381. };
  382. /**
  383. * Default map for characters inside <mn>
  384. */
  385. protected static defaultMnMap: RemapMap = {
  386. 0x002D: '\u2212' // hyphen
  387. };
  388. /**
  389. * The default font parameters for the font
  390. */
  391. public static defaultParams: FontParameters = {
  392. x_height: .442,
  393. quad: 1,
  394. num1: .676,
  395. num2: .394,
  396. num3: .444,
  397. denom1: .686,
  398. denom2: .345,
  399. sup1: .413,
  400. sup2: .363,
  401. sup3: .289,
  402. sub1: .15,
  403. sub2: .247,
  404. sup_drop: .386,
  405. sub_drop: .05,
  406. delim1: 2.39,
  407. delim2: 1.0,
  408. axis_height: .25,
  409. rule_thickness: .06,
  410. big_op_spacing1: .111,
  411. big_op_spacing2: .167,
  412. big_op_spacing3: .2,
  413. big_op_spacing4: .6,
  414. big_op_spacing5: .1,
  415. surd_height: .075,
  416. scriptspace: .05,
  417. nulldelimiterspace: .12,
  418. delimiterfactor: 901,
  419. delimitershortfall: .3,
  420. min_rule_thickness: 1.25, // in pixels
  421. separation_factor: 1.75, // expansion factor for spacing e.g. between accents and base
  422. extra_ic: .033 // extra spacing for scripts (compensate for not having actual ic values)
  423. };
  424. /**
  425. * The default delimiter data
  426. */
  427. protected static defaultDelimiters: DelimiterMap<any> = {};
  428. /**
  429. * The default character data
  430. */
  431. protected static defaultChars: CharMapMap<any> = {};
  432. /**
  433. * The default variants for the fixed size stretchy delimiters
  434. */
  435. protected static defaultSizeVariants: string[] = [];
  436. /**
  437. * The default variants for the assembly parts for stretchy delimiters
  438. */
  439. protected static defaultStretchVariants: string[] = [];
  440. /**
  441. * The font options
  442. */
  443. protected options: OptionList;
  444. /**
  445. * The actual variant information for this font
  446. */
  447. protected variant: VariantMap<C, V> = {};
  448. /**
  449. * The actual delimiter information for this font
  450. */
  451. protected delimiters: DelimiterMap<D> = {};
  452. /**
  453. * The actual size variants to use for this font
  454. */
  455. protected sizeVariants: string[];
  456. /**
  457. * The actual stretchy variants to use for this font
  458. */
  459. protected stretchVariants: string[];
  460. /**
  461. * The data to use to make variants to default fonts and css for unknown characters
  462. */
  463. protected cssFontMap: CssFontMap = {};
  464. /**
  465. * A prefix to use for explicit font-family CSS settings
  466. */
  467. public cssFamilyPrefix: string;
  468. /**
  469. * The character maps
  470. */
  471. protected remapChars: RemapMapMap = {};
  472. /**
  473. * The actual font parameters for this font
  474. */
  475. public params: FontParameters;
  476. /**
  477. * Factor by which to multiply italic correction for computation of delta in munderover
  478. */
  479. public skewIcFactor: number = .75;
  480. /**
  481. * Any styles needed for the font
  482. */
  483. protected _styles: StyleList;
  484. /**
  485. * @param {CharMap} font The font to check
  486. * @param {number} n The character to get options for
  487. * @return {CharOptions} The options for the character
  488. */
  489. public static charOptions(font: CharMap<CharOptions>, n: number): CharOptions {
  490. const char = font[n];
  491. if (char.length === 3) {
  492. (char as any)[3] = {};
  493. }
  494. return char[3];
  495. }
  496. /**
  497. * Copies the data from the defaults to the instance
  498. *
  499. * @param {OptionList} options The options for this font
  500. *
  501. * @constructor
  502. */
  503. constructor(options: OptionList = null) {
  504. let CLASS = (this.constructor as typeof FontData);
  505. this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
  506. this.params = {...CLASS.defaultParams};
  507. this.sizeVariants = [...CLASS.defaultSizeVariants];
  508. this.stretchVariants = [...CLASS.defaultStretchVariants];
  509. this.cssFontMap = {...CLASS.defaultCssFonts};
  510. for (const name of Object.keys(this.cssFontMap)) {
  511. if (this.cssFontMap[name][0] === 'unknown') {
  512. this.cssFontMap[name][0] = this.options.unknownFamily;
  513. }
  514. }
  515. this.cssFamilyPrefix = CLASS.defaultCssFamilyPrefix;
  516. this.createVariants(CLASS.defaultVariants);
  517. this.defineDelimiters(CLASS.defaultDelimiters);
  518. for (const name of Object.keys(CLASS.defaultChars)) {
  519. this.defineChars(name, CLASS.defaultChars[name]);
  520. }
  521. this.defineRemap('accent', CLASS.defaultAccentMap);
  522. this.defineRemap('mo', CLASS.defaultMoMap);
  523. this.defineRemap('mn', CLASS.defaultMnMap);
  524. }
  525. /**
  526. * Returns list of styles needed for the font
  527. */
  528. get styles(): StyleList {
  529. return this._styles;
  530. }
  531. /**
  532. * Sets styles needed for that font.
  533. */
  534. set styles(style: StyleList) {
  535. this._styles = style;
  536. }
  537. /**
  538. * Creates the data structure for a variant -- an object with
  539. * prototype chain that includes a copy of the linked variant,
  540. * and then the inherited variant chain.
  541. *
  542. * The reason for this extra link is that for a mathvariant like
  543. * bold-italic, you want to inherit from both the bold and
  544. * italic variants, but the prototype chain can only inherit
  545. * from one. So for bold-italic, we make an object that has a
  546. * prototype consisting of a copy of the bold data, and add the
  547. * italic data as the prototype chain. (Since this is a copy, we
  548. * keep a record of this link so that if bold is changed later,
  549. * we can update this copy. That is not needed for the prototype
  550. * chain, since the prototypes are the actual objects, not
  551. * copies.) We then use this bold-plus-italic object as the
  552. * prototype chain for the bold-italic object
  553. *
  554. * That means that bold-italic will first look in its own object
  555. * for specifically bold-italic glyphs that are defined there,
  556. * then in the copy of the bold glyphs (only its top level is
  557. * copied, not its prototype chain), and then the specifically
  558. * italic glyphs, and then the prototype chain for italics,
  559. * which is the normal glyphs. Effectively, this means
  560. * bold-italic looks for bold-italic, then bold, then italic,
  561. * then normal glyphs in order to find the given character.
  562. *
  563. * @param {string} name The new variant to create
  564. * @param {string} inherit The variant to use if a character is not in this one
  565. * @param {string} link A variant to search before the inherit one (but only
  566. * its top-level object).
  567. */
  568. public createVariant(name: string, inherit: string = null, link: string = null) {
  569. let variant = {
  570. linked: [] as CharMap<C>[],
  571. chars: (inherit ? Object.create(this.variant[inherit].chars) : {}) as CharMap<C>
  572. } as V;
  573. if (link && this.variant[link]) {
  574. Object.assign(variant.chars, this.variant[link].chars);
  575. this.variant[link].linked.push(variant.chars);
  576. variant.chars = Object.create(variant.chars);
  577. }
  578. this.remapSmpChars(variant.chars, name);
  579. this.variant[name] = variant;
  580. }
  581. /**
  582. * Create the mapping from Basic Latin and Greek blocks to
  583. * the Math Alphanumeric block for a given variant.
  584. */
  585. protected remapSmpChars(chars: CharMap<C>, name: string) {
  586. const CLASS = (this.constructor as typeof FontData);
  587. if (CLASS.VariantSmp[name]) {
  588. const SmpRemap = CLASS.SmpRemap;
  589. const SmpGreek = [null, null, CLASS.SmpRemapGreekU, CLASS.SmpRemapGreekL];
  590. for (const [i, lo, hi] of CLASS.SmpRanges) {
  591. const base = CLASS.VariantSmp[name][i];
  592. if (!base) continue;
  593. for (let n = lo; n <= hi; n++) {
  594. if (n === 0x3A2) continue;
  595. const smp = base + n - lo;
  596. chars[n] = this.smpChar(SmpRemap[smp] || smp);
  597. }
  598. if (SmpGreek[i]) {
  599. for (const n of Object.keys(SmpGreek[i]).map((x) => parseInt(x))) {
  600. chars[n] = this.smpChar(base + SmpGreek[i][n]);
  601. }
  602. }
  603. }
  604. }
  605. if (name === 'bold') {
  606. chars[0x3DC] = this.smpChar(0x1D7CA);
  607. chars[0x3DD] = this.smpChar(0x1D7CB);
  608. }
  609. }
  610. /**
  611. * @param {number} n Math Alphanumerics position for this remapping
  612. * @return {CharData<C>} The character data for the remapping
  613. */
  614. protected smpChar(n: number): CharData<C> {
  615. return [ , , , {smp: n} as C];
  616. }
  617. /**
  618. * Create a collection of variants
  619. *
  620. * @param {string[][]} variants Array of [name, inherit?, link?] values for
  621. * the variants to define
  622. */
  623. public createVariants(variants: string[][]) {
  624. for (const variant of variants) {
  625. this.createVariant(variant[0], variant[1], variant[2]);
  626. }
  627. }
  628. /**
  629. * Defines new character data in a given variant
  630. * (We use Object.assign() here rather than the spread operator since
  631. * the character maps are objeccts with prototypes, and we don't
  632. * want to loose those by doing {...chars} or something similar.)
  633. *
  634. * @param {string} name The variant for these characters
  635. * @param {CharMap} chars The characters to define
  636. */
  637. public defineChars(name: string, chars: CharMap<C>) {
  638. let variant = this.variant[name];
  639. Object.assign(variant.chars, chars);
  640. for (const link of variant.linked) {
  641. Object.assign(link, chars);
  642. }
  643. }
  644. /**
  645. * Defines stretchy delimiters
  646. *
  647. * @param {DelimiterMap} delims The delimiters to define
  648. */
  649. public defineDelimiters(delims: DelimiterMap<D>) {
  650. Object.assign(this.delimiters, delims);
  651. }
  652. /**
  653. * Defines a character remapping map
  654. *
  655. * @param {string} name The name of the map to define or augment
  656. * @param {RemapMap} remap The characters to remap
  657. */
  658. public defineRemap(name: string, remap: RemapMap) {
  659. if (!this.remapChars.hasOwnProperty(name)) {
  660. this.remapChars[name] = {};
  661. }
  662. Object.assign(this.remapChars[name], remap);
  663. }
  664. /**
  665. * @param {number} n The delimiter character number whose data is desired
  666. * @return {DelimiterData} The data for that delimiter (or undefined)
  667. */
  668. public getDelimiter(n: number): DelimiterData {
  669. return this.delimiters[n];
  670. }
  671. /**
  672. * @param {number} n The delimiter character number whose variant is needed
  673. * @param {number} i The index in the size array of the size whose variant is needed
  674. * @return {string} The variant of the i-th size for delimiter n
  675. */
  676. public getSizeVariant(n: number, i: number): string {
  677. if (this.delimiters[n].variants) {
  678. i = this.delimiters[n].variants[i];
  679. }
  680. return this.sizeVariants[i];
  681. }
  682. /**
  683. * @param {number} n The delimiter character number whose variant is needed
  684. * @param {number} i The index in the stretch array of the part whose variant is needed
  685. * @return {string} The variant of the i-th part for delimiter n
  686. */
  687. public getStretchVariant(n: number, i: number): string {
  688. return this.stretchVariants[this.delimiters[n].stretchv ? this.delimiters[n].stretchv[i] : 0];
  689. }
  690. /**
  691. * @param {string} name The variant whose character data is being querried
  692. * @param {number} n The unicode number for the character to be found
  693. * @return {CharData} The data for the given character (or undefined)
  694. */
  695. public getChar(name: string, n: number): CharData<C> {
  696. return this.variant[name].chars[n];
  697. }
  698. /**
  699. * @param {string} name The name of the variant whose data is to be obtained
  700. * @return {V} The data for the requested variant (or undefined)
  701. */
  702. public getVariant(name: string): V {
  703. return this.variant[name];
  704. }
  705. /**
  706. * @param {string} variant The name of the variant whose data is to be obtained
  707. * @return {CssFontData} The CSS data for the requested variant
  708. */
  709. public getCssFont(variant: string): CssFontData {
  710. return this.cssFontMap[variant] || ['serif', false, false];
  711. }
  712. /**
  713. * @param {string} family The font camily to use
  714. * @return {string} The family with the css prefix
  715. */
  716. public getFamily(family: string): string {
  717. return (this.cssFamilyPrefix ? this.cssFamilyPrefix + ', ' + family : family);
  718. }
  719. /**
  720. * @param {string} name The name of the map to query
  721. * @param {number} c The character to remap
  722. * @return {string} The remapped character (or the original)
  723. */
  724. public getRemappedChar(name: string, c: number): string {
  725. const map = this.remapChars[name] || {} as RemapMap;
  726. return map[c];
  727. }
  728. }
  729. /**
  730. * The class interface for the FontData class
  731. *
  732. * @template C The CharOptions type
  733. * @template V The VariantData type
  734. * @template D The DelimiterData type
  735. */
  736. export interface FontDataClass<C extends CharOptions, V extends VariantData<C>, D extends DelimiterData> {
  737. OPTIONS: OptionList;
  738. defaultCssFonts: CssFontMap;
  739. defaultVariants: string[][];
  740. defaultParams: FontParameters;
  741. /* tslint:disable-next-line:jsdoc-require */
  742. charOptions(font: CharMap<C>, n: number): C;
  743. new(...args: any[]): FontData<C, V, D>;
  744. }