Files
vroni/ipe_io.cc
T
SaraP 739088af9f Vroni 7.8 :
- aggiornamento versione.
2025-01-29 16:24:30 +01:00

365 lines
17 KiB
C++

/*****************************************************************************/
/* */
/* Copyright (C) 1996-2025 M. Held */
/* */
/* This code is not in the public domain. All rights reserved! Please make */
/* sure to read the full copyright statement contained in "README.pdf" or in */
/* the "main" file of this code, such as "main.cc". */
/* */
/*****************************************************************************/
/* */
/* Purpose: This is an interface to Ipe. */
/* */
/* E-Mail: held@cs.sbg.ac.at */
/* Fax Mail: (+43 662) 8044-172 */
/* Voice Mail: (+43 662) 6044-6304 */
/* Snail Mail: Martin Held */
/* Universitaet Salzburg */
/* FB Informatik */
/* A-5020 Salzburg, Austria */
/* */
/*****************************************************************************/
/* */
/* get standard libraries */
/* */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <exception>
/* */
/* get my header files */
/* */
#include "fpkernel.h"
#include "ipe_io.h"
#include "util.h"
/* */
/* some constants which are global within this file */
/* */
static double scale;
static double ipe_x_min, ipe_x_max, ipe_y_min, ipe_y_max;
static double x_min, x_max, y_min, y_max;
static float oldX, oldY;
static vr_bool newgroup = false;
static vr_bool openpath = false;
static vr_bool newpath = false;
static char color[1024];
static char width[1024];
FILE *InitIpeFile(char *filename)
{
FILE *ipe_file;
if ((ipe_file = fopen(filename, "w")) != NULL) {
fprintf(ipe_file, "<?xml version=\"1.0\"?>\n<!DOCTYPE ipe SYSTEM \"ipe.dtd\">\n<ipe version=\"70218\" creator=\"VRONI\">\n<info bbox=\"cropbox\"/>\n<ipestyle name=\"VRONIstyle\">\n<symbol name=\"arrow/arc(spx)\">\n<path stroke=\"sym-stroke\" fill=\"sym-stroke\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/farc(spx)\">\n<path stroke=\"sym-stroke\" fill=\"white\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/ptarc(spx)\">\n<path stroke=\"sym-stroke\" fill=\"sym-stroke\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-0.8 0 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/fptarc(spx)\">\n<path stroke=\"sym-stroke\" fill=\"white\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-0.8 0 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"mark/circle(sx)\" transformations=\"translations\">\n<path fill=\"sym-stroke\">\n0.6 0 0 0.6 0 0 e\n0.4 0 0 0.4 0 0 e\n</path>\n</symbol>\n<symbol name=\"mark/disk(sx)\" transformations=\"translations\">\n<path fill=\"sym-stroke\">\n0.6 0 0 0.6 0 0 e\n</path>\n</symbol>\n<symbol name=\"mark/fdisk(sfx)\" transformations=\"translations\">\n<group>\n<path fill=\"sym-fill\">\n0.5 0 0 0.5 0 0 e\n</path>\n<path fill=\"sym-stroke\" fillrule=\"eofill\">\n0.6 0 0 0.6 0 0 e\n0.4 0 0 0.4 0 0 e\n</path>\n</group>\n</symbol>\n<symbol name=\"mark/box(sx)\" transformations=\"translations\">\n<path fill=\"sym-stroke\" fillrule=\"eofill\">\n-0.6 -0.6 m\n0.6 -0.6 l\n0.6 0.6 l\n-0.6 0.6 l\nh\n-0.4 -0.4 m\n0.4 -0.4 l\n0.4 0.4 l\n-0.4 0.4 l\nh\n</path>\n</symbol>\n<symbol name=\"mark/square(sx)\" transformations=\"translations\">\n<path fill=\"sym-stroke\">\n-0.6 -0.6 m\n0.6 -0.6 l\n0.6 0.6 l\n-0.6 0.6 l\nh\n</path>\n</symbol>\n<symbol name=\"mark/fsquare(sfx)\" transformations=\"translations\">\n<group>\n<path fill=\"sym-fill\">\n-0.5 -0.5 m\n0.5 -0.5 l\n0.5 0.5 l\n-0.5 0.5 l\nh\n</path>\n<path fill=\"sym-stroke\" fillrule=\"eofill\">\n-0.6 -0.6 m\n0.6 -0.6 l\n0.6 0.6 l\n-0.6 0.6 l\nh\n-0.4 -0.4 m\n0.4 -0.4 l\n0.4 0.4 l\n-0.4 0.4 l\nh\n</path>\n</group>\n</symbol>\n<symbol name=\"mark/cross(sx)\" transformations=\"translations\">\n<group>\n<path fill=\"sym-stroke\">\n-0.43 -0.57 m\n0.57 0.43 l\n0.43 0.57 l\n-0.57 -0.43 l\nh\n</path>\n<path fill=\"sym-stroke\">\n-0.43 0.57 m\n0.57 -0.43 l\n0.43 -0.57 l\n-0.57 0.43 l\nh\n</path>\n</group>\n</symbol>\n<symbol name=\"arrow/fnormal(spx)\">\n<path stroke=\"sym-stroke\" fill=\"white\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/pointed(spx)\">\n<path stroke=\"sym-stroke\" fill=\"sym-stroke\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-0.8 0 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/fpointed(spx)\">\n<path stroke=\"sym-stroke\" fill=\"white\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-0.8 0 l\n-1 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/linear(spx)\">\n<path stroke=\"sym-stroke\" pen=\"sym-pen\">\n-1 0.333 m\n0 0 l\n-1 -0.333 l\n</path>\n</symbol>\n<symbol name=\"arrow/fdouble(spx)\">\n<path stroke=\"sym-stroke\" fill=\"white\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-1 -0.333 l\nh\n-1 0 m\n-2 0.333 l\n-2 -0.333 l\nh\n</path>\n</symbol>\n<symbol name=\"arrow/double(spx)\">\n<path stroke=\"sym-stroke\" fill=\"sym-stroke\" pen=\"sym-pen\">\n0 0 m\n-1 0.333 l\n-1 -0.333 l\nh\n-1 0 m\n-2 0.333 l\n-2 -0.333 l\nh\n</path>\n</symbol>\n<pen name=\"heavier\" value=\"0.8\"/>\n<pen name=\"fat\" value=\"1.2\"/>\n<pen name=\"ultrafat\" value=\"2\"/>\n<symbolsize name=\"large\" value=\"5\"/>\n<symbolsize name=\"small\" value=\"2\"/>\n<symbolsize name=\"tiny\" value=\"1.1\"/>\n<arrowsize name=\"large\" value=\"10\"/>\n<arrowsize name=\"small\" value=\"5\"/>\n<arrowsize name=\"tiny\" value=\"3\"/>\n<color name=\"red\" value=\"1 0 0\"/>\n<color name=\"green\" value=\"0 1 0\"/>\n<color name=\"blue\" value=\"0 0 1\"/>\n<color name=\"yellow\" value=\"1 1 0\"/>\n<color name=\"orange\" value=\"1 0.647 0\"/>\n<color name=\"gold\" value=\"1 0.843 0\"/>\n<color name=\"purple\" value=\"0.627 0.125 0.941\"/>\n<color name=\"gray\" value=\"0.745\"/>\n<color name=\"brown\" value=\"0.647 0.165 0.165\"/>\n<color name=\"navy\" value=\"0 0 0.502\"/>\n<color name=\"pink\" value=\"1 0.753 0.796\"/>\n<color name=\"seagreen\" value=\"0.18 0.545 0.341\"/>\n<color name=\"turquoise\" value=\"0.251 0.878 0.816\"/>\n<color name=\"violet\" value=\"0.933 0.51 0.933\"/>\n<color name=\"darkblue\" value=\"0 0 0.545\"/>\n<color name=\"darkcyan\" value=\"0 0.545 0.545\"/>\n<color name=\"darkgray\" value=\"0.663\"/>\n<color name=\"darkgreen\" value=\"0 0.392 0\"/>\n<color name=\"darkmagenta\" value=\"0.545 0 0.545\"/>\n<color name=\"darkorange\" value=\"1 0.549 0\"/>\n<color name=\"darkred\" value=\"0.545 0 0\"/>\n<color name=\"lightblue\" value=\"0.678 0.847 0.902\"/>\n<color name=\"lightcyan\" value=\"0.878 1 1\"/>\n<color name=\"lightgray\" value=\"0.827\"/>\n<color name=\"lightgreen\" value=\"0.565 0.933 0.565\"/>\n<color name=\"lightyellow\" value=\"1 1 0.878\"/>\n<dashstyle name=\"dashed\" value=\"[4] 0\"/>\n<dashstyle name=\"dotted\" value=\"[1 3] 0\"/>\n<dashstyle name=\"dash dotted\" value=\"[4 2 1 2] 0\"/>\n<dashstyle name=\"dash dot dotted\" value=\"[4 2 1 2 1 2] 0\"/>\n<textsize name=\"large\" value=\"\\large\"/>\n<textsize name=\"Large\" value=\"\\Large\"/>\n<textsize name=\"LARGE\" value=\"\\LARGE\"/>\n<textsize name=\"huge\" value=\"\\huge\"/>\n<textsize name=\"Huge\" value=\"\\Huge\"/>\n<textsize name=\"small\" value=\"\\small\"/>\n<textsize name=\"footnote\" value=\"\\footnotesize\"/>\n<textsize name=\"tiny\" value=\"\\tiny\"/>\n<textstyle name=\"center\" begin=\"\\begin{center}\" end=\"\\end{center}\"/>\n<textstyle name=\"itemize\" begin=\"\\begin{itemize}\" end=\"\\end{itemize}\"/>\n<textstyle name=\"item\" begin=\"\\begin{itemize}\\item{}\" end=\"\\end{itemize}\"/>\n<gridsize name=\"4 pts\" value=\"4\"/>\n<gridsize name=\"8 pts (~3 mm)\" value=\"8\"/>\n<gridsize name=\"16 pts (~6 mm)\" value=\"16\"/>\n<gridsize name=\"32 pts (~12 mm)\" value=\"32\"/>\n<gridsize name=\"10 pts (~3.5 mm)\" value=\"10\"/>\n<gridsize name=\"20 pts (~7 mm)\" value=\"20\"/>\n<gridsize name=\"14 pts (~5 mm)\" value=\"14\"/>\n<gridsize name=\"28 pts (~10 mm)\" value=\"28\"/>\n<gridsize name=\"56 pts (~20 mm)\" value=\"56\"/>\n<anglesize name=\"90 deg\" value=\"90\"/>\n<anglesize name=\"60 deg\" value=\"60\"/>\n<anglesize name=\"45 deg\" value=\"45\"/>\n<anglesize name=\"30 deg\" value=\"30\"/>\n<anglesize name=\"22.5 deg\" value=\"22.5\"/>\n<opacity name=\"10%%\" value=\"0.1\"/>\n<opacity name=\"30%%\" value=\"0.3\"/>\n<opacity name=\"50%%\" value=\"0.5\"/>\n<opacity name=\"75%%\" value=\"0.75\"/>\n<tiling name=\"falling\" angle=\"-60\" step=\"4\" width=\"1\"/>\n<tiling name=\"rising\" angle=\"30\" step=\"4\" width=\"1\"/>\n</ipestyle>\n\n");
fprintf(ipe_file,
"\n");
fprintf(ipe_file, "<page>\n");
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER0);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER1);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER2);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER3);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER4);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER5);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER6);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER7);
fprintf(ipe_file, "<layer name=\"%s\"/>\n", LAYER8);
fprintf(ipe_file, "<view layers=\"%s %s %s %s %s %s %s %s %s\" active=\"%s\"/>\n",
LAYER0, LAYER1, LAYER2, LAYER3, LAYER4, LAYER5, LAYER6, LAYER7, LAYER8, LAYER0);
}
else {
throw std::runtime_error("VRONI error: InitIpeFile() - Ipe output file not opened!");
}
return(ipe_file);
}
FILE *InitIpe(char *filename, double_arg xl, double_arg xr, double_arg yl, double_arg yr)
{
FILE *ipe_file = InitIpeFile(filename);
SetIpeDimensions(XMIN, XMAX, YMIN, YMAX);
SetWorldDimensions(xl, xr, yl, yr);
SetScaleFactor();
return(ipe_file);
}
void CloseIpeFile(FILE *ipe_file)
{
fprintf(ipe_file, "</page>\n");
fprintf(ipe_file, "</ipe>\n");
fclose(ipe_file);
return;
}
void WriteBeginGroup(FILE *ipe_file, short int layer)
{
char layername[1024];
switch(layer) {
case 0:
strncpy(layername, LAYER0, 1023);
strncpy(color, COLOR0, 1023);
strncpy(width, PEN0, 1023);
break;
case 1:
strncpy(layername, LAYER1, 1023);
strncpy(color, COLOR1, 1023);
strncpy(width, PEN1, 1023);
break;
case 2:
strncpy(layername, LAYER2, 1023);
strncpy(color, COLOR2, 1023);
strncpy(width, PEN2, 1023);
break;
case 3:
strncpy(layername, LAYER3, 1023);
strncpy(color, COLOR3, 1023);
strncpy(width, PEN3, 1023);
break;
case 4:
strncpy(layername, LAYER4, 1023);
strncpy(color, COLOR4, 1023);
strncpy(width, PEN4, 1023);
break;
case 6:
strncpy(layername, LAYER6, 1023);
strncpy(color, COLOR6, 1023);
strncpy(width, PEN6, 1023);
break;
case 7:
strncpy(layername, LAYER7, 1023);
strncpy(color, COLOR7, 1023);
strncpy(width, PEN7, 1023);
break;
case 8:
strncpy(layername, LAYER8, 1023);
strncpy(color, COLOR8, 1023);
strncpy(width, PEN8, 1023);
break;
default:
strncpy(layername, LAYER5, 1023);
strncpy(color, COLOR5, 1023);
strncpy(width, PEN5, 1023);
break;
}
FP_fprintf(ipe_file, "<group layer=\"%s\">\n", layername);
newgroup = true;
openpath = false;
return;
}
void WriteBeginPath(FILE *ipe_file, double x1, double y1)
{
if (newgroup) {
newpath = true;
newgroup = false;
}
else {
newpath = !((oldX==REAL_TO_FLOAT(x1)) && (oldY==REAL_TO_FLOAT(y1)));
}
if (newpath) {
if (openpath) fprintf(ipe_file,"</path>\n");
FP_fprintf(ipe_file, "<path stroke=\"%s\" pen=\"%s\">\n", color, width);
FP_fprintf(ipe_file,"%f %f m\n",
FP_PRNTARG(scaleX(x1)), FP_PRNTARG(scaleY(y1)));
}
return;
}
void WriteEndGroup(FILE *ipe_file)
{
if (openpath) fprintf(ipe_file, "</path>\n");
fprintf(ipe_file, "</group>\n");
openpath = false;
return;
}
void SetIpeDimensions(double_arg xmin, double_arg xmax, double_arg ymin, double_arg ymax)
{
ipe_x_min = xmin;
ipe_x_max = xmax;
ipe_y_min = ymin;
ipe_y_max = ymax;
return;
}
void SetWorldDimensions(double_arg xmin, double_arg xmax, double_arg ymin, double_arg ymax)
{
x_min = xmin;
x_max = xmax;
y_min = ymin;
y_max = ymax;
return;
}
void SetScaleFactor(void)
{
scale = VroniMin((ipe_x_max - ipe_x_min) / (x_max - x_min),
(ipe_y_max - ipe_y_min) / (y_max - y_min));
return;
}
void InitIpeDimensions(double_arg xmin, double_arg ymin, double_arg xmax, double_arg ymax,
double_arg ixmin, double_arg iymin, double_arg ixmax, double_arg iymax)
{
ipe_x_min = ixmin;
ipe_x_max = ixmax;
ipe_y_min = iymin;
ipe_y_max = iymax;
x_min = xmin;
x_max = xmax;
y_min = ymin;
y_max = ymax;
scale = VroniMin((ipe_x_max - ipe_x_min) / (x_max - x_min),
(ipe_y_max - ipe_y_min) / (y_max - y_min));
return;
}
double scaleX(double x)
{
return ipe_x_min + (x - x_min) * scale;
}
double scaleY(double y)
{
return ipe_y_min + (y - y_min) * scale;
}
void WriteMark(FILE *ipe_file, int type, int size, double_arg x, double_arg y)
{
/* */
/* type = 1 ... circle */
/* type = 2 ... disk */
/* type = 3 ... box */
/* type = 4 ... square */
/* type = 5 ... cross */
/* type = 6 ... fdisk */
/* type = 7 ... fsquare */
/* */
switch(type) {
case 2:
fprintf(ipe_file, "<use name=\"mark/disk(sx)\" ");
break;
case 3:
fprintf(ipe_file, "<use name=\"mark/box(sx)\" ");
break;
case 4:
fprintf(ipe_file, "<use name=\"mark/square(sx)\" ");
break;
case 5:
fprintf(ipe_file, "<use name=\"mark/cross(sx)\" ");
break;
case 6:
fprintf(ipe_file, "<use name=\"mark/fdisk(sfx)\" ");
break;
case 7:
fprintf(ipe_file, "<use name=\"mark/fsquare(sfx)\" ");
break;
default:
fprintf(ipe_file, "<use name=\"mark/circle(sx)\" ");
break;
}
FP_fprintf(ipe_file,
"pos=\"%f %f\" stroke=\"%s\" size=\"%i\"/>\n",
FP_PRNTARG(scaleX(x)), FP_PRNTARG(scaleY(y)), color, size);
return;
}
void WriteLineSegment(FILE *ipe_file,
double x1, double y1, double x2, double y2)
{
WriteBeginPath(ipe_file, x1, y1);
FP_fprintf(ipe_file, "%f %f l\n", FP_PRNTARG(scaleX(x2)), FP_PRNTARG(scaleY(y2)));
openpath = true;
oldX = REAL_TO_FLOAT(x2);
oldY = REAL_TO_FLOAT(y2);
return;
}
void WriteCircularArc(FILE *ipe_file, double_arg xc, double_arg yc,
double_arg x1, double_arg y1, double_arg x2, double_arg y2, vr_bool ccw)
{
double rad, xvec, yvec;
xvec = xc - x1;
yvec = yc - y1;
rad = sqrt(xvec * xvec + yvec * yvec) * scale;
WriteBeginPath(ipe_file, x1, y1);
if (ccw) {
FP_fprintf(ipe_file, "%f 0 0 %f %f %f %f %f a\n",
FP_PRNTARG(rad), FP_PRNTARG(rad), FP_PRNTARG(scaleX(xc)),
FP_PRNTARG(scaleY(yc)), FP_PRNTARG(scaleX(x2)), FP_PRNTARG(scaleY(y2)));
}
else {
FP_fprintf(ipe_file, "%f 0 0 %f %f %f %f %f a\n",
FP_PRNTARG(rad), FP_PRNTARG(-rad), FP_PRNTARG(scaleX(xc)),
FP_PRNTARG(scaleY(yc)), FP_PRNTARG(scaleX(x2)), FP_PRNTARG(scaleY(y2)));
}
openpath = true;
oldX = REAL_TO_FLOAT(x2);
oldY = REAL_TO_FLOAT(y2);
return;
}
void WriteCircle(FILE *ipe_file, double_arg xc, double_arg yc, double_arg rad)
{
if (newgroup) newgroup = false;
if (openpath) fprintf(ipe_file,"</path>\n");
FP_fprintf(ipe_file, "<path stroke=\"%s\" pen=\"%s\">\n", color, width);
FP_fprintf(ipe_file,"%f 0 0 %f %f %f e\n",
FP_PRNTARG(rad) * scale, FP_PRNTARG(rad) * scale,
FP_PRNTARG(scaleX(xc)), FP_PRNTARG(scaleY(yc)));
openpath = true;
return;
}