MCPcopy Create free account
hub / github.com/microsoft/SandDance / TinySDF

Function TinySDF

docs/app/js/sanddance-app.js:105750–105779  ·  view source on GitHub ↗
(fontSize, buffer, radius, cutoff, fontFamily, fontWeight)

Source from the content-addressed store, hash-verified

105748module.exports.default = TinySDF;
105749var INF = 1e20;
105750function TinySDF(fontSize, buffer, radius, cutoff, fontFamily, fontWeight) {
105751 this.fontSize = fontSize || 24;
105752 this.buffer = buffer === undefined ? 3 : buffer;
105753 this.cutoff = cutoff || 0.25;
105754 this.fontFamily = fontFamily || "sans-serif";
105755 this.fontWeight = fontWeight || "normal";
105756 this.radius = radius || 8;
105757 // For backwards compatibility, we honor the implicit contract that the
105758 // size of the returned bitmap will be fontSize + buffer * 2
105759 var size = this.size = this.fontSize + this.buffer * 2;
105760 // Glyphs may be slightly larger than their fontSize. The canvas already
105761 // has buffer space, but create extra buffer space in the output grid for the
105762 // "halo" to extend into (if metric extraction is enabled)
105763 var gridSize = size + this.buffer * 2;
105764 this.canvas = document.createElement("canvas");
105765 this.canvas.width = this.canvas.height = size;
105766 this.ctx = this.canvas.getContext("2d");
105767 this.ctx.font = this.fontWeight + " " + this.fontSize + "px " + this.fontFamily;
105768 this.ctx.textAlign = "left"; // Necessary so that RTL text doesn't have different alignment
105769 this.ctx.fillStyle = "black";
105770 // temporary arrays for the distance transform
105771 this.gridOuter = new Float64Array(gridSize * gridSize);
105772 this.gridInner = new Float64Array(gridSize * gridSize);
105773 this.f = new Float64Array(gridSize);
105774 this.z = new Float64Array(gridSize + 1);
105775 this.v = new Uint16Array(gridSize);
105776 this.useMetrics = this.ctx.measureText("A").actualBoundingBoxLeft !== undefined;
105777 // hack around https://bugzilla.mozilla.org/show_bug.cgi?id=737852
105778 this.middle = Math.round(size / 2 * (navigator.userAgent.indexOf("Gecko/") >= 0 ? 1.2 : 1));
105779}
105780function prepareGrids(imgData, width, height, glyphWidth, glyphHeight, gridOuter, gridInner) {
105781 // Initialize grids outside the glyph range to alpha 0
105782 gridOuter.fill(INF, 0, width * height);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected