
    ^j#                     N   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ej.                  eej0                  eej2                  eej4                  eiZ ed	
       G d d             Z G d d      Z e       j<                  Zy)z
build123d font and text objects

name: text.py
by:   jwagenet
date: July 28th 2025

desc:
    This python module contains font and text objects.

    N)	dataclass)TTFontttCollection)Font_FA_BoldFont_FA_BoldItalicFont_FA_ItalicFont_FA_RegularFont_FontMgrFont_SystemFont)TCollection_AsciiString)TColStd_SequenceOfHAsciiString)	FontStyleT)frozenc                   <    e Zd ZU dZeed<   eedf   ed<   defdZy)FontInfozRepresentation for registered font.

    Not immediately compatible with Font_SystemFont, which only contains a single
    style/aspect.
    name.stylesreturnc                 `    t        d | j                  D              }d| j                  d| dS )Nc              3   4   K   | ]  }|j                     y wNr   ).0ss     G/opt/ringagent/.cad-venv/lib/python3.12/site-packages/build123d/text.py	<genexpr>z$FontInfo.__repr__.<locals>.<genexpr>5   s     8qAFF8s   z
Font(name=z	, styles=))tupler   r   )selfstyle_namess     r   __repr__zFontInfo.__repr__4   s/    8DKK88DII=	+a@@    N)	__name__
__module____qualname____doc__str__annotations__r   r   r!    r"   r   r   r   )   s+     I)S.!!A# Ar"   r   c                       e Zd ZdZdZdgZd Zdee   fdZ	de
dedz  fd	Zd
e
dedefdZ	 dde
dedee
   fdZ	 dde
dedee
   fdZd Zdede
dee   fdZy)FontManagerzWrap OCP Font_FontMgrz
data/fonts)Relief SingleLine CADz0reliefsingleline/ReliefSingleLineCAD-Regular.ttfTc           	         t         j                  j                  d      r&dt        j                  d<   dt        j                  d<   t        j                         | _        t               }| j                  j                  |       t        d|j                         dz         D cg c]!  }|j                  |      j                         # }}d|vrt        j                         dk(  r| j                          t        j                   j#                  t        j                   j%                  t&                    }| j(                  D ]c  }t        j                   j+                  t        j                   j-                  || j.                  |d               }| j1                  ||d	   
       e | j                  j3                  t5        d      t5        d             yyc c}w )zoInitialize FontManager

        Bundled fonts are added to global OCP instance if they haven't already
        linuxz/etc/fonts/fonts.confFONTCONFIG_FILEz/etc/fonts/FONTCONFIG_PATH   
singlelineWindows   )single_stroker,   N)sysplatform
startswithosenvironr
   GetInstance_smanagerr   GetAllAliasesrangeLengthValue	ToCStringsystemregister_system_fontspathdirnameabspath__file__bundled_fontsnormpathjoinbundled_pathregister_fontAddFontAliasr   )r   aliasesiworking_pathfont	font_paths         r   __init__zFontManager.__init__E   sm    <<""7+,CBJJ(),9BJJ()#113 12""7+9>q'..BRUVBV9WXA7==#--/XXw& I-**,77??277??8+DEL** EGG,,GGLLt/@/@$q'J	 ""9DG"D	E LL%%'5'(?@ ' Ys   #&Gr   c                 P   t         t        t        t        d}g }| j                  j                         D ][  t        fd|j                         D              }|j                  t        j                         j                         |             ] |j                  d        |S )zNGet list of available fonts by name and available styles (also called aspects))REGULARBOLD
BOLDITALICITALICc              3   Z   K   | ]"  \  }}j                  |      st        |    $ y wr   )HasFontAspectr   )r   nafs      r   r   z.FontManager.available_fonts.<locals>.<genexpr>s   s*      "!%AQRAS	!"s   ++c                     | j                   S r   r   )xs    r   <lambda>z-FontManager.available_fonts.<locals>.<lambda>x   s
    QVV r"   )key)r	   r   r   r   r<   GetAvailableFontsr   itemsappendr   FontNamerA   sort)r   font_aspects	font_listavail_aspectsr]   s       @r   available_fontszFontManager.available_fontsg   s     ' ,$	
 	//1 	PA! ")5););)=" M Xajjl&<&<&>NO		P 	+,r"   rD   Nc                 8    | j                   j                  |      S )z3Check if font exists at path and return system font)r<   	CheckFont)r   rD   s     r   
check_fontzFontManager.check_font|   s    ||%%d++r"   r   stylec                 Z    | j                   j                  t        |      t        |         S )z2Find font in FontManager library by name and style)r<   FindFontr   FONT_ASPECT)r   r   rn   s      r   	find_fontzFontManager.find_font   s%    ||$$%<T%BKPUDVWWr"   overridec                    t         j                  j                  |      \  }}|j                  d      j	                         dk(  rt        j                  |      }nt        |      g}g }|D ]u  }| j                  ||      }|D ]\  }	|j                  |	j                         j                                |	j                  |       | j                  j                  |	|       ^ w |S )zBRegister all font faces in a font file and return font face names..ttc)r9   rD   splitextstriplowerr   TTCollectionr   _get_font_facesrd   re   rA   SetSingleStrokeFontr<   RegisterFont)
r   rD   rs   r5   _extfonts
font_facesrQ   r]   s
             r   rL   zFontManager.register_font   s     !!$'399S>!U* --d3ED\NE
 	7D((t4E 7!!!**,"8"8":;%%m4))!X67	7 r"   c                 (   g d}g }|D ]t  }t         j                  j                  t         j                  j                  |      d|z         }t	        j                  |      }|D ]  }	|| j                  |	||      z  } v t        t        |            S )zRegister all fonts in a folder)ttfotfrv   *)r9   rD   rJ   rI   globrL   listset)
r   rD   rs   r5   extsr   r   searchresultsresults
             r   register_folderzFontManager.register_folder   s     %
 	RCWW\\"''"2"24"8#)DFii'G! Rd00=QQ
R	R C
O$$r"   c                     t        j                         dk(  rt        j                         }dd| dg}n t        j                         dk(  rddg}ng d}|D ]  }| j	                  |        y	)
zRunner to (re)inititalize the OCCT FontMgr font list since user folder is
        missing on Windows and some fonts may not be imported correctly.r3   zC:/Windows/Fontsz	C:/Users/z&/AppData/Local/Microsoft/Windows/FontsDarwinz/System/Library/Fontsz/Library/Fonts)z/system/fontsz/usr/share/fontsz/usr/local/share/fontsN)r7   rB   r9   getloginr   )r   userpathsrD   s       r   rC   z!FontManager.register_system_fonts   sw     ??	);;=D"D6!GHE __(*,.>?EE  	'D  &	'r"   ft_fontc                     d\  }}}|d   j                   D ]Y  }	 |j                         }|j                  dk(  r|dk(  r|}+|j                  dk(  r|dk(  r|}B|j                  dk(  sR|dk(  sX|}[ |dk7  r|n|}d|v rl|d   j                  D cg c]  }|j
                   }	}g }
|d   j                   D ]1  }|j                  |	v s	 |
j                  |j                                3 n|g}
g d}g }t        |
      D ]D  \  }}|j                         }|D ch c]	  }||v s| }}d	|v r)|D cg c]  }|d
vr|
 }}d|v sd|v rt        }n8t        }n1d|v sd|v r|D cg c]	  }|dvs| }}t        }nd|v rg n|}t        }dj                  |      }|dk7  rdj                  ||g      n|}|j                         }t        t!        |            }|j#                  |t!        |      |dz         	 |j%                  |j'                         j)                         j+                  d             |j                  |       G |S # t        $ r Y ?w xY wc c}w # t        $ r Y w xY wc c}w c c}w c c}w # t        $ r |cY c S w xY w)zAExtract font info from font files and return list of font object.) r   r   r   r1   r   r4      fvar)RegularBoldItalicObliquer   )r   r   r   r   r   )r   r   r    zolf )names	toUnicodeUnicodeDecodeErrornameID	instancessubfamilyNameIDrd   	enumeratesplitr   r   r   r	   rJ   rx   r   r   SetFontPathr|   FontKeyrA   r8   )r   r   rD   familysub	preferredrecordvaluerO   sub_idssubfamiliesaspectsr   	subfamilylabelsaspectmatcheslabel	font_nameocp_fonts                       r   r{   zFontManager._get_font_faces   s   
 ",Yfo++ 	"F((* }}!fl!#r	"$b!		" (2o6W29&/2K2KLQq((LGLK!&/// !==G+!#**6+;+;+=>! %K
 ;')%k2 &	#LAy__&F,3H&v7GvHGH  "($AA  
 w&)w*>/F)FW$	W(<'-#>S1SE  ((G3((I9Bb&)!45fI!)I&'>y'IJH  )@)FRP ,,$$&002==fE LL"M&	#P M &  M . ! ! I( & sY   I	I	I	I.$I.2I3#	I8-I87<I=		II	I+*I+=JJ)FF)r#   r$   r%   r&   rK   rH   rS   r   r   rj   r'   r   rm   r   rr   boolrL   r   rC   r   r{   r)   r"   r   r+   r+   9   s    L	
M Dh *,s ,'= ,Xc X) X X
 @E#'	c* @E%%#'%	c%'.OO%(O	o	Or"   r+   )r&   r   r9   r7   r6   dataclassesr   fontTools.ttLibr   r   OCP.Fontr   r   r   r	   r
   r   OCP.TCollectionr   OCP.TColStdr   build123d.build_enumsr   rU   rV   rX   rW   rq   r   r+   rj   r)   r"   r   <module>r      s   
  	  
 ! 0  4 6 + NNLn,	 $A A AS Sl -//r"   