Line data Source code
1 : /* Copyright (C) 2000-2012 by George Williams */
2 : /* 2013mar3, bug-fixes plus type-formatting, Jose Da Silva */
3 : /*
4 : * Redistribution and use in source and binary forms, with or without
5 : * modification, are permitted provided that the following conditions are met:
6 :
7 : * Redistributions of source code must retain the above copyright notice, this
8 : * list of conditions and the following disclaimer.
9 :
10 : * Redistributions in binary form must reproduce the above copyright notice,
11 : * this list of conditions and the following disclaimer in the documentation
12 : * and/or other materials provided with the distribution.
13 :
14 : * The name of the author may not be used to endorse or promote products
15 : * derived from this software without specific prior written permission.
16 :
17 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 : * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 : * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 : * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 : * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 : * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 : * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 : * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 : */
28 : #include "gimage.h"
29 : #include "string.h"
30 :
31 0 : static void WriteBase(FILE *file, struct _GImage *base, char *stem, int instance) {
32 : /* Write one image in C code which can be compiled into FontForge. */
33 : /* This routine is called and used by GImageWriteGImage() */
34 : int i,j,k;
35 : uint32 *ipt;
36 : uint8 *pt;
37 : long val;
38 :
39 0 : if ( base->image_type==it_true ) {
40 0 : fprintf(file,"static uint32 %s%d_data[] = {\n",stem,instance);
41 0 : for ( i=0; i<base->height; ++i ) {
42 0 : ipt = (uint32 *) (base->data+i*base->bytes_per_line);
43 0 : for ( j=0; j<base->width; ) {
44 0 : fprintf(file,j==0?" ":"\t");
45 0 : for ( k=0; k<8 && j<base->width; ++k, ++j, ++ipt ) {
46 0 : val=*ipt&0xffffffff;
47 0 : fprintf(file,"0x%.8x%s",(unsigned int) val,j==base->width-1 && i==base->height-1?"":", ");
48 : }
49 0 : fprintf(file,"\n");
50 : }
51 : }
52 : } else {
53 0 : fprintf(file,"static uint8 %s%d_data[] = {\n",stem,instance);
54 0 : for ( i=0; i<base->height; ++i ) {
55 0 : pt = (uint8 *) (base->data+i*base->bytes_per_line);
56 0 : for ( j=0; j<base->bytes_per_line; ) {
57 0 : fprintf(file,j==0?" ":"\t");
58 0 : for ( k=0; k<8 && j<base->bytes_per_line; ++k, ++j, ++pt )
59 0 : fprintf(file,"0x%.2x%s",*pt,j==base->width-1 && i==base->height-1?"":", ");
60 0 : fprintf(file,"\n");
61 : }
62 : }
63 : }
64 0 : fprintf(file,"};\n");
65 :
66 0 : if ( base->clut!=NULL ) {
67 0 : fprintf(file,"\nstatic GClut %s%d_clut = { %d, %d, %ld,\n",
68 : stem,instance,
69 0 : base->clut->clut_len, base->clut->is_grey,(unsigned long) base->clut->trans_index&0xfffffff );
70 0 : for ( i=0; i<base->clut->clut_len; ) {
71 0 : fprintf(file," ");
72 0 : for ( k=0; k<8 && i<base->clut->clut_len; ++k, ++i ) {
73 0 : val=base->clut->clut[i]&0xffffffff;
74 0 : fprintf(file,"0x%.8x%s",(unsigned int) val,i==base->clut->clut_len-1?" };":", ");
75 : }
76 0 : fprintf(file,"\n");
77 : }
78 : }
79 0 : fprintf(file,"\nstatic struct _GImage %s%d_base = {\n",stem,instance);
80 0 : fprintf(file,base->image_type==it_true?" it_true,\n":
81 0 : base->image_type==it_index?" it_index,\n":
82 : " it_mono,\n" );
83 0 : fprintf(file," %d,%ld,%ld,%ld,\n",(int) base->delay,(long) base->width,(long) base->height,(long) base->bytes_per_line);
84 0 : fprintf(file," (uint8 *) %s%d_data,\n",stem,instance);
85 0 : if (base->clut==NULL)
86 0 : fprintf(file," NULL,\n" );
87 : else
88 0 : fprintf(file," &%s%d_clut,\n",stem,instance);
89 0 : fprintf(file," 0x%.8x\n};\n\n",(unsigned int) base->trans&0xffffffff);
90 0 : }
91 :
92 0 : int GImageWriteGImage(GImage *gi, char *filename) {
93 : /* Export a GImage that can be used by FontForge. Return 0 if all done okay */
94 : FILE *file;
95 : int i;
96 : char stem[256];
97 : char *pt;
98 :
99 0 : if ( gi==NULL )
100 0 : return( -1 );
101 :
102 : /* get filename stem (255chars max) */
103 0 : if ( (pt=strrchr(filename,'/'))!=NULL )
104 0 : ++pt;
105 : else
106 0 : pt=filename;
107 0 : strncpy(stem,pt,sizeof(stem)); stem[255]='\0';
108 0 : if ( (pt=strrchr(stem,'.'))!=NULL && pt!=stem )
109 0 : *pt = '\0';
110 :
111 : /* Begin writing C code to the file */
112 0 : if ( (file=fopen(filename,"w"))==NULL ) {
113 0 : fprintf(stderr,"Can't open \"%s\"\n", filename);
114 0 : return( -1 );
115 : }
116 0 : fprintf(file,"/* This file was generated using GImageWriteGImage(gi,\"%s\") */\n",filename);
117 0 : fprintf(file,"#include \"gimage.h\"\n\n" );
118 0 : if ( gi->list_len==0 ) {
119 : /* Construct a single image */
120 0 : WriteBase(file,gi->u.image,stem,0);
121 0 : fprintf(file,"GImage %s = { 0, &%s0_base };\n",stem,stem);
122 : } else {
123 : /* Construct an array of images */
124 0 : for ( i=0; i<gi->list_len; ++i )
125 0 : WriteBase(file,gi->u.images[i],stem,i);
126 0 : fprintf(file,"static struct _GImage *%s_bases = {\n",stem);
127 0 : for ( i=0; i<gi->list_len; ++i )
128 0 : fprintf(file," &%s%d_base%s\n", stem, i, i==gi->list_len-1?"":"," );
129 0 : fprintf(file,"};\n\n" );
130 :
131 0 : fprintf(file,"GImage %s = { %d, (struct _GImage *) %s_bases };\n",stem,gi->list_len,stem);
132 : }
133 0 : fflush(file);
134 0 : i=ferror(file);
135 0 : fclose(file);
136 0 : return( i );
137 : }
|