Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

1.6 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    text/SdfGenerator.hpp
 * @brief   Signed distance field generation from an 8-bit alpha coverage bitmap.
 *
 * Used by the runtime dynamic glyph atlas: glyphs are rasterized
 * to alpha via Canvas2D on the TS side, then converted to an SDF here so they
 * stay crisp at any scale and support cheap outline/shadow in the shader. Pure
 * (no GL / no emscripten) so it is unit-testable and portable.
 */
#pragma once

#include "../core/Types.hpp"

namespace esengine::text {

/**
 * @brief Signed distance field from an antialiased alpha coverage bitmap.
 *
 * Exact Felzenszwalb–Huttenlocher squared-distance transform with
 * coverage-derived fractional seeds: partially covered texels place the edge
 * at sub-texel accuracy, so magnified glyphs keep smooth edges instead of the
 * source rasterization's pixel staircase.
 *
 * @param alpha   width*height coverage bytes (antialiased coverage is used,
 *                not thresholded).
 * @param out     width*height output bytes. 128 encodes the edge; values above
 *                128 are inside, below are outside. The mapping is linear with
 *                `spread` pixels spanning half the [0,255] range, matching what
 *                the SDF text shader expects.
 * @param spread  distance in pixels mapped to half the byte range (the SDF
 *                "range"/padding). Larger = softer falloff / more outline room.
 */
void sdfFromAlpha(const u8* alpha, u8* out, u32 width, u32 height, f32 spread);

}  // namespace esengine::text