@@ -11,6 +11,7 @@ const Gio = imports.gi.Gio;
1111const Gettext = imports . gettext ;
1212const Tooltips = imports . ui . tooltips ;
1313const PopupMenu = imports . ui . popupMenu ;
14+ const Pango = imports . gi . Pango ;
1415
1516
1617const UUID = "todo@NotSirius-A" ;
@@ -70,8 +71,6 @@ MyDesklet.prototype = {
7071 this . settings . bindProperty ( Settings . BindingDirection . IN , "task-not-marked-icon" , "taskNotMarkedDoneIcon" , this . on_setting_changed ) ;
7172 this . settings . bindProperty ( Settings . BindingDirection . IN , "task-marked-icon" , "taskMarkedDoneIcon" , this . on_setting_changed ) ;
7273 this . settings . bindProperty ( Settings . BindingDirection . IN , "font" , "fontRaw" , this . on_setting_changed ) ;
73- this . settings . bindProperty ( Settings . BindingDirection . IN , "font-bold" , "fontBold" , this . on_setting_changed ) ;
74- this . settings . bindProperty ( Settings . BindingDirection . IN , "font-italic" , "fontItalic" , this . on_setting_changed ) ;
7574 this . settings . bindProperty ( Settings . BindingDirection . IN , "text-color" , "customTextColor" , this . on_setting_changed ) ;
7675 this . settings . bindProperty ( Settings . BindingDirection . IN , "text-shadow" , "textShadow" , this . on_setting_changed ) ;
7776 this . settings . bindProperty ( Settings . BindingDirection . IN , "text-shadow-color" , "textShadowColor" , this . on_setting_changed ) ;
@@ -123,7 +122,7 @@ MyDesklet.prototype = {
123122 * Load visual theme from settings
124123 */
125124 loadTheme : function ( ) {
126- this . font = this . parseFont ( this . fontRaw ) ;
125+ this . font = this . parseFontStringToCSS ( this . fontRaw ) ;
127126
128127 const default_item_width = 150 ;
129128 this . scale = this . scaleSize * global . ui_scale ;
@@ -144,11 +143,12 @@ MyDesklet.prototype = {
144143 "TODOlist" : {
145144 "num_of_columns" : this . numOfColumns ,
146145 "item_width" : default_item_width * this . scale * this . text_scale ,
147- "font_family" : this . font [ "family" ] ,
148- "font_size" : this . font [ "size" ] * this . text_scale ,
146+ "font_family" : this . font [ "font- family" ] ,
147+ "font_size" : this . font [ "font- size" ] * this . text_scale ,
149148 "font_color" : this . customTextColor ,
150- "font_bold" : this . fontBold ,
151- "font_italic" : this . fontItalic ,
149+ "font_weight" : this . font [ "font-weight" ] ,
150+ "font_style" : this . font [ "font-style" ] ,
151+ "font_stretch" : this . font [ "font-stretch" ] ,
152152 "text_align" : this . textAlign ,
153153 "text_shadow_enabled" : this . textShadow ,
154154 "text_shadow_color" : this . textShadowColor ,
@@ -220,29 +220,74 @@ MyDesklet.prototype = {
220220 } ,
221221
222222
223+
224+
223225 /**
224- * Parse raw font string, TODO improve parsing, detect bold/italic etc .
225- * @param {string } font_string - Font descriptor
226- * @returns {{"family": string, "size": Number} } Font descriptor object
226+ * Parse raw font string.
227+ * @param {string } font_string - Font descriptor string
228+ * @returns {{"font- family": string, "font- size": Number, "font-weight": Number, "font-style": string, "font-stretch": string } } Font descriptor object
227229 */
228- parseFont : function ( font_string ) {
229- // String are passed by reference here so
230+ parseFontStringToCSS : function ( font_string ) {
231+ // Some fonts don't work, so a fallback font is a good idea
232+ const fallback_font_str = "Ubuntu Regular 16" ;
233+
234+ // String are passed by reference here
230235 // make sure to copy the string to avoid triggering settings callback on change
231236 const font_string_copy = font_string . slice ( ) . trim ( ) ;
237+
238+ let css_font ;
239+ try {
240+ const my_font_description = Pango . font_description_from_string ( font_string_copy ) ;
241+ css_font = this . _PangoFontDescriptionToCSS ( my_font_description ) ;
242+ } catch ( e ) {
243+ Main . notifyError (
244+ _ ( "Sorry, this font is not supported, please select a different one." )
245+ + _ ( " Font: `" ) + font_string_copy + _ ( "` Error: " )
246+ + e . toString ( )
247+ ) ;
248+
249+ const fallback_font_description = Pango . font_description_from_string ( fallback_font_str ) ;
250+ css_font = this . _PangoFontDescriptionToCSS ( fallback_font_description ) ;
251+ } finally {
252+ return css_font ;
253+ }
254+
255+ } ,
232256
233- const font_split = font_string_copy . split ( " " ) ;
234257
235- const font_size = parseInt ( font_split . pop ( ) ) ;
236- let font_family = font_split . join ( " " ) ;
258+ /**
259+ * Process Pango.FontDescription and return valid CSS values
260+ * @param {Pango.FontDescription } font_description - Font descriptor
261+ * @returns {{"font-family": string, "font-size": Number, "font-weight": Number, "font-style": string, "font-stretch": string} } Font descriptor object
262+ */
263+ _PangoFontDescriptionToCSS : function ( font_description ) {
264+ const PangoStyle_to_CSS_map = {
265+ [ Pango . Style . NORMAL ] : "normal" ,
266+ [ Pango . Style . OBLIQUE ] : "oblique" ,
267+ [ Pango . Style . ITALIC ] : "italic" ,
268+ } ;
237269
270+ // font-stretch CSS property seems to be ignored by the CSS renderer
271+ const PangoStretch_to_CSS_map = {
272+ [ Pango . Stretch . ULTRA_CONDENSED ] : "ultra-condensed" ,
273+ [ Pango . Stretch . EXTRA_CONDENSED ] : "extra-condensed" ,
274+ [ Pango . Stretch . CONDENSED ] : "condensed" ,
275+ [ Pango . Stretch . NORMAL ] : "normal" ,
276+ [ Pango . Stretch . SEMI_EXPANDED ] : "semi-expanded" ,
277+ [ Pango . Stretch . EXPANDED ] : "expanded" ,
278+ [ Pango . Stretch . EXTRA_EXPANDED ] : "extra-expanded" ,
279+ [ Pango . Stretch . ULTRA_EXPANDED ] : "ultra-expanded" ,
280+ } ;
281+
238282 return {
239- "family" : font_family ,
240- "size" : font_size
283+ "font-family" : font_description . get_family ( ) ,
284+ "font-size" : Math . floor ( font_description . get_size ( ) / Pango . SCALE ) ,
285+ "font-weight" : font_description . get_weight ( ) ,
286+ "font-style" : PangoStyle_to_CSS_map [ font_description . get_style ( ) ] ,
287+ "font-stretch" : PangoStretch_to_CSS_map [ font_description . get_stretch ( ) ]
241288 } ;
242289 } ,
243290
244-
245-
246291 /**
247292 * Render desklet decorations
248293 */
0 commit comments