<commit_before>/******************************************************************************
  font.c

  Change Control:                                                      DDMMYYYY
    <NAME>    File created                                      22062000

  Purpose:
    Routines related to fonts.

  Copyright (C) <NAME> 2000 - 2002
  
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
******************************************************************************/

#if defined _WINDOWS
#include "stdafx.h"
#else
#include <panda/constants.h>
#include <panda/functions.h>
#endif

/******************************************************************************
DOCBOOK START

FUNCTION panda_createfont
PURPOSE return a handle to the requested font

SYNOPSIS START
#include&lt;panda/constants.h&gt;
#include&lt;panda/functions.h&gt;
char * panda_createfont (panda_pdf * output, char *fontname, int type, char *encoding)
SYNOPSIS END

DESCRIPTION <command>PANDA INTERNAL</command>. This funtion call creates a font object for the requested font and returns the identifier that should be used when the font is set within the PDF document.

RETURNS A font identifier (handle) as a null terminated string.

EXAMPLE START
#include&lt;panda/constants.h&gt;
#include&lt;panda/functions.h&gt;

panda_pdf *output;
char *fonthandle;

panda_init();
output = panda_open("output.pdf", "w");
fonthandle = (output, "Helventica", 3, "MacRomanEncoding");
EXAMPLE END
SEEALSO panda_setfont
DOCBOOK END
******************************************************************************/

char *
panda_createfont (panda_pdf * output, char *fontname, int type,
		  char *encoding)
{
  // Create a font object in the PDF
  panda_object *font;
  char *tempBuffer, *fontident;

  // Make the new object
  font = (panda_object *) panda_newobject (output, panda_normal);

  // Add it to the tree of font objects
  panda_addchild (output->fonts, font);

  // Setup some values within the font object
  panda_adddictitem (output, font, "Type", panda_textvalue, "Font");

  tempBuffer = panda_xsnprintf ("Type%d", type);
  panda_adddictitem (output, font, "Subtype", panda_textvalue, tempBuffer);
  panda_xfree (tempBuffer);

  // Make a font identifier string for this font
  fontident = panda_xsnprintf ("F%08d", output->nextFontNumber);
  output->nextFontNumber++;

  panda_adddictitem (output, font, "Name", panda_textvalue, fontident);

  panda_adddictitem (output, font, "BaseFont", panda_textvalue, fontname);
  panda_adddictitem (output, font, "Encoding", panda_textvalue, encoding);
<commit_msg>Check to see if we really need a new font id<commit_after>/******************************************************************************
  font.c

  Change Control:                                                      DDMMYYYY
    <NAME>    File created                                      22062000

  Purpose:
    Routines related to fonts.

  Copyright (C) <NAME> 2000 - 2002
  
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
******************************************************************************/

#if defined _WINDOWS
#include "stdafx.h"
#else
#include <panda/constants.h>
#include <panda/functions.h>
#endif

/******************************************************************************
DOCBOOK START

FUNCTION panda_createfont
PURPOSE return a handle to the requested font

SYNOPSIS START
#include&lt;panda/constants.h&gt;
#include&lt;panda/functions.h&gt;
char * panda_createfont (panda_pdf * output, char *fontname, int type, char *encoding)
SYNOPSIS END

DESCRIPTION <command>PANDA INTERNAL</command>. This funtion call creates a font object for the requested font and returns the identifier that should be used when the font is set within the PDF document.

RETURNS A font identifier (handle) as a null terminated string.

EXAMPLE START
#include&lt;panda/constants.h&gt;
#include&lt;panda/functions.h&gt;

panda_pdf *output;
char *fonthandle;

panda_init();
output = panda_open("output.pdf", "w");
fonthandle = (output, "Helventica", 3, "MacRomanEncoding");
EXAMPLE END
SEEALSO panda_setfont
DOCBOOK END
******************************************************************************/

char *
panda_createfont (panda_pdf * output, char *fontname, int type,
		  char *encoding)
{
  // Create a font object in the PDF
  panda_child *fonts;
  panda_object *font;
  char *tempBuffer, *fontident, *dictBf, *dictEnc, *dictType, *dictName, *valBf, *valEnc, *valType;
  int found = 0;

  tempBuffer = panda_xsnprintf ("Type%d", type);

  // See if this font has been used before. If so, re-use the object.
  fonts = output->fonts->children;
  while ( (fonts->next != NULL) && (found == 0) ) {
    // Ugly, but efficient and cleans up after itself.
    if ( (dictBf = panda_finddictitem(output, fonts->me, "BaseFont")) != NULL ) {
      valBf = panda_dbread(output, dictBf);
      if ( strcmp(valBf+1, fontname) == 0 ) {
	if ( (dictEnc = panda_finddictitem(output, fonts->me, "Encoding")) != NULL ) {
	  valEnc = panda_dbread(output, dictEnc);
	  if ( strcmp(valEnc+1, encoding) == 0 ) {
	    if ( (dictType = panda_finddictitem(output, fonts->me, "Subtype")) != NULL ) {
	      valType = panda_dbread(output, dictType);
	      if ( strcmp(valType+1, tempBuffer) == 0 ) {
		if ( (dictName = panda_finddictitem(output, fonts->me, "Name")) != NULL ) {
		  fontident = panda_xsnprintf("%s", panda_dbread(output, dictName)+1);
		  found = 1;
		}
		panda_xfree(dictName);
	      }
	      panda_xfree(valType);
	    }
	    panda_xfree(dictType);
	  }
	  panda_xfree(valEnc);
	}
	panda_xfree(dictEnc);
      }
      panda_xfree(valBf);
    }
    panda_xfree(dictBf);
    fonts = fonts->next;
  }

  if ( found != 1 ) {
    // Make the new object
    font = (panda_object *) panda_newobject (output, panda_normal);

    // Add it to the tree of font objects
    panda_addchild (output->fonts, font);

    // Setup some values within the font object
    panda_adddictitem (output, font, "Type", panda_textvalue, "Font");
    panda_adddictitem (output, font, "Subtype", panda_textvalue, tempBuffer);

    // Make a font identifier string for this font
    fontident = panda_xsnprintf ("F%08d", output->nextFontNumber);
    output->nextFontNumber++;

    panda_adddictitem (output, font, "Name", panda_textvalue, fontident);

    panda_adddictitem (output, font, "BaseFont", panda_textvalue, fontname);
    panda_adddictitem (output, font, "Encoding", panda_textvalue, encoding);
  }

  panda_xfree (tempBuffer);

{'fraction_non_alphanumeric': 0.16988914668729055, 'fraction_numerical': 0.008636246455271977, 'mean_word_length': 4.041585445094217, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2777029', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2272, 'n_words': 775}