|
| 1 | +# Usage: |
| 2 | +# copy font.txt to your project directory |
| 3 | +# include the font data in the font_data list |
| 4 | +# list font_data = file ```font.txt```; |
| 5 | +# generate your own font using font.py |
| 6 | +# in inkscape, use these options: |
| 7 | +# Input/Output > SVG output > Path data > Path string format = Absolute |
| 8 | +# Input/Output > SVG output > Path data > Force repeat commands = Checked |
| 9 | + |
| 10 | +var font_offset = 0; |
| 11 | +var font_x = 0; |
| 12 | +var font_y = 0; |
| 13 | +var font_scale = 1; |
| 14 | +var font_x1 = 0; |
| 15 | +var font_x2 = 240; |
| 16 | +var font_char_spacing = 0; |
| 17 | +var font_line_spacing = 0; |
| 18 | +var font_linelen = 0; |
| 19 | + |
| 20 | +%define FONT_NAME font_data[font_offset+1] |
| 21 | +%define FONT_CREATOR font_data[font_offset+2] |
| 22 | +%define FONT_RIGHTS font_data[font_offset+3] |
| 23 | +%define FONT_WIDTH font_data[font_offset+4] |
| 24 | +%define FONT_HEIGHT font_data[font_offset+5] |
| 25 | + |
| 26 | +%define FONT_CALCULATE_WIDTH_FROM_LENGTH(LENGTH) \ |
| 27 | + (LENGTH*(FONT_WIDTH+2+font_char_spacing)-(2+font_char_spacing))*font_scale |
| 28 | +%define FONT_CALCULATE_WIDTH(TEXT) FONT_CALCULATE_WIDTH_FROM_LENGTH(length($TEXT)) |
| 29 | + |
| 30 | +proc font_render_char char { |
| 31 | + switch_costume $char; |
| 32 | + local x = ""; |
| 33 | + local y = ""; |
| 34 | + local i = font_offset+font_data[5+font_offset+costume_number()]; |
| 35 | + forever { |
| 36 | + if font_data[i] == "M" { |
| 37 | + pen_up; |
| 38 | + goto font_x+font_scale*font_data[i+1], font_y-font_scale*font_data[i+2]; |
| 39 | + i += 3; |
| 40 | + if x == "" { |
| 41 | + x = x_position(); |
| 42 | + y = y_position(); |
| 43 | + } |
| 44 | + } elif font_data[i] == "L" { |
| 45 | + pen_down; |
| 46 | + goto font_x+font_scale*font_data[i+1], font_y-font_scale*font_data[i+2]; |
| 47 | + i += 3; |
| 48 | + } elif font_data[i] == "H" { |
| 49 | + pen_down; |
| 50 | + set_x font_x+font_scale*font_data[i+1]; |
| 51 | + i += 2; |
| 52 | + } elif font_data[i] == "V" { |
| 53 | + pen_down; |
| 54 | + set_y font_y-font_scale*font_data[i+1]; |
| 55 | + i += 2; |
| 56 | + } elif font_data[i] == "Z" { |
| 57 | + pen_down; |
| 58 | + goto x, y; |
| 59 | + i += 1; |
| 60 | + } else { |
| 61 | + pen_up; |
| 62 | + stop_this_script; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +proc font_render_begin { |
| 68 | + font_x = font_x1; |
| 69 | + font_linelen = 0; |
| 70 | +} |
| 71 | + |
| 72 | +proc font_render_text text { |
| 73 | + local i = 1; |
| 74 | + repeat length($text) { |
| 75 | + font_render_char $text[i]; |
| 76 | + font_x += (FONT_WIDTH+2+font_char_spacing)*font_scale; |
| 77 | + i++; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +proc font_render_text_softwrap text { |
| 82 | + local maxlen = (font_x2 - font_x1) // ((FONT_WIDTH+2+font_char_spacing)*font_scale); |
| 83 | + local i = 1; |
| 84 | + local font_linelen = 0; |
| 85 | + local word = ""; |
| 86 | + until i > length($text) { |
| 87 | + until $text[i] == " " or i > length($text) { |
| 88 | + word &= $text[i]; |
| 89 | + i++; |
| 90 | + } |
| 91 | + if font_linelen + length(word) > maxlen { |
| 92 | + font_y -= (FONT_HEIGHT+4+font_line_spacing)*font_scale; |
| 93 | + font_x = font_x1; |
| 94 | + font_linelen = 0; |
| 95 | + } |
| 96 | + local j = 1; |
| 97 | + repeat length(word) { |
| 98 | + font_render_char word[j]; |
| 99 | + font_x += (FONT_WIDTH+2+font_char_spacing)*font_scale; |
| 100 | + font_linelen += 1; |
| 101 | + if font_x > font_x2 { |
| 102 | + font_y -= (FONT_HEIGHT+4+font_line_spacing)*font_scale; |
| 103 | + font_x = font_x1; |
| 104 | + font_linelen = 0; |
| 105 | + } |
| 106 | + j++; |
| 107 | + } |
| 108 | + word = ""; |
| 109 | + until $text[i] != " " or i > length($text) { |
| 110 | + word &= $text[i]; |
| 111 | + i++; |
| 112 | + } |
| 113 | + local j = 1; |
| 114 | + repeat length(word) { |
| 115 | + font_render_char word[j]; |
| 116 | + font_x += (FONT_WIDTH+2+font_char_spacing)*font_scale; |
| 117 | + font_linelen += 1; |
| 118 | + if font_x > font_x2 { |
| 119 | + font_y -= (FONT_HEIGHT+4+font_line_spacing)*font_scale; |
| 120 | + font_x = font_x1; |
| 121 | + font_linelen = 0; |
| 122 | + } |
| 123 | + j++; |
| 124 | + } |
| 125 | + word = ""; |
| 126 | + } |
| 127 | +} |
0 commit comments