
    7j<                        d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZmZmZ d dlmZmZ d dlmZ d dlZd	 Z G d
 de      Z G d d      Z G d d      Z G d d      Z G d d      Zd Z G d d      Z y)    )Path)DictAny)
rgb_to_hsv)Image	ImageDraw	ImageFont
ImageChops)o8o16leo32le)QGFBlockHeaderQGFFramePaletteDescriptorV1)AttrDictNc                 B    t        | dz        t        | dz  dz	        z   S )N  i      )o16r   )is    H/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/painter_qff.pyo24r      s#    q6z?RX" 4555    c                   $     e Zd Z fdZd Z xZS )QFFGlyphInfoc                     t         |           t        |      D ]  \  }}|| d| <    |j                         D ]
  \  }}|| |<    y )Nzarg:)super__init__	enumerateitems)selfargskwargsnvaluekey	__class__s         r   r   zQFFGlyphInfo.__init__   sY    !$ 	%HAu$D4s	% !,,. 	JCDI	r   c                     |du r-|j                  t        t        | j                                     | j                  dz  dz  | j
                  dz  z  }|j                  t        |             y )NT   i ?   )writer   ord
code_pointdata_offsetw)r    fpinclude_code_pointr$   s       r   r*   zQFFGlyphInfo.write"   sV    %HHST__-./""a'83F
Ur   )__name__
__module____qualname__r   r*   __classcell__)r&   s   @r   r   r      s    r   r   c                   X    e Zd ZdZdZdZd Zd Zed        Z	e	j                  d        Z	y)	QFFFontDescriptorr      iQFF c                    t               | _        t        j                  | j                  _        t        j                  | j                  _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        y )N   r   F   )r   headerr6   type_idlengthversiontotal_file_sizeline_heighthas_ascii_tableunicode_glyph_countformatflagscompressiontransparency_indexr    s    r   r   zQFFFontDescriptor.__init__2   su    $&/77.55 $#$ 
"&r   c                 b   | j                   j                  |       |j                  dt        t        j                        z   t        | j                        z   t        | j                        z   t        | j                   dz        z   t        | j                        z   t        | j                  du rdnd      z   t        | j                  dz        z   t        | j                        z   t        | j                        z   t        | j                        z   t        | j                         z          y )Nr   l    Tr9   r   r   )r;   r*   r   r6   magicr   r>   o32r?   r@   rA   r   rB   rC   rD   rE   rF   r    r/   s     r   r*   zQFFFontDescriptor.write@   s   "
#))*+ $&&'( D(((J67	8
 !!"# d**d2:; $**V345 o n	 !!"
# (()*	
r   c                 &    | j                   dz  dk(  S )Nr9   rD   rG   s    r   is_transparentz QFFFontDescriptor.is_transparentQ   s    

T!d**r   c                 ^    |r| xj                   dz  c_         y | xj                   dz  c_         y )Nr9   rM   )r    vals     r   rN   z QFFFontDescriptor.is_transparentU   s!    JJ$JJJ%Jr   N)r1   r2   r3   r<   r=   rI   r   r*   propertyrN   setter r   r   r6   r6   -   sI    GFE'
" + +    r   r6   c                   ,    e Zd ZdZdZd ZdefdZd Zy)QFFAsciiGlyphTableV1r9   i  c                     t               | _        t        j                  | j                  _        t        j                  | j                  _        i | _        y N)r   r;   rV   r<   r=   glyphsrG   s    r   r   zQFFAsciiGlyphTableV1.__init__d   s9    $&2::188 r   glyphc                 H    || j                   t        |j                        <   y rX   rY   r+   r,   r    rZ   s     r   	add_glyphzQFFAsciiGlyphTableV1.add_glyphl       -2C(()*r   c                     | j                   j                  |       t        dd      D ]!  }| j                  |   j                  |d       # y )N       F)r;   r*   rangerY   r    r/   r#   s      r   r*   zQFFAsciiGlyphTableV1.writeo   sB    "tT" 	,AKKN  U+	,r   N)	r1   r2   r3   r<   r=   r   r   r^   r*   rT   r   r   rV   rV   `   s"    GF3| 3,r   rV   c                   (    e Zd ZdZd ZdefdZd Zy)QFFUnicodeGlyphTableV1   c                     t               | _        t        j                  | j                  _        d| j                  _        i | _        y Nr   )r   r;   rf   r<   r=   rY   rG   s    r   r   zQFFUnicodeGlyphTableV1.__init__|   s3    $&4<< r   rZ   c                 H    || j                   t        |j                        <   y rX   r\   r]   s     r   r^   z QFFUnicodeGlyphTableV1.add_glyph   r_   r   c                 2   t        | j                  j                               dz  | j                  _        | j                  j                  |       t        | j                  j                               D ]!  }| j                  |   j                  |d       # y )Nr(   T)lenrY   keysr;   r=   r*   sortedrd   s      r   r*   zQFFUnicodeGlyphTableV1.write   sp     !1!1!34q8"((*+ 	+AKKN  T*	+r   N)r1   r2   r3   r<   r   r   r^   r*   rT   r   r   rf   rf   y   s    G3| 3+r   rf   c                       e Zd ZdZd Zd Zy)QFFFontDataDescriptorV1   c                 n    t               | _        t        j                  | j                  _        g | _        y rX   )r   r;   rp   r<   datarG   s    r   r   z QFFFontDataDescriptorV1.__init__   s%    $&5==	r   c                     t        | j                        | j                  _        | j                  j	                  |       |j	                  t        | j                               y rX   )rl   rs   r;   r=   r*   bytesrK   s     r   r*   zQFFFontDataDescriptorV1.write   s=     ^"
tyy!"r   N)r1   r2   r3   r<   r   r*   rT   r   r   rp   rp      s    G
#r   rp   c                     i }| du rt        dd      D ]  }d|t        |      <    t        |      }|D ]  }d||<   	 t        |j	                               S )NTra   rb   )rc   chrlistrn   rm   )	use_asciiunicode_glyphsrY   cs       r   _generate_font_glyphs_listr|      sk    F DtT" 	"A!F3q6N	" .)N q	 &++-  r   c                       e Zd Zd Zd ZddedefdZddede	dededed	efd
Z
defdZddededefdZdeeef   defdZy)QFFFontc                 <    || _         d | _        i | _        d| _        y ri   )loggerimage
glyph_dataglyph_height)r    r   s     r   r   zQFFFont.__init__   s"    
r   c                 P   d}d}t         j                  j                  | j                  |      }t         j                  j	                  ||      \  | _        }| j                  j                         D ]  \  }}|j                  |j                  d|j                  |j                  z   d| j                  z   f      }t         j                  j	                  ||      \  }}t         j                  j                  |      }	|t        |      z  }|t        |	      z  }||d<   |	|d<    ||fS )Nr   r9   image_uncompressed_bytesimage_compressed_bytes)qmkpainterconvert_requested_formatr   convert_image_bytespaletter   r   cropxr.   r   compress_bytes_qmk_rlerl   )
r    rC   total_data_sizetotal_rle_data_sizeconverted_img_glyph_entry	glyph_imgthis_glyph_image_bytesthis_glyph_rle_bytess
             r   _extract_glyphszQFFFont._extract_glyphs   s   <<TZZPKK;;M6Rq #oo335 	INA{%**KMM1kmmkmm>[]^aearar]r+stI*-++*I*I)U[*\'Q&#&;;#E#EF\#] s#9::O3';#<<6LK234HK01	I  !455r   include_ascii_glyphsrz   c                 t   d | _         i | _        d| _        t        ||      }|j                  \  }}g }g }|j                         }	|	d   }
|j                  d       d}t        d|      D ]4  }|	|df   |
k(  s|j                  |       |j                  ||z
         |}6 |j                  ||z
         t        |      t        |      k7  r| j                  j                  d       y t        dt        |            D ])  }t        ||   ||   ||         | j                  ||   <   + || _         |dz
  | _        y )Nr   )r   r   r9   z`The number of glyphs to generate doesn't match the number of detected glyphs in the input image.)r,   r   r.   )r   r   r   r|   sizeloadappendrc   rl   r   errorr   )r    imgr   rz   rY   widthheightglyph_pixel_offsetsglyph_pixel_widthspixelsglyph_split_colorlast_offsetr   r#   s                 r   _parse_imagezQFFFont._parse_image   s`   
 ,,@.Q (( ! #4L""1%q% 	 Aad|00#**1-"))!k/:		 
 	!!%+"56 "#s6{2KK  B  C q#123 	A)5NabcNdhz{|h})~DOOF1I&	 
"QJr   ttf_file	font_sizeinclude_before_leftuse_aac                 X   t        j                  t        |      t        |            }|j                  j
                  t        |j                  j                        z   }t        ||      }	d}
d}d}|	D ]M  }|j                  |d      \  }}}}|r||z
  n|}|j                  |d      d   }||k  r|}||z  }|
|kD  sL|}
O t        j                  d|d	z   |d
z  d	z   fd      }d}|	D ]  }|j                  |d      \  }}}}|r||z
  n|}||z
  }| }||
z
  }t        j                  d||fd      }t        j                  |      }|sd|_        |j                  ||f||d       |j!                  ||d	f       |j#                         }d||df<   ||z  } |j#                         }d||df<   t        j                  d|d	z   |d	z   fd      }t%        j&                  ||      j                         }|d   |d	   |d
   d	z
  |d   f}| j)                  |j+                  |      ||       y )Ni r   ls)anchorla   RGBr9   rg   )r   r   r   r:   1lt)fontr   )r:   r   r:   )r	   truetypestrintr   ascentabsdescentr|   getbboxr   newr   Drawfontmodetextpaster   r
   
differencer   r   )r    r   r   r   rz   r   r   r   max_font_sizerY   baseline_offsettotal_glyph_widthmax_glyph_heightrZ   ls_lls_tls_rls_bglyph_widthr   r   	cur_x_posx_offsety_offsetr   
glyph_drawr   	dummy_imgbboxs                                r   generate_imagezQFFFont.generate_image   st   !!#h-Y@		((3tyy/@/@+AA+,@.Q!  	'E'+||E$|'G$T4t+>4$;TK<<d<;A>L,.#/ ,%"&	' ii 1A 5}q7H17LM~^	  	%E'+||E$|'G$T4t+>4$;TK$;LuHo-H 		%+})E~VI"	2J&)
#OOXx0%d4OP IIi)Q0 XXZF#0F9a<  $I/	%4 ,y!| IIe&7!&;]Q=N%OQ_`	$$S)4<<>Qa$q'A+tAw7 	#((4.*>Or   img_filec                     | j                   | j                  j                  d       y | j                   j                  t	        |             y )NNo image is loaded.)r   r   r   saver   )r    r   s     r   save_to_imagezQFFFont.save_to_image:  s8    ::KK34 	

H&r   c                 b    | j                  t        j                  t        |            ||       y rX   )r   r   openr   )r    r   r   rz   s       r   read_from_imagezQFFFont.read_from_imageC  s%    %**S]35I>Zr   rC   use_rlec                    | j                   | j                  j                  d       y | j                  |      \  }}|r||k  }t	               }| j
                  j                         D ];  \  }}t        |      |d<   |r|j                  n|j                  }	|t	        |	      z  }= t               }
t               }t               }t               }||_        t        t!        dd      D cg c]  }t#        |      | j
                  v  c}      }| j
                  j                         D ]F  \  }}t%        |      dk\  r"t%        |      dk  r|r|j'                  |       6|j'                  |       H | j(                  |
_        ||
_        t        |j.                  j1                               |
_        d|
_        |d   |
_        |rdnd	|
_        |j;                         }|
j=                  |       |
j,                  r|j=                  |       |
j2                  d	kD  r|j=                  |       |d
   rBt?               }d }tA        tC        || jD                              |_#        |j=                  |       |j=                  |       |j;                         |
_$        |jK                  |d	       |
j=                  |       y c c}w )Nr   r-   ra   rb   ~   Fimage_format_byter9   r   has_palettec                     t        | d   dz  | d   dz  | d   dz        }t        |d   dz        t        |d   dz        t        |d   dz        fS )Nr   g     o@r9   rg   )r   r   )ehsvs     r   rgb888_to_qmk_hsv888z1QFFFont.save_to_qff.<locals>.rgb888_to_qmk_hsv888  s\     1qte|QqTE\JCFUN+SQ%-@#c!funBUVVr   )&r   r   r   r   ru   r   r   rl   r   r   r6   rV   rf   rp   rs   allrc   rw   r+   r^   r   r@   rA   rY   rm   rB   rN   rC   rE   tellr*   r   rx   mapr   palette_entriesr?   seek)r    rC   r   r/   r   r   
img_bufferr   r   glyph_img_bytesfont_descriptorascii_tableunicode_tabledata_descriptorr#   r   r,   font_descriptor_locationpalette_descriptorr   s                       r   save_to_qffzQFFFont.save_to_qffH  s   ::KK34 261E1Ef1M.-*_<G W
"oo335 	1NA{),ZK&DKk@@Q\QuQuO%00J	1
 ,-*,.013)  #uTSWGX#Y!CFdoo$=#YZ (,'<'<'> 	5#J:$&3z?d+BG[%%k2''4		5 '+&7&7#*>'.1-2F2F2K2K2M.N+).&!'(;!<.5d4# $&779 b! **b! ..2# - !<!>W
 26c:NPTP\P\6]1^.$$R( 	b! +-'')'
(!,b!a $Zs   (J;N)T )Tr   FT)r1   r2   r3   r   r   boolr   r   r   r   r   r   r   r   r   r   rT   r   r   r~   r~      s    6&('d ('SV ('T?Pt ?P ?PSW ?Pps ?P  PT ?P  fj ?PB'd ' D ad 
I"$sCx. I"4 I"r   r~   )!pathlibr   typingr   r   colorsysr   PILr   r   r	   r
   PIL._binaryr   r   r   r   rJ   qmk.painter_qgfr   r   milc.attrdictr   qmk.painterr   r   r   r6   rV   rf   rp   r|   r~   rT   r   r   <module>r      sp       7 7 6 6 G " 68 *-  - f, ,2+ +2# #"!"]" ]"r   