<commit_before>/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

/**********************************************************
*      This software is part of the graphviz package      *
*                http://www.graphviz.org/                 *
*                                                         *
*            Copyright (c) 1994-2004 AT&T Corp.           *
*                and is licensed under the                *
*            Common Public License, Version 1.0           *
*                      by AT&T Corp.                      *
*                                                         *
*        Information and Software Systems Research        *
*              AT&T Research, Florham Park NJ             *
**********************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_LIBZ
#include <zlib.h>
#endif

#ifdef WIN32
#include <io.h>
#include "compat.h"
#endif

#include "gvplugin_render.h"
#include "graph.h"
#include "agxbuf.h"
#include "utils.h"

/* for CHAR_LATIN1  */
#include "const.h"

/*
 *     J$: added `pdfmark' URL embedding.  PostScript rendered from
 *         dot files with URL attributes will get active PDF links
 *         from Adobe's Distiller.
 */
#define PDFMAX  14400           /*  Maximum size of PDF page  */

extern void epsf_define(FILE * of);
extern char *ps_string(char *ins, int latin);
extern void core_fputs(GVJ_t * job, char *s);
extern void core_printf(GVJ_t * job, const char *format, ...);

typedef enum { FORMAT_PS, FORMAT_PS2, } format_type;

static int isLatin1;
static char setupLatin1;

static char *last_fontname;
static double last_fontsize;
static gvcolor_t last_color;

static void psgen_begin_job(GVJ_t * job)
{
    last_fontname = NULL;
    last_fontsize = 0.0;
    last_color.u.HSVA[0] = 0.0;
    last_color.u.HSVA[1] = 0.0;
    last_color.u.HSVA[2] = 0.0;
    last_color.u.HSVA[3] = 1.0;  /* opaque */

    core_fputs(job, "%%!PS-Adobe-2.0\n");
    core_printf(job, "%%%%Creator: %s version %s (%s)\n",
	    job->common->info[0], job->common->info[1], job->common->info[2]);
    core_printf(job, "%%%%For: %s\n", job->common->user);
}

static void psgen_end_job(GVJ_t * job)
{
    core_fputs(job, "%%Trailer\n");
    core_printf(job, "%%%%Pages: %d\n", job->common->viewNum);
    if (job->common->show_boxes == NULL)
	core_printf(job, "%%%%BoundingBox: %d %d %d %d\n",
	    job->boundingBox.LL.x, job->boundingBox.LL.y,
	    job->boundingBox.UR.x, job->boundingBox.UR.y);
    core_fputs(job, "end\nrestore\n");
    core_fputs(job, "%%EOF\n");
}

static void psgen_begin_graph(GVJ_t * job)
{
    obj_state_t *obj = job->obj;

    setupLatin1 = FALSE;

    if (job->common->viewNum == 0) {
        core_printf(job, "%%%%Title: %s\n", obj->u.g->name);
        core_fputs(job, "%%%%Pages: (atend)\n");
        if (job->common->show_boxes == NULL)
            core_fputs(job, "%%%%BoundingBox: (atend)\n");
        core_fputs(job, "%%%%EndComments\nsave\n");
        cat_preamble(job, job->common->lib);
        epsf_define(job->output_file);
    }
    isLatin1 = (GD_charset(obj->u.g) == CHAR_LATIN1);
    if (isLatin1 && !setupLatin1) {
	core_fputs(job, "setupLatin1\n");	/* as defined in ps header */
	setupLatin1 = TRUE;
    }
    /*  Set base URL for relative links (for Distiller >= 3.0)  */
    if (obj->url)
	core_printf(job, "[ {Catalog} << /URI << /Base (%s) >> >>\n"
		"/PUT pdfmark\n", obj->url);
}

static void psgen_begin_layer(GVJ_t * job, char *layername, int layerNum, int numLayers)
{
    core_printf(job, "%d %d setlayer\n", layerNum, numLayers);
}

static void psgen_begin_page(GVJ_t * job)
{
    box pbr = job->pageBoundingBox;

// FIXME
    point page = {0,0};
    int N_pages = 0;

    core_printf(job, "%%%%Page: %d %d\n",
	    job->common->viewNum + 1, job->common->viewNum + 1);
    if (job->common->show_boxes == NULL)
        core_printf(job, "%%%%PageBoundingBox: %d %d %d %d\n",
	    pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
    core_printf(job, "%%%%PageOrientation: %s\n",
	    (job->rotation ? "Landscape" : "Portrait"));
    if (job->render.id == FORMAT_PS2)
        core_printf(job, "<< /PageSize [%d %d] >> setpagedevice\n",
            pbr.UR.x, pbr.UR.y);
    if (job->common->show_boxes == NULL)
        core_printf(job, "gsave\n%d %d %d %d boxprim clip newpath\n",
	    pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
    core_printf(job, "%d %d %d beginpage\n", page.x, page.y, N_pages);
    core_printf(job, "%g %g set_scale %d rotate %g %g translate\n",
	    job->scale.x, job->scale.y,
	    job->rotation,
	    job->translation.x, job->translation.y);

    /*  Define the size of the PS canvas  */
    if (job->render.id == FORMAT_PS2) {
	if (pbr.UR.x >= PDFMAX || pbr.UR.y >= PDFMAX)
	    job->common->errorfn("canvas size (%d,%d) exceeds PDF limit (%d)\n"
		  "\t(suggest setting a bounding box size, see dot(1))\n",
		  pbr.UR.x, pbr.UR.y, PDFMAX);
	core_printf(job, "[ /CropBox [%d %d %d %d] /PAGES pdfmark\n",
		pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
    }
}

static void psgen_end_page(GVJ_t * job)
{
//    if (job->common->show_boxes)
//	cat_libfile(job->output_file, NULL, job->common->show_boxes + 1);
    /* the showpage is really a no-op, but at least one PS processor
     * out there needs to see this literal token.  endpage does the real work.
     */
    core_fputs(job, "endpage\nshowpage\ngrestore\n");
    core_fputs(job, "%%%%PageTrailer\n");
    core_printf(job, "%%%%EndPage: %d\n", job->common->viewNum);
}

static void psgen_begin_cluster(GVJ_t * job)
{
    obj_state_t *obj = job->obj;

    core_printf(job, "%% %s\n", obj->u.sg->name);

    if (obj->url &&  obj->url_map_p) {
        core_printf(job, "[ /Rect [ %g %g %g %g ]\n",
		obj->url_map_p[0].x, obj->url_map_p[0].y,
		obj->url_map_p[1].x, obj->url_map_p[1].y);
        core_printf(job, "  /Border [ 0 0 0 ]\n"
		"  /Action << /Subtype /URI /URI %s >>\n"
		"  /Subtype /Link\n"
		"/ANN pdfmark\n",
		ps_string(obj->url, isLatin1));
    }
}

static void psgen_begin_node(GVJ_t * job)
{
    obj_state_t *obj = job->obj;

    if (obj->url && obj->url_map_p) {
        core_printf(job, "[ /Rect [ %g %g %g %g ]\n",
		obj->url_map_p[0].x, obj->url_map_p[0].y,
		obj->url_map_p[1].x, obj->url_map_p[1].y);
        core_printf(job, "  /Border [ 0 0 0 ]\n"<commit_msg>another case of extra "%%"<commit_after>/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

/**********************************************************
*      This software is part of the graphviz package      *
*                http://www.graphviz.org/                 *
*                                                         *
*            Copyright (c) 1994-2004 AT&T Corp.           *
*                and is licensed under the                *
*            Common Public License, Version 1.0           *
*                      by AT&T Corp.                      *
*                                                         *
*        Information and Software Systems Research        *
*              AT&T Research, Florham Park NJ             *
**********************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_LIBZ
#include <zlib.h>
#endif

#ifdef WIN32
#include <io.h>
#include "compat.h"
#endif

#include "gvplugin_render.h"
#include "graph.h"
#include "agxbuf.h"
#include "utils.h"

/* for CHAR_LATIN1  */
#include "const.h"

/*
 *     J$: added `pdfmark' URL embedding.  PostScript rendered from
 *         dot files with URL attributes will get active PDF links
 *         from Adobe's Distiller.
 */
#define PDFMAX  14400           /*  Maximum size of PDF page  */

extern void epsf_define(FILE * of);
extern char *ps_string(char *ins, int latin);
extern void core_fputs(GVJ_t * job, char *s);
extern void core_printf(GVJ_t * job, const char *format, ...);

typedef enum { FORMAT_PS, FORMAT_PS2, } format_type;

static int isLatin1;
static char setupLatin1;

static char *last_fontname;
static double last_fontsize;
static gvcolor_t last_color;

static void psgen_begin_job(GVJ_t * job)
{
    last_fontname = NULL;
    last_fontsize = 0.0;
    last_color.u.HSVA[0] = 0.0;
    last_color.u.HSVA[1] = 0.0;
    last_color.u.HSVA[2] = 0.0;
    last_color.u.HSVA[3] = 1.0;  /* opaque */

    core_fputs(job, "%%!PS-Adobe-2.0\n");
    core_printf(job, "%%%%Creator: %s version %s (%s)\n",
	    job->common->info[0], job->common->info[1], job->common->info[2]);
    core_printf(job, "%%%%For: %s\n", job->common->user);
}

static void psgen_end_job(GVJ_t * job)
{
    core_fputs(job, "%%Trailer\n");
    core_printf(job, "%%%%Pages: %d\n", job->common->viewNum);
    if (job->common->show_boxes == NULL)
	core_printf(job, "%%%%BoundingBox: %d %d %d %d\n",
	    job->boundingBox.LL.x, job->boundingBox.LL.y,
	    job->boundingBox.UR.x, job->boundingBox.UR.y);
    core_fputs(job, "end\nrestore\n");
    core_fputs(job, "%%EOF\n");
}

static void psgen_begin_graph(GVJ_t * job)
{
    obj_state_t *obj = job->obj;

    setupLatin1 = FALSE;

    if (job->common->viewNum == 0) {
        core_printf(job, "%%%%Title: %s\n", obj->u.g->name);
        core_fputs(job, "%%%%Pages: (atend)\n");
        if (job->common->show_boxes == NULL)
            core_fputs(job, "%%%%BoundingBox: (atend)\n");
        core_fputs(job, "%%%%EndComments\nsave\n");
        cat_preamble(job, job->common->lib);
        epsf_define(job->output_file);
    }
    isLatin1 = (GD_charset(obj->u.g) == CHAR_LATIN1);
    if (isLatin1 && !setupLatin1) {
	core_fputs(job, "setupLatin1\n");	/* as defined in ps header */
	setupLatin1 = TRUE;
    }
    /*  Set base URL for relative links (for Distiller >= 3.0)  */
    if (obj->url)
	core_printf(job, "[ {Catalog} << /URI << /Base (%s) >> >>\n"
		"/PUT pdfmark\n", obj->url);
}

static void psgen_begin_layer(GVJ_t * job, char *layername, int layerNum, int numLayers)
{
    core_printf(job, "%d %d setlayer\n", layerNum, numLayers);
}

static void psgen_begin_page(GVJ_t * job)
{
    box pbr = job->pageBoundingBox;

// FIXME
    point page = {0,0};
    int N_pages = 0;

    core_printf(job, "%%%%Page: %d %d\n",
	    job->common->viewNum + 1, job->common->viewNum + 1);
    if (job->common->show_boxes == NULL)
        core_printf(job, "%%%%PageBoundingBox: %d %d %d %d\n",
	    pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
    core_printf(job, "%%%%PageOrientation: %s\n",
	    (job->rotation ? "Landscape" : "Portrait"));
    if (job->render.id == FORMAT_PS2)
        core_printf(job, "<< /PageSize [%d %d] >> setpagedevice\n",
            pbr.UR.x, pbr.UR.y);
    if (job->common->show_boxes == NULL)
        core_printf(job, "gsave\n%d %d %d %d boxprim clip newpath\n",
	    pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
    core_printf(job, "%d %d %d beginpage\n", page.x, page.y, N_pages);
    core_printf(job, "%g %g set_scale %d rotate %g %g translate\n",
	    job->scale.x, job->scale.y,
	    job->rotation,
	    job->translation.x, job->translation.y);

    /*  Define the size of the PS canvas  */
    if (job->render.id == FORMAT_PS2) {
	if (pbr.UR.x >= PDFMAX || pbr.UR.y >= PDFMAX)
	    job->common->errorfn("canvas size (%d,%d) exceeds PDF limit (%d)\n"
		  "\t(suggest setting a bounding box size, see dot(1))\n",
		  pbr.UR.x, pbr.UR.y, PDFMAX);
	core_printf(job, "[ /CropBox [%d %d %d %d] /PAGES pdfmark\n",
		pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
    }
}

static void psgen_end_page(GVJ_t * job)
{
//    if (job->common->show_boxes)
//	cat_libfile(job->output_file, NULL, job->common->show_boxes + 1);
    /* the showpage is really a no-op, but at least one PS processor
     * out there needs to see this literal token.  endpage does the real work.
     */
    core_fputs(job, "endpage\nshowpage\ngrestore\n");
    core_fputs(job, "%%PageTrailer\n");
    core_printf(job, "%%%%EndPage: %d\n", job->common->viewNum);
}

static void psgen_begin_cluster(GVJ_t * job)
{
    obj_state_t *obj = job->obj;

    core_printf(job, "%% %s\n", obj->u.sg->name);

    if (obj->url &&  obj->url_map_p) {
        core_printf(job, "[ /Rect [ %g %g %g %g ]\n",
		obj->url_map_p[0].x, obj->url_map_p[0].y,
		obj->url_map_p[1].x, obj->url_map_p[1].y);
        core_printf(job, "  /Border [ 0 0 0 ]\n"
		"  /Action << /Subtype /URI /URI %s >>\n"
		"  /Subtype /Link\n"
		"/ANN pdfmark\n",
		ps_string(obj->url, isLatin1));
    }
}

static void psgen_begin_node(GVJ_t * job)
{
    obj_state_t *obj = job->obj;

    if (obj->url && obj->url_map_p) {
        core_printf(job, "[ /Rect [ %g %g %g %g ]\n",
		obj->url_map_p[0].x, obj->url_map_p[0].y,
		obj->url_map_p[1].x, obj->url_map_p[1].y);
        core_printf(job, "  /Border [ 0 0 0 ]\n"
{'fraction_non_alphanumeric': 0.19427226363279718, 'fraction_numerical': 0.011926245586504512, 'mean_word_length': 2.6448384329425223, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 179, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4529255', 'n_tokens_mistral': 5136, 'n_tokens_neox': 4894, 'n_words': 1073}