Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.1

0.6.1 / src / web_fonts / font_style_mapping.lucb

#==============================================================================================
#
#   font_style_mapping - the names of font weights, slopes and widths
#
#   DESCRIPTION:
#       Port of Libraries/LibGfx/Font/FontStyleMapping.h (lines 1-105). The three constexpr
#       tables hold StringViews, which a constant initializer cannot build from string
#       literals here, so the tables (the generated module vars) are filled on first use.
#
#==============================================================================================

var font_style_mapping_initialized: bool

## Fills font_weight_names, font_slope_names and font_width_names (the C++ initializers).
func font_style_mapping_ensure_initialized():
    if font_style_mapping_initialized:
        return
    font_style_mapping_initialized = true
    let weights: (i32, str)[10] = [(100, "Thin"), (200, "Extra Light"), (300, "Light"), (400, "Regular"), (500, "Medium"), (600, "Semi Bold"), (700, "Bold"), (800, "Extra Bold"), (900, "Black"), (950, "Extra Black")]
    for (index, entry) in weights.indexed():
        font_weight_names[index] = FontStyleMapping(style = entry.0, name = ak.sv(entry.1))
    let slopes: (i32, str)[4] = [(0, "Regular"), (1, "Italic"), (2, "Oblique"), (3, "Reclined")]
    for (index, entry) in slopes.indexed():
        font_slope_names[index] = FontStyleMapping(style = entry.0, name = ak.sv(entry.1))
    let widths: (i32, str)[9] = [(1, "Ultra Condensed"), (2, "Extra Condensed"), (3, "Condensed"), (4, "Semi Condensed"), (5, "Normal"), (6, "Semi Expanded"), (7, "Expanded"), (8, "Extra Expanded"), (9, "Ultra Expanded")]
    for (index, entry) in widths.indexed():
        font_width_names[index] = FontStyleMapping(style = entry.0, name = ak.sv(entry.1))

## Ported from Libraries/LibGfx/Font/FontStyleMapping.h:51 Gfx::weight_to_name.
pub func weight_to_name(weight: i32) -> ak.StringView:
    font_style_mapping_ensure_initialized()
    for it in font_weight_names:
        if it.style == weight:
            return it.name
    return ak.sv("")

## Ported from Libraries/LibGfx/Font/FontStyleMapping.h:60 Gfx::name_to_weight.
pub func name_to_weight(name: ak.StringView) -> i32:
    font_style_mapping_ensure_initialized()
    for it in font_weight_names:
        if ak.string_view_eq_string_view(&it.name, name):
            return it.style
    return 0

## Ported from Libraries/LibGfx/Font/FontStyleMapping.h:69 Gfx::slope_to_name.
pub func slope_to_name(slope: i32) -> ak.StringView:
    font_style_mapping_ensure_initialized()
    for it in font_slope_names:
        if it.style == slope:
            return it.name
    return ak.sv("")

## Ported from Libraries/LibGfx/Font/FontStyleMapping.h:78 Gfx::name_to_slope.
pub func name_to_slope(name: ak.StringView) -> i32:
    font_style_mapping_ensure_initialized()
    for it in font_slope_names:
        if ak.string_view_eq_string_view(&it.name, name):
            return it.style
    return 0

## Ported from Libraries/LibGfx/Font/FontStyleMapping.h:87 Gfx::width_to_name.
pub func width_to_name(width: i32) -> ak.StringView:
    font_style_mapping_ensure_initialized()
    for it in font_width_names:
        if it.style == width:
            return it.name
    return ak.sv("")

## Ported from Libraries/LibGfx/Font/FontStyleMapping.h:96 Gfx::name_to_width.
pub func name_to_width(name: ak.StringView) -> i32:
    font_style_mapping_ensure_initialized()
    for it in font_width_names:
        if ak.string_view_eq_string_view(&it.name, name):
            return it.style
    return 0