2018-07-19 17:49:33 +02:00
|
|
|
/*
|
|
|
|
* GDSII-Converter
|
|
|
|
* Copyright (C) 2018 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* This file is part of GDSII-Converter.
|
|
|
|
*
|
|
|
|
* GDSII-Converter is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* GDSII-Converter 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 GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-07-30 12:35:30 +02:00
|
|
|
/**
|
2018-07-24 14:42:28 +02:00
|
|
|
* @file cairo-output.c
|
|
|
|
* @brief Output renderer for Cairo PDF export
|
|
|
|
* @author Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @addtogroup Cairo-Renderer
|
|
|
|
* @{
|
|
|
|
*/
|
2018-07-19 17:49:33 +02:00
|
|
|
|
|
|
|
#include <math.h>
|
2018-07-19 18:45:37 +02:00
|
|
|
#include <stdlib.h>
|
2018-07-19 17:49:33 +02:00
|
|
|
#include <cairo.h>
|
|
|
|
#include <cairo-pdf.h>
|
2018-07-30 12:35:30 +02:00
|
|
|
#include <cairo-svg.h>
|
2018-07-19 17:49:33 +02:00
|
|
|
|
2019-06-19 19:01:40 +02:00
|
|
|
#include <gds-render/output-renderers/cairo-renderer.h>
|
2019-05-30 14:49:35 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2019-03-26 19:57:19 +01:00
|
|
|
|
2018-07-24 14:42:28 +02:00
|
|
|
/**
|
|
|
|
* @brief The cairo_layer struct
|
|
|
|
* Each rendered layer is represented by this struct.
|
|
|
|
*/
|
2018-07-19 18:45:37 +02:00
|
|
|
struct cairo_layer {
|
2018-07-30 12:35:30 +02:00
|
|
|
cairo_t *cr; /**< @brief cairo context for layer*/
|
|
|
|
cairo_surface_t *rec; /**< @brief Recording surface to hold the layer */
|
|
|
|
struct layer_info *linfo; /**< @brief Reference to layer information */
|
2018-07-19 18:45:37 +02:00
|
|
|
};
|
|
|
|
|
2018-07-24 14:42:28 +02:00
|
|
|
/**
|
|
|
|
* @brief Revert the last transformation on all layers
|
|
|
|
* @param layers Pointer to #cairo_layer structures
|
|
|
|
*/
|
2018-07-20 22:32:23 +02:00
|
|
|
static void revert_inherited_transform(struct cairo_layer *layers)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_LAYERS; i++) {
|
|
|
|
if (layers[i].cr == NULL)
|
2018-07-22 16:38:26 +02:00
|
|
|
continue;
|
2018-07-20 22:32:23 +02:00
|
|
|
cairo_restore(layers[i].cr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:42:28 +02:00
|
|
|
/**
|
|
|
|
* @brief Applies transformation to all layers
|
|
|
|
* @param layers Array of layers
|
|
|
|
* @param origin Origin translation
|
|
|
|
* @param magnification Scaling
|
|
|
|
* @param flipping Mirror image on x-axis before rotating
|
|
|
|
* @param rotation Rotattion in degrees
|
|
|
|
* @param scale Scale the image down by. Only used for sclaing origin coordinates. Not applied to layer.
|
|
|
|
*/
|
2018-07-20 22:32:23 +02:00
|
|
|
static void apply_inherited_transform_to_all_layers(struct cairo_layer *layers,
|
|
|
|
const struct gds_point *origin,
|
|
|
|
double magnification,
|
|
|
|
gboolean flipping,
|
|
|
|
double rotation,
|
|
|
|
double scale)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
cairo_t *temp_layer_cr;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_LAYERS; i++) {
|
|
|
|
temp_layer_cr = layers[i].cr;
|
|
|
|
if (temp_layer_cr == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Save the state and apply transformation */
|
|
|
|
cairo_save(temp_layer_cr);
|
|
|
|
cairo_translate(temp_layer_cr, (double)origin->x/scale, (double)origin->y/scale);
|
|
|
|
cairo_rotate(temp_layer_cr, M_PI*rotation/180.0);
|
|
|
|
cairo_scale(temp_layer_cr, magnification,
|
|
|
|
(flipping == TRUE ? -magnification : magnification));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:42:28 +02:00
|
|
|
/**
|
|
|
|
* @brief render_cell Render a cell with its sub-cells
|
|
|
|
* @param cell Cell to render
|
|
|
|
* @param layers Cell will be rendered into these layers
|
|
|
|
* @param scale sclae image down by this factor
|
|
|
|
*/
|
2018-07-20 22:32:23 +02:00
|
|
|
static void render_cell(struct gds_cell *cell, struct cairo_layer *layers, double scale)
|
|
|
|
{
|
|
|
|
GList *instance_list;
|
|
|
|
struct gds_cell *temp_cell;
|
|
|
|
struct gds_cell_instance *cell_instance;
|
|
|
|
GList *gfx_list;
|
|
|
|
struct gds_graphics *gfx;
|
|
|
|
GList *vertex_list;
|
|
|
|
struct gds_point *vertex;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
/* Render child cells */
|
|
|
|
for (instance_list = cell->child_cells; instance_list != NULL; instance_list = instance_list->next) {
|
|
|
|
cell_instance = (struct gds_cell_instance *)instance_list->data;
|
|
|
|
if ((temp_cell = cell_instance->cell_ref) != NULL) {
|
|
|
|
apply_inherited_transform_to_all_layers(layers,
|
|
|
|
&cell_instance->origin,
|
|
|
|
cell_instance->magnification,
|
|
|
|
cell_instance->flipped,
|
|
|
|
cell_instance->angle,
|
|
|
|
scale);
|
|
|
|
render_cell(temp_cell, layers, scale);
|
|
|
|
revert_inherited_transform(layers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Render graphics */
|
|
|
|
for (gfx_list = cell->graphic_objs; gfx_list != NULL; gfx_list = gfx_list->next) {
|
|
|
|
gfx = (struct gds_graphics *)gfx_list->data;
|
|
|
|
|
|
|
|
/* Get layer renderer */
|
|
|
|
if (gfx->layer >= MAX_LAYERS)
|
|
|
|
continue;
|
|
|
|
if ((cr = layers[gfx->layer].cr) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Apply settings */
|
2018-07-22 16:38:26 +02:00
|
|
|
cairo_set_line_width(cr, (gfx->width_absolute ? gfx->width_absolute/scale : 1));
|
2018-07-20 22:32:23 +02:00
|
|
|
|
|
|
|
switch (gfx->path_render_type) {
|
|
|
|
case PATH_FLUSH:
|
|
|
|
cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
|
|
|
|
break;
|
|
|
|
case PATH_ROUNDED:
|
|
|
|
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
|
|
|
|
break;
|
|
|
|
case PATH_SQUARED:
|
|
|
|
cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add vertices */
|
|
|
|
for (vertex_list = gfx->vertices; vertex_list != NULL; vertex_list = vertex_list->next) {
|
|
|
|
vertex = (struct gds_point *)vertex_list->data;
|
|
|
|
|
|
|
|
/* If first point -> move to, else line to */
|
|
|
|
if (vertex_list->prev == NULL)
|
|
|
|
cairo_move_to(cr, vertex->x/scale, vertex->y/scale);
|
|
|
|
else
|
|
|
|
cairo_line_to(cr, vertex->x/scale, vertex->y/scale);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create graphics object */
|
|
|
|
switch (gfx->gfx_type) {
|
|
|
|
case GRAPHIC_PATH:
|
|
|
|
cairo_stroke(cr);
|
|
|
|
break;
|
2018-07-20 23:29:27 +02:00
|
|
|
case GRAPHIC_BOX:
|
2018-07-20 22:32:23 +02:00
|
|
|
case GRAPHIC_POLYGON:
|
2018-07-22 16:38:26 +02:00
|
|
|
cairo_set_line_width(cr, 0.1/scale);
|
2018-07-20 22:32:23 +02:00
|
|
|
cairo_close_path(cr);
|
2018-07-22 16:38:26 +02:00
|
|
|
cairo_stroke_preserve(cr); // Prevent graphic glitches
|
2018-07-20 22:32:23 +02:00
|
|
|
cairo_fill(cr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:35:30 +02:00
|
|
|
void cairo_render_cell_to_vector_file(struct gds_cell *cell, GList *layer_infos, char *pdf_file, char *svg_file, double scale)
|
2018-07-19 17:49:33 +02:00
|
|
|
{
|
2019-05-30 14:17:36 +02:00
|
|
|
cairo_surface_t *pdf_surface = NULL, *svg_surface = NULL;
|
|
|
|
cairo_t *pdf_cr = NULL, *svg_cr = NULL;
|
2018-07-19 18:45:37 +02:00
|
|
|
struct layer_info *linfo;
|
|
|
|
struct cairo_layer *layers;
|
|
|
|
struct cairo_layer *lay;
|
|
|
|
GList *info_list;
|
|
|
|
int i;
|
2018-07-22 16:38:26 +02:00
|
|
|
double rec_x0, rec_y0, rec_width, rec_height;
|
|
|
|
double xmin = INT32_MAX, xmax = INT32_MIN, ymin = INT32_MAX, ymax = INT32_MIN;
|
2019-05-30 14:49:35 +02:00
|
|
|
pid_t process_id;
|
2018-07-19 18:45:37 +02:00
|
|
|
|
2018-07-30 12:35:30 +02:00
|
|
|
if (pdf_file == NULL && svg_file == NULL) {
|
|
|
|
/* No output specified */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-30 14:49:35 +02:00
|
|
|
/* Fork to a new child process. This ensures the memory leaks (see issue #16) in Cairo don't
|
|
|
|
* brick everything.
|
|
|
|
*
|
|
|
|
* And by the way: This now bricks all Windows compatibility. Deal with it.
|
|
|
|
*/
|
|
|
|
process_id = fork();
|
|
|
|
if (process_id < 0) {
|
|
|
|
/* Well... shit... We have to run it in our process. */
|
|
|
|
} else if (process_id > 0) {
|
2019-05-30 14:55:37 +02:00
|
|
|
/* Woohoo... Successfully dumped the shitty code to an unknowing victim */
|
2019-05-30 14:49:35 +02:00
|
|
|
goto ret_parent;
|
|
|
|
}
|
|
|
|
|
2018-07-19 18:45:37 +02:00
|
|
|
layers = (struct cairo_layer *)calloc(MAX_LAYERS, sizeof(struct cairo_layer));
|
|
|
|
|
|
|
|
/* Clear layers */
|
|
|
|
for (i = 0; i < MAX_LAYERS; i++) {
|
|
|
|
layers[i].cr = NULL;
|
|
|
|
layers[i].rec = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create recording surface for each layer */
|
|
|
|
for (info_list = layer_infos; info_list != NULL; info_list = g_list_next(info_list)) {
|
|
|
|
linfo = (struct layer_info *)info_list->data;
|
|
|
|
if (linfo->layer < MAX_LAYERS) {
|
|
|
|
lay = &(layers[(unsigned int)linfo->layer]);
|
|
|
|
lay->linfo = linfo;
|
|
|
|
lay->rec = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA,
|
|
|
|
NULL);
|
|
|
|
lay->cr = cairo_create(layers[(unsigned int)linfo->layer].rec);
|
2018-07-20 22:32:23 +02:00
|
|
|
cairo_scale(lay->cr, 1, -1); // Fix coordinate system
|
2018-07-22 16:38:26 +02:00
|
|
|
cairo_set_source_rgb(lay->cr, linfo->color.red, linfo->color.green, linfo->color.blue);
|
2018-07-19 18:45:37 +02:00
|
|
|
} else {
|
|
|
|
printf("Layer number (%d) too high!\n", linfo->layer);
|
|
|
|
goto ret_clear_layers;
|
|
|
|
}
|
|
|
|
}
|
2018-07-19 17:49:33 +02:00
|
|
|
|
|
|
|
|
2018-07-20 22:32:23 +02:00
|
|
|
render_cell(cell, layers, scale);
|
|
|
|
|
2018-07-22 16:38:26 +02:00
|
|
|
/* get size of image and top left coordinate */
|
|
|
|
for (info_list = layer_infos; info_list != NULL; info_list = g_list_next(info_list)) {
|
|
|
|
linfo = (struct layer_info *)info_list->data;
|
|
|
|
|
|
|
|
if (linfo->layer >= MAX_LAYERS) {
|
|
|
|
printf("Layer outside of Spec.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print size */
|
|
|
|
cairo_recording_surface_ink_extents(layers[linfo->layer].rec, &rec_x0, &rec_y0,
|
|
|
|
&rec_width, &rec_height);
|
2019-03-22 22:15:56 +01:00
|
|
|
printf("Size of layer %d%s%s%s: <%lf x %lf> @ (%lf | %lf)\n",
|
|
|
|
linfo->layer,
|
|
|
|
(linfo->name && linfo->name[0] ? " (" : ""),
|
|
|
|
(linfo->name && linfo->name[0] ? linfo->name : ""),
|
|
|
|
(linfo->name && linfo->name[0] ? ")" : ""),
|
|
|
|
rec_width, rec_height, rec_x0, rec_y0);
|
2018-07-22 16:38:26 +02:00
|
|
|
|
|
|
|
/* update bounding box */
|
|
|
|
xmin = MIN(xmin, rec_x0);
|
|
|
|
xmax = MAX(xmax, rec_x0);
|
|
|
|
ymin = MIN(ymin, rec_y0);
|
|
|
|
ymax = MAX(ymax, rec_y0);
|
|
|
|
xmin = MIN(xmin, rec_x0+rec_width);
|
|
|
|
xmax = MAX(xmax, rec_x0+rec_width);
|
|
|
|
ymin = MIN(ymin, rec_y0+rec_height);
|
|
|
|
ymax = MAX(ymax, rec_y0+rec_height);
|
|
|
|
|
|
|
|
}
|
2018-07-20 22:32:23 +02:00
|
|
|
|
2019-03-22 22:15:56 +01:00
|
|
|
printf("Cell bounding box: (%lf | %lf) -- (%lf | %lf)\n", xmin, ymin, xmax, ymax);
|
2018-07-22 16:38:26 +02:00
|
|
|
|
2018-07-30 12:35:30 +02:00
|
|
|
if (pdf_file) {
|
|
|
|
pdf_surface = cairo_pdf_surface_create(pdf_file, xmax-xmin, ymax-ymin);
|
|
|
|
pdf_cr = cairo_create(pdf_surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (svg_file) {
|
2018-08-01 12:36:31 +02:00
|
|
|
svg_surface = cairo_svg_surface_create(svg_file, xmax-xmin, ymax-ymin);
|
2018-07-30 12:35:30 +02:00
|
|
|
svg_cr = cairo_create(svg_surface);
|
|
|
|
}
|
2018-07-22 16:38:26 +02:00
|
|
|
|
|
|
|
/* Write layers to PDF */
|
|
|
|
for (info_list = layer_infos; info_list != NULL; info_list = g_list_next(info_list)) {
|
|
|
|
linfo = (struct layer_info *)info_list->data;
|
|
|
|
|
|
|
|
if (linfo->layer >= MAX_LAYERS) {
|
|
|
|
printf("Layer outside of Spec.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-30 14:17:36 +02:00
|
|
|
if (pdf_file && pdf_cr) {
|
2018-07-30 12:35:30 +02:00
|
|
|
cairo_set_source_surface(pdf_cr, layers[linfo->layer].rec, -xmin, -ymin);
|
|
|
|
cairo_paint_with_alpha(pdf_cr, linfo->color.alpha);
|
|
|
|
}
|
|
|
|
|
2019-05-30 14:17:36 +02:00
|
|
|
if (svg_file && svg_cr) {
|
2018-07-30 12:35:30 +02:00
|
|
|
cairo_set_source_surface(svg_cr, layers[linfo->layer].rec, -xmin, -ymin);
|
|
|
|
cairo_paint_with_alpha(svg_cr, linfo->color.alpha);
|
|
|
|
}
|
2018-07-22 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 12:35:30 +02:00
|
|
|
if (pdf_file) {
|
|
|
|
cairo_show_page(pdf_cr);
|
|
|
|
cairo_destroy(pdf_cr);
|
|
|
|
cairo_surface_destroy(pdf_surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (svg_file) {
|
|
|
|
cairo_show_page(svg_cr);
|
|
|
|
cairo_destroy(svg_cr);
|
|
|
|
cairo_surface_destroy(svg_surface);
|
|
|
|
}
|
2018-07-20 22:32:23 +02:00
|
|
|
|
2018-07-19 18:45:37 +02:00
|
|
|
ret_clear_layers:
|
|
|
|
for (i = 0; i < MAX_LAYERS; i++) {
|
|
|
|
lay = &layers[i];
|
2018-07-22 16:38:26 +02:00
|
|
|
if(lay->cr) {
|
|
|
|
cairo_destroy(lay->cr);
|
|
|
|
cairo_surface_destroy(lay->rec);
|
|
|
|
}
|
2018-07-19 18:45:37 +02:00
|
|
|
}
|
|
|
|
free(layers);
|
2018-07-19 17:49:33 +02:00
|
|
|
|
2019-06-07 19:28:07 +02:00
|
|
|
printf("Cairo export finished. It might still be buggy!\n");
|
2019-05-30 14:49:35 +02:00
|
|
|
|
|
|
|
/* If forked, suspend process */
|
|
|
|
if (process_id == 0)
|
|
|
|
exit(0);
|
|
|
|
|
2019-06-07 19:28:07 +02:00
|
|
|
/* Fork didn't work. Just return here */
|
2019-05-30 14:49:35 +02:00
|
|
|
return;
|
|
|
|
ret_parent:
|
|
|
|
waitpid(process_id, NULL, 0);
|
|
|
|
return;
|
2018-07-19 17:49:33 +02:00
|
|
|
}
|
2018-07-24 14:42:28 +02:00
|
|
|
|
|
|
|
/** @} */
|