From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Titus von der Malsburg Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Implement functions for measuring fonts and max chars per line Date: Sat, 17 Jan 2015 10:13:27 -0800 Message-ID: <874mrp46ns.fsf@posteo.de> References: <87k31j3g03.fsf@posteo.de> <87d27b2ugp.fsf@posteo.de> <87zjaeo7eg.fsf@posteo.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" X-Trace: ger.gmane.org 1421518451 3384 80.91.229.3 (17 Jan 2015 18:14:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 17 Jan 2015 18:14:11 +0000 (UTC) Cc: Eli Zaretskii To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jan 17 19:14:11 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YCXsv-00039A-JZ for ged-emacs-devel@m.gmane.org; Sat, 17 Jan 2015 19:14:09 +0100 Original-Received: from localhost ([::1]:59957 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YCXsu-00022O-PL for ged-emacs-devel@m.gmane.org; Sat, 17 Jan 2015 13:14:08 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YCXsW-0001Xx-Lq for emacs-devel@gnu.org; Sat, 17 Jan 2015 13:13:45 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YCXsU-0006ON-Ps for emacs-devel@gnu.org; Sat, 17 Jan 2015 13:13:44 -0500 Original-Received: from mx02.posteo.de ([89.146.194.165]:37268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YCXsQ-0006M2-U3; Sat, 17 Jan 2015 13:13:39 -0500 Original-Received: from dovecot03.posteo.de (unknown [185.67.36.28]) by mx02.posteo.de (Postfix) with ESMTPS id D9B6E23705F; Sat, 17 Jan 2015 19:13:36 +0100 (CET) Original-Received: from mail.posteo.de (localhost [127.0.0.1]) by dovecot03.posteo.de (Postfix) with ESMTPSA id 3kPnSb2QSKz5vN8; Sat, 17 Jan 2015 19:13:35 +0100 (CET) In-reply-to: <87zjaeo7eg.fsf@posteo.de> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 89.146.194.165 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:181379 Archived-At: --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable I signed and submitted the copyright forms a while ago. As far as I'm concerned this patch is ready to be merged. Below is an updated version. Best, Titus diff --git a/etc/NEWS b/etc/NEWS index be283bb..603bbc8 100644 =2D-- a/etc/NEWS +++ b/etc/NEWS @@ -165,6 +165,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. =20 +** 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. + * Editing Changes in Emacs 25.1 =20 diff --git a/lisp/simple.el b/lisp/simple.el index 25293ed..3369606 100644 =2D-- a/lisp/simple.el +++ b/lisp/simple.el @@ -5377,7 +5377,10 @@ lines." (declare-function font-info "font.c" (name &optional frame)) =20 (defun default-font-height () =2D "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) @@ -5388,6 +5391,25 @@ lines." (aref (font-info default-font) 3)) (t (frame-char-height))))) =20 +(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=3D (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. =20 diff --git a/lisp/window.el b/lisp/window.el index abc6006..1606f6c 100644 =2D-- a/lisp/window.el +++ b/lisp/window.el @@ -1835,6 +1835,61 @@ optional argument PIXELWISE is passed to the functio= ns." (window-body-width window pixelwise) (window-body-height window pixelwise))) =20 +(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 WI= NDOW. +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 + (/=3D (frame-parameter nil 'left-fringe) 0) + (/=3D (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. --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJUuqZIAAoJEK40WfPGE2idgnEIAMGC48pEw2XcA5eAVS3XEFbA xcHzEqECHxQ1MxnNml5IB1k/qbZFR9e9U7jPCXbQhz6n2WonIVQtKcQhYheZ3vjQ oCRALo6qYxsQVNx5UlcVY8ihYcWY7A1YJIFUpGvSYWz35uED93raWirzBzv8qalP umS5skACg3nxQK9m8czHD8gAyF0Z/67AHtv3xeppw63au4Fu5aAg9u6DZMHpUc0r /SxQgGLXmN3bMo1ixgh0ilnkmcrXC6LBqS7EC9TXZ8ldYx8oXBl6QlyzDUWZ1Ky0 8mXidZSFTnPt8FKIH/dNTtXg/cYRzEz46GW+GZ/ZMgsgQiA+zySiaDCwQHfQXKY= =zZ4e -----END PGP SIGNATURE----- --=-=-=--