/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
/* Character cache routines for Ghostscript library */
#include "gzdevice.h" /* requires gsstate.h */
extern ulong
gs_next_ids(P1(uint
));
/* Define the size of the cache structures. */
/* We round the size of a cached_char so that */
/* an immediately following bitmap will be properly aligned. */
const uint cached_char_sizeof
=
sizeof(cached_char
) + (-sizeof(cached_char
) & 3);
#define cc_bits(cc) ((byte *)(cc) + cached_char_sizeof)
const uint cached_fm_pair_sizeof
= sizeof(cached_fm_pair
);
/* Define the hash chain for a (code, fm_pair) key. */
#define chars_head(dir, code, pair)\
&(dir)->chars[((uint)(code) + ((uint)(pair) << 4)) & (dir)->chars_mask]
private void shorten_cached_char(P3(gs_font_dir
*, cached_char
*, uint
));
private void purge_fm_pair(P2(gs_font_dir
*, cached_fm_pair
*));
/* Initialize the character cache. */
gx_char_cache_init(register gs_font_dir
*dir
)
{ cached_char_head
*cdata
= (cached_char_head
*)dir
->cdata
;
cdata
->size
= dir
->cdata_size
;
memset((char *)dir
->chars
, 0,
(dir
->chars_mask
+ 1) * sizeof(cached_char
*));
for ( i
= dir
->mmax
, pair
= dir
->mdata
; --i
>= 0; pair
++ )
/* Allocate storage for caching a rendered character, */
/* and set up the memory device. */
/* Return the cached_char if OK, 0 if too big. */
gx_alloc_char_bits(gs_font_dir
*dir
, gx_device_memory
*dev
,
ushort iwidth
, ushort iheight
)
#define hcc ((cached_char *)cch)
isize
= gdev_mem_bitmap_size(dev
); /* sets raster */
if ( dev
->raster
!= 0 && iheight
> dir
->upper
/ dev
->raster
)
cdsize
= isize
+ cached_char_sizeof
;
if ( cdsize
>= dir
->cmax
)
/* Look for and/or free enough space. */
cch
= (cached_char_head
*)(dir
->cdata
+ dir
->cnext
);
{ if ( (byte
*)cc
+ fsize
== dir
->cdata
+ dir
->cdata_size
)
cch
= (cached_char_head
*)dir
->cdata
, cc
= hcc
, fsize
= 0;
if ( !cc_head_is_free(cch
) )
{ /* Free the character */
cached_char
**pcc
= chars_head(dir
, hcc
->code
, cch
->pair
);
*pcc
= hcc
->next
; /* remove from chain */
gx_free_cached_char(dir
, hcc
);
if_debug2('K', "[K]merging free 0x%lx(%u)\n",
fsize
>= cdsize
+ sizeof(cached_char_head
)
break; /* enough room here */
cch
= (cached_char_head
*)((byte
*)cc
+ fsize
);
{ shorten_cached_char(dir
, cc
, fsize
- cdsize
);
if_debug2('K', "[K]shortening 0x%lx by %u (initial)\n",
(ulong
)cc
, (uint
)(fsize
- cdsize
));
if_debug4('k', "[k]adding 0x%lx:%u(%u,%u)\n",
(ulong
)cc
, (uint
)cdsize
, iwidth
, iheight
);
memset((char *)bits
, 0, (uint
)isize
);
cc
->raster
= dev
->raster
;
cc
->head
.pair
= 0; /* not linked in yet */
(*dev
->procs
->open_device
)((gx_device
*)dev
); /* initialize */
dir
->cnext
= (byte
*)cc
+ cdsize
- dir
->cdata
;
/* Remove a character from the cache. */
gx_free_cached_char(gs_font_dir
*dir
, cached_char
*cc
)
{ dir
->cnext
= (byte
*)cc
- dir
->cdata
;
dir
->bsize
-= cc
->head
.size
;
if ( cc
->head
.pair
!= 0 )
{ /* might be allocated but not added to table yet */
cc
->head
.pair
->num_chars
--;
if_debug2('k', "[k]freeing 0x%lx, pair=0x%lx\n",
(ulong
)cc
, (ulong
)cc
->head
.pair
);
/* Look up, and if necessary add, a font/matrix pair in the cache */
gx_lookup_fm_pair(register gs_state
*pgs
)
{ float mxx
= pgs
->char_tm
.xx
, mxy
= pgs
->char_tm
.xy
,
myx
= pgs
->char_tm
.yx
, myy
= pgs
->char_tm
.yy
;
gs_font
*font
= pgs
->font
;
register gs_font_dir
*dir
= font
->dir
;
register cached_fm_pair
*pair
= dir
->mdata
+ dir
->mnext
;
if ( font
->FontType
!= ft_composite
)
{ uid
= font
->data
.base
.UniqueID
;
if ( uid
!= -1 ) font
= 0;
{ if ( pair
== dir
->mdata
) pair
+= dir
->mmax
;
if ( pair
->font
== font
&& pair
->UniqueID
== uid
&&
pair
->mxx
== mxx
&& pair
->mxy
== mxy
&&
pair
->myx
== myx
&& pair
->myy
== myy
/* Add the pair to the cache */
mend
= dir
->mdata
+ dir
->mmax
;
if ( dir
->msize
== dir
->mmax
) /* cache is full */
{ /* Prefer an entry with num_chars == 0, if any. */
for ( count
= dir
->mmax
; --count
>= 0 && pair
->num_chars
!= 0; )
if ( ++pair
== mend
) pair
= dir
->mdata
;
purge_fm_pair(dir
, pair
);
{ /* Look for an empty entry. (We know there is one.) */
while ( !fm_pair_is_free(pair
) )
if ( ++pair
== mend
) pair
= dir
->mdata
;
dir
->mnext
= pair
+ 1 - dir
->mdata
;
if ( dir
->mnext
== dir
->mmax
) dir
->mnext
= 0;
pair
->mxx
= mxx
, pair
->mxy
= mxy
;
pair
->myx
= myx
, pair
->myy
= myy
;
/* Add a character to the cache */
gx_add_cached_char(gs_font_dir
*dir
, gx_device_memory
*dev
,
cached_char
*cc
, cached_fm_pair
*pair
)
{ cc
->id
= gs_next_ids(1);
/* Make sure the bits are in the right order */
/* to use as a source. */
gdev_mem_ensure_byte_order(dev
);
/* Add the new character at the tail of its chain. */
{ register cached_char
**head
=
chars_head(dir
, cc
->code
, pair
);
while ( *head
!= 0 ) head
= &(*head
)->next
;
/* Discard the memory device overhead that follows the bits. */
{ uint diff
= gdev_mem_bitmap_size(dev
) - cc
->raster
* cc
->height
;
if ( diff
>= sizeof(cached_char_head
) )
{ shorten_cached_char(dir
, cc
, diff
);
if_debug2('K', "[K]shortening 0x%lx by %u (mdev overhead)\n",
/* Look up a character in the cache. */
/* Return the cached_char or 0. */
gx_lookup_cached_char(gs_state
*pgs
, cached_fm_pair
*pair
, char_code ccode
)
{ gs_font_dir
*dir
= pgs
->font
->dir
;
register cached_char
*cc
= *chars_head(dir
, ccode
, pair
);
{ if ( cc
->code
== ccode
&& cc
->head
.pair
== pair
)
/* Copy a cached character to the screen. */
/* Assume the caller has already done gx_color_load, */
/* and the color is not a halftone. */
/* Return 0 if OK, 1 if we couldn't do the operation but no error */
/* occurred, or a negative error code. */
gx_image_cached_char(register gs_show_enum
*penum
, register cached_char
*cc
)
{ register gs_state
*pgs
= penum
->pgs
;
gx_device
*dev
= pgs
->device
->info
;
code
= gx_path_current_point_inline(pgs
->path
, &pt
);
if ( code
< 0 ) return code
;
/* Abort if the device color isn't pure. */
if ( !penum
->color_loaded
)
{ if ( !color_is_pure(pgs
->dev_color
) )
return 1; /* can't use cache */
/* If the character doesn't lie entirely within the */
/* quick-check clipping rectangle, we have to */
/* set up an intermediate clipping device. */
x
= fixed2int_var_rounded(pt
.x
) + penum
->ftx
;
y
= fixed2int_var_rounded(pt
.y
) + penum
->fty
;
dprintf3("[K]copying 0x%lx, offset=(%g,%g)\n", (ulong
)cc
,
fixed2float(-cc
->offset
.x
), fixed2float(-cc
->offset
.y
)),
dprintf6(" at (%g,%g)+(%d,%d)->(%d,%d)\n", fixed2float(pt
.x
),
fixed2float(pt
.y
), penum
->ftx
, penum
->fty
, x
, y
);
if ( x
< penum
->cxmin
|| x
+ w
> penum
->cxmax
||
y
< penum
->cymin
|| y
+ h
> penum
->cymax
cdev
.list
= pgs
->clip_path
->list
;
dev
= (gx_device
*)&cdev
;
(*dev
->procs
->open_device
)(dev
);
if_debug0('K', "[K](clipping)\n");
code
= (*dev
->procs
->copy_mono
)
(dev
, cc_bits(cc
), 0, cc
->raster
, cc
->id
,
gx_no_color_index
, pgs
->dev_color
->color1
);
return ( code
< 0 ? code
: 0 );
/* Purge from the caches all references to a given font. */
gs_purge_font_from_char_caches(gs_font_dir
*dir
, gs_font
*font
)
{ cached_fm_pair
*pair
= dir
->mdata
;
if_debug1('k', "[k]purging font 0x%lx\n",
{ if ( pair
->font
== font
) purge_fm_pair(dir
, pair
);
/* ------ Internal routines ------ */
/* Shorten a cached character. */
/* diff >= sizeof(cached_char_head). */
shorten_cached_char(gs_font_dir
*dir
, cached_char
*cc
, uint diff
)
{ cached_char_head
*next
;
if ( (byte
*)cc
+ cc
->head
.size
== dir
->cdata
+ dir
->cnext
)
next
= (cached_char_head
*)((byte
*)cc
+ cc
->head
.size
);
if_debug2('K', "[K]shortening creates free block 0x%lx(%u)\n",
/* Purge from the caches all references to a given font/matrix pair. */
purge_fm_pair(gs_font_dir
*dir
, cached_fm_pair
*pair
)
if_debug1('k', "[k]purging pair 0x%lx\n",
for ( chi
= dir
->chars_mask
; pair
->num_chars
!= 0; )
{ cached_char
**pcc
= dir
->chars
+ chi
--;
{ cached_char
*cc
= *pcc
;
if ( cc
->head
.pair
== pair
)
{ gx_free_cached_char(dir
, cc
);