Compare commits

..

3 Commits

8 changed files with 245 additions and 97 deletions

View File

@ -34,8 +34,8 @@
#include <gds-render/gds-utils/gds-parser.h>
#include <gds-render/layer/mapping-parser.h>
#include <gds-render/layer/layer-info.h>
#include <gds-render/output-renderers/cairo-output.h>
#include <gds-render/output-renderers/latex-output.h>
#include <gds-render/output-renderers/cairo-renderer.h>
#include <gds-render/output-renderers/latex-renderer.h>
#include <gds-render/output-renderers/external-renderer.h>
#include <gds-render/gds-utils/gds-tree-checker.h>
@ -55,12 +55,10 @@ static void delete_layer_info_with_name(struct layer_info *info)
}
}
void command_line_convert_gds(char *gds_name, char *pdf_name, char *tex_name, gboolean pdf, gboolean tex,
char *layer_file, char *cell_name, double scale, gboolean pdf_layers,
gboolean pdf_standalone, gboolean svg, char *svg_name, char *so_name, char *so_out_file)
void command_line_convert_gds(const char *gds_name, const char *cell_name, const char *output_file_name, const char *layer_file, const char *so_path,
enum command_line_renderer renderer, enum cmd_options options, double scale)
{
GList *libs = NULL;
FILE *tex_file;
int res;
GFile *file;
int i;
@ -75,10 +73,11 @@ void command_line_convert_gds(char *gds_name, char *pdf_name, char *tex_name, gb
struct layer_info *linfo_temp;
struct gds_library *first_lib;
struct gds_cell *toplevel_cell = NULL, *temp_cell;
GdsOutputRenderer *output_renderer;
gboolean tex_layers = FALSE, tex_standalone = FALSE;
/* Check if parameters are valid */
if (!gds_name || (!pdf_name && pdf) || (!tex_name && tex) || !layer_file || !cell_name) {
if (!gds_name || !cell_name || !output_file_name || !layer_file) {
printf("Probably missing argument. Check --help option\n");
return;
}
@ -165,30 +164,43 @@ void command_line_convert_gds(char *gds_name, char *pdf_name, char *tex_name, gb
* Deal with it.
*/
/* Render outputs */
if (pdf == TRUE || svg == TRUE) {
cairo_render_cell_to_vector_file(toplevel_cell, layer_info_list, (pdf == TRUE ? pdf_name : NULL),
(svg == TRUE ? svg_name : NULL), scale);
/* Render */
if (options & CMD_OPT_LATEX_LAYERS)
tex_layers = TRUE;
if (options & CMD_OPT_LATEX_STANDALONE)
tex_standalone = TRUE;
switch (renderer) {
case CMD_CAIRO_SVG:
output_renderer = GDS_RENDER_OUTPUT_RENDERER(cairo_renderer_new_svg());
break;
case CMD_LATEX:
output_renderer = GDS_RENDER_OUTPUT_RENDERER(latex_renderer_new_with_options(tex_layers, tex_standalone));
break;
case CMD_CAIRO_PDF:
output_renderer = GDS_RENDER_OUTPUT_RENDERER(cairo_renderer_new_pdf());
break;
case CMD_EXTERNAL:
output_renderer = GDS_RENDER_OUTPUT_RENDERER(external_renderer_new_with_so(so_path));
break;
case CMD_NONE:
/* Do nothing */
output_renderer = NULL;
break;
default:
output_renderer = NULL;
fprintf(stderr, "Invalid renderer supplied");
break;
}
if (tex == TRUE) {
tex_file = fopen(tex_name, "w");
if (!tex_file)
goto ret_clear_layer_list;
latex_render_cell_to_code(toplevel_cell, layer_info_list, tex_file, scale, pdf_layers, pdf_standalone);
fclose(tex_file);
if (output_renderer) {
gds_output_renderer_render_output(output_renderer, toplevel_cell, layer_info_list, output_file_name, scale);
g_object_unref(output_renderer);
}
if (so_name && so_out_file) {
if (strlen(so_name) == 0 || strlen(so_out_file) == 0)
goto ret_clear_layer_list;
/* Render output using external renderer */
printf("Invoking external renderer!\n");
external_renderer_render_cell(toplevel_cell, layer_info_list, so_out_file, so_name);
printf("External renderer finished!\n");
}
/* Render end */
ret_clear_layer_list:
g_list_free_full(layer_info_list, (GDestroyNotify)delete_layer_info_with_name);

View File

@ -33,28 +33,32 @@
#include <glib.h>
enum command_line_renderer {
CMD_NONE = 0,
CMD_EXTERNAL,
CMD_CAIRO_SVG,
CMD_CAIRO_PDF,
CMD_LATEX,
};
enum cmd_options {
CMD_OPT_NONE = 0U,
CMD_OPT_LATEX_STANDALONE = (1U<<0),
CMD_OPT_LATEX_LAYERS = (1U<<1),
};
/**
* @brief Convert GDS according to supplied parameters
* @param gds_name GDS File path
* @param pdf_name Cairo-PDF path
* @param tex_name TeX/TikZ path
* @param pdf Render Cairo
* @param tex Render LaTeX
* @param layer_file Layer mapping file
* @param cell_name Cell name to render
* @param scale Scale image down by this value
* @param pdf_layers TikZ creates OCG layers
* @param pdf_standalone LaTeX document is standalone7
* @param svg Render to SVG file
* @param so_name Path to shared object of custom renderer
* @param so_out_file Output file path for custom renderer
* @param svg_name SVG file name
*
* @note This function is pretty damn retarded (Lots of parameters). Will be reworked when generating GObjects for renderers.
* @brief render output file according to command line parameters
* @param gds_name Name of GDS file
* @param cell_name Name of cell to render
* @param output_file_name Output file name
* @param so_file Shared object file to search external rendering function
* @param renderer Type of output renderer
* @param options Additional options for output renderer
* @param scale Scale value
*/
void command_line_convert_gds(char *gds_name, char *pdf_name, char *tex_name, gboolean pdf, gboolean tex,
char *layer_file, char *cell_name, double scale, gboolean pdf_layers,
gboolean pdf_standalone, gboolean svg, char *svg_name, char *so_name, char *so_out_file);
void command_line_convert_gds(const char *gds_name, const char *cell_name, const char *output_file_name, const char *layer_file,
const char *so_file, enum command_line_renderer renderer, enum cmd_options options, double scale);
#endif /* _COMMAND_LINE_H_ */

View File

@ -26,23 +26,36 @@
#include <gds-render/gds-utils/gds-types.h>
#include <gds-render/layer/layer-info.h>
#include <gds-render/output-renderers/gds-output-renderer.h>
#include <glib-object.h>
/** @addtogroup Cairo-Renderer
G_BEGIN_DECLS
/** @addtogroup CairoRenderer
* @{
*/
G_DECLARE_FINAL_TYPE(CairoRenderer, cairo_renderer, GDS_RENDER, CAIRO_RENDERER, GdsOutputRenderer)
#define GDS_RENDER_TYPE_CAIRO_RENDERER (cairo_renderer_get_type())
#define MAX_LAYERS (300) /**< \brief Maximum layer count the output renderer can process. Typically GDS only specifies up to 255 layers.*/
/**
* @brief Render \p cell to a PDF file specified by \p pdf_file
* @param cell Toplevel cell to @ref Cairo-Renderer
* @param layer_infos List of layer information. Specifies color and layer stacking
* @param pdf_file PDF output file. Set to NULL if no PDF file has to be generated
* @param svg_file SVG output file. Set to NULL if no SVG file has to be generated
* @param scale Scale the output image down by \p scale
* @brief Create new CairoRenderer for SVG output
* @return New object
*/
void cairo_render_cell_to_vector_file(struct gds_cell *cell, GList *layer_infos, char *pdf_file, char *svg_file, double scale);
CairoRenderer *cairo_renderer_new_svg();
/**
* @brief Create new CairoRenderer for PDF output
* @return New object
*/
CairoRenderer *cairo_renderer_new_pdf();
/** @} */
G_END_DECLS
#endif /* _CAIRO_OUTPUT_H_ */

View File

@ -38,7 +38,7 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE(LatexRenderer, latex_renderer, GDS_RENDER, LATEX_RENDERER, GdsOutputRenderer)
#define GDS_RENDERER_TYPE_LATEX_RENDERER (latex_renderer_get_type())
#define GDS_RENDER_TYPE_LATEX_RENDERER (latex_renderer_get_type())
/**
* @brief Buffer for LaTeX Code line in KiB
@ -51,6 +51,22 @@ G_DECLARE_FINAL_TYPE(LatexRenderer, latex_renderer, GDS_RENDER, LATEX_RENDERER,
*/
LatexRenderer *latex_renderer_new();
/**
* @brief Create new LatexRenderer object
*
* This function sets the 'pdf-layers' and 'standalone'
* properties for the newly created object.
*
* They can later be changes by modifying the properties again.
* On top of that, The options can be changed in the resulting
* LaTeX output file if needed.
*
* @param pdf_layers If PDF OCR layers should be enabled
* @param standalone If output TeX file should be standalone compilable
* @return New object
*/
LatexRenderer *latex_renderer_new_with_options(gboolean pdf_layers, gboolean standalone);
G_END_DECLS
#endif /* _LATEX_OUTPUT_H_ */

72
main.c
View File

@ -241,29 +241,25 @@ int main(int argc, char **argv)
GOptionContext *context;
gchar *gds_name;
gchar *basename;
gchar *pdfname = NULL, *texname = NULL, *mappingname = NULL, *cellname = NULL, *svgname = NULL;
gboolean tikz = FALSE, pdf = FALSE, pdf_layers = FALSE, pdf_standalone = FALSE, svg = FALSE;
gboolean version = FALSE;
gchar *output_path = NULL, *mappingname = NULL, *cellname = NULL;
gchar *renderer_arg = NULL;
gboolean version = FALSE, pdf_standalone = FALSE, pdf_layers = FALSE;
gchar *custom_library_path = NULL;
gchar *custom_library_file_name = NULL;
int scale = 1000;
int app_status = 0;
enum command_line_renderer renderer = CMD_NONE;
enum cmd_options opt = CMD_OPT_NONE;
GOptionEntry entries[] = {
{"version", 'v', 0, G_OPTION_ARG_NONE, &version, "Print version", NULL},
{"tikz", 't', 0, G_OPTION_ARG_NONE, &tikz, "Output TikZ code", NULL },
{"pdf", 'p', 0, G_OPTION_ARG_NONE, &pdf, "Output PDF document", NULL },
//{"svg", 'S', 0, G_OPTION_ARG_NONE, &svg, "Output SVG image", NULL },
{"renderer", 'r', 0, G_OPTION_ARG_STRING, &renderer_arg, "Renderer to use", "pdf|svg|tikz|ext"},
{"scale", 's', 0, G_OPTION_ARG_INT, &scale, "Divide output coordinates by <SCALE>", "<SCALE>" },
{"tex-output", 'o', 0, G_OPTION_ARG_FILENAME, &texname, "Optional path for TeX file", "PATH" },
{"pdf-output", 'O', 0, G_OPTION_ARG_FILENAME, &pdfname, "Optional path for PDF file", "PATH" },
//{"svg-output", 0, 0, G_OPTION_ARG_FILENAME, &svgname, "Optional path for PDF file", "PATH"},
{"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &output_path, "Output file path", "PATH" },
{"mapping", 'm', 0, G_OPTION_ARG_FILENAME, &mappingname, "Path for Layer Mapping File", "PATH" },
{"cell", 'c', 0, G_OPTION_ARG_STRING, &cellname, "Cell to render", "NAME" },
{"tex-standalone", 'a', 0, G_OPTION_ARG_NONE, &pdf_standalone, "Create standalone PDF", NULL },
{"tex-layers", 'l', 0, G_OPTION_ARG_NONE, &pdf_layers, "Create PDF Layers (OCG)", NULL },
{"custom-render-lib", 'P', 0, G_OPTION_ARG_FILENAME, &custom_library_path, "Path to a custom shared object, that implements the " EXTERNAL_LIBRARY_FUNCTION " function", "PATH"},
{"external-lib-output", 'e', 0, G_OPTION_ARG_FILENAME, &custom_library_file_name, "Output path for external render library", "PATH"},
{NULL}
};
@ -287,9 +283,6 @@ int main(int argc, char **argv)
scale = 1;
}
/* No format selected */
if (!(tikz || pdf || svg))
tikz = TRUE;
/* Get gds name */
gds_name = argv[1];
@ -302,29 +295,50 @@ int main(int argc, char **argv)
/* Check if PDF/TeX names are supplied. if not generate */
basename = g_path_get_basename(gds_name);
if (!texname)
texname = g_strdup_printf("./%s.tex", basename);
if (!strcmp(renderer_arg, "pdf")) {
renderer = CMD_CAIRO_PDF;
if (!output_path)
output_path = g_strdup_printf("./%s.pdf", basename);
}
else if (!strcmp(renderer_arg, "svg")) {
renderer = CMD_NONE; // To buggy atm CMD_CAIRO_SVG;
if (!output_path)
output_path = g_strdup_printf("./%s.svg", basename);
} else if (!strcmp(renderer_arg, "tikz")) {
renderer = CMD_LATEX;
if (pdf_layers)
opt |= CMD_OPT_LATEX_LAYERS;
if (pdf_standalone)
opt |= CMD_OPT_LATEX_STANDALONE;
if (!output_path)
output_path = g_strdup_printf("./%s.tex", basename);
} else if (!strcmp(renderer_arg, "ext")) {
renderer = CMD_EXTERNAL;
} else {
fprintf(stderr, "No valid renderer specified\n");
}
if (!pdfname)
pdfname = g_strdup_printf("./%s.pdf", basename);
if (basename)
g_free(basename);
if (!svgname)
svgname = g_strdup_printf("./%s.svg", basename);
if (!output_path || strlen(output_path) == 0) {
app_status = -2;
goto ret_free_renderer;
}
command_line_convert_gds(gds_name, pdfname, texname, pdf, tikz,
mappingname, cellname, (double)scale,
pdf_layers, pdf_standalone, svg, svgname,
custom_library_path, custom_library_file_name);
command_line_convert_gds(gds_name, cellname, output_path, mappingname, custom_library_path, renderer, opt, scale);
/* Clean up */
g_free(pdfname);
g_free(texname);
g_free(svgname);
g_free(basename);
app_status = 0;
ret_free_renderer:
if (output_path)
g_free(output_path);
if (renderer_arg)
g_free(renderer_arg);
if (mappingname)
g_free(mappingname);
if (cellname)
g_free(cellname);
app_status = 0;
} else {
app_status = start_gui(argc, argv);
}

View File

@ -22,7 +22,7 @@
* @author Mario Hüttel <mario.huettel@gmx.net>
*/
/** @addtogroup Cairo-Renderer
/** @addtogroup CairoRenderer
* @{
*/
@ -32,10 +32,17 @@
#include <cairo-pdf.h>
#include <cairo-svg.h>
#include <gds-render/output-renderers/cairo-output.h>
#include <gds-render/output-renderers/cairo-renderer.h>
#include <sys/wait.h>
#include <unistd.h>
struct _CairoRenderer {
GdsOutputRenderer parent;
gboolean svg; /**< @brief TRUE: SVG output, FALSE: PDF output */
};
G_DEFINE_TYPE(CairoRenderer, cairo_renderer, GDS_RENDER_TYPE_OUTPUT_RENDERER)
/**
* @brief The cairo_layer struct
* Each rendered layer is represented by this struct.
@ -181,7 +188,17 @@ static void render_cell(struct gds_cell *cell, struct cairo_layer *layers, doubl
}
void cairo_render_cell_to_vector_file(struct gds_cell *cell, GList *layer_infos, char *pdf_file, char *svg_file, double scale)
/**
* @brief Render \p cell to a PDF file specified by \p pdf_file
* @param cell Toplevel cell to @ref Cairo-Renderer
* @param layer_infos List of layer information. Specifies color and layer stacking
* @param pdf_file PDF output file. Set to NULL if no PDF file has to be generated
* @param svg_file SVG output file. Set to NULL if no SVG file has to be generated
* @param scale Scale the output image down by \p scale
* @return Error
*/
static int cairo_renderer_render_cell_to_vector_file(struct gds_cell *cell, GList *layer_infos, const char *pdf_file,
const char *svg_file, double scale)
{
cairo_surface_t *pdf_surface = NULL, *svg_surface = NULL;
cairo_t *pdf_cr = NULL, *svg_cr = NULL;
@ -196,7 +213,7 @@ void cairo_render_cell_to_vector_file(struct gds_cell *cell, GList *layer_infos,
if (pdf_file == NULL && svg_file == NULL) {
/* No output specified */
return;
return -1;
}
/* Fork to a new child process. This ensures the memory leaks (see issue #16) in Cairo don't
@ -332,10 +349,64 @@ ret_clear_layers:
exit(0);
/* Fork didn't work. Just return here */
return;
return 0;
ret_parent:
waitpid(process_id, NULL, 0);
return;
return 0;
}
static void cairo_renderer_init(CairoRenderer *self)
{
/* PDF default */
self->svg = FALSE;
}
static int cairo_renderer_render_output(GdsOutputRenderer *renderer,
struct gds_cell *cell,
GList *layer_infos,
const char *output_file,
double scale)
{
CairoRenderer *c_renderer = GDS_RENDER_CAIRO_RENDERER(renderer);
const char *pdf_file = NULL;
const char *svg_file = NULL;
if (!c_renderer)
return -2000;
if (c_renderer->svg == TRUE)
svg_file = output_file;
else
pdf_file = output_file;
return cairo_renderer_render_cell_to_vector_file(cell, layer_infos, pdf_file, svg_file, scale);
}
static void cairo_renderer_class_init(CairoRendererClass *klass)
{
GdsOutputRendererClass *renderer_class = GDS_RENDER_OUTPUT_RENDERER_CLASS(klass);
renderer_class->render_output = cairo_renderer_render_output;
}
CairoRenderer *cairo_renderer_new_pdf()
{
CairoRenderer *renderer;
renderer = GDS_RENDER_CAIRO_RENDERER(g_object_new(GDS_RENDER_TYPE_CAIRO_RENDERER, NULL));
renderer->svg = FALSE;
return renderer;
}
CairoRenderer *cairo_renderer_new_svg()
{
CairoRenderer *renderer;
renderer = GDS_RENDER_CAIRO_RENDERER(g_object_new(GDS_RENDER_TYPE_CAIRO_RENDERER, NULL));
renderer->svg = TRUE;
return renderer;
}
/** @} */

View File

@ -86,8 +86,11 @@ static int external_renderer_render_cell(struct gds_cell *toplevel_cell, GList *
}
/* Execute */
if (so_render_func)
if (so_render_func) {
g_message("Calling external renderer.");
ret = so_render_func(toplevel_cell, layer_info_list, output_file, scale);
g_message("External renderer finished.");
}
ret_close_so_handle:
dlclose(so_handle);
@ -139,8 +142,10 @@ static void external_renderer_set_property(GObject *obj, guint property_id, cons
}
}
static void external_renderer_dispose(ExternalRenderer *self)
static void external_renderer_dispose(GObject *self_obj)
{
ExternalRenderer *self = GDS_RENDER_EXTERNAL_RENDERER(self_obj);
if (self->shared_object_path) {
g_free(self->shared_object_path);
self->shared_object_path = NULL;
@ -157,7 +162,7 @@ static void external_renderer_class_init(ExternalRendererClass *klass)
GObjectClass *oclass;
inherited_parent_class = GDS_RENDER_OUTPUT_RENDERER_CLASS(klass);
oclass = G_OBJECT_CLASS(oclass);
oclass = G_OBJECT_CLASS(klass);
/* Override virtual function */
inherited_parent_class->render_output = external_renderer_render_output;

View File

@ -25,7 +25,7 @@
#include <math.h>
#include <stdio.h>
#include <gds-render/output-renderers/latex-output.h>
#include <gds-render/output-renderers/latex-renderer.h>
#include <gdk/gdk.h>
#include <gds-render/layer/layer-info.h>
/**
@ -410,4 +410,17 @@ static void latex_renderer_class_init(LatexRendererClass *klass)
g_object_class_install_properties(oclass, N_PROPERTIES, latex_renderer_properties);
}
LatexRenderer *latex_renderer_new()
{
return GDS_RENDER_LATEX_RENDERER(g_object_new(GDS_RENDER_TYPE_LATEX_RENDERER, NULL));
}
LatexRenderer *latex_renderer_new_with_options(gboolean pdf_layers, gboolean standalone)
{
GObject *obj;
obj = g_object_new(GDS_RENDER_TYPE_LATEX_RENDERER, "standalone", standalone, "pdf-layers", pdf_layers, NULL);
return GDS_RENDER_LATEX_RENDERER(obj);
}
/** @} */