From e2c5fc9af5e23461dda47d1a15f15398dcc2c7d1 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Sat, 13 May 2023 03:18:20 -0400 Subject: [PATCH] Add docstrings to FIGletFontWriter --- cspell.json | 1 + src/textual_paint/repack_font.py | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/cspell.json b/cspell.json index 2e78bca..b1808e0 100644 --- a/cspell.json +++ b/cspell.json @@ -28,6 +28,7 @@ "Bresenham", "Bresenham's", "cmdpxl", + "Deutsch", "DIALOGEX", "Figlet", "Fira", diff --git a/src/textual_paint/repack_font.py b/src/textual_paint/repack_font.py index 3b67aff..9a8914d 100755 --- a/src/textual_paint/repack_font.py +++ b/src/textual_paint/repack_font.py @@ -30,8 +30,67 @@ def blankLines(num: int, width: int) -> str: return '\n'.join(lines) class FIGletFontWriter: + """Used to write FIGlet fonts. + + createFigFileData() returns a string that can be written to a .flf file. + + It can automatically fix some common problems with FIGlet fonts, such as + incorrect character widths/heights, and missing lowercase characters. + """ charOrder: list[int] = [ii for ii in range(32, 127)] + [196, 214, 220, 228, 246, 252, 223] + R"""Character codes that are required to be in any FIGlet font. + + Printable portion of the ASCII character set: +32 (blank/space) 64 @ 96 ` +33 ! 65 A 97 a +34 " 66 B 98 b +35 # 67 C 99 c +36 $ 68 D 100 d +37 % 69 E 101 e +38 & 70 F 102 f +39 ' 71 G 103 g +40 ( 72 H 104 h +41 ) 73 I 105 i +42 * 74 J 106 j +43 + 75 K 107 k +44 , 76 L 108 l +45 - 77 M 109 m +46 . 78 N 110 n +47 / 79 O 111 o +48 0 80 P 112 p +49 1 81 Q 113 q +50 2 82 R 114 r +51 3 83 S 115 s +52 4 84 T 116 t +53 5 85 U 117 u +54 6 86 V 118 v +55 7 87 W 119 w +56 8 88 X 120 x +57 9 89 Y 121 y +58 : 90 Z 122 z +59 ; 91 [ 123 { +60 < 92 \ 124 | +61 = 93 ] 125 } +62 > 94 ^ 126 ~ +63 ? 95 _ +Additional required Deutsch FIGcharacters, in order: +196 Ä (umlauted "A" -- two dots over letter "A") +214 Ö (umlauted "O" -- two dots over letter "O") +220 Ü (umlauted "U" -- two dots over letter "U") +228 ä (umlauted "a" -- two dots over letter "a") +246 ö (umlauted "o" -- two dots over letter "o") +252 ü (umlauted "u" -- two dots over letter "u") +223 ß ("ess-zed" -- see FIGcharacter illustration below) + ___ + / _ \ + | |/ / + Ess-zed >>---> | |\ \ + | ||_/ + |_| + +Additional characters must use code tagged characters, which are not yet supported. +""" def __init__( self, @@ -50,20 +109,35 @@ class FIGletFontWriter: caseInsensitive: bool = False ): self.fontName = fontName + """Name of the font.""" self.figChars: dict[int, str] = figChars + """Dictionary that maps character codes to FIGcharacter strings.""" self.height = height + """Height of a FIGcharacter, in characters.""" self.baseline = baseline + """Distance from the top of the character to the baseline. If not specified, defaults to height.""" self.maxLength = maxLength + """Maximum length of a line INCLUDING two endMark characters.""" self.commentLines: list[str] = commentLines + """List of comment lines to be included in the header. It's recommended to include at least the name of the font and the name of the author.""" self.rightToLeft = rightToLeft + """Indicates RTL writing direction (or LTR if False).""" self.codeTagCount = codeTagCount + """Number of extra characters included in the font (in addition to the required 102 untagged characters). Outputting tagged characters is not yet supported.""" self.hardBlank = hardBlank + """Invisible character used to prevent smushing.""" self.endMark = endMark + """Denotes the end of a line. Two of these characters in a row denotes the end of a FIGcharacter.""" self.horizontalLayout = horizontalLayout + """One of 'Full', 'Fitted', 'Universal Smushing', or 'Controlled Smushing'""" self.verticalLayout = verticalLayout + """One of 'Full', 'Fitted', 'Universal Smushing', or 'Controlled Smushing'""" self.hRule = [False] * 7 + """Horizontal Smushing Rules, 1-6 (0 is not used, so that indices correspond with the names of the parameters). horizontalLayout must be 'Controlled Smushing' for these to take effect.""" self.vRule = [False] * 6 + """Vertical Smushing Rules, 1-5 (0 is not used, so that indices correspond with the names of the parameters). verticalLayout must be 'Controlled Smushing' for these to take effect.""" self.caseInsensitive = caseInsensitive + """Makes lowercase same as uppercase. Note that this is one-way overwrite. It doesn't check if a character already exists, and it won't fill in uppercase using lowercase.""" def getOldLayoutValue(self) -> int: val = 0 @@ -177,6 +251,7 @@ class FIGletFontWriter: self.maxLength = maxWidth + 2 def createFigFileData(self) -> str: + """Generates the FIGlet file data for the current font.""" output = '' self.fixFigChars() @@ -193,6 +268,8 @@ class FIGletFontWriter: return output def extract_textures(image_path: str): + """Removes the border around glyphs in an image, saves a new image without the border, and converts the image into FIGlet format font files.""" + # Open the image image = Image.open(image_path)