text2xpm in shell

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

text2xpm in shell

#1 Post 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
Attachments
text2xpm.gz
(1.48 KiB) Downloaded 535 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post 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?
Attachments
text2xpm.gz
(1.83 KiB) Downloaded 555 times
text2xpm.png
(18.22 KiB) Downloaded 1046 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#3 Post by zigbert »

I like this :D

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post 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.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#5 Post 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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#6 Post 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.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#7 Post 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]
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post 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.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#9 Post 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.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#10 Post 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
Attachments
test.png
(1.4 KiB) Downloaded 1092 times
Last edited by big_bass on Sat 10 Sep 2011, 14:14, edited 1 time in total.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#11 Post 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`

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#12 Post 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

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#13 Post 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
Attachments
text2xpm.gz
development version with some notes to remind me what to do later,
sometime, maybe, eventually
(2.39 KiB) Downloaded 544 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#14 Post 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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#15 Post by 01micko »

Ok, so I put this to actual use

Note the feature request.
Puppy Linux Blog - contact me for access

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#16 Post 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.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#17 Post 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?
Puppy Linux Blog - contact me for access

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#18 Post 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)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#19 Post 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.
Attachments
cpu.png
(47.85 KiB) Downloaded 539 times

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#20 Post 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}'" ...
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply