Skip to content

Commit

Permalink
Added additional fonts for better glyph coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ESultanik committed Jun 24, 2022
1 parent c383bb1 commit f2f5ace
Show file tree
Hide file tree
Showing 19 changed files with 395 additions and 122 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ optional arguments:

Cast2Gif is licensed and distributed under the [AGPLv3](LICENSE) license. [Contact us](mailto:[email protected]) if you’re looking for an exception to the terms.

Adobe's SourceCodePro font is distributed with Cast2Gif and is licensed under the [SIL OPEN FONT LICENSE Version 1.1](cast2gif/Source_Code_Pro/OFL.txt).
Several fonts are distributed with Cast2Gif. They are licensed as follows:
- **Fira Code Nerd Font**: Copyright © 2014, The Fira Code Project Authors and distributed under the
[SIL Open Font License, Version 1.1](cast2gif/fonts/FiraCode/LICENSE)
- **Hack Nerd Font**: Copyright © 2018, Source Foundry Authors and distributed under the
[MIT License](cast2gif/fonts/Hack/LICENSE.md)
- **Adobe SourceCodePro**: Copyright © 2010, 2012 Adobe Systems Incorporated and distributed under the
[SIL Open Font License, Version 1.1](cast2gif/fonts/SourceCodePro/OFL.txt)
27 changes: 15 additions & 12 deletions cast2gif/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import argparse
import os
from pathlib import Path
import sys

from PIL import ImageFont

from . import __version_name__
from .asciicast import AsciiCast
from .fonts import FontCollection
from .recording import TerminalRecording


Expand Down Expand Up @@ -90,8 +90,9 @@ def main(argv=None):
parser.add_argument(
"--font",
type=str,
default=None,
help="Path to a TrueType font for rendering; defaults to SourceCodePro",
action="append",
help="Path to a TrueType font for rendering; defaults to SourceCodePro; this argument can be supplied multiple "
"times, with additional fonts used in the event that one is missing a required glyph",
)
parser.add_argument(
"-s", "--font-size", type=int, default=12, help="Font size (default=12)"
Expand Down Expand Up @@ -138,13 +139,6 @@ def main(argv=None):
print(__version_name__)
sys.exit(0)

if args.font is None:
args.font = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"Source_Code_Pro",
"SourceCodePro-Regular.ttf",
)

if args.ASCIICAST is None and not args.exec:
parser.print_usage(sys.stderr)
sys.stderr.write("\nerror: you must provide either an AsciiCast v2 input file or use the `--exec` argument\n")
Expand Down Expand Up @@ -188,7 +182,16 @@ def main(argv=None):
sys.exit(1)
output_stream = open(args.output, "wb")
try:
font = ImageFont.truetype(font=args.font, size=args.font_size)
if not args.font:
font_dir = Path(__file__).absolute().parent / "fonts"
font = FontCollection(
font_dir / "FiraCode" / "Fira Code Regular Nerd Font Complete Mono.otf",
font_dir / "Hack" / "Hack Regular Nerd Font Complete Mono.ttf",
font_dir / "SourceCodePro" / "SourceCodePro-Regular.ttf",
size=args.font_size
)
else:
font = FontCollection(*args.font, size=args.font_size)

if args.quiet or not sys.stderr.isatty():
status_logger = None
Expand Down
84 changes: 84 additions & 0 deletions cast2gif/fonts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from functools import lru_cache
from pathlib import Path
from typing import List, Iterator, Optional, Sequence, Tuple, Union

from PIL import ImageFont
from PIL.ImageFont import FreeTypeFont
from fontTools.ttLib import TTFont

from .sets import OrderedMutableSet


class FontCollection:
def __init__(self, *fonts: Union[str, Path, FreeTypeFont, TTFont], size: int):
self.size: int = size
self._fonts: List[FreeTypeFont] = []
self._ttfonts: List[TTFont] = []
for font in fonts:
self.add(font)

def __iter__(self) -> Iterator[FreeTypeFont]:
return iter(self._fonts)

def __len__(self):
return len(self._fonts)

def __getitem__(self, index: int) -> FreeTypeFont:
return self._fonts[index]

def with_size(self, size: int) -> "FontCollection":
return FontCollection(*self._fonts, size=size)

@property
def fonts(self) -> Sequence[FreeTypeFont]:
return self._fonts

def add(self, font: Union[str, Path, FreeTypeFont]):
if isinstance(font, FreeTypeFont):
if any(f.path == font.path for f in self._fonts):
# we already added this font
return
if font.size != self.size:
font = ImageFont.truetype(font=font.path, size=self.size)
self._fonts.append(font)
self._ttfonts.append(TTFont(font.path))
else:
font = str(Path(font).expanduser().absolute())
if any(f.path == font for f in self._fonts):
# we already added this font
return
self._fonts.append(ImageFont.truetype(font=font, size=self.size))
self._ttfonts.append(TTFont(font))

@lru_cache(maxsize=1024)
def fonts_satisfying(self, text: str) -> OrderedMutableSet[FreeTypeFont]:
"""Returns the set of fonts that satisfy every glyph in the text"""
if len(text) == 0:
return OrderedMutableSet(self.fonts)
if len(text) > 1:
fonts: OrderedMutableSet[FreeTypeFont] = OrderedMutableSet(self.fonts)
for c in text:
fonts &= self.fonts_satisfying(c)
if not fonts:
return fonts
return fonts
# len(text) == 1 here
fonts = OrderedMutableSet()
for font, ttfont in zip(self._fonts, self._ttfonts):
for table in ttfont["cmap"].tables:
if ord(text) in table.cmap.keys():
fonts.add(font)
return fonts

@lru_cache(maxsize=1024)
def getsize(self, for_text: str) -> Tuple[int, int]:
return self.get_font(for_text).getsize(for_text)

def get_font(self, for_text: str) -> Optional[FreeTypeFont]:
try:
return next(iter(self.fonts_satisfying(for_text)))
except StopIteration:
try:
return next(iter(self.fonts))
except StopIteration:
return None
Binary file not shown.
93 changes: 93 additions & 0 deletions cast2gif/fonts/FiraCode/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright (c) 2014, The Fira Code Project Authors (https://github.com/tonsky/FiraCode)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
45 changes: 45 additions & 0 deletions cast2gif/fonts/Hack/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
The work in the Hack project is Copyright 2018 Source Foundry Authors and licensed under the MIT License

The work in the DejaVu project was committed to the public domain.

Bitstream Vera Sans Mono Copyright 2003 Bitstream Inc. and licensed under the Bitstream Vera License with Reserved Font Names "Bitstream" and "Vera"

### MIT License

Copyright (c) 2018 Source Foundry Authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

### BITSTREAM VERA LICENSE

Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is a trademark of Bitstream, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of the fonts accompanying this license ("Fonts") and associated documentation files (the "Font Software"), to reproduce and distribute the Font Software, including without limitation the rights to use, copy, merge, publish, distribute, and/or sell copies of the Font Software, and to permit persons to whom the Font Software is furnished to do so, subject to the following conditions:

The above copyright and trademark notices and this permission notice shall be included in all copies of one or more of the Font Software typefaces.

The Font Software may be modified, altered, or added to, and in particular the designs of glyphs or characters in the Fonts may be modified and additional glyphs or characters may be added to the Fonts, only if the fonts are renamed to names not containing either the words "Bitstream" or the word "Vera".

This License becomes null and void to the extent applicable to Fonts or Font Software that has been modified and is distributed under the "Bitstream Vera" names.

The Font Software may be sold as part of a larger software package but no copy of one or more of the Font Software typefaces may be sold by itself.

THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.

Except as contained in this notice, the names of Gnome, the Gnome Foundation, and Bitstream Inc., shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Font Software without prior written authorization from the Gnome Foundation or Bitstream Inc., respectively. For further information, contact: fonts at gnome dot org.
Loading

0 comments on commit f2f5ace

Please sign in to comment.