#ascii-art #font #user #string #cli

text2art

Simple library for translate text to ascii art

3 stable releases

1.0.2 Oct 10, 2023
1.0.1 Aug 25, 2022
0.3.0 Aug 16, 2022
0.2.0 Aug 16, 2022
0.1.0 Aug 9, 2022

#1406 in Text processing

Download history 1/week @ 2024-09-18 8/week @ 2024-09-25 11/week @ 2024-10-02

61 downloads per month
Used in chatgpt_rust

MIT license

31KB
611 lines

 _                ___                _   
| |              |__ \              | |  
| |_   ___ __  __   ) |  __ _  _ __ | |_ 
| __| / _ \\ \/ /  / /  / _` || '__|| __|
| |_ |  __/ )  (  / /_ | (_| || |   | |_ 
 \__| \___|/_/\_\|____| \__,_||_|    \__|

Status Crates.io downloads Crates.io downloads License


Description

Simple library for translating text into ascii art. The library has several fonts at once, and the user can use their fonts from a file or string.

Install

text2art = "1.0.2"

How to use

Run
cargo run --example basic_usage

Code

use text2art::BasicFonts;
use text2art::Font;
use text2art::Printer;

fn main() {
    let font = match Font::from_basic(BasicFonts::Big) {
        Ok(font) => font,
        Err(_) => panic!("something wrong with font"),
    };
    let prntr = Printer::with_font(font);
    prntr.print_to_stdio("Welcome to tex2art! :)").ok();

    prntr.print_to_stdio("text for print_to_stdio").ok();
    prntr
        .print_to("text for print_to", &mut std::io::stdout())
        .ok();

    let rendered_text = prntr.render_text("text for render");
    match rendered_text {
        Ok(rendered_text) => println!("{}", rendered_text),
        Err(_) => println!("Something went wrong!"),
    }
}

Output

                                                                                                                         __  
__          __       _                                   _              _                ___                _    _       \ \ 
\ \        / /      | |                                 | |            | |              |__ \              | |  | |    _  | |
 \ \  /\  / /   ___ | |  ___   ___   _ __ ___    ___    | |_   ___     | |_   ___ __  __   ) |  __ _  _ __ | |_ | |   (_) | |
  \ \/  \/ /   / _ \| | / __| / _ \ | '_ ` _ \  / _ \   | __| / _ \    | __| / _ \\ \/ /  / /  / _` || '__|| __|| |       | |
   \  /\  /   |  __/| || (__ | (_) || | | | | ||  __/   | |_ | (_) |   | |_ |  __/ )  (  / /_ | (_| || |   | |_ |_|    _  | |
    \/  \/     \___||_| \___| \___/ |_| |_| |_| \___|    \__| \___/     \__| \___|/_/\_\|____| \__,_||_|    \__|(_)   (_)/_/ 
 _                _        __                               _         _            _                        _        _  _        
| |              | |      / _|                             (_)       | |          | |                      | |      | |(_)       
| |_   ___ __  __| |_    | |_   ___   _ __     _ __   _ __  _  _ __  | |_         | |_   ___           ___ | |_   __| | _   ___  
| __| / _ \\ \/ /| __|   |  _| / _ \ | '__|   | '_ \ | '__|| || '_ \ | __|        | __| / _ \         / __|| __| / _` || | / _ \ 
| |_ |  __/ )  ( | |_    | |  | (_) || |      | |_) || |   | || | | || |_  ______ | |_ | (_) | ______ \__ \| |_ | (_| || || (_) |
 \__| \___|/_/\_\ \__|   |_|   \___/ |_|      | .__/ |_|   |_||_| |_| \__||______| \__| \___/ |______||___/ \__| \__,_||_| \___/ 
                                              | |                                                                                
                                              |_|                                                                                
 _                _        __                               _         _            _          
| |              | |      / _|                             (_)       | |          | |         
| |_   ___ __  __| |_    | |_   ___   _ __     _ __   _ __  _  _ __  | |_         | |_   ___  
| __| / _ \\ \/ /| __|   |  _| / _ \ | '__|   | '_ \ | '__|| || '_ \ | __|        | __| / _ \ 
| |_ |  __/ )  ( | |_    | |  | (_) || |      | |_) || |   | || | | || |_  ______ | |_ | (_) |
 \__| \___|/_/\_\ \__|   |_|   \___/ |_|      | .__/ |_|   |_||_| |_| \__||______| \__| \___/ 
                                              | |                                             
                                              |_|                                             
 _                _        __                                         _             
| |              | |      / _|                                       | |            
| |_   ___ __  __| |_    | |_   ___   _ __     _ __   ___  _ __    __| |  ___  _ __ 
| __| / _ \\ \/ /| __|   |  _| / _ \ | '__|   | '__| / _ \| '_ \  / _` | / _ \| '__|
| |_ |  __/ )  ( | |_    | |  | (_) || |      | |   |  __/| | | || (_| ||  __/| |   
 \__| \___|/_/\_\ \__|   |_|   \___/ |_|      |_|    \___||_| |_| \__,_| \___||_|   

How to create font

You can use your font from str or file. You can use these functions

pub fn from_basic(font: basic_fonts::BasicFonts) -> Result<Font, FontError>
pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Font, FontError>

Font rules:

  1. Constant line width. All lines intro one grapheme should be with equal width
  2. Font line must be in format. Font must contain 3 segments: grapheme, shift and data.

For example:

'a':0:  __ _ \n / _` |\n| (_| |\n \__,_|\n

Where:

'a' - font grapheme (you can use any unicode grapheme here)
: - segmentation symbol
0 - shift
  __ _ \n / _` |\n| (_| |\n \__,_|\n - data
  1. Use "\n" for line segmentation For example:
 _   
| |  
| |_ 
| __|
| |_ 
 \__|

Will be implemented as

't':0: _   \n| |  \n| |_ \n| __|\n| |_ \n \__|\n
  1. Use shift if it needed. With shift you can move you grapheme up or down. All graphemes align on the zero line. Examples from big font below.

Grapheme without shift

't':0: _   \n| |  \n| |_ \n| __|\n| |_ \n \__|\n
 _    ___ 5
| |  
| |_  
| __|
| |_ 
 \__| ___ 0
|   |
1   5
Internal parameters:
Width - 5
Height - 6
Shift - 0

Grapheme with negative shift

'p':-2: _ __  \n| '_ \ \n| |_) |\n| .__/ \n| |    \n|_|    \n
 _ __   ___ 3
| '_ \ 
| |_) |
| .__/  ___ 0
| |    
|_|     __ -2
|     |
1     7
Internal parameters:
Width - 7
Height - 6
Shift - -2

Grapheme with positive shift

'"':3: _ _ \n( | )\n V V \n
 _ _  ___ 5
( | )
 V V  ___ 3
 
 
      ___ 0
|   |
1   5
Internal parameters:
Width - 5
Height - 3
Shift - 3
 

All examples in one structure

 _           _ _ 
| |         ( | )
| |_  _ __   V V 
| __|| '_ \ 
| |_ | |_) |
 \__|| .__/       
     | |    
     |_|     
  1. You can leave comments. Use '#' for comments.

For example:

# letters [a-z]
'a':0:  __ _ \n / _` |\n| (_| |\n \__,_|\n
'b':0: _     \n| |    \n| |__  \n| '_ \ \n| |_) |\n|_.__/ \n

Dependencies

~3.5MB
~60K SLoC