[comp.windows.news] converting SunView icons to NeWS icons

siegel@hc.DSPO.GOV (josh Siegel) (03/07/89)

NeWSer converts suntools icons to a "NeWS" icon.  Then, there
is "example.cps" which includes a "foobar" icon and displays it.

Is this good enough or do you want to convert it to a NeWS font?!?

	
--------- NeWSer
#! /bin/sh
ed - $1.icon<<EOF
1d
1d
1,\$s/^ //g
1,\$s/,//g
1,\$s/0x//g
1s/^\(.*\)$/<\1> /g
2,\$s/^\(.*\)$/<\1> append/g
w $1.news
q
EOF
------- example.cps
/win
    framebuffer /new DefaultWindow send
def

/showicon {
    dup type /stringtype eq {
        % use string as an icon image
        gsave
            currentpoint translate
            64 64 scale
            0 setgray
            64 64 true [1 0 0 1 0 0]
            5 -1 roll imagemask
        grestore
    } {
        % assume literal and use as standard icon lookup
        % this comes straight out of 'icons.ps'
        currentfont ( ) dup 0 icondict 5 index get put
        iconfont setfont show setfont pop
    } ifelse
} def

{
    /IconImage
#include "foobar2.news"
    def         % Give it an icon

    200 200 700 350 reshape         % Give it a size
} win send

/map win send

-- 
Josh Siegel		(siegel@hc.dspo.gov)
I like using a C-47A "puff dragon" to go shooting beer cans with.

thaeler@hc.DSPO.GOV (Bret K. Thaeler) (03/08/89)

This is another program that was written when the code that Josh
previously posted was created. This program converts each character
of a vfont into a seperate file in suntools iconedit format.  This
allows you to take the existing NeWS Icon font and convert it into a
vfont, you can then dump that into files sutable for use with the
iconedit tool. You can then take this and with the code previously
posted and put the new modified icon back into your NeWS applications...

Here is how to do it:
	1) First compile the inclosed program.
		a) extract the end of this article into the
		   file 'extract_font.c'.
		b) % cc -o extract_font extract_font.c
	
	2) Now copy the NeWS Icon font and make it into a vfont.
		a) % mkdir play
		b) % cd play
		c) % cp /usr/NeWS/fonts/Icon12.fb .
		d) % dumpfont -d . -v Icon12.fb
	
	3) Now run the extract font program.
		a) % extract_font Icon12.vft


The extract font program will produce a file for each character in
the font. Each file will be named 'Icon12.vft.??' where ?? is the
'character number' of the icon. You can now type '% iconedit' and
load the file '*' to lookat and select to modify any of the icons...

If you have any questions or comments let me know.  Have fun.....

		-Bret Thaeler
		Los Alamos Nations Labs (MEE-10)
		thaeler@hc.dspo.gov


--------------- extract_font.c --------------

/*
 * Written by Bret K. Thaeler
 *    Los Alamos National Labs (MEE-10)
 *    thaeler@hc.dspo.gov
 *
 *  extract_font:
 *		This program converts a vfont (a possibly produced by the 
 *		NeWS program 'dumpfont') into a series of ascii files
 *		sutable for use with the suntools utility 'iconedit'.
 *		You can use this code to modify NeWS icons...
 */

#include <vfont.h>
#include <sys/file.h>
#include <stdio.h>

main(argc, argv)
int argc;
char *argv[];
{
	FILE *font_file, *output_file;
	int i, j, k, bits_to_read, row_rounded;
	long pos;
	struct header header;
	struct dispatch characters[NUM_DISPATCH];
	char buff[256];
	unsigned char *char_buff, *data_pointer, data, sub_data;

	char_buff = 0;

	if (!(argc >= 2)) {
		printf("Usage: %s font_list\n", argv[0]);
		exit(-1);
	}

	for(argc--, argv++; argc > 0; argc--, argv++) {
		if ((font_file = fopen(argv[0], "r")) == NULL) {
			printf("Can't open file '%s'.\n", argv[0]);
			continue;
		}

		if (fread(&header, sizeof(struct header), 1, font_file) != 1)
			goto error;

		if (header.magic != VFONT_MAGIC) {
			printf("Bad MAGIC number in file '%s'.\n", argv[0]);
			fclose(font_file);
			continue;
		}

		if (char_buff) {
			free(char_buff);
			char_buff = 0;
		}
		char_buff = (unsigned char *) malloc(header.maxx/8 * header.maxy);

		if (fread(characters, sizeof(struct dispatch), NUM_DISPATCH, font_file)
		  != NUM_DISPATCH)
			goto error;
		
		pos = ftell(font_file);

		for(i = 0; i < NUM_DISPATCH; i++) {
			if (characters[i].nbytes == 0)
				continue;

			fseek(font_file, (long)(pos + characters[i].addr), 0);

			sprintf(buff, "%s.%d", argv[0], i);
			if ((output_file = fopen(buff, "w")) == NULL) {
				printf("Can't open outputfile '%s'.\n", buff);
				continue;
			}

			if (fread(char_buff, characters[i].nbytes, 1, font_file) != 1) {
				printf("Can't read from input file '%s' character '%d'.\n",
				  argv[0], i);
				fclose(output_file);
				continue;
			}

			if (((characters[i].left + characters[i].right) % 8) != 0) {
				bits_to_read =
				((8 - ((characters[i].left + characters[i].right) % 8)) +
				  (characters[i].left + characters[i].right)) *
				  (characters[i].up + characters[i].down);
				row_rounded =
				  (8 - ((characters[i].left + characters[i].right) % 8)) +
					(characters[i].left + characters[i].right);
			} else {
				bits_to_read =
				  (characters[i].left + characters[i].right) *
				  (characters[i].up + characters[i].down);
				row_rounded = (characters[i].left + characters[i].right);
			}

			data_pointer = char_buff;

			fprintf(output_file,
"/* Format_version=1, Width=%d, Height=%d, Depth=1, Valid_bits_per_item=16\n */\n",
			  /*
			  (characters[i].left + characters[i].right),
			  */
			  row_rounded,
			  (characters[i].up + characters[i].down));

			while(1) {
				fprintf(output_file, "\t");
				for(k = 0; k < 8; k++) {
					fprintf(output_file, "0x%02X", *(data_pointer++));
					fprintf(output_file, "%02X", *(data_pointer++));

					if ((bits_to_read -= 16) < 0)
						bits_to_read = 0;

					if (bits_to_read != 0)
						fprintf(output_file, ",");
					else {
						fprintf(output_file, "\n");
						goto done;
					}
				}
				fprintf(output_file, "\n");
			}
done:

			fclose(output_file);
		}

		continue;
error:
		printf("Error while reading file '%s'.\n", argv[0]);
		if (char_buff) {
			free(char_buff);
			char_buff = 0;
		}
		fclose(font_file);
	}
}