Page 1 of 2

text2xpm in shell

Posted: Thu 08 Sep 2011, 16:47
by technosaurus
here is the 1st working stub, I will eventually merge features from my previous xpm generator that would do vertical or horizontal percent bars

usage
text2xpm "your text here" >/tmp/tmp.xpm

It takes ~0.01s to generate a Hello World xpm image

(yes I know it would have been simpler if I used bash arrays, but I want it to be compatible with other shells)

Code: Select all

#!/bin/ash

#Given string input $1, go char by char & create 7x13 xpm representation

CHARS=" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&()_+-=<>,./?;':\"{}[]\|"

ROWA="                                                                                                                                                                                                                                                                                                                                                                                                                                                           #                                          ##               #  #                                                                  #                  #            # #      ##  ##      ####  ####  #         #   "
ROWB="          ##      #    ####  ######     #  ######   ###  ######  ####   ####    ##   ####    ####  ####   ###### ######  ####  #    #  #####    ### #    # #      #    # #    #  ####  #####   ####  #####   ####  ########    # #    #  #   # #    #  #   # ######        #                  #           ##         #         #        # #        ##                                                      #                                              # #  #    #     ###    # #     #    ##  #  #  #    ##       #    #                                                                 #  ####            #            # #     #      #     #        #  #         #   "
ROWC="         #  #    ##   #    #      #    ##  #       #          # #    # #    #  #  #  #   #  #    # #   #  #      #      #    # #    #    #       #  #   #  #      ##  ## #    # #    # #    # #    # #    # #    #    #   #    # #    #  #   # #    #  #   #      #        #                  #          #  #        #         #        # #         #                                                      #                                              #  # #    #    #   #   # #   ####  #  # # #    #  #  #     #      #                                    #  #                       #  #    #           #            # #     #      #     #        #   #        #   "
ROWD="        #    #  # #   #    #     #    # #  #      #          #  #    # #    # #    # #    # #    # #    # #      #      #    # #    #    #       #  #  #   #      ##  ## ##   # #    # #    # #    # #    # #    #    #   #    # #    #  #   #  #  #   #   #     #         #                  #          #           #                    #         #                                                      #                                                  #     #   #  ###   # #  #  # # #  ##          #  #     #      #             #                     #    #                      #  #    #           #      #     # #     #      #     #        #   #        #   "
ROWE="        #    #    #        #    #     # #  #####  #          #   #  #  #    # #    # #   #  #      #    # #      #      #      #    #    #       #  # #    #      # ## # ##   # #    # #    # #    # #    # #         #   #    # #    #  #   #  #  #    # #     #    ####  # ###   ####   ### #  ####    #     ### # # ###    ##       ## #   #     #    ## #  # ###   ####  # ###   ### # # ###   ####  #####  #    #  #   #  #   # #    # #    # ######           #   # #  #  ##### #  # #  ###           #  #    #        #            #          ######    #      #                    #       #    ##            ###            #      #     #        #    #       #   "
ROWF="        #    #    #       #    ###   #  #  #    # # ###     #     ##   #   ## #    # ####   #      #    # ####   ####   #      ######    #       #  ##     #      # ## # # #  # #    # #    # #    # #    #  ##       #   #    #  #  #   #   #   ##     # #     #   #    # ##   # #    # #   ## #    # #####  #   #  ##   #    #        # #  #      #    # # # ##   # #    # ##   # #   ## ##   # #    #   #    #    #  #   #  #   # #    # #    #     #            #   # #  #   # #   # #      #            ##     #        #            #                   #        #                   #      #     ##             #             #      #     #        #    #       #   "
ROWG="        #    #    #       #       #  #  #       # ##   #    #    #  #   ### # ###### #   #  #      #    # #      #      #  ### #    #    #       #  # #    #      #    # #  # # #    # #####  #    # #####     ##     #   #    #  #  #   #   #   ##      #     #         # #    # #      #    # #    #   #    #   #  #    #    #        # # #       #    # # # #    # #    # #    # #    # #    #  #       #    #    #  #   #  # # #  #  #  #    #    #             #   # #  #   # #    ###    #            ##  #   #        #          #####  #####         #          #                  #      #                                 #        #    #        #    #       #   "
ROWH="        #    #    #      #        # #   #       # #    #   #    #    #      # #    # #    # #      #    # #      #      #    # #    #    #       #  #  #   #      #    # #   ## #    # #      ###  # #  #        #    #   #    #  #  #   # # #  #  #     #    #      ##### #    # #      #    # ######   #    #   #  #    #    #        # ###       #    # # # #    # #    # #    # #    # #        ##     #    #    #   # #   # # #   ##   #    #   #              #   # #  #  #####    # #   ###         #  # #   #        #            #          ######   #        #                  #       #                                  #      #     #        #     #      #   "
ROWI="        #    #    #     #    #    # ###### #    # #    #   #    #    # #    # #    # #    # #    # #    # #      #      #    # #    #    #   #   #  #   #  #      #    # #   ## #    # #      #  # # #   #  #    #    #   #    #   ##    # # #  #  #     #    #     #    # #    # #      #    # #        #     ###   #    #    #        # #  #      #    # # # #    # #    # #    # #    # #          #    #    #    #   # #   # # #  #  #  #   ##   #                  #  ###   # #  #  # #  ##  #        #   #    #        #            #                    #      #                   #             ##             #             #      #     #        #     #      #   "
ROWJ="         #  #     #    #     #    #     #  #    # #    #  #     #    # #   #  #    # #   #  #    # #   #  #      #      #   ## #    #    #   #   #  #    # #      #    # #    # #    # #      #   ## #    # #    #    #   #    #   ##    # # # #    #    #   #      #    # ##   # #    # #   ## #    #   #     #     #    #    #        # #   #     #    # # # #    # #    # ##   # #   ## #      #    #   #  # #   ##    #    # # # #    #  ### #  #               #    #       # #  #  # # # #  #        #  ##     #      #             #                     #    #      ##      #    #        #      #            ###            #      #     #        #      #     #   "
ROWK="          ##    ##### ######  ####      #   ####   ####   #      ####   ###   #    # ####    ####  ####   ###### #       ### # #    #  #####  ###   #    # ###### #    # #    #  ####  #       ####  #    #  ####     #    ####    ##     # #  #    #    #   ######  ##### # ###   ####   ### #  ####    #    # ###  #    #  #####  #   # #    #  #####  #   # #    #  ####  # ###   ### # #       ####     ##   ### #    #     # #  #    #      # ######           #     ####   # #   ####  #  ##          ##  #    #      #                                    #  #        #     ###   #        #      #             #             #      #     #        #      #     #   "
ROWL="                                                                                                                                                                                                  #                                                                                                           #    #                #   #                                    #           #                                                  #    #                                       #                            #    #                                                 #      #   #               #                            #      #     #        #       #    #   "
ROWM="                                                                                                                                                                                                   #                                                                                                           ####                  ###                                     #           #                                                   ####                                                                      #  #     ######                                      #           #                                             ##  ##      ####  ####       #    #   "

#echo $CHARS
j=0
while ([ "${j}" -lt "${#1}" ]) do
	i=${CHARS%${1:$j:1}*} #cut the string after current char, length = position
	i=${#i} #note each ROW{A-M}
	RA=${RA}${ROWA:$(( $i*7 )):7}
	RB=${RB}${ROWB:$(( $i*7 )):7}
	RC=${RC}${ROWC:$(( $i*7 )):7}
	RD=${RD}${ROWD:$(( $i*7 )):7}
	RE=${RE}${ROWE:$(( $i*7 )):7}
	RF=${RF}${ROWF:$(( $i*7 )):7}
	RG=${RG}${ROWG:$(( $i*7 )):7}
	RH=${RH}${ROWH:$(( $i*7 )):7}
	RI=${RI}${ROWI:$(( $i*7 )):7}
	RJ=${RJ}${ROWJ:$(( $i*7 )):7}
	RK=${RK}${ROWK:$(( $i*7 )):7}
	RL=${RL}${ROWL:$(( $i*7 )):7}
	RM=${RM}${ROWM:$(( $i*7 )):7}
	j=$(($j+1))
done

IMAGE='/* XPM */
static char *tmp_xpm[] = {
"'${#RA}' 13 2 1",
" 	c #000000",
"#	c #FFFFFF",
"'$RA'",
"'$RB'",
"'$RC'",
"'$RD'",
"'$RE'",
"'$RF'",
"'$RG'",
"'$RH'",
"'$RI'",
"'$RJ'",
"'$RK'",
"'$RL'",
"'$RM'"
};'
echo "$IMAGE"
Edit: ok that is pretty ugly ... uploading the gzipped code

Posted: Thu 08 Sep 2011, 21:39
by technosaurus
it now accepts bg1= bg2= fg= width= and percent=

TODO vertical bars, maybe rounded corners if I can come up with a clever way to do it?

Posted: Fri 09 Sep 2011, 07:53
by zigbert
I like this :D

Posted: Fri 09 Sep 2011, 08:57
by sunburnt
Vertical ones stacked side by side would make a good set of tray meters.

Amounts for: Cpu load, Ram used, Save file used, Dnld. speed, etc.

Posted: Fri 09 Sep 2011, 14:43
by technosaurus
The vertically oriented icons are next, I already have the height parameter working (with the text centered vertically... should there be an option to put text on top or bottom?... and left/right/center for that matter?)

I also have a method partially worked out to do graphs(without text), but not sure if there will be a way to integrate it here... its a _little_ more complex

Posted: Fri 09 Sep 2011, 20:52
by sunburnt
Text justification is a nicety, if you have a use for it it`s good.
Should be easy to do though...
Centered I`d think would be the most common use / need.

When I made M$ controls and ActiveX I added stuff like this if it was easy.
M$ controls have lots of properties like this, which is overkill mostly.
M$ put lots of effort into nice working controls, and the quality shows.

Posted: Fri 09 Sep 2011, 21:37
by technosaurus
Currently if you don't specify a width, it uses the width of your text.

Code: Select all

text2xpm "used                 free" percent=50 
You would get a justified image with used all the way on the left and free all the way to the right. Think partinfo
(BTW each char is 7 pixels ... width=${#STRING})[/code]

Posted: Fri 09 Sep 2011, 21:44
by sunburnt
What about fonts? Maybe as important if not more than justified: left, right, center.

Fonts would potentially change the 7 pixels per character of course.

Posted: Sat 10 Sep 2011, 01:22
by technosaurus
There is only 1 font and it is built-in to the script (it looks like a bunch of garbage in the 1st post, but you can see it fine in geany)

You can modify it but it would have to be a fixed width font to work. (otherwise the math would be ridiculously slow)

Like the ones here.
http://www.cfcl.com/vlb/h/fontmono.html

BTW apologies for not adding localization chars.

I just used the text part of mtpaint, cropped and saved as xpm... I don't have the other chars on my keyboard.

Posted: Sat 10 Sep 2011, 03:18
by big_bass
Hey technosaurus

this looks interesting

I wonder if this is possible with png format too
using just shell/bash
or even a very small c program

updated using imagemagick you get a nice image

Code: Select all

convert -size 200x30 xc:transparent -font /usr/share/fonts/default/TTF/DejaVuSans-BoldOblique.ttf -pointsize 12 -draw "text 5,15 'this is just a test'" test.png
Joe

Posted: Sat 10 Sep 2011, 07:48
by jpeps
..and then add animation. I might use this for a timer.

Code: Select all

#!/bin/sh
cd /tmp

for num in $(seq 1 5); do
case "$num" in 
1 )  vw one.xpm ; sleep .5  ;;
2 )  vw two.xpm ; sleep .5  ;;
3 )  vw three.xpm ; sleep .5 ;;
4 )  vw four.xpm ; sleep .5 ;;
5 )  vw five.xpm ; sleep .5 ;;
esac

done

kill  `pidof viewnior`
Crude "working" bar: (vw=viewnior)

Code: Select all

for num in $(seq 1 5); do
var="${var}X"
./text2xpm "${var}" bg1=0000FF fg=0000FF >/tmp/"${num}.xpm"
wait
vw /tmp/${num}.xpm
sleep .5
rm /tmp/${num}.xpm
done

kill -9 `pidof viewnior`

Posted: Sat 10 Sep 2011, 17:02
by big_bass
Vertical ones stacked side by side would make a good set of tray meters.
I got this tip from imagmagick
you could add this snippet to text2xpm also after some mods

Code: Select all

echo -n "Vertical" | sed 's/./&@/g; s/@$//' | tr '@' '\012' 
Note that the "sed" command adds a '@' character after every character,
except at the end of the string.
The "tr" then replaces the '@' characters with newlines.
It also assumes the input text does not end with a newline,
which would cause an extra blank space to be added at the
bottom of the resulting image.

Joe

Posted: Sun 11 Sep 2011, 05:09
by technosaurus
I have some real world projects to deal with for a while so I am uploading my current development snapshot with some notes.

I currently have added a rounded option, and support for height since the last version, some code improvements and haphazardly put my best guess as to what the code for other options will look like in comments

Posted: Thu 15 Sep 2011, 14:04
by technosaurus
new todo, support prepended xpm (drive icons for example)

this is an experiment for my jwm-tools project, as it will allow drives icons to display the partition name and optionally percent used/free without needing a tooltip ... freeing up the tooltip to display kb used/free

Posted: Thu 29 Dec 2011, 06:00
by 01micko
Ok, so I put this to actual use

Note the feature request.

Posted: Thu 29 Dec 2011, 22:21
by technosaurus
01micko wrote:Ok, so I put this to actual use

Note the feature request.
that was exactly the kind of thing I had in mind when I wrote this. Things like partview, battview, tempview... Then I found out how to do the same thing in svg much easier, but it is still good for xcb/X11+xpm only apps I guess.

Posted: Tue 03 Jan 2012, 19:31
by 01micko
Another one,

cpuicon

There's a difference, no thread topic, this is just a link to the source code and support files including a build script.

You could say I'm going a bit mad on tray icons! But I find them more useful than widgets or conky as I actually use them! They don't look fantastic but they are functional.

techno, care to share the svg knowledge?

Posted: Wed 04 Jan 2012, 03:38
by technosaurus
Try playing with this:
(save as <something>.svg and open with viewnior)

Code: Select all

<svg>
  <rect width="100"
     height="50"
     x="0"
     y="0"
     style="font-size:12;fill:#0000ff;fill-opacity:0.75;fill-rule:evenodd;stroke-width:3pt;"
     id="rect1" />
  <rect
     width="50"
     height="50"
     x="0"
     y="0"
     style="font-size:12;fill:#ff00ff;fill-opacity:0.75;fill-rule:evenodd;stroke-width:3pt;"
     id="rect2" />
  <text
     x="0"
     y="50"
     style="font-size:12;font-weight:normal;fill-opacity:0.75;stroke-width:3pt;font-family:helvetica;"
     id="text1">
    <tspan
       id="tspan1">test</tspan>
  </text>
</svg>
you can add as many rectangles as you want, or you can save some svgS from inklite (uncompressed) for other shapes etc... (crap, now Iremember where I posted this before - it was in the project management / planner thread ... covered lines too)

Posted: Wed 04 Jan 2012, 11:16
by jpeps
01micko wrote:Another one,

cpuicon
You could also eliminate a folder with 100 xpms, etc, with a small tray icon that just toggles cpu-usage on and off in a small window when you need it.

Posted: Wed 04 Jan 2012, 17:32
by technosaurus
this just requires echo and variables to generate svg

Code: Select all

echo '<svg>
	<rect width="100" height="50" x="0" y="0" id="rect1" style="font-size:12;fill:none;fill-opacity:0.75;fill-rule:evenodd;stroke-width:3pt;"/>
	<rect width="50" height="50" x="0" y="0" id="rect2" style="font-size:12;fill:#ff00ff;fill-opacity:0.75;fill-rule:evenodd;stroke-width:3pt;"/>
	<ellipse cx="24" cy="24" rx="24" ry="24" style="font-size:12;fill:#ff0000;fill-opacity:0.75;fill-rule:evenodd;stroke-width:3pt;"/>
	<ellipse cx="74" cy="24" rx="24" ry="24" style="font-size:12;fill:#00ff00;fill-opacity:0.75;fill-rule:evenodd;stroke-width:3pt;"/>
	<text x="12" y="25" id="text1" style="font-size:12;font-weight:normal;fill-opacity:0.75;stroke-width:3pt;font-family:helvetica;">test</text>
	<path d="M 50 25 L 100.0 25.0 " style="font-size:12;fill:none;fill-opacity:0.75;fill-rule:evenodd;stroke:#000000;stroke-width:1pt;"/>
</svg>' >$FILE
simply edit the fixed parameters in place and then just replace the rest with single quoted variables. ex.

... width="50" .... becomes ... width="'${WIDTH}'" ...