123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /*************************************************************
- *
- * Copyright (c) 2019-2022 The MathJax Consortium
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Implements the FontCache object for SVG output
- *
- * @author dpvc@mathjax.org (Davide Cervone)
- */
- import {SVG} from '../svg.js';
- export class FontCache<N, T, D> {
- /**
- * The SVG jax that owsn this cache
- */
- protected jax: SVG<N, T, D>;
- /**
- * The cache of font character IDs to their paths
- */
- protected cache: Map<string, string> = new Map();
- /**
- * The SVG <defs> element for storing the cache
- */
- protected defs: N = null;
- /**
- * A string to use to make per-equation cache IDs unique
- */
- protected localID: string = '';
- /**
- * A number used to make localID values to use for each equation
- */
- protected nextID: number = 0;
- /**
- * @param {SVG} jax The SVG jax owning this font cache
- */
- constructor(jax: SVG<N, T, D>) {
- this.jax = jax;
- }
- /**
- * Cache a character from a particular variant and return the cache ID
- *
- * @param {string} variant The variant name for the character
- * @param {string} C The character to be cached
- * @param {string} path The SVG path data for the character
- * @return {string} The id for the cached <path> element
- */
- public cachePath(variant: string, C: string, path: string): string {
- const id = 'MJX-' + this.localID + (this.jax.font.getVariant(variant).cacheID || '') + '-' + C;
- if (!this.cache.has(id)) {
- this.cache.set(id, path);
- this.jax.adaptor.append(this.defs, this.jax.svg('path', {id: id, d: path}));
- }
- return id;
- }
- /**
- * Clear the localID value
- */
- public clearLocalID() {
- this.localID = '';
- }
- /**
- * Use a localID (for font-specific caching), either with a specific string,
- * or from the nextID number.
- */
- public useLocalID(id: string = null) {
- this.localID = (id == null ? ++this.nextID : id) + (id === '' ? '' : '-');
- }
- /**
- * Clear the cache
- */
- public clearCache() {
- this.cache = new Map();
- this.defs = this.jax.svg('defs');
- }
- /**
- * Return the font cache <defs> element
- */
- public getCache() {
- return this.defs;
- }
- }
|