Как получить x-высоту шрифта? - PullRequest
0 голосов
/ 06 августа 2020

font-info возвращает некоторые метрики шрифта, такие как подъем и спуск. Как получить х-высоту?

Чтобы убедиться, что текст вопроса соответствует стандартам качества Stack Overflow, вот случайно сгенерированное хайку. Я не думаю, что это на самом деле что-то добавляет, но кто я такой, чтобы спорить с эвристией c?

Oh stack overflow
before common sense ruins
at the perfect ai

1 Ответ

0 голосов
/ 06 августа 2020

Следующее работает, но 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))))))
...