UNPKG

@bokeh/bokehjs

Version:

Interactive, novel data visualization

61 lines 2.24 kB
import { assert } from "./assert"; import { canvas } from "../dom"; const _offscreen_context = (() => { // Support Firefox ESR, etc., see https://github.com/bokeh/bokeh/issues/14006. const canvas_el = typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(0, 0) : canvas({ width: 0, height: 0 }); const ctx = canvas_el.getContext("2d"); assert(ctx != null, "can't obtain 2d context"); return ctx; })(); function _font_metrics(font) { const ctx = _offscreen_context; ctx.font = font; const cap_metrics = ctx.measureText("M"); const x_metrics = ctx.measureText("x"); const metrics = ctx.measureText("ÅŚg|"); // Support Firefox ESR, etc., see https://github.com/bokeh/bokeh/issues/13969. const ascent = typeof metrics.fontBoundingBoxAscent !== "undefined" ? metrics.fontBoundingBoxAscent : metrics.actualBoundingBoxAscent; const descent = typeof metrics.fontBoundingBoxDescent !== "undefined" ? metrics.fontBoundingBoxDescent : metrics.actualBoundingBoxDescent; return { height: ascent + descent, ascent, descent, cap_height: cap_metrics.actualBoundingBoxAscent, x_height: x_metrics.actualBoundingBoxAscent, }; } const _metrics_cache = new Map(); export function font_metrics(font) { let metrics = _metrics_cache.get(font); if (metrics == null) { const loaded = document.fonts.check(font); metrics = { font: _font_metrics(font) }; if (loaded) { _metrics_cache.set(font, metrics); } } return metrics.font; } export function parse_css_font_size(size) { const match = size.match(/^\s*(\d+(\.\d+)?)(\w+)\s*$/); if (match != null) { const [, value, , unit] = match; const number = Number(value); if (isFinite(number)) { return { value: number, unit }; } } return null; } export function parse_css_length(size) { const match = size.match(/^\s*(-?\d+(\.\d+)?)(\w+)\s*$/); if (match != null) { const [, value, , unit] = match; const number = Number(value); if (isFinite(number)) { return { value: number, unit }; } } return null; } //# sourceMappingURL=text.js.map