e576bb | 2002-10-11 | Martin Nilsson | | |
20d055 | 2009-04-22 | Martin Stjernholm | | || $Id$
|
e576bb | 2002-10-11 | Martin Nilsson | | */
|
1b10db | 2002-10-08 | Martin Nilsson | |
|
fec625 | 1998-03-25 | Fredrik Hübinette (Hubbe) | | #include "global.h"
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
7abac3 | 1997-09-01 | Per Hedbor | | #define SPACE_CHAR 'i'
|
2062bd | 2003-02-20 | Henrik Grubbström (Grubba) | | extern const unsigned char image_default_font[];
|
0e756f | 1999-05-24 | Mirar (Pontus Hagland) | | #define IMAGE_DEFAULT_FONT_SIZE 30596
|
e3cb4b | 1997-04-03 | Mirar (Pontus Hagland) | |
|
ab5e03 | 2001-11-18 | Martin Nilsson | | |
15dfec | 1997-05-28 | Mirar (Pontus Hagland) | | */
|
ab5e03 | 2001-11-18 | Martin Nilsson | | |
cf98cf | 2002-11-29 | Johan Sundström | | *! capabilities of the @[Image] module.
|
ab5e03 | 2001-11-18 | Martin Nilsson | | *!
*! For simple usage, see @[write] and @[load].
*!
*! @note
*! @pre{
|
cf98cf | 2002-11-29 | Johan Sundström | | *! struct file_head
|
ab5e03 | 2001-11-18 | Martin Nilsson | | *! {
|
cf98cf | 2002-11-29 | Johan Sundström | | *! unsigned INT32 cookie; - 0x464f4e54
*! unsigned INT32 version; - 1
|
ab5e03 | 2001-11-18 | Martin Nilsson | | *! unsigned INT32 chars; - number of chars
*! unsigned INT32 height; - height of font
*! unsigned INT32 baseline; - font baseline
*! unsigned INT32 o[1]; - position of char_head's
*! } *fh;
*! struct char_head
*! {
*! unsigned INT32 width; - width of this character
*! unsigned INT32 spacing; - spacing to next character
*! unsigned char data[1]; - pixmap data (1byte/pixel)
*! } *ch;
*!
*! version 2:
*!
*!
*! On-disk syntax (everything in N.B.O), int is 4 bytes, a byte is 8 bits:
*!
*! pos
*! 0 int cookie = 'FONT'; or 0x464f4e54
*! 4 int version = 2; 1 was the old version without the last four chars
*! 8 int numchars; Always 256 in this version of the dump program
*! 12 int height; in (whole) pixels
*! 16 int baseline; in (whole) pixels
*! 20 char direction; 1==right to left, 0 is left to right
*! 21 char format; Font format
*! 22 char colortablep; Colortable format
*! 23 char kerningtablep; Kerning table format
*!
*! 24 int offsets[numchars]; pointers into the data, realative to &cookie.
*! [colortable]
*! [kerningtable]
*!
*! At each offset:
*!
*!
*! 0 int width; in pixels
*! 4 int spacing; in 1/1000:th of a pixels
*! 8 char data[]; Enough data to plot width * font->height pixels
*! Please note that if width is 0, there is no data.
*!
*! Font formats:
*! id type
*! 0 Raw 8bit data
*! 1 RLE encoded data, char length, char data, 70% more compact than raw data
*! 2 ZLib compressed data 60% more compact than RLE
*!
*! Colortable types:
*! 0 No colortable (the data is an alpha channel)
*! 1 24bit RGB with alpha (index->color, 256*4 bytes, rgba)
*! 2 8bit Greyscale with alpha (index->color, 256*2 bytes)
*!
*! Kerningtable types:
*! 0 No kerning table
*! 1 numchars*numchars entries, each a signed char with the kerning value
*! 2 numchar entries, each with a list of kerning pairs, like this:
*! int len
*! len * (short char, short value)
*! @}
*!
*! @seealso
*! @[Image], @[Image.Image]
*/
|
15dfec | 1997-05-28 | Mirar (Pontus Hagland) | |
|
da4684 | 1999-09-14 | Mirar (Pontus Hagland) | | #include "image_machine.h"
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_FCNTL_H
#include <sys/fcntl.h>
#endif
|
721019 | 1997-12-22 | Fredrik Hübinette (Hubbe) | | #ifdef HAVE_NETINET_IN_H
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #include <netinet/in.h>
|
721019 | 1997-12-22 | Fredrik Hübinette (Hubbe) | | #endif
|
4fcf44 | 2003-04-30 | Henrik Grubbström (Grubba) | | #ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#elif defined(HAVE_WINSOCK_H)
|
32434a | 1998-02-10 | Fredrik Hübinette (Hubbe) | | #include <winsock.h>
|
75920f | 1997-12-28 | Fredrik Hübinette (Hubbe) | | #endif
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #include <errno.h>
#include "stralloc.h"
#include "object.h"
#include "interpret.h"
#include "svalue.h"
#include "threads.h"
|
9c6f7d | 1997-04-15 | Fredrik Hübinette (Hubbe) | | #include "builtin_functions.h"
|
7d60af | 2004-05-19 | Martin Nilsson | | #include "module_support.h"
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
#include "image.h"
|
c6b2f6 | 1999-10-16 | Mirar (Pontus Hagland) | | #ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
|
3c0c28 | 1998-01-26 | Fredrik Hübinette (Hubbe) | | #include "dmalloc.h"
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
da4684 | 1999-09-14 | Mirar (Pontus Hagland) | | #include "fdlib.h"
|
50c6fa | 2000-08-14 | Henrik Grubbström (Grubba) | | #include "bignum.h"
|
6dc277 | 2000-07-28 | Fredrik Hübinette (Hubbe) | |
|
6ad237 | 2002-05-11 | Martin Nilsson | | #define sp Pike_sp
|
15e40e | 1999-06-03 | Mirar (Pontus Hagland) | | extern struct program *font_program;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | extern struct program *image_program;
|
75920f | 1997-12-28 | Fredrik Hübinette (Hubbe) | | #undef THIS
|
39221e | 2000-07-07 | Henrik Grubbström (Grubba) | | #define THIS (*(struct font **)(Pike_fp->current_storage))
#define THISOBJ (Pike_fp->current_object)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
struct font
{
unsigned long height;
unsigned long baseline;
#ifdef HAVE_MMAP
unsigned long mmaped_size;
#endif
void *mem;
unsigned long chars;
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | double xspacing_scale;
double yspacing_scale;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | enum {
J_LEFT,
J_RIGHT,
J_CENTER
} justification;
struct _char
{
unsigned long width;
unsigned long spacing;
unsigned char *pixels;
} charinfo [1];
};
|
9037d0 | 1998-02-18 | Per Hedbor | | static INLINE void free_font_struct(struct font *font)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
if (font)
{
|
0e756f | 1999-05-24 | Mirar (Pontus Hagland) | | if (font->mem && font->mem!=image_default_font)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
#ifdef HAVE_MMAP
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (font->mmaped_size) {
munmap(font->mem,font->mmaped_size);
} else
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #else
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | free(font->mem);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | font->mem = NULL;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
free(font);
}
}
static void init_font_struct(struct object *o)
{
THIS=NULL;
}
static void exit_font_struct(struct object *obj)
{
free_font_struct(THIS);
THIS=NULL;
}
|
3d94d1 | 2001-07-12 | Henrik Grubbström (Grubba) | | static INLINE int char_space(struct font *this, INT32 c)
|
d4f244 | 1997-09-06 | Per Hedbor | | {
if(c==0x20)
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | return DOUBLE_TO_INT((double)(this->height*this->xspacing_scale)/4.5);
|
d4f244 | 1997-09-06 | Per Hedbor | | else if(c==0x20+128)
|
3d94d1 | 2001-07-12 | Henrik Grubbström (Grubba) | | return DOUBLE_TO_INT((this->height*this->xspacing_scale)/18);
return DOUBLE_TO_INT(this->charinfo[c].spacing*this->xspacing_scale);
|
d4f244 | 1997-09-06 | Per Hedbor | | }
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | static INLINE int char_width(struct font *this, INT32 c)
|
d4f244 | 1997-09-06 | Per Hedbor | | {
if(c==0x20 || c==0x20+128) return 0;
return this->charinfo[c].width;
}
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | static INLINE ptrdiff_t my_read(int fd, void *t, size_t towrite)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
|
bea2ad | 2000-08-14 | Henrik Grubbström (Grubba) | | ptrdiff_t res;
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | while((res = fd_read(fd, t, towrite)) < 0)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
switch(errno)
{
case EAGAIN: case EINTR:
continue;
default:
res = 0;
return 0;
}
}
return res;
}
|
4d869c | 1999-04-25 | Henrik Grubbström (Grubba) | | static INLINE off_t file_size(int fd)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
|
40962a | 2003-03-27 | Martin Stjernholm | | PIKE_STAT_T tmp;
|
e42eaf | 1998-01-02 | Fredrik Hübinette (Hubbe) | | if((!fd_fstat(fd, &tmp)) &&
|
7a5e47 | 1999-11-22 | Fredrik Hübinette (Hubbe) | | ( tmp.st_mode & S_IFMT) == S_IFREG)
|
4d869c | 1999-04-25 | Henrik Grubbström (Grubba) | | return (off_t)tmp.st_size;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | return -1;
}
|
d4f244 | 1997-09-06 | Per Hedbor | | static INLINE void write_char(struct _char *ci,
|
9c6f7d | 1997-04-15 | Fredrik Hübinette (Hubbe) | | rgb_group *pos,
INT32 xsize,
INT32 height)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
rgb_group *nl;
INT32 x,y;
unsigned char *p;
p=ci->pixels;
for (y=height; y>0; y--)
{
nl=pos+xsize;
for (x=(INT32)ci->width; x>0; x--)
{
|
1f9416 | 1997-09-03 | Per Hedbor | | int r,c;
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | if((c=255-*p)) {
|
1f9416 | 1997-09-03 | Per Hedbor | | if ((r=pos->r+c)>255)
pos->r=pos->g=pos->b=255;
else
pos->r=pos->g=pos->b=r;
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | }
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | pos++;
p++;
}
pos=nl;
}
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | #ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
void font_load(INT32 args)
{
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | struct file_head
{
unsigned INT32 cookie;
unsigned INT32 version;
unsigned INT32 chars;
unsigned INT32 height;
unsigned INT32 baseline;
unsigned INT32 o[1];
} *fh = NULL;
#ifdef HAVE_MMAP
size_t mmaped_size = 0;
#endif
size_t size = 0;
|
ddc1a3 | 2010-07-27 | Martin Stjernholm | | char *filename = NULL;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
62522d | 2005-11-13 | Martin Nilsson | | get_all_args("Image.Font->load()", args, ".%s", &filename);
|
0e756f | 1999-05-24 | Mirar (Pontus Hagland) | |
|
ddc1a3 | 2010-07-27 | Martin Stjernholm | | if (!filename)
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | {
fh = (struct file_head *)image_default_font;
size = IMAGE_DEFAULT_FONT_SIZE;
} else {
int fd = -1;
|
0e756f | 1999-05-24 | Mirar (Pontus Hagland) | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | do
{
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT open '%s'\n",sp[-args].u.string->str);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
62522d | 2005-11-13 | Martin Nilsson | | fd = fd_open(filename,fd_RDONLY,0);
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (errno == EINTR) check_threads_etc();
} while(fd < 0 && errno == EINTR);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (fd >= 0)
{
|
758b14 | 1999-04-25 | Henrik Grubbström (Grubba) | | size = (size_t) file_size(fd);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | if (size > 0)
{
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | THREADS_ALLOW();
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef HAVE_MMAP
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fh = (struct file_head *)
mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
if (fh != (struct file_head *)MAP_FAILED)
mmaped_size = size;
else
{
|
c6b2f6 | 1999-10-16 | Mirar (Pontus Hagland) | | #endif
|
00b2e1 | 2006-03-06 | Peter Bortas | | fh = (struct file_head *)xalloc(size);
|
e42eaf | 1998-01-02 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT Malloced %p (%d)\n", fh, size);
|
e42eaf | 1998-01-02 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (fh && (!my_read(fd, fh, size)))
{
free(fh);
fh = NULL;
}
#ifdef HAVE_MMAP
}
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | THREADS_DISALLOW();
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | }
#ifdef FONT_DEBUG
else fprintf(stderr,"FONT size failure\n");
#endif
fd_close(fd);
}
#ifdef FONT_DEBUG
else fprintf(stderr,"FONT fd failure\n");
#endif
}
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (THIS)
{
free_font_struct(THIS);
THIS=NULL;
}
|
0e756f | 1999-05-24 | Mirar (Pontus Hagland) | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (fh)
{
struct char_head
{
unsigned INT32 width;
unsigned INT32 spacing;
unsigned char data[1];
} *ch;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
#ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT mapped ok\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (ntohl(fh->cookie)==0x464f4e54)
{
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT cookie ok\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (ntohl(fh->version)==1)
{
struct font *new_font;
unsigned long num_chars;
unsigned long i;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
#ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT version 1\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | num_chars = ntohl(fh->chars);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | new_font=malloc(sizeof(struct font)+
sizeof(struct _char)*(num_chars-1));
if(!new_font) {
|
ddc1a3 | 2010-07-27 | Martin Stjernholm | | if (filename) {
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef HAVE_MMAP
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | if (mmaped_size)
munmap((void *)fh, mmaped_size);
else
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | free(fh);
}
SIMPLE_OUT_OF_MEMORY_ERROR(0,0);
}
new_font->mem = (void *)fh;
#ifdef HAVE_MMAP
new_font->mmaped_size = mmaped_size;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | new_font->chars = num_chars;
new_font->xspacing_scale = 1.0;
new_font->yspacing_scale = 1.0;
new_font->justification = J_LEFT;
new_font->height=ntohl(fh->height);
new_font->baseline=ntohl(fh->baseline);
for (i=0; i<num_chars; i++)
{
if (i*sizeof(INT32)<size
&& ntohl(fh->o[i])<size
&& ! ( ntohl(fh->o[i]) % 4) )
{
ch = (struct char_head*)
(((char *)(fh)) + ntohl(fh->o[i]));
new_font->charinfo[i].width = ntohl(ch->width);
new_font->charinfo[i].spacing = ntohl(ch->spacing);
new_font->charinfo[i].pixels = ch->data;
}
else
{
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT failed on char %02xh %d '%c'\n",
i,i,i);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | free_font_struct(new_font);
pop_n_elems(args);
push_int(0);
return;
}
}
pop_n_elems(args);
THIS = new_font;
ref_push_object(THISOBJ);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | fprintf(stderr,"FONT successfully loaded\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | return;
}
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | else fprintf(stderr,"FONT unknown version\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | }
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | else fprintf(stderr,"FONT wrong cookie\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
ddc1a3 | 2010-07-27 | Martin Stjernholm | | if (filename) {
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | #ifdef HAVE_MMAP
if (mmaped_size)
munmap((void *)fh, mmaped_size);
else
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | free(fh);
}
}
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #ifdef FONT_DEBUG
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | else fprintf(stderr,"FONT mem failure\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | #endif
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | pop_n_elems(args);
push_int(0);
return;
}
|
0e756f | 1999-05-24 | Mirar (Pontus Hagland) | |
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | void font_create(INT32 args)
{
font_load(args);
pop_stack();
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
|
ab5e03 | 2001-11-18 | Martin Nilsson | | |
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | | */
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_write(INT32 args)
{
struct object *o;
struct image *img;
|
d4f244 | 1997-09-06 | Per Hedbor | | INT32 xsize=0,i,maxwidth2,j;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | int *width_of;
|
086cf0 | 1998-10-11 | Marcus Comstedt | | p_wchar0 *to_write0;
p_wchar1 *to_write1;
p_wchar2 *to_write2;
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | ptrdiff_t to_write_len;
|
086cf0 | 1998-10-11 | Marcus Comstedt | | INT32 c;
|
39221e | 2000-07-07 | Henrik Grubbström (Grubba) | | struct font *this = (*(struct font **)(Pike_fp->current_storage));
|
7d60af | 2004-05-19 | Martin Nilsson | | ONERROR err;
|
7abac3 | 1997-09-01 | Per Hedbor | | if (!this)
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | Pike_error("font->write: no font loaded\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | if (args==0)
{
|
7863d6 | 2005-05-06 | Martin Nilsson | | push_empty_string();
|
7d60af | 2004-05-19 | Martin Nilsson | | args=1;
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | }
|
5cb001 | 1998-02-10 | Mirar (Pontus Hagland) | | maxwidth2=1;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
7d60af | 2004-05-19 | Martin Nilsson | | width_of=(int *)xalloc((args+1)*sizeof(int));
SET_ONERROR(err, free, width_of);
|
1f9416 | 1997-09-03 | Per Hedbor | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | for (j=0; j<args; j++)
{
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | int max;
if (sp[j-args].type!=T_STRING)
|
80f30b | 1999-06-18 | Mirar (Pontus Hagland) | | bad_arg_error("font->write",sp-args,args,0,"",sp-args,
"Bad arguments to font->write()\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | xsize = max = 1;
to_write_len = sp[j-args].u.string->len;
|
086cf0 | 1998-10-11 | Marcus Comstedt | | switch(sp[j-args].u.string->size_shift)
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | {
|
086cf0 | 1998-10-11 | Marcus Comstedt | | case 0:
to_write0 = STR0(sp[j-args].u.string);
for (i = 0; i < to_write_len; i++)
{
|
2d6c52 | 1998-11-03 | Per Hedbor | | if (to_write0[i] < (INT32)this->chars)
{
if (xsize+char_width(this,to_write0[i]) > max)
max=xsize+char_width(this,to_write0[i]);
xsize += char_space(this,to_write0[i]);
if (xsize > max) max=xsize;
}
|
086cf0 | 1998-10-11 | Marcus Comstedt | | }
break;
case 1:
to_write1 = STR1(sp[j-args].u.string);
for (i = 0; i < to_write_len; i++)
{
|
2d6c52 | 1998-11-03 | Per Hedbor | | if (to_write1[i] < (INT32)this->chars)
{
if (xsize+char_width(this,to_write1[i]) > max)
max=xsize+char_width(this,to_write1[i]);
xsize += char_space(this,to_write1[i]);
if (xsize > max) max=xsize;
}
|
086cf0 | 1998-10-11 | Marcus Comstedt | | }
break;
case 2:
to_write2 = STR2(sp[j-args].u.string);
for (i = 0; i < to_write_len; i++)
{
|
2d6c52 | 1998-11-03 | Per Hedbor | | if (to_write2[i] < (unsigned INT32)this->chars)
{
if (xsize+char_width(this,to_write2[i]) > max)
max=xsize+char_width(this,to_write2[i]);
xsize += char_space(this,to_write2[i]);
if (xsize > max) max=xsize;
}
|
086cf0 | 1998-10-11 | Marcus Comstedt | | }
break;
|
62522d | 2005-11-13 | Martin Nilsson | | #ifdef PIKE_DEBUG
|
086cf0 | 1998-10-11 | Marcus Comstedt | | default:
|
5aad93 | 2002-08-15 | Marcus Comstedt | | Pike_fatal("Illegal shift size!\n");
|
62522d | 2005-11-13 | Martin Nilsson | | #endif
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | }
width_of[j]=max;
if (max>maxwidth2) maxwidth2=max;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
|
e70975 | 1997-03-12 | Fredrik Hübinette (Hubbe) | | o = clone_object(image_program,0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | img = ((struct image*)o->storage);
img->xsize = maxwidth2;
if(args>1)
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | img->ysize = DOUBLE_TO_INT(this->height+
((double)this->height*(double)(args-1)*
(double)this->yspacing_scale)+1);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | else
|
7a65d6 | 1998-02-07 | Mirar (Pontus Hagland) | | img->ysize = this->height;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | img->rgb.r=img->rgb.g=img->rgb.b=255;
|
20d055 | 2009-04-22 | Martin Stjernholm | | img->img=malloc(img->xsize*img->ysize*sizeof(rgb_group)+RGB_VEC_PAD);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
7d60af | 2004-05-19 | Martin Nilsson | | if (!img->img) {
free_object(o);
SIMPLE_OUT_OF_MEMORY_ERROR("write",
img->xsize*img->ysize*sizeof(rgb_group)+1);
}
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
MEMSET(img->img,0,img->xsize*img->ysize*sizeof(rgb_group));
for (j=0; j<args; j++)
{
|
1f9416 | 1997-09-03 | Per Hedbor | | to_write_len = sp[j-args].u.string->len;
|
7abac3 | 1997-09-01 | Per Hedbor | | switch(this->justification)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
|
1f9416 | 1997-09-03 | Per Hedbor | | case J_LEFT: xsize = 0; break;
case J_RIGHT: xsize = img->xsize-width_of[j]-1; break;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | case J_CENTER: xsize = img->xsize/2-width_of[j]/2-1; break;
}
if(xsize<0) xsize=0;
|
1f9416 | 1997-09-03 | Per Hedbor | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | switch(sp[j-args].u.string->size_shift)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
|
086cf0 | 1998-10-11 | Marcus Comstedt | | case 0:
to_write0 = STR0(sp[j-args].u.string);
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | for (i = 0; i < to_write_len; i++)
{
c=*(to_write0++);
if (c < (INT32)this->chars)
{
if(char_width(this,c))
write_char(this->charinfo+c,
(img->img+xsize)+
|
bea2ad | 2000-08-14 | Henrik Grubbström (Grubba) | | (img->xsize*DOUBLE_TO_INT(j*this->height*
this->yspacing_scale)),
|
086cf0 | 1998-10-11 | Marcus Comstedt | | img->xsize,
this->height);
xsize += char_space(this, c);
}
}
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | break;
case 1:
to_write1 = STR1(sp[j-args].u.string);
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | for (i = 0; i < to_write_len; i++)
{
c=*(to_write1++);
if (c < (INT32)this->chars)
{
if(char_width(this,c))
write_char(this->charinfo+c,
(img->img+xsize)+
|
bea2ad | 2000-08-14 | Henrik Grubbström (Grubba) | | (img->xsize*DOUBLE_TO_INT(j*this->height*
this->yspacing_scale)),
|
086cf0 | 1998-10-11 | Marcus Comstedt | | img->xsize,
this->height);
xsize += char_space(this, c);
}
}
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | break;
case 2:
to_write2 = STR2(sp[j-args].u.string);
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | for (i = 0; i < to_write_len; i++)
{
c=*(to_write2++);
if (c < (INT32)this->chars)
{
if(char_width(this,c))
write_char(this->charinfo+c,
(img->img+xsize)+
|
bea2ad | 2000-08-14 | Henrik Grubbström (Grubba) | | (img->xsize*DOUBLE_TO_INT(j*this->height*
this->yspacing_scale)),
|
086cf0 | 1998-10-11 | Marcus Comstedt | | img->xsize,
this->height);
xsize += char_space(this, c);
}
}
|
74e435 | 2003-10-07 | Martin Stjernholm | |
|
086cf0 | 1998-10-11 | Marcus Comstedt | | break;
|
62522d | 2005-11-13 | Martin Nilsson | | #ifdef PIKE_DEBUG
|
086cf0 | 1998-10-11 | Marcus Comstedt | | default:
|
5aad93 | 2002-08-15 | Marcus Comstedt | | Pike_fatal("Illegal shift size!\n");
|
62522d | 2005-11-13 | Martin Nilsson | | #endif
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
}
|
092c7c | 2005-11-03 | Henrik Grubbström (Grubba) | | CALL_AND_UNSET_ONERROR(err);
|
7abac3 | 1997-09-01 | Per Hedbor | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | pop_n_elems(args);
push_object(o);
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_height(INT32 args)
{
pop_n_elems(args);
if (THIS)
push_int(THIS->height);
else
push_int(0);
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_text_extents(INT32 args)
{
|
87b4cb | 1997-11-11 | Mirar (Pontus Hagland) | | INT32 xsize,i,maxwidth2,j;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
b2d3e4 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | if (!THIS) Pike_error("font->text_extents: no font loaded\n");
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
maxwidth2=0;
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | if (args==0)
{
|
7863d6 | 2005-05-06 | Martin Nilsson | | push_empty_string();
|
7d60af | 2004-05-19 | Martin Nilsson | | args=1;
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | }
for (j=0; j<args; j++)
|
d4f244 | 1997-09-06 | Per Hedbor | | {
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | int max;
|
086cf0 | 1998-10-11 | Marcus Comstedt | | p_wchar0 *to_write0;
p_wchar1 *to_write1;
p_wchar2 *to_write2;
|
0af185 | 2000-08-11 | Henrik Grubbström (Grubba) | | ptrdiff_t to_write_len;
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | if (sp[j-args].type!=T_STRING)
|
734fac | 2002-11-08 | Anders Johansson | | bad_arg_error("font->text_extents",sp-args,args,0,"",sp-args,
"Bad arguments to font->text_extents()\n");
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | |
xsize = max = 1;
to_write_len = sp[j-args].u.string->len;
|
086cf0 | 1998-10-11 | Marcus Comstedt | | switch(sp[j-args].u.string->size_shift)
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | {
|
086cf0 | 1998-10-11 | Marcus Comstedt | | case 0:
to_write0 = STR0(sp[j-args].u.string);
for (i = 0; i < to_write_len; i++)
{
if (xsize+char_width(THIS,to_write0[i]) > max)
max=xsize+char_width(THIS,to_write0[i]);
xsize += char_space(THIS,to_write0[i]);
if (xsize > max) max=xsize;
}
break;
case 1:
to_write1 = STR1(sp[j-args].u.string);
for (i = 0; i < to_write_len; i++)
{
if (xsize+char_width(THIS,to_write1[i]) > max)
max=xsize+char_width(THIS,to_write1[i]);
xsize += char_space(THIS,to_write1[i]);
if (xsize > max) max=xsize;
}
break;
case 2:
to_write2 = STR2(sp[j-args].u.string);
for (i = 0; i < to_write_len; i++)
{
if (xsize+char_width(THIS,to_write2[i]) > max)
max=xsize+char_width(THIS,to_write2[i]);
xsize += char_space(THIS,to_write2[i]);
if (xsize > max) max=xsize;
}
break;
|
2061ac | 2008-05-04 | Martin Nilsson | | #ifdef PIKE_DEBUG
|
086cf0 | 1998-10-11 | Marcus Comstedt | | default:
|
5aad93 | 2002-08-15 | Marcus Comstedt | | Pike_fatal("Illegal shift size!\n");
|
2061ac | 2008-05-04 | Martin Nilsson | | #endif
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | | }
if (max>maxwidth2) maxwidth2=max;
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | |
|
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | | pop_n_elems(args);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | push_int(maxwidth2);
|
3d94d1 | 2001-07-12 | Henrik Grubbström (Grubba) | | push_int64(DO_NOT_WARN((INT64)(args * THIS->height * THIS->yspacing_scale)));
|
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | | f_aggregate(2);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_set_xspacing_scale(INT32 args)
{
|
7d60af | 2004-05-19 | Martin Nilsson | | FLOAT_TYPE f;
if(!THIS) Pike_error("font->set_xspacing_scale(): No font loaded.\n");
get_all_args("set_xspaxing_scale", args, "%f", &f);
if(f < 0.0) f=0.1;
THIS->xspacing_scale = (double)f;
pop_n_elems(args);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
void font_set_yspacing_scale(INT32 args)
{
|
7d60af | 2004-05-19 | Martin Nilsson | | FLOAT_TYPE f;
if(!THIS) Pike_error("font->set_yspacing_scale(): No font loaded.\n");
get_all_args("set_yspacing_scale", args, "%f", &f);
if(f <= 0.0) f=0.1;
THIS->yspacing_scale = (double)f;
pop_n_elems(args);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | }
|
b9284c | 1997-04-09 | Mirar (Pontus Hagland) | |
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_baseline(INT32 args)
{
pop_n_elems(args);
if (THIS)
push_int(THIS->baseline);
else
push_int(0);
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_set_center(INT32 args)
{
pop_n_elems(args);
if(THIS) THIS->justification=J_CENTER;
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_set_right(INT32 args)
{
pop_n_elems(args);
if(THIS) THIS->justification=J_RIGHT;
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | void font_set_left(INT32 args)
{
pop_n_elems(args);
if(THIS) THIS->justification=J_LEFT;
}
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
8db2c3 | 1999-05-23 | Mirar (Pontus Hagland) | | void init_image_font(void)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
|
90e978 | 1999-01-31 | Fredrik Hübinette (Hubbe) | | ADD_STORAGE(struct font*);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("load",font_load,tFunc(tStr,tOr(tObj,tInt)),0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("create",font_create,tFunc(tOr(tVoid,tStr),tVoid),0);
|
885629 | 1998-05-04 | Mirar (Pontus Hagland) | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
07228a | 1999-06-19 | Fredrik Hübinette (Hubbe) | | ADD_FUNCTION("write",font_write,tFuncV(tNone,tStr,tObj),0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
07228a | 1999-06-19 | Fredrik Hübinette (Hubbe) | | ADD_FUNCTION("height",font_height,tFunc(tNone,tInt),0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
07228a | 1999-06-19 | Fredrik Hübinette (Hubbe) | | ADD_FUNCTION("baseline",font_baseline,tFunc(tNone,tInt),0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
07228a | 1999-06-19 | Fredrik Hübinette (Hubbe) | | ADD_FUNCTION("extents",font_text_extents,tFuncV(tNone,tStr,tArr(tInt)),0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
07228a | 1999-06-19 | Fredrik Hübinette (Hubbe) | | ADD_FUNCTION("text_extents",font_text_extents,tFuncV(tNone,tStr,tArr(tInt)),0);
|
1f9416 | 1997-09-03 | Per Hedbor | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("set_x_spacing",font_set_xspacing_scale,tFunc(tFlt,tVoid),0);
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("set_y_spacing",font_set_yspacing_scale,tFunc(tFlt,tVoid),0);
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("center", font_set_center,tFunc(tVoid,tVoid), 0);
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("left", font_set_left,tFunc(tVoid,tVoid), 0);
|
ab5e03 | 2001-11-18 | Martin Nilsson | |
|
45ee5d | 1999-02-10 | Fredrik Hübinette (Hubbe) | |
|
448c20 | 1999-04-13 | Mirar (Pontus Hagland) | | ADD_FUNCTION("right", font_set_right,tFunc(tVoid,tVoid), 0);
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | |
set_init_callback(init_font_struct);
set_exit_callback(exit_font_struct);
}
|
8db2c3 | 1999-05-23 | Mirar (Pontus Hagland) | | void exit_image_font(void)
|
ab6aec | 1997-02-11 | Fredrik Hübinette (Hubbe) | | {
}
|