如何获得字体的x高度?

时间:2020-08-05 22:39:50

标签: fonts elisp

font-info返回一些字体的度量,例如上升和下降。如何获得x高度?


为确保问题正文符合Stack Overflow的质量标准,这是随机生成的句。我认为它实际上并没有增加任何东西,但是我该与谁争论呢?

Oh stack overflow
before common sense ruins
at the perfect ai

1 个答案:

答案 0 :(得分:0)

以下内容有效,但是1.它需要ttfdump(包含在Texlive中)和2.可能不适用于所有字体。

(defun my-xheight (font)
  "Return the x-height of FONT."
  (let* ((info (font-info font))
         (file (elt info 12))
         (ascent (elt info 8)))
    (round (* ascent (my-xheight-frac file)))))

(defun my-xheight-frac (file)
  "Return the x-height of a font as a fraction of its ascent height."
  (with-temp-buffer
    (let ((exitcode
           (call-process "ttfdump" nil t nil "-t" "OS/2" file)))
      (unless (= exitcode 0)
        (error (buffer-string))))
    (goto-char (point-min))
    (save-match-data
      (let ((sxheight (save-excursion
                        (search-forward-regexp (rx bol (0+ blank) "sxHeight:" (0+ blank)
                                                   (group (1+ digit))))
                        (string-to-number (match-string 1))))
            (ascent (save-excursion
                      (search-forward-regexp (rx bol (0+ blank) "usWinAscent:" (0+ blank)
                                                 (group (1+ digit))))
                      (string-to-number (match-string 1)))))
        (/ sxheight (float ascent))))))