/* Graphic state functions for contexts. Copyright (C) 2001 Johan Rydberg. All Rights Reserved. This file is part of Crust. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include "priv.h" static void _GFGStateDestroy (void *instp); CFTypeID _GFGStateTypeID; /* CoreFoundation runtime class for GFContext. */ CFRuntimeClass _GFGStateRuntimeClass = { 0, /* version */ "GFGState", /* Name of class. */ sizeof (struct GFGState), /* Instance size. */ 0, /* Init function. */ _GFGStateDestroy /* Final function. */ }; static void initialize_gstate_runtime (void) __attribute__ ((constructor)); /* Initialize runtime class for GFGState. This has a "constructor" attribute so that is should be called at initialization time. */ static void initialize_gstate_runtime (void) { _GFGStateTypeID = CFRuntimeRegisterClass (&_GFGStateRuntimeClass); } /* Graphic space lost its last reference - destroy it. */ static void _GFGStateDestroy (void *gstatep) { GFGStateRef gstate = gstatep; GFColorSpaceRelease (gstate->fill_space); GFColorSpaceRelease (gstate->stroke_space); } /* Create a new default graphic state. WIDTH and HEIGHT in SIZE is the size of the context - used for creating the default clipping path. */ GFGStateRef _GFGStateCreate (GFSize size) { GFGStateRef gstate; /* Allocate a new graphic state instance. */ gstate = (GFGStateRef) CFRuntimeCreateInstance (_GFGStateTypeID); if (! gstate) return 0; /* Set default values for the graphic state. ??? what about color? */ art_affine_identity (gstate->ctm); gstate->alpha_value = 1.0; gstate->line_width = 1.0; gstate->line_cap = 0; /* ??? default. */ gstate->line_join = 0; /* ??? default. */ gstate->miter_limit = 10.0; gstate->flatness = 0.5; /* Create two device gray color spaces. */ gstate->fill_space = GFColorSpaceCreateDeviceGray (); gstate->stroke_space = GFColorSpaceCreateDeviceGray (); return gstate; } /* Clone TEMPLATE graphic state. Return new graphic state, or NULL if we ran out of memory. ??? support for colorspaces. */ GFGStateRef _GFGStateClone (GFGStateRef template) { GFGStateRef gstate; /* Allocate a new graphic state instance. */ gstate = (GFGStateRef) CFRuntimeCreateInstance (_GFGStateTypeID); if (! gstate) return 0; memcpy (((void *) gstate) + sizeof (CFRuntimeBase), ((void *) template) + sizeof (CFRuntimeBase), sizeof *gstate - sizeof (CFRuntimeBase)); /* Some magic for color spaces. */ gstate->fill_space = _GFColorSpaceClone (template->fill_space); gstate->stroke_space = _GFColorSpaceClone (template->stroke_space); return gstate; } /* Restore saved graphic state on graphic state stack in CONTEXT. */ void GFContextRestoreGState (GFContextRef context) { GFGStateRef used_gstate; used_gstate = context->gstate; if (! used_gstate->next_on_stack) return; context->gstate = used_gstate->next_on_stack; _GFGStateDestroy (used_gstate); } /* Save graphic state on graphic state stack in CONTEXT. */ void GFContextSaveGState (GFContextRef context) { GFGStateRef cloned_gstate = _GFGStateClone (context->gstate); cloned_gstate->next_on_stack = context->gstate; context->gstate = cloned_gstate; }