]> code.delx.au - gnu-emacs/commitdiff
Add new functions for computing default font dimensions
authorTitus von der Malsburg <malsburg@posteo.de>
Sat, 21 Mar 2015 10:31:29 +0000 (12:31 +0200)
committerEli Zaretskii <eliz@gnu.org>
Sat, 21 Mar 2015 10:31:29 +0000 (12:31 +0200)
 lisp/window.el (window-font-width, window-font-height)
 (window-max-chars-per-line): New functions.
 lisp/simple.el (default-font-height): Doc fix.
 (default-font-width): New function.

 etc/NEWS: Mention `default-font-width', `window-font-height',
 `window-font-width', and `window-max-chars-per-line'.

etc/ChangeLog
etc/NEWS
lisp/ChangeLog
lisp/simple.el
lisp/window.el

index cd5c54037c81207a1f78121184d180256b5cebbd..c94e12296edb896510b337aaf4aabd61adb95ced 100644 (file)
@@ -1,3 +1,8 @@
+2015-03-21  Titus von der Malsburg  <malsburg@posteo.de>
+
+       * NEWS: Mention `default-font-width', `window-font-height',
+       `window-font-width', and `window-max-chars-per-line'.
+
 2015-03-03  Kelvin White  <kwhite@gnu.org>
 
        * NEWS.24: Add section to include ERC changes.
index cabd0087d92c11c65152a336ab07ee1e316f46a4..7cdb24b402e8562401cbc31e5671c3a1fe808cef 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -173,6 +173,24 @@ possible inaccuracies in the end position.
 In particular, it now returns the average width of the font's
 characters, which can be used for geometry-related calculations.
 
+** A new function `default-font-width' returns the average width of a
+character in the current buffer's default font.  If the default face
+is remapped (see `face-remapping-alist'), the value for the remapped
+face is returned.  This function complements the existing function
+`default-font-height'.
+
+** New functions `window-font-height' and `window-font-width' return
+the height and average width of characters in a specified face and
+window.  If FACE is remapped (see `face-remapping-alist'), the
+function returns the information for the remapped face.
+
+** A new function `window-max-chars-per-line' returns the maximal
+number of characters that can be displayed on one line.  If a face
+and/or window are provided, these values are used for the
+calculation.  This function is different from `window-body-width' in
+that it accounts for (i) continuation glyphs, (ii) the size of the
+font, and (iii) the specified window.
+
 \f
 * Editing Changes in Emacs 25.1
 
index 1cfefaa39dfe7045cca54bbe30c2b84235737696..2f9c430e45db74a0b1e8265ad14c008b9be6875d 100644 (file)
@@ -1,3 +1,11 @@
+2015-03-21  Titus von der Malsburg  <malsburg@posteo.de>
+
+       * window.el (window-font-width, window-font-height)
+       (window-max-chars-per-line): New functions.
+
+       * simple.el (default-font-height): Doc fix.
+       (default-font-width): New function.
+
 2015-03-21  Tassilo Horn  <tsdh@gnu.org>
 
        * emacs-lisp/lisp-mode.el (lisp-el-font-lock-keywords-1): Also
index 98188a07b6f55fe37aaf82eb47859777ba60c705..f7f35564f35c735de8f155cd2ed3630a9b91af8b 100644 (file)
@@ -5409,7 +5409,10 @@ lines."
 (declare-function font-info "font.c" (name &optional frame))
 
 (defun default-font-height ()
-  "Return the height in pixels of the current buffer's default face font."
+  "Return the height in pixels of the current buffer's default face font.
+
+If the default font is remapped (see `face-remapping-alist'), the
+function returns the height of the remapped face."
   (let ((default-font (face-font 'default)))
     (cond
      ((and (display-multi-font-p)
@@ -5420,6 +5423,25 @@ lines."
       (aref (font-info default-font) 3))
      (t (frame-char-height)))))
 
+(defun default-font-width ()
+  "Return the width in pixels of the current buffer's default face font.
+
+If the default font is remapped (see `face-remapping-alist'), the
+function returns the width of the remapped face."
+  (let ((default-font (face-font 'default)))
+    (cond
+     ((and (display-multi-font-p)
+          ;; Avoid calling font-info if the frame's default font was
+          ;; not changed since the frame was created.  That's because
+          ;; font-info is expensive for some fonts, see bug #14838.
+          (not (string= (frame-parameter nil 'font) default-font)))
+      (let* ((info (font-info (face-font 'default)))
+            (width (aref info 11)))
+       (if (> width 0)
+           width
+         (aref info 10))))
+     (t (frame-char-width)))))
+
 (defun default-line-height ()
   "Return the pixel height of current buffer's default-face text line.
 
index cc8a7fc402f34ec162ef9e207acf5ff220b067ee..d17605099a0aadc60c7cef6dd2e3a426267615fa 100644 (file)
@@ -1835,6 +1835,61 @@ optional argument PIXELWISE is passed to the functions."
       (window-body-width window pixelwise)
     (window-body-height window pixelwise)))
 
+(defun window-font-width (&optional window face)
+   "Return average character width for the font of FACE used in WINDOW.
+WINDOW must be a live window and defaults to the selected one.
+
+If FACE is nil or omitted, the default face is used.  If FACE is
+remapped (see `face-remapping-alist'), the function returns the
+information for the remapped face."
+   (with-selected-window (window-normalize-window window t)
+     (if (display-multi-font-p)
+        (let* ((face (if face face 'default))
+               (info (font-info (face-font face)))
+               (width (aref info 11)))
+          (if (> width 0)
+             width
+            (aref info 10)))
+       (frame-char-width))))
+
+(defun window-font-height (&optional window face)
+   "Return character height for the font of FACE used in WINDOW.
+WINDOW must be a live window and defaults to the selected one.
+
+If FACE is nil or omitted, the default face is used.  If FACE is
+remapped (see `face-remapping-alist'), the function returns the
+information for the remapped face."
+   (with-selected-window (window-normalize-window window t)
+     (if (display-multi-font-p)
+        (let* ((face (if face face 'default))
+               (info (font-info (face-font face))))
+          (aref info 3))
+       (frame-char-height))))
+
+(defun window-max-chars-per-line (&optional window face)
+  "Return the number of characters that can be displayed on one line in WINDOW.
+WINDOW must be a live window and defaults to the selected one.
+
+The character width of FACE is used for the calculation.  If FACE
+is nil or omitted, the default face is used.  If FACE is
+remapped (see `face-remapping-alist'), the function uses the
+remapped face.
+
+This function is different from `window-body-width' in two
+ways.  First, it accounts for the portions of the line reserved
+for the continuation glyph.  Second, it accounts for the size of
+the font."
+  (with-selected-window (window-normalize-window window t)
+    (let* ((window-width (window-body-width window t))
+          (font-width (window-font-width window face))
+          (ncols (/ window-width font-width)))
+      (if (and (display-graphic-p)
+              overflow-newline-into-fringe
+              (/= (frame-parameter nil 'left-fringe) 0)
+              (/= (frame-parameter nil 'right-fringe) 0))
+         ncols
+       (1- ncols)))))
+
 (defun window-current-scroll-bars (&optional window)
   "Return the current scroll bar types for WINDOW.
 WINDOW must be a live window and defaults to the selected one.