FontCache.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2019-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 FontCache object for SVG output
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {SVG} from '../svg.js';
  23. export class FontCache<N, T, D> {
  24. /**
  25. * The SVG jax that owsn this cache
  26. */
  27. protected jax: SVG<N, T, D>;
  28. /**
  29. * The cache of font character IDs to their paths
  30. */
  31. protected cache: Map<string, string> = new Map();
  32. /**
  33. * The SVG <defs> element for storing the cache
  34. */
  35. protected defs: N = null;
  36. /**
  37. * A string to use to make per-equation cache IDs unique
  38. */
  39. protected localID: string = '';
  40. /**
  41. * A number used to make localID values to use for each equation
  42. */
  43. protected nextID: number = 0;
  44. /**
  45. * @param {SVG} jax The SVG jax owning this font cache
  46. */
  47. constructor(jax: SVG<N, T, D>) {
  48. this.jax = jax;
  49. }
  50. /**
  51. * Cache a character from a particular variant and return the cache ID
  52. *
  53. * @param {string} variant The variant name for the character
  54. * @param {string} C The character to be cached
  55. * @param {string} path The SVG path data for the character
  56. * @return {string} The id for the cached <path> element
  57. */
  58. public cachePath(variant: string, C: string, path: string): string {
  59. const id = 'MJX-' + this.localID + (this.jax.font.getVariant(variant).cacheID || '') + '-' + C;
  60. if (!this.cache.has(id)) {
  61. this.cache.set(id, path);
  62. this.jax.adaptor.append(this.defs, this.jax.svg('path', {id: id, d: path}));
  63. }
  64. return id;
  65. }
  66. /**
  67. * Clear the localID value
  68. */
  69. public clearLocalID() {
  70. this.localID = '';
  71. }
  72. /**
  73. * Use a localID (for font-specific caching), either with a specific string,
  74. * or from the nextID number.
  75. */
  76. public useLocalID(id: string = null) {
  77. this.localID = (id == null ? ++this.nextID : id) + (id === '' ? '' : '-');
  78. }
  79. /**
  80. * Clear the cache
  81. */
  82. public clearCache() {
  83. this.cache = new Map();
  84. this.defs = this.jax.svg('defs');
  85. }
  86. /**
  87. * Return the font cache <defs> element
  88. */
  89. public getCache() {
  90. return this.defs;
  91. }
  92. }