def character_ratio(style, character):
    """Return the ratio of 1ex/font_size or 1ch/font_size."""
    # TODO: use context to use @font-face fonts

    assert character in ('x', '0')

    cache = style.cache[f'ratio_{"ex" if character == "x" else "ch"}']
    cache_key = _font_style_cache_key(style)
    if cache_key in cache:
        return cache[cache_key]

    # Avoid recursion for letter-spacing and word-spacing properties
    style = style.copy()
    style['letter_spacing'] = 'normal'
    style['word_spacing'] = 0
    # Random big value
    style['font_size'] = 1000

    layout = Layout(context=None, style=style)
    layout.set_text(character)
    line, _ = layout.get_first_line()

    ink_extents = ffi.new('PangoRectangle *')
    logical_extents = ffi.new('PangoRectangle *')
    pango.pango_layout_line_get_extents(line, ink_extents, logical_extents)
    if character == 'x':
        measure = -ink_extents.y * FROM_UNITS
    else:
        measure = logical_extents.width * FROM_UNITS
    ffi.release(ink_extents)
    ffi.release(logical_extents)

    # Zero means some kind of failure, fallback is 0.5.
    # We round to try keeping exact values that were altered by Pango.
    ratio = round(measure / style['font_size'], 5) or 0.5
    cache[cache_key] = ratio
    return ratio
Return the ratio of 1ex/font_size or 1ch/font_size.
weasyprint/css/computed_values.py