
    j5j                    <    d Z ddlmZ ddlmZ ddlZ G d d      Zy)zBase font management for PDF rendering.

This module provides the base FontManager class that handles font loading
and glyph checking using uharfbuzz.
    )annotations)PathNc                  :    e Zd ZdZdd	dZd
dZddZddZddZy)FontManagera  Manages font loading and glyph checking for PDF rendering.

    This base class handles loading fonts with uharfbuzz for glyph checking
    and text shaping. Renderer-specific subclasses should extend this to
    add their own font objects.

    Attributes:
        font_path: Path to the font file
        font_data: Raw font file bytes
        font_index: Index within TTC collection (0 for single-font files)
        hb_face: uharfbuzz Face object
        hb_font: uharfbuzz Font object
    c                    || _         || _        |j                         | _        t	        j
                  | j                  |      | _        t	        j                  | j                        | _        y)zInitialize font manager.

        Args:
            font_path: Path to TrueType/OpenType font file
            font_index: Index of font within a TTC collection (default 0).
                        For single-font files (.ttf, .otf), use 0.
        N)		font_path
font_index
read_bytes	font_datahbFacehb_faceFonthb_font)selfr   r	   s      P/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/font/font_manager.py__init__zFontManager.__init__    sP     #$ #--/ wwt~~z:wwt||,    c                    | j                   S )znGet uharfbuzz Font object for text measurement.

        Returns:
            UHarfBuzz Font instance
        )r   )r   s    r   get_hb_fontzFontManager.get_hb_font3   s     ||r   c                N    | j                   j                  |      }|duxr |dk7  S )zCheck if font has a glyph for given codepoint.

        Args:
            codepoint: Unicode codepoint

        Returns:
            True if font has a real glyph (not .notdef)
        Nr   )r   get_nominal_glyph)r   	codepointglyph_ids      r   	has_glyphzFontManager.has_glyph;   s,     <<11)<t#5A5r   c                    | j                   j                  d      }| j                  j                  }|j                  |j
                  |fS )a%  Get normalized font metrics (ascent, descent, units_per_em).

        Returns:
            Tuple of (ascent, descent, units_per_em) where ascent and descent
            are in font units. Ascent is positive (above baseline), descent
            is typically negative (below baseline).
        ltr)r   get_font_extentsr   upemascender	descender)r   extentsunits_per_ems      r   get_font_metricszFontManager.get_font_metricsG   s@     ,,//6||((  '"3"3\BBr   c                    |syt        |      }| j                  j                  |      }||dk(  ry| j                  j                  |      }|y| j                  j
                  }|j                  }||z  |z  }|S )a  Get the left side bearing of a character at a given font size.

        The left side bearing (lsb) is the horizontal distance from the glyph
        origin (x=0) to the leftmost pixel of the glyph. A positive lsb means
        there's whitespace before the glyph starts.

        Args:
            char: Single character to get lsb for
            font_size: Font size in points

        Returns:
            Left side bearing in points. Returns 0 if character not found.
        g        r   )ordr   r   get_glyph_extentsr   r   	x_bearing)	r   char	font_sizer   r   r"   r#   	lsb_unitslsb_pts	            r   get_left_side_bearingz!FontManager.get_left_side_bearingS   s     I	<<11)<x1} ,,00:? ||((%%	Y&5r   N)r   )r   r   r	   int)returnzhb.Font)r   r.   r/   bool)r/   ztuple[float, float, float])r)   strr*   floatr/   r2   )	__name__
__module____qualname____doc__r   r   r   r$   r-    r   r   r   r      s!    -&
6
C r   r   )r6   
__future__r   pathlibr   	uharfbuzzr   r   r7   r   r   <module>r;      s!    #  b br   