1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-10-04 06:46:00 +03:00
eda-tools/genex/pdf.c

196 lines
4.0 KiB
C
Raw Normal View History

/*
* pdf.c - Generate PDF
*
* Copyright 2012 by Werner Almesberger
*
* 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.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include "comp.h"
#include "pdf.h"
static struct format {
int left;
struct text {
const char *font;
int size;
int y;
} name, path, lib, comment;
int comment_line_skip;
} format = {
.left = 20,
.name = { "Helvetica-Bold", 24, 57 },
.path = { "Helvetica-Bold", 18, 30 },
.lib = { "Courier", 12, 75 },
.comment = { "Helvetica", 12, 600 },
.comment_line_skip = 14,
};
static int children(const struct node *node)
{
int n = 0;
while (node) {
n++;
node = node->next;
}
return n;
}
static void ps_string(FILE *file, const char *s)
{
fputc('(', file);
while (*s) {
if (*s == '(' || *s == ')' || *s == '\\')
fputc('\\', file);
fputc(*s, file);
s++;
}
fputc(')', file);
}
static void print_path(FILE *file, const struct node *node)
{
if (node->parent) {
print_path(file, node->parent);
fprintf(file, "( > ) show\n");
}
ps_string(file, node->name);
fprintf(file, " 0.5 setgray show 0 setgray\n");
}
static void make_title(FILE *file, const struct node *node, int unit)
{
fprintf(file, "gsave 90 rotate 0 setgray\n");
fprintf(file, "/%s findfont %d scalefont setfont\n",
format.name.font, format.name.size);
fprintf(file, "%d %d moveto\n", format.left, -format.name.y);
ps_string(file, node->canon);
fprintf(file, " show\n");
if (node->units > 1)
fprintf(file, " ( \\(%c\\)) show\n", 'A'+unit);
fprintf(file, "/%s findfont %d scalefont setfont\n",
format.path.font, format.path.size);
fprintf(file, "%d %d moveto\n", format.left, -format.path.y);
print_path(file, node);
fprintf(file, "/%s findfont %d scalefont setfont\n",
format.lib.font, format.lib.size);
fprintf(file, "%d %d moveto\n", format.left, -format.lib.y);
ps_string(file, node->lib);
fprintf(file, " show\n");
fprintf(file, "grestore\n");
fflush(file);
}
static void print_comment(FILE *file, const struct line *comment)
{
const struct line *line;
int lines = 0;
int n;
fprintf(file, "gsave 90 rotate 0 setgray\n");
fprintf(file, "/%s findfont %d scalefont setfont\n",
format.comment.font, format.comment.size);
for (line = comment; line; line = line->next)
lines++;
n = 0;
for (line = comment; line; line = line->next) {
n++;
fprintf(file, "%d -%d moveto\n", format.left,
format.comment.y+(n-lines)*format.comment_line_skip);
ps_string(file, line->s);
fprintf(file, " show\n");
}
fprintf(file, "grestore\n");
fflush(file);
}
static void convert_comp(const struct node *node, FILE *out)
{
char *tmp;
int i, res;
for (i = 0; i != node->units; i++) {
make_title(stdout, node, i);
if (!i && node->comment)
print_comment(out, node->comment);
if (asprintf(&tmp, "./sym2xps '%s' '%s' %d '%s' '%s'",
node->lib, node->canon, i+1, "tmp", "tmp.ps") < 0) {
perror("asprintf");
exit(1);
}
res = system(tmp);
if (res < 0) {
perror("system");
exit(1);
}
if (res) {
fprintf(stderr, "sym2xps returned %d\n", res);
exit(1);
}
fflush(out);
system("cat tmp.ps");
}
}
static void convert_tree(const struct node *node, FILE *out)
{
while (node) {
fprintf(out, "[ /Title (%s) /Count %d /OUT pdfmark\n",
node->name, -children(node->child));
if (node->child)
convert_tree(node->child, out);
else
convert_comp(node, out);
node = node->next;
}
}
void make_pdf(void)
{
FILE *out;
int res;
#if 0
out = popen(
"gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=- -f -",
"w");
#else
out = popen("cat", "w");
#endif
if (!out) {
perror("gs");
exit(1);
}
convert_tree(tree, out);
res = pclose(out);
if (res < 0) {
perror("pclose");
exit(1);
}
if (res) {
fprintf(stderr, "exit status %d\n", res);
exit(1);
}
}