@@ -9,10 +9,7 @@ use std::{
99 cmp:: min
1010} ;
1111use std:: os:: fd:: AsFd ;
12- use cairo:: {
13- ImageSurface , Format , Context , Surface ,
14- FontSlant , FontWeight , Rectangle
15- } ;
12+ use cairo:: { ImageSurface , Format , Context , Surface , Rectangle , FontFace } ;
1613use rsvg:: { Loader , CairoRenderer , SvgHandle } ;
1714use drm:: control:: ClipRect ;
1815use anyhow:: { Error , Result } ;
@@ -30,15 +27,18 @@ use input_linux_sys::{uinput_setup, input_id, timeval, input_event};
3027use nix:: poll:: { poll, PollFd , PollFlags } ;
3128use privdrop:: PrivDrop ;
3229use serde:: Deserialize ;
30+ use freetype:: Library as FtLibrary ;
3331
3432mod backlight;
3533mod display;
3634mod pixel_shift;
35+ mod fonts;
3736
3837use backlight:: BacklightManager ;
3938use display:: DrmBackend ;
4039use pixel_shift:: PixelShiftManager ;
4140use pixel_shift:: PIXEL_SHIFT_WIDTH_PX ;
41+ use fonts:: { FontConfig , Pattern } ;
4242
4343const DFR_WIDTH : i32 = 2008 ;
4444const DFR_HEIGHT : i32 = 64 ;
@@ -53,13 +53,15 @@ const TIMEOUT_MS: i32 = 10 * 1000;
5353struct ConfigProxy {
5454 media_layer_default : Option < bool > ,
5555 show_button_outlines : Option < bool > ,
56- enable_pixel_shift : Option < bool >
56+ enable_pixel_shift : Option < bool > ,
57+ font_template : Option < String >
5758}
5859
5960struct Config {
6061 media_layer_default : bool ,
6162 show_button_outlines : bool ,
62- enable_pixel_shift : bool
63+ enable_pixel_shift : bool ,
64+ font_face : FontFace
6365}
6466
6567enum ButtonImage {
@@ -124,7 +126,7 @@ impl FunctionLayer {
124126
125127 c. set_source_rgb ( 0.0 , 0.0 , 0.0 ) ;
126128 c. paint ( ) . unwrap ( ) ;
127- c. select_font_face ( "sans-serif" , FontSlant :: Normal , FontWeight :: Bold ) ;
129+ c. set_font_face ( & config . font_face ) ;
128130 c. set_font_size ( 32.0 ) ;
129131 for ( i, button) in self . buttons . iter ( ) . enumerate ( ) {
130132 let left_edge = ( i as f64 * ( button_width + BUTTON_SPACING_PX as f64 ) ) . floor ( ) + pixel_shift_x + ( PIXEL_SHIFT_WIDTH_PX / 2 ) as f64 ;
@@ -223,6 +225,18 @@ fn toggle_key<F>(uinput: &mut UInputHandle<F>, code: Key, value: i32) where F: A
223225 emit ( uinput, EventKind :: Synchronize , SynchronizeKind :: Report as u16 , 0 ) ;
224226}
225227
228+ fn load_font ( name : & str ) -> FontFace {
229+ let fontconfig = FontConfig :: new ( ) ;
230+ let mut pattern = Pattern :: new ( name) ;
231+ fontconfig. perform_substitutions ( & mut pattern) ;
232+ let pat_match = fontconfig. match_pattern ( & pattern) ;
233+ let file_name = pat_match. get_file_name ( ) ;
234+ let file_idx = pat_match. get_font_index ( ) ;
235+ let ft_library = FtLibrary :: init ( ) . unwrap ( ) ;
236+ let face = ft_library. new_face ( file_name, file_idx) . unwrap ( ) ;
237+ FontFace :: create_from_ft ( & face) . unwrap ( )
238+ }
239+
226240fn load_config ( ) -> Config {
227241 let mut base = toml:: from_str :: < ConfigProxy > ( & read_to_string ( "/usr/share/tiny-dfr/config.toml" ) . unwrap ( ) ) . unwrap ( ) ;
228242 let user = read_to_string ( "/etc/tiny-dfr/config.toml" ) . map_err :: < Error , _ > ( |e| e. into ( ) )
@@ -231,11 +245,13 @@ fn load_config() -> Config {
231245 base. media_layer_default = user. media_layer_default . or ( base. media_layer_default ) ;
232246 base. show_button_outlines = user. show_button_outlines . or ( base. show_button_outlines ) ;
233247 base. enable_pixel_shift = user. enable_pixel_shift . or ( base. enable_pixel_shift ) ;
248+ base. font_template = user. font_template . or ( base. font_template ) ;
234249 } ;
235250 Config {
236251 media_layer_default : base. media_layer_default . unwrap ( ) ,
237252 show_button_outlines : base. show_button_outlines . unwrap ( ) ,
238253 enable_pixel_shift : base. enable_pixel_shift . unwrap ( ) ,
254+ font_face : load_font ( & base. font_template . unwrap ( ) )
239255 }
240256}
241257
0 commit comments