1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #include "font/anti_prop.hh"
|
---|
10 | #include "image/image.hh"
|
---|
11 | #include "palette/pal.hh"
|
---|
12 | #include "image/context.hh"
|
---|
13 |
|
---|
14 | /* Printable english characters
|
---|
15 | ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | }
|
---|
16 |
|
---|
17 | "
|
---|
18 | */
|
---|
19 |
|
---|
20 | static inline int i4_is_black(w32 c)
|
---|
21 | {
|
---|
22 | if ((c&0xff000000)==0)
|
---|
23 | return 1;
|
---|
24 | else return 0;
|
---|
25 | }
|
---|
26 |
|
---|
27 | static inline int i4_scanline_empty(i4_image_class *im, int y)
|
---|
28 | {
|
---|
29 | for (int x=0; x<im->width(); x++)
|
---|
30 | if (!i4_is_black(im->get_pixel(x,y)))
|
---|
31 | return 0;
|
---|
32 |
|
---|
33 | return 1;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | static inline int i4_colum_empty(i4_image_class *im, int x)
|
---|
39 | {
|
---|
40 | for (int y=0; y<im->height(); y++)
|
---|
41 | if (!i4_is_black(im->get_pixel(x,y)))
|
---|
42 | return 0;
|
---|
43 |
|
---|
44 | return 1;
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | static inline int i4_image_has_alpha(i4_image_class *im)
|
---|
49 | {
|
---|
50 | for (int y=0; y<im->height(); y++)
|
---|
51 | for (int x=0; x<im->width(); x++)
|
---|
52 | {
|
---|
53 | w32 cl=im->get_pixel(x,y);
|
---|
54 | if (cl>>24)
|
---|
55 | i4_warning("alpha %d", cl>>24);
|
---|
56 | }
|
---|
57 |
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | class i4_anti_image_class : public i4_image_class
|
---|
64 | {
|
---|
65 | public:
|
---|
66 |
|
---|
67 | w32 current_color;
|
---|
68 |
|
---|
69 |
|
---|
70 | virtual i4_image_class *copy()
|
---|
71 | {
|
---|
72 | i4_anti_image_class *i=new i4_anti_image_class(w,h);
|
---|
73 | memcpy(i->data, data, w*h);
|
---|
74 | return i;
|
---|
75 | }
|
---|
76 |
|
---|
77 | virtual w16 width() { return w; }
|
---|
78 | virtual w16 height() { return h; }
|
---|
79 |
|
---|
80 | void set_color(w32 c) { current_color=c; }
|
---|
81 |
|
---|
82 | virtual void put_pixel(i4_coord x, i4_coord y, w32 color)
|
---|
83 | {
|
---|
84 | *((w8 *)data+x+y*w)=color>>24;
|
---|
85 | }
|
---|
86 |
|
---|
87 | virtual w32 get_pixel(i4_coord x, i4_coord y)
|
---|
88 | {
|
---|
89 | return ((*((w8 *)data+x+y*w))<<24) | current_color;
|
---|
90 | }
|
---|
91 |
|
---|
92 | i4_anti_image_class(w16 _w, w16 _h)
|
---|
93 | {
|
---|
94 | w=_w;
|
---|
95 | h=_h;
|
---|
96 | bpl=w;
|
---|
97 | i4_pixel_format fmt;
|
---|
98 | fmt.default_format();
|
---|
99 | pal=i4_pal_man.register_pal(&fmt);
|
---|
100 | data=i4_malloc(w*h,"");
|
---|
101 |
|
---|
102 | current_color=0xffffff;
|
---|
103 | }
|
---|
104 |
|
---|
105 | ~i4_anti_image_class()
|
---|
106 | {
|
---|
107 | i4_free(data);
|
---|
108 | }
|
---|
109 |
|
---|
110 | };
|
---|
111 |
|
---|
112 | void i4_anti_proportional_font_class::put_string(i4_image_class *screen,
|
---|
113 | sw16 x, sw16 y,
|
---|
114 | const i4_const_str &string,
|
---|
115 | i4_draw_context_class &context)
|
---|
116 | {
|
---|
117 | if (!string.null())
|
---|
118 | {
|
---|
119 | for (i4_const_str::iterator p=string.begin(); p!=string.end(); ++p)
|
---|
120 | {
|
---|
121 | char ch=p.get().ascii_value();
|
---|
122 | if (offsets[ch])
|
---|
123 | {
|
---|
124 | aim->put_part(screen, x,y, offsets[ch], 0, offsets[ch]+widths[ch]-1, aim->height()-1,
|
---|
125 | context);
|
---|
126 |
|
---|
127 | x+=widths[ch];
|
---|
128 | }
|
---|
129 | else x+=4;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | void i4_anti_proportional_font_class::put_character(i4_image_class *screen,
|
---|
136 | sw16 x, sw16 y,
|
---|
137 | const i4_char &c,
|
---|
138 | i4_draw_context_class &context)
|
---|
139 | {
|
---|
140 | int ch=c.ascii_value();
|
---|
141 | int xo=offsets[ch];
|
---|
142 | if (xo)
|
---|
143 | aim->put_part(screen, x,y, xo,0,xo+widths[ch]-1,aim->height(), context);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | i4_anti_proportional_font_class::i4_anti_proportional_font_class(i4_image_class *im,
|
---|
148 | int start_ch)
|
---|
149 | {
|
---|
150 | memset(offsets, 0, sizeof(offsets));
|
---|
151 | longest_w=2;
|
---|
152 |
|
---|
153 |
|
---|
154 | // i4_image_has_alpha(im);
|
---|
155 |
|
---|
156 | int y_top=0, y_bottom=im->height()-1, x1=0,x2,i;
|
---|
157 | for (i=0; i<256; i++)
|
---|
158 | widths[i]=4;
|
---|
159 |
|
---|
160 | while (y_top<=y_bottom && i4_scanline_empty(im, y_top))
|
---|
161 | y_top++;
|
---|
162 |
|
---|
163 | if (y_top==y_bottom-1)
|
---|
164 | i4_error("image is empty");
|
---|
165 |
|
---|
166 | while (i4_scanline_empty(im, y_bottom))
|
---|
167 | y_bottom--;
|
---|
168 |
|
---|
169 | int char_on=start_ch;
|
---|
170 | int x=1;
|
---|
171 |
|
---|
172 | do
|
---|
173 | {
|
---|
174 | while (x1!=im->width() && i4_colum_empty(im, x1)) x1++;
|
---|
175 | if (x1<im->width())
|
---|
176 | {
|
---|
177 | x2=x1+1;
|
---|
178 | while (x2!=im->width() &&
|
---|
179 | !(i4_colum_empty(im, x2) && i4_colum_empty(im, x2+1))) x2++;
|
---|
180 | widths[char_on]=x2-x1+1;
|
---|
181 | if (widths[char_on]>longest_w)
|
---|
182 | longest_w=widths[char_on];
|
---|
183 |
|
---|
184 | offsets[char_on]=x;
|
---|
185 | x+=x2-x1+1;
|
---|
186 | x1=x2+2;
|
---|
187 | }
|
---|
188 | char_on++;
|
---|
189 | } while (x1<im->width());
|
---|
190 |
|
---|
191 | h=y_bottom-y_top+1;
|
---|
192 | aim=new i4_anti_image_class(x, h);
|
---|
193 |
|
---|
194 | x1=0; x=1;
|
---|
195 | i4_draw_context_class context(0,0, aim->width()-1, aim->height()-1);
|
---|
196 | char_on=start_ch;
|
---|
197 | do
|
---|
198 | {
|
---|
199 | while (x1!=im->width() && i4_colum_empty(im, x1)) x1++;
|
---|
200 | if (x1<im->width())
|
---|
201 | {
|
---|
202 | x2=x1+1;
|
---|
203 | while (x2!=im->width() &&
|
---|
204 | !(i4_colum_empty(im, x2) && i4_colum_empty(im, x2+1))) x2++;
|
---|
205 |
|
---|
206 | im->i4_image_class::put_part(aim, x, 0, x1, y_top, x2, y_bottom, context);
|
---|
207 | x+=x2-x1+1;
|
---|
208 | x1=x2+2;
|
---|
209 | }
|
---|
210 | char_on++;
|
---|
211 | } while (x1<im->width());
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | void i4_anti_proportional_font_class::set_color(i4_color color)
|
---|
216 | {
|
---|
217 | aim->set_color(color);
|
---|
218 | }
|
---|
219 |
|
---|
220 | i4_anti_proportional_font_class::~i4_anti_proportional_font_class()
|
---|
221 | {
|
---|
222 | delete aim;
|
---|
223 | }
|
---|