Last active
September 20, 2019 04:01
-
-
Save gelin/fd51163ef399e27ae2e55c943a534369 to your computer and use it in GitHub Desktop.
Print 4 badges on a long sticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
PRINTER="/dev/usb/lp0" # the printer device | |
FORCE=false | |
DIRECTION=1 # 1 - human-friendly, 0 - paper-friendly | |
DOTS_MM=8 # printer dots per mm, 8 == 203 dpi | |
WIDTH=$(( 100 * $DOTS_MM )) # sticker width | |
HEIGHT=$(( 150 * $DOTS_MM )) # sticker height | |
X_CENTER=$(( $WIDTH / 2 )) # position for center alignment | |
FONT="0" # built-in vector font, scalable by X and Y | |
FONT_WIDTH=36 # font width, in points, reduce to make letters narrower | |
FONT_HEIGHT=36 # font height, in points | |
FONT_X_MULT=$FONT_WIDTH # font multiplication, for "0" font is size in points | |
FONT_Y_MULT=$FONT_HEIGHT # font multiplication, for "0" font is size in points | |
GAP_MM=6 | |
LINES_GAP_MM=1 # 0 for 143mm, 1 for 150mm | |
GAP=$(( $GAP_MM * $DOTS_MM )) | |
LINE_HEIGHT=$(( ( $FONT_HEIGHT / 3 + $LINES_GAP_MM ) * $DOTS_MM )) | |
usage() { | |
echo "Usage:" | |
echo "$0 [-d|--dry-run] [-y|--yes] [-o|--orient 0|1] PERS1LINE1 PERS1LINE2 PERS2LINE1 PERS2LINE2" | |
exit 1 | |
} | |
print_spaces() { # prints N spaces in a line | |
local N=$1 | |
for i in $( seq 1 $N ) | |
do | |
echo -n ' ' | |
done | |
} | |
print_line_centered() { # prints text centered in a field with the specified width | |
local TEXT="$1" | |
local WIDTH="$2" | |
local LENGTH=$( echo -n "$TEXT" | wc -m ) | |
local SPACES=$(( ( $WIDTH - $LENGTH ) / 2 )) | |
print_spaces $SPACES | |
echo "$TEXT" | |
} | |
confirm() { # prints how the badge may looks like and confirms | |
local LINE1="$1" | |
local LINE2="$2" | |
local LINE3="$3" | |
local LINE4="$4" | |
echo '================' | |
print_line_centered "$LINE1" 16 | |
print_line_centered "$LINE2" 16 | |
echo '----------------' | |
print_line_centered "$LINE1" 16 | |
print_line_centered "$LINE2" 16 | |
echo '----------------' | |
print_line_centered "$LINE3" 16 | |
print_line_centered "$LINE4" 16 | |
echo '----------------' | |
print_line_centered "$LINE3" 16 | |
print_line_centered "$LINE4" 16 | |
echo '================' | |
read -p "Print? [Y/n] " -r REPLY | |
echo | |
case "$REPLY" in | |
[Yy] | "") return 0 ;; | |
*) return 1 ;; | |
esac | |
} | |
print() { # sends commands to print two lines to the printer | |
local LINE1="$1" | |
local LINE2="$2" | |
local LINE3="$3" | |
local LINE4="$4" | |
local Y_L1_1=$(( $GAP )) | |
local Y_L1_2=$(( $Y_L1_1 + $LINE_HEIGHT )) | |
local Y_S1=$(( $Y_L1_2 + $LINE_HEIGHT + $GAP )) | |
local Y_L2_1=$(( $Y_S1 + $GAP )) | |
local Y_L2_2=$(( $Y_L2_1 + $LINE_HEIGHT )) | |
local Y_S2=$(( $Y_L2_2 + $LINE_HEIGHT + $GAP )) | |
local Y_L3_1=$(( $Y_S2 + $GAP )) | |
local Y_L3_2=$(( $Y_L3_1 + $LINE_HEIGHT )) | |
local Y_S3=$(( $Y_L3_2 + $LINE_HEIGHT + $GAP )) | |
local Y_L4_1=$(( $Y_S3 + $GAP )) | |
local Y_L4_2=$(( $Y_L4_1 + $LINE_HEIGHT )) | |
cat <<COMMANDS > $PRINTER | |
CODEPAGE UTF-8 | |
DIRECTION $DIRECTION | |
CLS | |
TEXT $X_CENTER,$Y_L1_1,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE1" | |
TEXT $X_CENTER,$Y_L1_2,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE2" | |
DIAGONAL 0,$Y_S1,70,$Y_S1,1 | |
DIAGONAL 250,$Y_S1,310,$Y_S1,1 | |
DIAGONAL 490,$Y_S1,550,$Y_S1,1 | |
DIAGONAL 730,$Y_S1,800,$Y_S1,1 | |
TEXT $X_CENTER,$Y_L2_1,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE1" | |
TEXT $X_CENTER,$Y_L2_2,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE2" | |
DIAGONAL 0,$Y_S2,70,$Y_S2,1 | |
DIAGONAL 250,$Y_S2,310,$Y_S2,1 | |
DIAGONAL 490,$Y_S2,550,$Y_S2,1 | |
DIAGONAL 730,$Y_S2,800,$Y_S2,1 | |
TEXT $X_CENTER,$Y_L3_1,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE3" | |
TEXT $X_CENTER,$Y_L3_2,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE4" | |
DIAGONAL 0,$Y_S3,70,$Y_S3,1 | |
DIAGONAL 250,$Y_S3,310,$Y_S3,1 | |
DIAGONAL 490,$Y_S3,550,$Y_S3,1 | |
DIAGONAL 730,$Y_S3,800,$Y_S3,1 | |
TEXT $X_CENTER,$Y_L4_1,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE3" | |
TEXT $X_CENTER,$Y_L4_2,"$FONT",0,$FONT_X_MULT,$FONT_Y_MULT,2,"$LINE4" | |
PRINT 1 | |
COMMANDS | |
echo $LINE1 $LINE2 | |
echo $LINE3 $LINE4 | |
} | |
if [ "$#" = "0" ] | |
then | |
usage | |
fi | |
while [ "$#" != "0" ] | |
do | |
case $1 in | |
-d | --dry-run) PRINTER=/dev/stdout ;; | |
-y | --yes) FORCE=true ;; | |
-o | --orient) shift; DIRECTION=$1 ;; | |
-h | --help) usage ;; | |
--) shift; break ;; | |
-*) ;; | |
*) break ;; | |
esac | |
shift | |
done | |
if $FORCE || confirm $1 $2 $3 $4 | |
then | |
print $1 $2 $3 $4 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment