;; 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 analagous 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 is knowledge here about the regexp match numbers in;;; `mswindows-font-regexp' and `mswindows-font-regexp-foundry-and-family' defined in;;; mswindows-faces.el.;;;;;; 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";;;;;; (list-fonts "-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, then the font menu will try to show only bitmap fonts.":type'boolean:group'font-menu);;;###autoload(defcustomfont-menu-this-frame-only-pnil"*If non-nil, then changing the default font from the font menu will onlyaffect one frame instead of all frames.":type'boolean:group'font-menu)(defcustomfont-menu-max-items25"*Maximum number of items in the font menuIf number of entries in a menu is larger than this value, split menuinto submenus of nearly equal length. If nil, never split menu intosubmenus.":group'font-menu:type'(choice(const:tag"no submenus"nil)(integer)))(defcustomfont-menu-submenu-name-format"%-12.12s ... %.12s""*Format specification of the submenu name.Used by `font-menu-split-long-menu' if the number of entries in a menu islarger than `font-menu-menu-max-items'.This string should contain one %s for the name of the first entry andone %s for the name of the last entry in the submenu.If the value is a function, it should return the submenu name. Thefunction is be called with two arguments, the names of the first andthe last entry in the menu.":group'font-menu:type'(choice(string:tag"Format string")(function)))(defvarfont-menu-preferred-resolution(make-specifier-and-init'generic'((global((mswindows).":")((x)."*-*")))t)"Preferred horizontal and vertical font menu resolution (e.g. \"75:75\").")(defvarfont-menu-size-scaling(make-specifier-and-init'integer'((global((mswindows).1)((x).10)))t)"Scale factor used in defining font sizes.")(defunvassoc(keyvalist)"Search VALIST for a vector whose first element is equal to KEY.See also `assoc'.";; by Stig@hackvan.com(let(el)(catch'done(while(setqel(popvalist))(and(equalkey(arefel0))(throw'doneel))))));; only call XListFonts (and parse) once per device.;; ( (device . [parsed-list-fonts family-menu size-menu weight-menu]) ...)(defvardevice-fonts-cachenil)(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."(message"Getting list of fonts from server... ")(if(ornoninteractive(not(ordevice(setqdevice(selected-device)))))nil(call-device-method'reset-device-font-menusdevicedevicedebug)(message"Getting list of fonts from server... done.")))(defunfont-menu-split-long-menu(menu)"Split MENU according to `font-menu-max-items'."(let((len(lengthmenu)))(if(or(nullfont-menu-max-items)(null(featurep'lisp-float-type))(<=lenfont-menu-max-items))menu;; Submenu is max 2 entries longer than menu, never shorter, number of;; entries in submenus differ by at most one (with longer submenus first)(let*((outer(floor(sqrtlen)))(inner(/lenouter))(rest(%lenouter))(resultnil))(setqmenu(reversemenu))(whilemenu(let((ininner)(subnil)(to(carmenu)))(while(>in0)(setqin(1-in)sub(cons(carmenu)sub)menu(cdrmenu)))(setqresult(cons(cons(if(stringpfont-menu-submenu-name-format)(formatfont-menu-submenu-name-format(aref(carsub)0)(arefto0))(funcallfont-menu-submenu-name-format(aref(carsub)0)(arefto0)))sub)result)rest(1+rest))(if(=restouter)(setqinner(1+inner)))))result))));;;###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.(font-menu-split-long-menu(mapcar(lambda(item)(setqf(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))(if(eqsizes)(select-toggle-menu-itemitem)(deselect-toggle-menu-itemitem))item)(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)(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-fontnew-props)(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-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-sizefamilyweightsize)(error(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.(custom-set-face-update-spec'default(list(list'type(device-type)))(list:familyfamily:size(concat(int-to-string(/(orsizefrom-size)(specifier-instancefont-menu-size-scaling(selected-device))))"pt")))(message"Font %s"(face-font-name'default)))))(defunfont-menu-change-face(facefrom-familyfrom-weightfrom-sizeto-familyto-weightto-size)(or(symbolpface)(signal'wrong-type-argument(list'symbolpface)))(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-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