NetSurf
font_vdi.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Ole Loots <ole@monochrom.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef WITH_VDI_FONT_DRIVER
20
21#include <mt_gemx.h>
22
23#include "atari/plot/plot.h"
24#include "atari/plot/font_vdi.h"
25
26#include "utils/utf8.h"
27#include "utils/log.h"
28
29#include "atari/encoding.h"
30
31
32//static char * lstr = NULL;
33
34static int dtor( FONT_PLOTTER self );
35static int str_width( FONT_PLOTTER self,const plot_font_style_t *fstyle, const char * str, size_t length, int * width );
36static int str_split( FONT_PLOTTER self, const plot_font_style_t *fstyle,const char *string,
37 size_t length,int x, size_t *char_offset, int *actual_x );
38static int pixel_pos( FONT_PLOTTER self, const plot_font_style_t *fstyle,const char *string,
39 size_t length,int x, size_t *char_offset, int *actual_x );
40static int text( FONT_PLOTTER self, int x, int y, const char *text, size_t length, const plot_font_style_t *fstyle );
41
42static bool init = false;
43static int vdih;
44
45extern struct s_vdi_sysinfo vdi_sysinfo;
46
47static inline void atari_to_vdi_str(char *lstr, int length)
48{
49 int i, z;
50
51 for (i=z=0; i<length; ) {
52 if (((char)lstr[i]==(char)0xC2) && ((char)lstr[i+1] == (char)0xA0)) {
53 lstr[z] = ' ';
54 lstr[z+1] = ' ';
55 i=i+2;
56 z=z+2;
57 }
58 else {
59 lstr[z] = lstr[i];
60 i++;
61 z++;
62 }
63 }
64}
65
66int ctor_font_plotter_vdi( FONT_PLOTTER self )
67{
68 self->dtor = dtor;
69 self->str_width = str_width;
70 self->str_split = str_split;
71 self->pixel_pos = pixel_pos;
72 self->text = text;
73 NSLOG(netsurf, INFO, "%s: %s\n", (char *)__FILE__, __FUNCTION__);
74 if( !init ) {
75 vdih = self->vdi_handle;
76 }
77 init = true;
78 return( 1 );
79}
80
81static int dtor( FONT_PLOTTER self )
82{
83 return( 1 );
84}
85
86static int str_width( FONT_PLOTTER self,const plot_font_style_t *fstyle, const char * str,
87 size_t length, int * width )
88{
89 short cw, ch, cellw, cellh;
90 short pxsize;
91 short fx=0;
92 char * lstr = NULL;
93
94 utf8_to_local_encoding(str, length, &lstr);
95 assert( lstr != NULL );
96 int slen = strlen(lstr);
97
98
99 atari_to_vdi_str(lstr, slen);
100
101 if( fstyle->flags & FONTF_ITALIC )
102 fx |= 4;
103 if( fstyle->flags & FONTF_OBLIQUE )
104 fx |= 16;
105 if( fstyle->weight > 450 )
106 fx |= 1;
107 vst_effects( self->vdi_handle, fx );
108 /* TODO: replace 90 with global dpi setting */
109 //pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 / 72 );
110 //vst_height( self->vdi_handle, pxsize ,&cw, &ch, &cellw, &cellh);
111 pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 );
112 vst_point( self->vdi_handle, pxsize, &cw, &ch, &cellw, &cellh);
113/*
114 if(slen != utf8_bounded_length(str, length)){
115 printf("sl: %d, utl: %d\n ", slen, utf8_bounded_length(str, length));
116 printf("s: %s // %s\n", str, lstr );
117 }*/
118
119
120 *width = slen * cellw;
121 free((void*)lstr);
122 return( 0 );
123}
124
125static int str_split( FONT_PLOTTER self, const plot_font_style_t * fstyle, const char *string,
126 size_t length,int x, size_t *char_offset, int *actual_x )
127{
128 short cw, ch, cellw, cellh;
129 short pxsize;
130 short fx=0;
131 char *lstr = NULL;
132 size_t slen = 0;
133 int last_space_x = 0;
134 int last_space_idx = 0;
135 size_t nxtchr = 0;
136
137 utf8_to_local_encoding(string, length, &lstr );
138 assert( lstr != NULL );
139 slen = strlen(lstr);
140
141
142 atari_to_vdi_str(lstr, slen);
143
144 if( fstyle->flags & FONTF_ITALIC )
145 fx |= 4;
146 if( fstyle->flags & FONTF_OBLIQUE )
147 fx |= 16;
148 if( fstyle->weight > 450 )
149 fx |= 1;
150 vst_effects( self->vdi_handle, fx );
151 //pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 / 72 );
152 //vst_height( self->vdi_handle, pxsize ,&cw, &ch, &cellw, &cellh);
153
154 pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 );
155 vst_point( self->vdi_handle, pxsize, &cw, &ch, &cellw, &cellh);
156 *actual_x = 0;
157 //*char_offset = 0;
158 while (nxtchr < slen) {
159 if( lstr[nxtchr] == ' ' ) {
160 last_space_x = *actual_x;
161 last_space_idx = nxtchr;
162 }
163 *actual_x += cellw;
164 if (*actual_x > x && last_space_idx != 0) {
165 *actual_x = last_space_x;
166 *char_offset = last_space_idx;
167 //printf("at: %s\n", lstr);
168 return(0);
169 }
170
171 nxtchr++;
172 }
173 if(nxtchr >= length){
174 nxtchr = length-1;
175 }
176
177 *char_offset = nxtchr;
178
179// for( i=0; i<slen; i++) {
180// if( lstr[i] == ' ' ) {
181// last_space_x = *actual_x;
182// last_space_idx = cpos;
183// }
184// if( *actual_x > x ) {
185// *actual_x = last_space_x;
186// *char_offset = last_space_idx;
187// return true;
188// }
189// *actual_x += cellw;
190// cpos++;
191// }
192// *char_offset = cpos;
193 free( (void*)lstr );
194 return( 0 );
195}
196
197static int pixel_pos( FONT_PLOTTER self, const plot_font_style_t * fstyle,const char *string,
198 size_t length,int x, size_t *char_offset, int *actual_x )
199{
200 short cw, ch, cellw, cellh;
201 short pxsize=0;
202 short fx=0;
203
204 char *lstr = NULL;
205 int i=0;
206 utf8_to_local_encoding(string, length, &lstr );
207 assert( lstr != NULL );
208 int slen = strlen(lstr);
209
210 atari_to_vdi_str(lstr, slen);
211
212 if( fstyle->flags & FONTF_ITALIC )
213 fx |= 4;
214 if( fstyle->flags & FONTF_OBLIQUE )
215 fx |= 16;
216 if( fstyle->weight > 450 )
217 fx |= 1;
218 vst_effects(self->vdi_handle, fx);
219 pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 / 72 );
220 vst_height( self->vdi_handle, pxsize ,&cw, &ch, &cellw, &cellh);
221 *actual_x = 0;
222 *char_offset = 0;
223 for( i=0; i<slen; i++) {
224 *actual_x += cellw;
225 if( *actual_x > x) {
226 *actual_x -= cellw;
227 *char_offset = i;
228 break;
229 }
230 }
231 free((void*)lstr);
232 lstr = NULL;
233 return( 0 );
234}
235
236static inline void vst_rgbcolor( short vdih, uint32_t cin )
237{
238#ifdef WITH_8BPP_SUPPORT
239 if( vdi_sysinfo.scr_bpp > 8 ) {
240#endif
241 //unsigned short c[4];
242 RGB1000 c;
243
244 rgb_to_vdi1000( (unsigned char*)&cin, &c );
245 vs_color( vdih, OFFSET_CUSTOM_COLOR, (unsigned short*)&c);
246 vst_color( vdih, OFFSET_CUSTOM_COLOR );
247#ifdef WITH_8BPP_SUPPORT
248 } else {
249 if( vdi_sysinfo.scr_bpp >= 4 )
250 vst_color( vdih, RGB_TO_VDI(cin) );
251 else
252 vst_color( vdih, BLACK );
253 }
254#endif
255}
256
257static int text( FONT_PLOTTER self, int x, int y, const char *text, size_t length,
258 const plot_font_style_t *fstyle )
259{
260 /* todo: either limit the string to max 80 chars, or use v_ftext instead of v_gtext */
261 short cw, ch, cellw, cellh;
262 short pxsize=8;
263 short fx=0;
264 GRECT canvas;
265 char *lstr = NULL;
266 assert( utf8_to_local_encoding(text, length, &lstr) == NSERROR_OK);
267 assert( lstr != NULL );
268
269 int slen = strlen(lstr);
270 if(slen > 800){
271 lstr[800]=0;
272 }
273
274
275 atari_to_vdi_str(lstr, slen);
276
277 if( fstyle->flags & FONTF_ITALIC )
278 fx |= 4;
279 if( fstyle->flags & FONTF_OBLIQUE )
280 fx |= 4;
281 if( fstyle->weight > 450 )
282 fx |= 1;
283
284 /* TODO: netsurf uses 90 as default dpi ( somewhere defined in libcss),
285 use that value or pass it as arg, to reduce netsurf dependency */
286 //pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 / 72 );
287 pxsize = ceil( (fstyle->size/PLOT_STYLE_SCALE) * 90 / 72 );
288
289 plot_get_dimensions(&canvas);
290 x += canvas.g_x;
291 y += canvas.g_y;
292 vst_effects( self->vdi_handle, fx );
293 vst_alignment(vdih, 0, 0, &cw, &ch );
294 vst_point( self->vdi_handle, pxsize, &cw, &ch, &cellw, &cellh);
295 //vst_height( self->vdi_handle, pxsize, &cw, &ch, &cellw, &cellh);
296 vswr_mode( self->vdi_handle, MD_TRANS );
297 vst_rgbcolor(self->vdi_handle, fstyle->foreground);
298
300 //printf("\nftext\n");
301 v_ftext( self->vdi_handle, x, y, (char*)lstr );
302 } else {
303 //printf("\ngtext\n");
304 v_gtext( self->vdi_handle, x, y, (char*)lstr );
305 }
306 free( lstr );
307 return( 0 );
308}
309
310#endif
struct s_vdi_sysinfo vdi_sysinfo
Definition: plot.c:196
short vdih
void rgb_to_vdi1000(unsigned char *in, RGB1000 *out)
Convert an RGB color to an VDI Color.
Definition: plot.c:564
bool plot_get_dimensions(GRECT *dst)
Get current canvas size.
Definition: plot.c:1838
#define OFFSET_CUSTOM_COLOR
Definition: plot.h:175
static bool init
Definition: toolbar.c:89
@ NSERROR_OK
No error.
Definition: errors.h:30
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
Definition: utf8.c:89
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
NS_ATARI_SYSINFO atari_sysinfo
Definition: osspec.c:38
@ FONTF_ITALIC
Definition: plot_style.h:103
@ FONTF_OBLIQUE
Definition: plot_style.h:104
#define PLOT_STYLE_SCALE
Scaling factor for plot styles.
Definition: plot_style.h:45
int width
Definition: gui.c:160
unsigned short gdos_FSMC
Definition: osspec.h:29
Font style for plotting.
Definition: plot_style.h:111
colour foreground
Colour of text.
Definition: plot_style.h:123
plot_font_flags_t flags
Font flags.
Definition: plot_style.h:121
plot_style_fixed size
Font size, in pt.
Definition: plot_style.h:119
int weight
Font weight: value in range [100,900] as per CSS.
Definition: plot_style.h:120
_fpmf_str_width str_width
Definition: fontplot.h:56
_fpmf_dtor dtor
Definition: fontplot.h:61
_fpmf_text text
Definition: fontplot.h:59
int vdi_handle
Definition: fontplot.h:53
_fpmf_str_split str_split
Definition: fontplot.h:57
_fpmf_pixel_pos pixel_pos
Definition: fontplot.h:58
short scr_bpp
bits per pixel
Definition: plot.h:64
UTF-8 manipulation functions (interface).
static nserror text(const struct redraw_context *ctx, const struct plot_font_style *fstyle, int x, int y, const char *text, size_t length)
Text plotting.
Definition: plot.c:978