freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Freetype] Freetype design problem?...


From: Pedriana, Paul
Subject: [Freetype] Freetype design problem?...
Date: Tue, 21 Aug 2001 14:31:59 -0700

I have a small problem with the design of the stream 
interface in FreeType, as exemplified in the code below.
The 'ft_new_input_stream' function (below) creates a 
new FTStream. The problem is that *it* is allocating 
the memory for FTStream, when in fact the FT_New_Memory_Stream
function or FT_New_Stream function should be doing the
allocation. The problem with the way it is now is that
you can't write a custom version of FT_New_Stream very
easily because it can't extend the definition of the 
FT_Stream record. It can't do this because somebody 
else allocated the stream record data for it. To be 
able to make anything but the most trivial implementation
of your own FT_New_Stream, you need to store more data
than that provided in the FT_Stream record. The pattern 
of letting the 'plug-innable' driver implement the 
allocation of the resources it uses is a common paradigm.

Now if there is an alternative solution to this problem,
I'd like to hear about it. The best I can do for now 
is do hacks to stuff properietary data into unused parts
of the FT_Stream record, like the 'pathname' field.

Thanks,

Paul Pedriana



-----------------------------------------------------------------
  static
  FT_Error  ft_new_input_stream( FT_Library     library,
                                 FT_Open_Args*  args,
                                 FT_Stream*     astream )
  {
    FT_Error   error;
    FT_Memory  memory;
    FT_Stream  stream;


    if ( !library )
      return FT_Err_Invalid_Library_Handle;

    if ( !args )
      return FT_Err_Invalid_Argument;

    *astream = 0;
    memory   = library->memory;
    if ( ALLOC( stream, sizeof ( *stream ) ) )
      goto Exit;

    stream->memory = memory;

    /* now, look at the stream flags */
    if ( args->flags & ft_open_memory )
    {
      error = 0;
      FT_New_Memory_Stream( library,
                            (FT_Byte*)args->memory_base,
                            args->memory_size,
                            stream );
    }
    else if ( args->flags & ft_open_pathname )
    {
      error = FT_New_Stream( args->pathname, stream );
      stream->pathname.pointer = args->pathname;
    }
    else if ( ( args->flags & ft_open_stream ) && args->stream )
    {
      /* in this case, we do not need to allocate a new stream object */
      /* since the caller is responsible for closing it himself       */
      FREE( stream );
      stream = args->stream;
    }
    else
      error = FT_Err_Invalid_Argument;

    if ( error )
      FREE( stream );

    *astream = stream;

  Exit:
    return error;
  }







reply via email to

[Prev in Thread] Current Thread [Next in Thread]