2

I often use text scaling, but it only seems to work on the default face. This is a problem, for instance, when I write text that also has italics (e.g., markdown, org text). The text scale commands change the size of the text with the default face but do not change the size of the text with italics face.

I define these faces like this (Inconsolata does not have an italic version):

(set-face-attribute  'default nil
                     :family "Inconsolata" 
                     :height 130
                     :foreground "gray90"
                     :background "gray25")

(set-face-attribute  'italic nil
                     :family "Cousine" 
                     :height 100
                     :slant 'italic
                     :underline nil)

Is there a way to make the text scale commands work on the italic face as well?

2 Answers 2

1

As text-scale-mode docstring says

... The amount of scaling is determined by the variable text-scale-mode-amount: one step scales the global default face size by the value of the variable text-scale-mode-step (a negative amount shrinks the text).

The text-scale-increase, text-scale-decrease, and text-scale-set functions may be used to interactively modify the variable text-scale-mode-amount (they also enable or disable `text-scale-mode' as necessary)."

So only the default face is affected. Looking at face-remap.el, we see this works by setting the variable face-remapping-alist, whose docstring includes:

Alist of face remappings. Each element is of the form:

(FACE . REPLACEMENT),

which causes display of the face FACE to use REPLACEMENT instead. REPLACEMENT is a face specification, i.e. one of the following:

(1) a face name

(2) a property list of attribute/value pairs, or

(3) a list in which each element has the form of (1) or (2).

Indeed if we display this variable value w/o text-scale-mode we see:

face-remapping-alist is a variable defined in ‘C source code’. Its value is nil

And after having typed C-x C-+:

Its value is ((default (:height 1.2) default))

Directly setting face-remapping-alist seems to work, ie try:

(setq face-remapping-alist '((default (:height 2.4) default)
                             (italic (:height 2.4) italic)))

The functions used by text-scale-mode to change face-remapping-alist are face-remap-add-relative and face-remap-remove-relative. We could modify text-scale-mode to call them for both 'default and 'italic fonts. Alternatively, we can advise both functions so that a remapping for the 'italic font is added/removed as well. Here's the code to do that:

(defun my/face-remap-add-relative-advice (face &rest specs)
  "advice to add remap for italic font"
  (if (eq face 'default)
      (setq my/italic-cookie (apply #'face-remap-add-relative 'italic specs))))

(defun my/face-remap-remove-relative-advice (cookie)
  "advice to remove remap for italic font"
  (if (eq (car cookie) 'default)
      (face-remap-remove-relative my/italic-cookie)))

(advice-add #'face-remap-add-relative :after #'my/face-remap-add-relative-advice)
(advice-add #'face-remap-remove-relative :after #'my/face-remap-remove-relative-advice)

That's a quick hack, it would need more work to handle more fonts, the clean solution may be to change text-scale-mode so that it can handle any font, but it seems to do what you need.

1
  • Thanks, it works. For better text scale appearance we can handle not only italic but also bold face.
    – xinfa tang
    May 5 at 6:29
-1

How about not setting :height 100 for "Cousine", but use face-font-rescale-alist for "Cousine" to set the ratio of it to "Inconsolata".

1
  • Is it an answer or just a possible hint (in which case it would better fit as a comment). If it does answer the question, could you please expand it?
    – JeanPierre
    Dec 22, 2016 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.