How do I have Emacs load a font from a file? - linux

In the interest of making my emacs setup more portable, I'd like to be able to set the current font by specifying a file rather than a font name, i.e. "Load ~/config/myfont.ttf and use size 12". Is there a way to do that in my .emacs? All the instructions I've found assume the font is already installed on the system. I'm using the XFT support on Linux, so a linux specific hack would be OK but I'd prefer something that would work on all targets.
Update: To be clear, I'm using a font that isn't standard on Windows / OS X / Linux. I'm not just looking to set a different font based on platform, but to specify a specific font file that I have (TTFs work on Windows and Linux, if not on Mac I'll get another version of the file but I still want to specify the font via file rather than name).

Unfortunately, you can't.
Emacs on different platforms uses different windowing toolkits, all of which take care of font handling for it. I don't believe you can specify a font filename in Emacs on any platform - it just doesn't work that way.
As for how to find the font:
On Linux, you could use XFT's support for a user-specific font config file which is usually ~/.fonts.conf (but check /etc/fonts/font.conf to be sure) to add whatever directory you place your fonts into.
On a Mac, you can add the font into ~/Library/Fonts. TTFs work fine on Macs, BTW.
On Windows, I think you'd just have to add it to the system fonts directory.
From there, you then go and tell Emacs (through customize or not) to use your font. You'll find the naming schemes to be different on each platform (not sure what Windows looks like), but customize should help take care of this for you - just keep a separate customize file per machine if need be.
...so basically your portable Emacs setup has to encompass more than just an Emacs config file (which, given that you're carrying a font file around, it already does).

Related

Tkinter cross-platform compatability

I have written a set of customized tkinter widgets, defined as classes, and loaded into the main app as modules. I am working in Windows 10, but have specific concerns in three areas in regard to compatibility with Linux and Mac. These are shown below.
Fonts
I am sticking with tkinter default fonts, and defining the desired font within each individual custom widget. I have found, surprisingly, that I can successfully specify fonts as follows, naming 'TkDefaultFont' just as I might name 'Arial' for example.
font=('TkDefaultFont',11)
font=('TkDefaultFont',10,'bold')
font=('TkDefaultFont',10,'italic')
Would this approach work across Linux and Mac as well as windows?
Importing modules
All of the resources for my main app are stored in a Folder named 'AppAssets' (which is in the same folder as the main app). The custom widgets are stored inside that folder, in another folder named 'TkMods'. In Windows, I am successfully importing these modules as follows, specifying a relative path:
from AppAssets.TkMods import ModButton
Again, would this work across Linux and Mac? If not, is there a line or lines of code that would work instead across all three platforms?
Importing image files
Many of the modules use custom image files (such as a rounded button image, for example). I am importing these as follows, again specifying a relative path.
btnimg = tk.PhotoImage(file="AppAssets/TkMods/Button.png")
Again, would this work cross-platform? If not, is there a single solution that would work across Windows, Mac and Linux?
Any advice appreciated.
I have found, surprisingly, that I can successfully specify fonts as follows, naming 'TkDefaultFont' just as I might name 'Arial' for example...
Would this approach work across Linux and Mac as well as windows?
It works, but probably not the way you think. You could use 'NotARealFont' instead of 'TkDefaultFont' and get the same results. The first parameter when defining a font as a tuple is a font family, and TkDefaultFont is not the name of a valid font family. It's the name of an internal font object, which is not the same thing. When you don't give a valid font family, tkinter will fall back to using the font defined by TkDefaultFont.
I am successfully importing these modules as follows... Again, would this work across Linux and Mac?
Yes, importing python modules works the same on platforms. This isn't anything unique to tkinter.
Many of the modules use custom image files ... I am importing these as follows, again specifying a relative path... Again, would this work cross-platform?
It should work the same on all platforms. Note that "work the same" also means it will fail in the same way on all platforms. The path is relative to the current working directory which may or may not be the same as the directory with the script.

linux console how to change the codepage to dos cp437

I want to view some ansi-art on the linux local-console. (my setup:raspberry pi3 / newest raspbian - no x11)
i've tried many different settings in raspi-config, dpkg-reconfigure console-setup, /etc files, environment vars but i had no luck yet. do i need a special pcf font to get it working?
a reliable way to enable it for remote terminals would also be great.
thanks in advance
It depends on what your data uses (see chart). Codes 0..31 are a problem unless you have a program that can map those codes to a printable value (as noted in Why does showconsolefont have different output in tmux?, the showconsolefont program does this mapping of 0..31).
Most of the usable fonts for the Linux console are "psf" fonts: having a header which tells which Unicode values each glyph corresponds to. Using that, along with a known character set (cp437), you could convert the data or "play" it using an application which knows how to do this:
You could convert it using iconv or recode, or
The line-drawing (128..255) could be done using luit in a UTF-8 console.

Can Vim reference a font stored locally in .vim or vimfiles?

I have my Vim configuration under source control, and instead of moving to a new platform and having to install the specific Powerline font I use on every machine, I'd like to be able to simply store my font in something like ~/.vim/fonts/my-powerline-font.ttf, so I can use a command in my vimrc to reference it locally.
Is this possible?
Like any other application, Vim uses the operating system's APIs to use one of the installed fonts. If a font is not properly installed, Vim cannot use it.
So, you need to install any font first. You could automate that by invoking external commands in your ~/.vimrc, though. For example, on Linux, "installing" can be as easy as copying the font into ~/.fonts:
if ! file_readable($HOME . '/.fonts/my-powerline-font.ttf')
call system('cp ~/.vim/fonts/my-powerline-font.ttf ~/.fonts/')
fi

How to get list of programs which can open a particular file extension in Linux?

Basically I am trying to get list of programs in Linux which are installed and can open particular file extension .jpg for example. If not all, At-least default program should get listed.
Linux (the kernel) has no knowledge on file types to application mapping. If you want to use Gnome programs you can look at https://people.gnome.org/~shaunm/admin-guide/mimetypes-7.html. For KDE there is another mechanism. Each toolkit can define it as it likes. And the programmer can use the defaults or not. So it is simply application specific!
What do you want to achieve?
If you (double) click with a explorer/browser application on an icon or file name, exactly the explorer/browser looks for the file type. Typically this is realized via mime type dictionary. But how a program looks for the file type and maybe execute another program is only related to the programmer who writes that program. The GUI tool-chains like Gnome and KDE have a lot of support for that topic and so you have basic conformity for each family of applications.
If you want to know how a application do the job, start it with strace. But it is quite hard to dig into the huge amount of data.
Also you can take a look for xdg-open. Many programs use this helper to start applications. As an example: If you start Dolphin with strace you will find a line like lstat64("/etc/xdg", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 after clicking on a file.
you can run from command line with:
xdg-open <file-name>
You maybe also want to have a look for applications which registers for file types: /usr/share/applications/*.desktop
Here you can find in each desktop file some mime-types which are registered for the applications. E.g. for audiacity is:
MimeType=application/x-audacity-project;audio/flac;audio/x-flac;audio/basic;audio/x-aiff;audio/x-wav;application/ogg;audio/x-vorbis+ogg;
For your example with jpg:
$ xdg-mime query filetype <any-jpg-file>
image/jpeg
$ grep 'image/jpeg' -R /usr/share/applications/*
...
/usr/share/applications/mimeinfo.cache:image/jpeg2000=kde4-kolourpaint.desktop;gimp.desktop;
So you can see that gimp is one of the default applications for jpg
The place to start looking is at the mailcap (/etc/mailcap) and MIME-types, e.g., in /etc/mime.types in Debian (the filename and path will vary according to who provides it).
The mailcap file gives some rules for opening a file, while MIME-types lists the known filetypes with a tag that allows multiple applications to know about the file types.
Except for embedded or reduced-functionality systems (such as those based on busybox), you would find these files on almost every UNIX-like system.

How to prepare a Linux app to find ttf font directory?

I'm working on an application primary targeted for Linux, which use a TTF font. I need the font's file name and path, because I have to load it with SDL function TTF_OpenFont(char *file, ...). The problem is that there are a lot of different directories for TTF fonts on different distribution. Which is the best way to deal with this problem? I've came up some solutution, but each of them seems suboptimal for me:
pack the font along with the application, and install it to the application's own /usr/share/ directory.
check the font path with fc-list : file.
hardcode every path variation to the application and try them out when load the file.
Your first and second solutions are quite good, except it may be better to call FcFontList function. Third one is quite unreliable, but it highly depends on application type (it can be ok in some cases, if you have this path configurable by user).

Resources