FontData.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 SVGFontData class for font data in SVG output.
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {CharMap, CharOptions, CharData, VariantData, DelimiterData, FontData} from '../common/FontData.js';
  23. export * from '../common/FontData.js';
  24. export type CharStringMap = {[name: number]: string};
  25. /**
  26. * Add the extra data needed for CharOptions in SVG
  27. */
  28. export interface SVGCharOptions extends CharOptions {
  29. c?: string; // the character value (overrides default value)
  30. p?: string; // svg path
  31. }
  32. /**
  33. * Shorthands for SVG char maps and char data
  34. */
  35. export type SVGCharMap = CharMap<SVGCharOptions>;
  36. export type SVGCharData = CharData<SVGCharOptions>;
  37. /**
  38. * The extra data needed for a Variant in SVG output
  39. */
  40. export interface SVGVariantData extends VariantData<SVGCharOptions> {
  41. cacheID: string;
  42. }
  43. /**
  44. * the extra data neede for a Delimiter in SVG output
  45. */
  46. export interface SVGDelimiterData extends DelimiterData {
  47. }
  48. /****************************************************************************/
  49. /**
  50. * The SVG FontData class
  51. */
  52. export class SVGFontData extends FontData<SVGCharOptions, SVGVariantData, SVGDelimiterData> {
  53. /**
  54. * @override
  55. */
  56. public static OPTIONS = {
  57. ...FontData.OPTIONS,
  58. dynamicPrefix: './output/svg/fonts'
  59. };
  60. /**
  61. * @override
  62. */
  63. public static JAX = 'SVG';
  64. /**
  65. * @override
  66. */
  67. public static charOptions(font: SVGCharMap, n: number) {
  68. return super.charOptions(font, n) as SVGCharOptions;
  69. }
  70. }
  71. export type SVGFontDataClass = typeof SVGFontData;
  72. /****************************************************************************/
  73. /**
  74. * @param {CharMap} font The font to augment
  75. * @param {CharStringMap} paths The path data to use for each character
  76. * @param {CharStringMap} content The string to use for remapped characters
  77. * @return {SVGCharMap} The augmented font
  78. */
  79. export function AddPaths(font: SVGCharMap, paths: CharStringMap, content: CharStringMap): SVGCharMap {
  80. for (const c of Object.keys(paths)) {
  81. const n = parseInt(c);
  82. SVGFontData.charOptions(font, n).p = paths[n];
  83. }
  84. for (const c of Object.keys(content)) {
  85. const n = parseInt(c);
  86. SVGFontData.charOptions(font, n).c = content[n];
  87. }
  88. return font;
  89. }