;; font-menu.el --- Managing menus of fonts.;; Copyright (C) 1994 Free Software Foundation, Inc.;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.;; Copyright (C) 1997 Sun Microsystems;; Adapted from x-font-menu.el by Andy Piper <andy@xemacs.org>;; This file is part of XEmacs.;; XEmacs is free software; you can redistribute it and/or modify it;; under the terms of the GNU General Public License as published by;; the Free Software Foundation; either version 2, or (at your option);; any later version.;; XEmacs is distributed in the hope that it will be useful, but;; WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU;; General Public License for more details.;; You should have received a copy of the GNU General Public License;; along with XEmacs; see the file COPYING. If not, write to the;; Free Software Foundation, 59 Temple Place - Suite 330,;; Boston, MA 02111-1307, USA.;; This file contains the device-nospecific font menu stuff;;; Commentary:;;;;;; Creates three menus, "Font", "Size", and "Weight", and puts them on the;;; "Options" menu. The contents of these menus are the superset of those;;; properties available on any fonts, but only the intersection of the three;;; sets is selectable at one time.;;;;;; Known Problems:;;; ===============;;; Items on the Font menu are selectable if and only if that font exists in;;; the same size and weight as the current font. This means that some fonts;;; are simply not reachable from some other fonts - if only one font comes;;; in only one point size (like "Nil", which comes only in 2), you will never;;; be able to select it. It would be better if the items on the Fonts menu;;; were always selectable, and selecting them would set the size to be the;;; closest size to the current font's size.;;;;;; This attempts to change all other faces in an analogous way to the change;;; that was made to the default face; if it can't, it will skip over the face.;;; However, this could leave incongruous font sizes around, which may cause;;; some nonreversibility problems if further changes are made. Perhaps it;;; should remember the initial fonts of all faces, and derive all subsequent;;; fonts from that initial state.;;;;;; xfontsel(1) is a lot more flexible (but probably harder to understand).;;;;;; The code to construct menus from all of the x11 fonts available from the;;; server is autoloaded and executed the very first time that one of the Font;;; menus is selected on each device. That is, if XEmacs has frames on two;;; different devices, then separate font menu information will be maintained;;; for each X display. If the font path changes after emacs has already;;; asked the X server on a particular display for its list of fonts, this;;; won't notice. Also, the first time that a font menu is posted on each;;; display will entail a lengthy delay, but that's better than slowing down;;; XEmacs startup. At any time (i.e.: after a font-path change or;;; immediately after device creation), you can call;;; `reset-device-font-menus' to rebuild the menus from all currently;;; available fonts.;;;;;; There are at least three kinds of fonts under X11r5:;;;;;; - bitmap fonts, which can be assumed to look as good as possible;;;; - bitmap fonts which have been (or can be) automatically scaled to;;; a new size, and which almost always look awful;;;; - and true outline fonts, which should look ok at any size, but in;;; practice (on at least some systems) look awful at any size, and;;; even in theory are unlikely ever to look as good as non-scaled;;; bitmap fonts.;;;;;; It would be nice to get this code to look for non-scaled bitmap fonts;;; first, then outline fonts, then scaled bitmap fonts as a last resort.;;; But it's not clear to me how to tell them apart based on their truenames;;; and/or the result of XListFonts(). I welcome any and all explanations;;; of the subtleties involved...;;;;;;;;; If You Think You'Re Seeing A Bug:;;; =================================;;; When reporting problems, send the following information:;;;;;; - Exactly what behavior you're seeing;;;; - The output of the `xlsfonts' program;;;; - The value of the variable `device-fonts-cache';;;; - The values of the following expressions, both before and after;;; making a selection from any of the fonts-related menus:;;; (face-font 'default);;; (font-truename (face-font 'default));;; (font-properties (face-font 'default));;; - The values of the following variables after making a selection:;;; font-menu-preferred-resolution;;; font-menu-registry-encoding;;;;;; There is a common misconception that "*-courier-medium-r-*-11-*", also;;; known as "-adobe-courier-medium-r-normal--11-80-100-100-m-60-iso8859-1",;;; is an 11-point font. It is not -- it is an 11-pixel font at 100dpi,;;; which is an 8-point font (the number after -11- is the size in tenths;;; of points). So if you expect to be seeing an "11" entry in the "Size";;; menu and are not, this may be why.;;;;;; In the real world (aka Solaris), one has to deal with fonts that;;; appear to be medium-i but are really light-r, and fonts that;;; resolve to different resolutions depending on the charset:;;;;;; (font-instance-truename;;; (make-font-instance "-*-mincho-medium-i-normal-*-*-*-*-*-*-*-jisx0201*-*"));;; ==>;;; "-morisawa-ryumin light kl-light-r-normal--10-100-72-72-m-50-jisx0201.1976-0";;;;;; (font-list "-dt-interface user-medium-r-normal-s*-*-*-*-*-*-*-*-*");;; ==>;;; ("-dt-interface user-medium-r-normal-s sans-12-120-72-72-m-70-iso8859-1";;; "-dt-interface user-medium-r-normal-s-14-120-75-75-m-120-jisx0208.1983-0";;; "-dt-interface user-medium-r-normal-s-14-120-75-75-m-60-jisx0201.1976-0");;;###autoload(defcustomfont-menu-ignore-scaled-fontsnil"*If non-nil, the font menu shows only bitmap fonts.Bitmap fonts at their design size are generally noticeably higher quality thanscaled fonts, unless the device is capable of interpreting antialiasing hints.In general, setting this option non-`nil' is useful mostly on older X servers.Not all devices make the distinction between bitmap and scaled fonts.":type'boolean:group'font-menu);;;###autoload(defcustomfont-menu-this-frame-only-pnil"*If non-nil, the menu affects the default font only on the selected frame.":type'boolean:group'font-menu)(defcustomfont-menu-max-numbernil"The maximum number of fonts retrieved from the display.":type'integer:group'font-menu)(defvaralias'font-menu-max-items'menu-max-items)(defvaralias'font-menu-submenu-name-format'menu-submenu-name-format);; #### Need to update for fontconfig/Xft? Document form for MS Windows.(defvarfont-menu-preferred-resolution(make-specifier-and-init'generic'((global((mswindows).":")((gtk)."*-*")((x)."*-*")))t)"Generic specifier containing preferred resolution as a string.Do not `setq' this variable; use `set-specifier'.For X11 and GTK devices, the instance value will be interpolated into anXLFD, and looks like \"75-75\").")(defvarfont-menu-size-scaling(make-specifier-and-init'integer'((global((mswindows).1)((gtk).10)((x).10)))t)"Generic specifier containing scale factor for font sizes. Don't touch.This is really a device type constant. Some devices specify size in points\(MS Windows), others in decipoints (X11).")(defvardevice-fonts-cachenil"Alist mapping devices to font lists and font menus. Don't use this.Instead, use the function `device-fonts-cache' which lazily updates thisvariable, and returns the value for the selected device.Each element has the form (DEVICE . [FONT-LIST FAMILY SIZE WEIGHT]) whereFAMILY, SIZE, and WEIGHT denote menus.")(defsubstdevice-fonts-cache()(or(cdr(assq(selected-device)device-fonts-cache))(and(reset-device-font-menus(selected-device))(cdr(assq(selected-device)device-fonts-cache)))));;;###autoload(fset'install-font-menus'reset-device-font-menus)(make-obsolete'install-font-menus'reset-device-font-menus);;;###autoload(defunreset-device-font-menus(&optionaldevicedebug)"Generates the `Font', `Size', and `Weight' submenus for the Options menu.This is run the first time that a font-menu is needed for each device.If you don't like the lazy invocation of this function, you can add it to`create-device-hook' and that will make the font menus respond more quicklywhen they are selected for the first time. If you add fonts to your system,or if you change your font path, you can call this to re-initialize the menus."(if(ornoninteractive(not(ordevice(setqdevice(selected-device)))))nil(message"Getting list of fonts from server... ")(call-device-method'reset-device-font-menusdevicedevicedebug)(message"Getting list of fonts from server... done.")));;;###autoload(defunfont-menu-family-constructor(ignored)(catch'menu(unless(console-on-window-system-p)(throw'menu'(["Cannot parse current font"dingnil])))(let*((dcache(device-fonts-cache))(font-data(font-menu-font-data'defaultdcache))(entry(areffont-data0))(family(areffont-data1))(size(areffont-data2))(weight(areffont-data3))f)(unlessfamily(throw'menu'(["Cannot parse current font"dingnil])));; Items on the Font menu are enabled iff that font exists in;; the same size and weight as the current font (scalable fonts;; exist in every size). Only the current font is marked as;; selected.(menu-split-long-menu(mapcar(lambda(item)(setqf(menu-item-strip-accelerator-spec(arefitem0))entry(vassocf(arefdcache0)))(if(and(or(memberweight(arefentry1));; mswindows often allows any weight(member""(arefentry1)))(or(membersize(arefentry2))(and(notfont-menu-ignore-scaled-fonts)(member0(arefentry2)))))(enable-menu-itemitem)(disable-menu-itemitem))(if(string-equalfamilyf)(select-toggle-menu-itemitem)(deselect-toggle-menu-itemitem))item)(arefdcache1))))))(define-device-method*font-menu-font-data);;;###autoload(defunfont-menu-size-constructor(ignored)(catch'menu(unless(console-on-window-system-p)(throw'menu'(["Cannot parse current font"dingnil])))(let*((dcache(device-fonts-cache))(font-data(font-menu-font-data'defaultdcache))(entry(areffont-data0))(family(areffont-data1))(size(areffont-data2));;(weight (aref font-data 3))s)(unlessfamily(throw'menu'(["Cannot parse current font"dingnil])));; Items on the Size menu are enabled iff current font has;; that size. Only the size of the current font is selected.;; (If the current font comes in size 0, it is scalable, and;; thus has every size.)(mapcar(lambda(item)(setqs(nth3(arefitem1)))(if(or(members(arefentry2))(and(notfont-menu-ignore-scaled-fonts)(member0(arefentry2))))(enable-menu-itemitem)(disable-menu-itemitem));; #### God save the Queen!;; well, if this fails because s or size is non-numeric, fuck 'em(if(=size(if(featurep'xft-fonts)(floats)s))(select-toggle-menu-itemitem)(deselect-toggle-menu-itemitem))item)(submenu-generate-accelerator-spec(arefdcache2))))));;;###autoload(defunfont-menu-weight-constructor(ignored)(catch'menu(unless(console-on-window-system-p)(throw'menu'(["Cannot parse current font"dingnil])))(let*((dcache(device-fonts-cache))(font-data(font-menu-font-data'defaultdcache))(entry(areffont-data0))(family(areffont-data1));;(size (aref font-data 2))(weight(areffont-data3))w)(unlessfamily(throw'menu'(["Cannot parse current font"dingnil])));; Items on the Weight menu are enabled iff current font;; has that weight. Only the weight of the current font;; is selected.(mapcar(lambda(item)(setqw(arefitem0))(if(memberw(arefentry1))(enable-menu-itemitem)(disable-menu-itemitem))(if(string-equalweightw)(select-toggle-menu-itemitem)(deselect-toggle-menu-itemitem))item)(submenu-generate-accelerator-spec(arefdcache3))))));;; Changing font sizes(defunfont-menu-set-font(familyweightsize);; This is what gets run when an item is selected from any of the three;; fonts menus. It needs to be rather clever.;; (size is measured in 10ths of points.)(let*((dcache(device-fonts-cache))(font-data(font-menu-font-data'defaultdcache))(from-family(areffont-data1))(from-size(areffont-data2))(from-weight(areffont-data3))(from-slant(areffont-data4))(face-list-to-change(delq'default(face-list)))new-default-face-font)(unlessfrom-family(signal'error'("couldn't parse font name for default face")))(whenweight(signal'error'("Setting weight currently not supported")))(setqnew-default-face-font(font-instance-name(font-menu-load-font(orfamilyfrom-family)(orweightfrom-weight)(orsizefrom-size)from-slant(specifier-instancefont-menu-preferred-resolution(selected-device)))));; #### This is such a gross hack. The border-glyph face under;; mswindows is in a symbol font. Thus it will not appear in the;; cache - being a junk family. What we should do is change the;; size but not the family, but this is more work than I care to;; invest at the moment.(when(eq(device-type)'mswindows)(setqface-list-to-change(delq'border-glyphface-list-to-change)))(dolist(faceface-list-to-change)(when(face-font-instanceface)(message"Changing font of `%s'..."face)(condition-casec(font-menu-change-facefacefrom-familyfrom-weightfrom-size(orfamilyfrom-family)(orweightfrom-weight)(orsizefrom-size))(error(message"Error updating font of `%s'"face)(display-errorcnil)(sit-for1)))));; Set the default face's font after hacking the other faces, so that;; the frame size doesn't change until we are all done.;; If we need to be frame local we do the changes ourselves.(iffont-menu-this-frame-only-p;;; WMP - we need to honor font-menu-this-frame-only-p here!(set-face-font'defaultnew-default-face-font(andfont-menu-this-frame-only-p(selected-frame)));; OK Let Customize do it.(let((fsize(if(featurep'xft-fonts)(int-to-string(orsizefrom-size))(concat(int-to-string(/(orsizefrom-size)(specifier-instancefont-menu-size-scaling(selected-device))))"pt")))new-spec-list);; If the font was initialised from X resources (the tag-set;; contains 'x-resource) pretend to Custom that it has;; responsibility for those settings.(map-specifier(face-font'default)(lambda(speclocaleinst-listarg)(loopfor(tag-set.inst)ininst-listdo(setqtag-set(delq'x-resourcetag-set)tag-set(delq'customtag-set)tag-set(cons'customtag-set))(push(constag-setinst)new-spec-list);; Need to return nil, else map-specifier stopsfinallyreturnnil))nilnil'(x-resource))(remove-specifier(face-font'default)nil'(x-resource))(whennew-spec-list(add-spec-list-to-specifier(face-font'default)(list(cons'globalnew-spec-list))))(custom-set-face-update-spec'default(list(list'type(device-type)))(list:family(orfamilyfrom-family):sizefsize))))(message"Font %s"(face-font-name'default))));; #### This should be called `font-menu-maybe-change-face';; I wonder if a better API wouldn't (face attribute from to)(defunfont-menu-change-face(facefrom-familyfrom-weightfrom-sizeto-familyto-weightto-size)"Maybe update the font of FACE per TO-FAMILY, TO-WEIGHT, and TO-SIZE."(check-typefacesymbol)(let*((dcache(device-fonts-cache))(font-data(font-menu-font-datafacedcache))(face-family(areffont-data1))(face-size(areffont-data2))(face-weight(areffont-data3))(face-slant(areffont-data4)))(orface-family(signal'error(list"couldn't parse font name for face"face)));; If this face matches the old default face in the attribute we;; are changing, then change it to the new attribute along that;; dimension. Also, the face must have its own global attribute.;; If its value is inherited, we don't touch it. If any of this;; is not true, we leave it alone.(when(and(face-fontface'global)(cond(to-family(string-equalface-familyfrom-family))(to-weight(string-equalface-weightfrom-weight))(to-size(=face-sizefrom-size))))(set-face-fontface(font-instance-name(font-menu-load-font(orto-familyface-family)(orto-weightface-weight)(orto-sizeface-size)face-slant(specifier-instancefont-menu-preferred-resolution(selected-device))))(andfont-menu-this-frame-only-p(selected-frame))))))(define-device-methodfont-menu-load-font)(defunflush-device-fonts-cache(device);; by Stig@hackvan.com(let((elt(assqdevicedevice-fonts-cache)))(andelt(setqdevice-fonts-cache(delqeltdevice-fonts-cache)))))(add-hook'delete-device-hook'flush-device-fonts-cache)(provide'font-menu);; font-menu ends here