Base parsing of Figlet fonts

Will be used later to fancy-print text!
This commit is contained in:
Jaidyn Ann 2023-06-29 15:50:15 -05:00
parent d0cb621320
commit 919dcd4d9a
2 changed files with 82 additions and 12 deletions

67
figlet.lisp Normal file
View File

@ -0,0 +1,67 @@
;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
;;;;
;;;; This program 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 3 of
;;;; the License, or (at your option) any later version.
;;;;
;;;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;;; FIGLET
;;;; A package for parsing Figlet fonts into simple associative lists, for
;;;; devious text-rendering purposes.
(in-package :figlet)
(defun string->integer (string)
"Convert a string to a number, potentially in 0x hexadecimal form.
If no number is parsed out, return NIL."
(let ((radix (if (str:starts-with-p "0x" string)
16 10))
(string (if (str:starts-with-p "0x" string)
(subseq string 2) string)))
(ignore-errors (parse-integer string :radix radix))))
(defun parse-lines (lines &optional (font-plist '()) (current-symbol 32))
"Parse a list of lines from a Figlet font-file (.FLF) into a plist
associating a character with its respective string in the font-file.
(#\A \"TEXT-ART-A\" #\B \"TEXT-ART-B\" )"
(if lines
(let* ((line (car lines))
(sans-@ (cl:string-trim "@" line))
(last-of-symbol-p (str:ends-with-p "@@" line))
(not-art-line (not (str:ends-with-p "@" line)))
(first-word (car (str:split " " line)))
(first-word-num (string->integer first-word))
(new-symbol-header-p (and not-art-line first-word-num)))
(cond (new-symbol-header-p
(parse-lines (cdr lines) font-plist first-word-num))
((not not-art-line)
(setf (getf font-plist (code-char current-symbol))
(format nil "~A~A~%"
(or (getf font-plist (code-char current-symbol))
"")
sans-@))
(parse-lines (cdr lines) font-plist (if last-of-symbol-p
(+ current-symbol 1)
current-symbol)))
('t
(parse-lines (cdr lines) font-plist current-symbol))))
font-plist))
(defun figlet-font-plist (path)
"Parse a Figlet font-file (.FLF) into a plist associating a character
with its respective string in the font-file.
(#\A \"TEXT-ART-A\" #\B \"TEXT-ART-B\" )"
(parse-lines
(str:lines
(alexandria:read-file-into-string path))))

View File

@ -30,10 +30,10 @@
(:export #:read-char-plist #:read-gamefied-char-plist (:export #:read-char-plist #:read-gamefied-char-plist
#:normalize-char-plist #:gameify-char-plist #:normalize-char-plist #:gameify-char-plist
#:plist-char-p #:plist-char-p
:control :meta :shift :control :meta :shift
+qwerty-layout+ +dvorak-layout+ +qwerty-layout+ +dvorak-layout+
+arrows-game-layout+ +wasd-game-layout+ +ijkl-game-layout+ +arrows-game-layout+ +wasd-game-layout+ +ijkl-game-layout+
: : : : :🆗 :)) : : : : :🆗 :))
(defpackage :flora-search-aurora.display (defpackage :flora-search-aurora.display
(:nicknames :fsa.d :display :) (:nicknames :fsa.d :display :)
@ -46,14 +46,14 @@
(:use :cl :assoc-utils) (:use :cl :assoc-utils)
(:export #:menu-state #:make-menu-state (:export #:menu-state #:make-menu-state
#:render-line #:render-string #:render-string-partially #:render-line #:render-string #:render-string-partially
:label :selection :selected)) :label :selection :selected))
(defpackage :flora-search-aurora.dialogue (defpackage :flora-search-aurora.dialogue
(:nicknames :fsa.dia :dialogue :💬) (:nicknames :fsa.dia :dialogue :💬)
(:use :cl) (:use :cl)
(:export #:dialogue-state #:make-dialogue-state (:export #:dialogue-state #:make-dialogue-state
#:start-dialogue #:face #:say #:mumble #:move #:start-dialogue #:face #:say #:mumble #:move
:normal-face :talking-face)) :normal-face :talking-face))
(defpackage :flora-search-aurora.inventory (defpackage :flora-search-aurora.inventory
(:nicknames :fsa.inv :inventory :🎒) (:nicknames :fsa.inv :inventory :🎒)
@ -64,11 +64,11 @@
(:nicknames :fsa.o.u :overworld.util :🌍.) (:nicknames :fsa.o.u :overworld.util :🌍.)
(:use :cl) (:use :cl)
(:export #:coords->symbol #:symbol->coords (:export #:coords->symbol #:symbol->coords
#:world-coords->screen-coords #:world-coords->screen-coords
#:world-coords-chunk #:world-coords-chunk
#:map->plist #:plist->map #:map->plist #:plist->map
#:string->symbol #:string->symbol
#:save-map-to-file)) #:save-map-to-file))
(defpackage :flora-search-aurora.overworld (defpackage :flora-search-aurora.overworld
(:nicknames :fsa.o :overworld :🌍) (:nicknames :fsa.o :overworld :🌍)
@ -91,9 +91,12 @@
(defpackage :flora-search-aurora (defpackage :flora-search-aurora
(:nicknames :fsa :) (:nicknames :fsa :)
(:export #:main (:export #:main
:player) :player)
(:use :cl (:use :cl
:flora-search-aurora.input :flora-search-aurora.display :flora-search-aurora.input :flora-search-aurora.display
:flora-search-aurora.overworld :flora-search-aurora.dialogue :flora-search-aurora.overworld :flora-search-aurora.dialogue
:flora-search-aurora.ui)) :flora-search-aurora.ui))
(defpackage :figlet-font-plist
(:export #:parse-figlet-font-file)
(:use cl))