M4444 / TMatrix

Terminal based replica of the digital rain from The Matrix.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

More Authentic Experience - Custom Font

bsodcat opened this issue · comments

Hey I stumbled upon this project after not being satisfied with the accuracy of cmatrix, I just got into linux and back into programming about 5 months ago and I would like to contribute some accurate aesthetic to this projects already extensible and simple code.

My first feature request would be to add a custom font file (to the default if at all possible) that is an exact font image of the one used in the movies. I recognize the amount of work this could be to handle, but in the next couple months I will try to be tackling it head on, making an accurate font image. If I run into any issues It might take a bit seeings how I’m not used to programming in anything but C++, but we will see how this goes.

Most if not all the characters are english/Japanese with mirrored versions every now and then and I feel as if this could be easily done, let me know if you have any ideas or any concerns, Sorry if this sounds a bit weird as this is my first time really using github.

Hey, I like your enthusiasm towards authenticity :)

Thoughts about about this have flown around before. One approach in creating a new font could be to replace some Latin characters with the katakana characters. A more elegant approach would be the replace the actual katakana characters with their flipped (and modified) versions. To do this one would need to figure out where the characters go in the font file. Most people on Arch install ttf-mplus to get fonts for Japanese characters so it could be useful to take a look at their font file and how it deals with this.

As for the looks there is for example [this design] that looks pretty spot on. Replacing the Japanese characters with something like this would solve the font side of the problem.

Now for the (perhaps) technically harder problem. The terminal pulls the font from the system so we have two parts that are outside the application. A perfect solution would be that the program changes the terminal font, runs and then changes it back to the original one. I didn't explore this deeply so I don't know how tractable this problem is but any partial solution would be a great step. For example changing the whole system font for Japanese characters (perhaps on installation), or changing the font for the current terminal session without returning it to the original.

Cmatrix has an option for changing the font to something weird in tty so exploring that might also be useful.

I've banged on this a lot in one of my pet projects, RoguePC (and its sibling PyRogue. So let me share my strategies and solutions, maybe some might be useful in TMatrix:

matrix() {
	if ! [[ "$TERM" == "linux" ]]; then
		cmatrix -sbu8  # X11, virtual TTY. See my next post
		return
	fi
	local default=$(mktemp) && trap 'rm -f -- "$default"' RETURN
	setfont -O "$default" matrix.psf  # Backup current font, set matrix PSF font
	cmatrix -sbxu8                    # run cmatrix, -x signals that font has japanese glyphs
	setfont "$default"                # restore previous font
}

For virtual terminals under X11, such as xterm or gnome-terminal, you need:

  • A TTF font. Just get any with Japanese glyphs for a great experience
  • Drop it somewhere under /usr/share/fonts/truetype, or have a run-time cmd-line option to copy it to ~/.fonts

For xterm:

  • Launch it using xterm -fa "$fontname" -fullscreen -bg black -tn 'xterm-256color'
  • $fontname is the font face name, not its filename. It's usually just the filename with spaces.
  • See my roguepc-xterm for more options on customizing xterm, some might be useful to Tmatrix.

For Gnome-Terminal:

  • A good strategy would be to create a terminal profile with all the custom settings, including the font. Similar to installing the TTF font, this could be done by installing a system-wide file or by a run-time cmd-line arg for the current user.
  • See gt_dconf() in my gnome-terminal-profile.install for a script that does that. It really just boils down to:
	# Created with: dconf dump "/org/gnome/terminal/legacy/profiles:/${profile}" | sort
	dconf load "/org/gnome/terminal/legacy/profiles:/${profile}" < <(sed -Ez 's/\n +//g' <<-EOF
		[/]
		background-color='#000000'
		font='Perfect DOS VGA 437 Unicode 24'
		foreground-color='#AAAAAA'
		palette=['#000000', '#AA0000', '#00AA00', '#AA5500',
		         '#0000AA', '#AA00AA', '#00AAAA', '#AAAAAA',
		         '#555555', '#FF5555', '#55FF55', '#FFFF55',
		         '#5555FF', '#FF55FF', '#55FFFF', '#FFFFFF']
		scrollbar-policy='never'
		use-system-font=false
		use-theme-colors=false
		use-theme-transparency=false
		use-transparent-background=false
		visible-name='$name'
	EOF
  • Only need to set the options that are different from default. Obviously font would be something else from the above.
  • The hard part is defining an appropriate ${profile} UUID. A new one can be generated with uuidgen, but we should avoid creating a new profile every time the install is triggered. All the needed logic for this is in gt_dconf(). Or you could simply use a hardcoded UUID.
  • Launch using gnome-terminal --window --profile "$profilename" --full-screen
  • See roguepc-gnome-terminal for more options.

Oh, this is very useful, you obviously know a lot about terminals. I won't be using matrix.psf because it has some characters that don't appear in the Matrix and it's also missing characters that do, so it doesn't really fit the goal of this project. I will look more deeply into the things you suggested so thanks for sharing your experience! Might contact you if I get stuck somewhere ;)