atlas-engineer / nyxt

Nyxt - the hacker's browser.
https://nyxt-browser.com/
9.88k stars 414 forks source link

Customize default fonts for specific writing systems #2697

Open rodrigomorales1 opened 1 year ago

rodrigomorales1 commented 1 year ago

Is your feature request related to a problem? Please describe. It is somewhat related to a problem: I don't like the default font that is used for Chinese characters and I would rather change it. In Firefox and Emacs, I can customize the font that is used for any writing system (e.g. Simplified Chinese, Greek, Hebrew, Korean, Bengali, etc). However, I don't know how to do the same with Nyxt.

The screenshot below shows the font that is currently being used in Nyxt. The font is rendered correctly, it is just that I'd rather use another font.

image

Describe the solution you'd like A way to customize the fonts that are used for specific writing systems. A similar feature to what it is possible to do with Emacs and Firefox. In the section "Additional context" below, I explain how this is done in Emacs and Firefox.

Describe alternatives you've considered I tried looking at Nyxt configuration files of other users and used grep to search for the word font to find potential configurations that might change the default font in Nyxt. I only was able to find the following configuration in aartaka/nyxt-config. (permalink)

     (webkit:webkit-settings-default-font-family settings) "Cantarell"
     (webkit:webkit-settings-default-font-size settings) 18
     ;; Use Hack-17 as the monospace font.
     (webkit:webkit-settings-monospace-font-family settings) "Hack"
     (webkit:webkit-settings-default-monospace-font-size settings) 17)))

I think the only available option I currently have is to change the default font by using those sexps and this will ultimately change the font for the Chinese characters. However, at the same time, because the default font was changed, this will change the font in all other places and I don't want this to happen.

Additional context

Customize font for different writing systems in Emacs

In Emacs, each character belongs to a script. The table shown below lists some examples.

character script
a latin
Я cyrillic
Ω greek
braille
han
kana
hangul

By calling what-cursor-position with a prefix argument while the cursor is on a character, you can get information about the character, including the script that character belongs to. You could also use (aref char-script-table ?悄) to get the script of a single character.

The function set-fontset-font can be used for specifying the font that is used for a specific script. For example, if I wanted to use a different font for the han script (i.e. Chinese characters), say Noto Sans CJK font (link to repository) and at the same time, I wanted to increase the size, I could do it with: (set-fontset-font t 'han (font-spec :family "Noto Sans CJK SC" :size 40)). The screenshot below shows this.

In the screenshot on the left, you can see the default fonts that are used in Emacs in my system because I started it with emacs -Q and executed view-hello-file. In the screenshot on the right, I used set-fontset-font for changing the font that is used for characters of the script han and armenian. The sexps I used can be seen in the buffer below the HELLO buffer.

(set-fontset-font t 'han (font-spec :family "Noto Sans CJK SC" :size 40))
(set-fontset-font t 'armenian (font-spec :family "Noto Sans Armenian" :size 40))

send

Customize font for different writing systems in Firefox

In Firefox, I can change the font for Chinese characters or, in general, to any writing system by following these steps

  1. Opening "Settings"
  2. Click "General" in the left sidebar
  3. Go to the "Font" section
  4. Click the "Advanced" button
  5. Choose the writing system (see first screenshot below)
  6. Choose the desired fonts (see second screenshot below)

image

image

aartaka commented 1 year ago

I believe that WebKitGTK does not allow us to change much of fonts via settings it provides. In the documentation, there's only this set of functions to alter the font settings:

webkit_settings_get_default_font_family ()
webkit_settings_set_default_font_family ()
webkit_settings_get_monospace_font_family ()
webkit_settings_set_monospace_font_family ()
webkit_settings_get_serif_font_family ()
webkit_settings_set_serif_font_family ()
webkit_settings_get_sans_serif_font_family ()
webkit_settings_set_sans_serif_font_family ()
webkit_settings_get_cursive_font_family ()
webkit_settings_set_cursive_font_family ()
webkit_settings_get_fantasy_font_family ()
webkit_settings_set_fantasy_font_family ()
webkit_settings_get_pictograph_font_family ()
webkit_settings_set_pictograph_font_family ()
webkit_settings_get_default_font_size ()
webkit_settings_set_default_font_size ()
webkit_settings_get_default_monospace_font_size ()
webkit_settings_set_default_monospace_font_size ()
ebkit_settings_get_minimum_font_size ()
webkit_settings_set_minimum_font_size ()

It says nothing of writing system specific fonts, unfortunately. So, using the approach I used in my config can only get you so far (and I myself am not exactly satisfied with it).

As an alternative approach, you can customize fonts per-website with user styles and user scripts infrastructure of Nyxt, namely user-script-mode (if you're on one of Nyxt 3 pre-releases). User styles are perfectly togglable on a per-website basis, so you can easily configure those for websites that break with the default font. For example, here's how one can use Hack as the default font for GitHub, :include-ing it as the match pattern (see the description of those patterns in WebKit docs) for the script:

#+nyxt-3
(define-configuration nyxt/user-script-mode:user-script-mode
  ((nyxt/user-script-mode:user-styles
    (append (list
             (make-instance 'nyxt/user-script-mode:user-style
                            :include '("https://github.com/*")
                            :code "* {font-family: 'Hack', monospace;}"))
            %slot-value%))))

After that works, you can add more patterns to the :include list or add one more make-instance form to the list — but with a new website now.


I'm marking this as wontfix, as we can do nothing (@aadcg, can we? Maybe documentation?) about this issue. @rdrg109, if you have more questions or problems setting up user styles, feel free to ping me here. Closing this issue within a week, if there's no more details required on it (҂⌣̀_⌣́)

aadcg commented 1 year ago

I'm not sure if this is a wontfix, since I'm not convinced yet that WebKitGTK prevents us from implementation this feature. But it's definitely not worth spending time on this right now.

From my cursory look at Emacs' set-fontset-font, I don't see any dependency on GTK or its dependencies to achieve it. It seems that it defines a derived fontset starting from a base one. Regardless, I don't understand much about fonts.