54 #include <pango/pango.h>
67 #define dprintf if(DEBUG) printf
69 #define NUMBER_OF_DEFAULT_COLORS 18
70 #define NUMBER_OF_DEFAULT_TRANSFORMATIONS 20
72 static int defaultColorIndex = 0;
98 {0,0,1,1,0,FALSE,FALSE,FALSE},
99 {0,0,1,1,0,FALSE,FALSE,FALSE},
100 {0,0,1,1,0,FALSE,FALSE,FALSE},
101 {0,0,1,1,0,FALSE,FALSE,FALSE},
102 {0,0,1,1,0,FALSE,FALSE,FALSE},
103 {0,0,1,1,0,FALSE,FALSE,FALSE},
104 {0,0,1,1,0,FALSE,FALSE,FALSE},
105 {0,0,1,1,0,FALSE,FALSE,FALSE},
106 {0,0,1,1,0,FALSE,FALSE,FALSE},
107 {0,0,1,1,0,FALSE,FALSE,FALSE},
108 {0,0,1,1,0,FALSE,FALSE,FALSE},
109 {0,0,1,1,0,FALSE,FALSE,FALSE},
110 {0,0,1,1,0,FALSE,FALSE,FALSE},
111 {0,0,1,1,0,FALSE,FALSE,FALSE},
112 {0,0,1,1,0,FALSE,FALSE,FALSE},
113 {0,0,1,1,0,FALSE,FALSE,FALSE},
114 {0,0,1,1,0,FALSE,FALSE,FALSE},
115 {0,0,1,1,0,FALSE,FALSE,FALSE},
116 {0,0,1,1,0,FALSE,FALSE,FALSE},
117 {0,0,1,1,0,FALSE,FALSE,FALSE},
127 returnProject->
path = g_get_current_dir ();
134 return returnProject;
144 if (gerbvProject->
file[i])
148 g_free (gerbvProject->
path);
151 g_free (gerbvProject->
project);
153 g_free (gerbvProject->
file);
154 g_free (gerbvProject);
162 g_free (fileInfo->
name);
164 cairo_surface_destroy ((cairo_surface_t *)
174 dprintf(
"Opening filename = %s\n", (gchar *) filename);
176 if (gerbv_open_image(gerbvProject, filename, ++gerbvProject->
last_loaded, FALSE, NULL, 0, TRUE) == -1) {
177 GERB_MESSAGE(_(
"Could not read %s[%d]"), (gchar *) filename,
183 dprintf(
" Successfully opened file!\n");
190 guint16 red, guint16 green, guint16 blue, guint16 alpha)
193 dprintf(
"Opening filename = %s\n", (gchar *) filename);
195 if (gerbv_open_image(gerbvProject, filename, ++gerbvProject->
last_loaded, FALSE, NULL, 0, TRUE) == -1) {
196 GERB_MESSAGE(_(
"Could not read %s[%d]"), (gchar *) filename,
202 GdkColor colorTemplate = {0, red, green, blue};
203 gerbvProject->
file[idx_loaded]->
color = colorTemplate;
204 gerbvProject->
file[idx_loaded]->
alpha = alpha;
205 dprintf(
" Successfully opened file!\n");
211 gerbv_save_layer_from_index(
gerbv_project_t *gerbvProject, gint index, gchar *filename)
213 if (strcmp (gerbvProject->
file[index]->
image->
info->type,
"RS274-X (Gerber) File")==0) {
217 else if (strcmp (gerbvProject->
file[index]->
image->
info->type,
"Excellon Drill File")==0) {
234 rv = gerbv_open_image(gerbvProject, gerbvProject->
file[idx]->
fullPathname, idx, TRUE, NULL, 0, TRUE);
245 for (idx = 0; idx <= gerbvProject->
last_loaded; idx++) {
247 (void) gerbv_revert_file (gerbvProject, idx);
262 for (i=index; i<(gerbvProject->
last_loaded); i++) {
263 gerbvProject->
file[i]=gerbvProject->
file[i+1];
278 for (index = gerbvProject->
last_loaded ; index >= 0; index--) {
279 if (gerbvProject->
file[index] && gerbvProject->
file[index]->
name) {
280 gerbv_unload_layer (gerbvProject, index);
288 gerbv_change_layer_order(
gerbv_project_t *gerbvProject, gint oldPosition, gint newPosition)
293 temp_file = gerbvProject->
file[oldPosition];
295 if (oldPosition < newPosition){
296 for (index = oldPosition; index < newPosition; index++) {
297 gerbvProject->
file[index] = gerbvProject->
file[index + 1];
300 for (index = oldPosition; index > newPosition; index--) {
301 gerbvProject->
file[index] = gerbvProject->
file[index - 1];
304 gerbvProject->
file[newPosition] = temp_file;
311 gchar *filename, gchar *baseName,
int idx,
int reload){
312 gerb_verify_error_t error = GERB_IMAGE_OK;
315 dprintf(
"In open_image, now error check file....\n");
316 error = gerbv_image_verify(parsed_image);
319 if (error & GERB_IMAGE_MISSING_NETLIST) {
320 GERB_COMPILE_ERROR(_(
"Missing netlist - aborting file read"));
326 if (error & GERB_IMAGE_MISSING_FORMAT)
327 g_warning(_(
"Missing format in file...trying to load anyways\n"));
328 if (error & GERB_IMAGE_MISSING_APERTURES) {
329 g_warning(_(
"Missing apertures/drill sizes...trying to load anyways\n"));
334 if (error & GERB_IMAGE_MISSING_INFO)
335 g_warning(_(
"Missing info...trying to load anyways\n"));
344 gerbvProject->
file[idx]->
image = parsed_image;
349 gerbvProject->
file[idx]->
image = parsed_image;
356 gerbvProject->
file[idx]->
name = g_strdup (baseName);
359 r = defaultColors[defaultColorIndex % NUMBER_OF_DEFAULT_COLORS].red*257;
360 g = defaultColors[defaultColorIndex % NUMBER_OF_DEFAULT_COLORS].green*257;
361 b = defaultColors[defaultColorIndex % NUMBER_OF_DEFAULT_COLORS].blue*257;
363 GdkColor colorTemplate = {0, r, g, b};
364 gerbvProject->
file[idx]->
color = colorTemplate;
365 gerbvProject->
file[idx]->
alpha = defaultColors[defaultColorIndex % NUMBER_OF_DEFAULT_COLORS].alpha*257;
367 gerbvProject->
file[idx]->
transform = defaultTransformations[defaultColorIndex % NUMBER_OF_DEFAULT_TRANSFORMATIONS];
378 gerbv_open_image(
gerbv_project_t *gerbvProject,
char *filename,
int idx,
int reload,
379 gerbv_HID_Attribute *fattr,
int n_fattr, gboolean forceLoadFile)
384 gboolean isPnpFile = FALSE, foundBinary;
385 gerbv_HID_Attribute *attr_list = NULL;
406 if ((idx+1) >= gerbvProject->
max_files) {
415 dprintf(
"In open_image, about to try opening filename = %s\n", filename);
417 fd = gerb_fopen(filename);
419 GERB_MESSAGE(_(
"Trying to open %s: %s"), filename, strerror(errno));
424 fd->filename = g_strdup(filename);
426 dprintf(
"In open_image, successfully opened file. Now check its type....\n");
434 dprintf(
"Found RS-274X file\n");
435 if (!foundBinary || forceLoadFile) {
438 gchar *currentLoadDirectory = g_path_get_dirname (filename);
439 parsed_image =
parse_gerb(fd, currentLoadDirectory);
440 g_free (currentLoadDirectory);
442 }
else if(drill_file_p(fd, &foundBinary)) {
443 dprintf(
"Found drill file\n");
444 if (!foundBinary || forceLoadFile)
445 parsed_image = parse_drillfile(fd, attr_list, n_attr, reload);
447 }
else if (pick_and_place_check_file_type(fd, &foundBinary)) {
448 dprintf(
"Found pick-n-place file\n");
449 if (!foundBinary || forceLoadFile) {
451 pick_and_place_parse_file_to_images(fd, &parsed_image, &parsed_image2);
456 parsed_image2 = (
void *)!NULL;
457 pick_and_place_parse_file_to_images(fd, &parsed_image, &parsed_image2);
458 parsed_image2 = NULL;
462 parsed_image2 = (
void *)!NULL;
463 pick_and_place_parse_file_to_images(fd, &parsed_image2, &parsed_image);
464 parsed_image2 = NULL;
467 GERB_COMPILE_ERROR(_(
"%s: unknown pick-and-place board side to reload"), filename);
474 dprintf(
"Most likely found a RS-274D file...trying to open anyways\n");
475 g_warning(_(
"Most likely found a RS-274D file...trying to open anyways\n"));
476 if (!foundBinary || forceLoadFile) {
479 gchar *currentLoadDirectory = g_path_get_dirname (filename);
480 parsed_image =
parse_gerb(fd, currentLoadDirectory);
481 g_free (currentLoadDirectory);
485 dprintf(
"Unknown filetype");
486 GERB_COMPILE_ERROR(_(
"%s: Unknown file type."), filename);
491 if (parsed_image == NULL) {
497 gchar *baseName = g_path_get_basename (filename);
498 gchar *displayedName;
500 displayedName = g_strconcat (baseName, _(
" (top)"), NULL);
502 displayedName = g_strdup (baseName);
503 retv = gerbv_add_parsed_image_to_project (gerbvProject, parsed_image, filename, displayedName, idx, reload);
505 g_free (displayedName);
515 gchar *baseName = g_path_get_basename (filename);
516 gchar *displayedName;
517 displayedName = g_strconcat (baseName, _(
" (bottom)"), NULL);
518 retv = gerbv_add_parsed_image_to_project (gerbvProject, parsed_image2, filename, displayedName, idx + 1, reload);
520 g_free (displayedName);
531 fd = gerb_fopen(filename);
533 GERB_MESSAGE(_(
"Trying to open %s: %s"), filename, strerror(errno));
536 gchar *currentLoadDirectory = g_path_get_dirname (filename);
537 returnImage =
parse_gerb(fd, currentLoadDirectory);
538 g_free (currentLoadDirectory);
547 double x1=HUGE_VAL,y1=HUGE_VAL, x2=-HUGE_VAL,y2=-HUGE_VAL;
550 gdouble minX, minY, maxX, maxY;
567 if (!isnormal(minX)||!isnormal(minY)||!isnormal(maxX)||!isnormal(maxY)){
572 cairo_matrix_t fullMatrix;
573 cairo_matrix_init (&fullMatrix, 1, 0, 0, 1, 0, 0);
584 cairo_matrix_scale (&fullMatrix, scaleX, scaleY);
587 cairo_matrix_transform_point (&fullMatrix, &minX, &minY);
588 cairo_matrix_transform_point (&fullMatrix, &maxX, &maxY);
601 boundingbox->
left = x1;
602 boundingbox->
right = x2;
603 boundingbox->
top = y1;
611 double width, height;
612 double x_scale, y_scale;
615 gerbv_render_get_boundingbox(gerbvProject, &bb);
624 if (!isnormal(width)||!isnormal(height)||((width < 0.01) && (height < 0.01))) {
657 gerbv_render_get_boundingbox(gerbvProject, &bb);
666 gerbv_render_to_pixmap_using_gdk (
gerbv_project_t *gerbvProject, GdkPixmap *pixmap,
668 GdkColor *selectionColor){
669 GdkGC *gc = gdk_gc_new(pixmap);
670 GdkPixmap *colorStamp, *clipmask;
677 gdk_colormap_alloc_color(gdk_colormap_get_system(), &gerbvProject->
background, FALSE, TRUE);
678 gdk_gc_set_foreground(gc, &gerbvProject->
background);
679 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, -1, -1);
684 colorStamp = gdk_pixmap_new(pixmap, renderInfo->
displayWidth,
686 clipmask = gdk_pixmap_new(NULL, renderInfo->
displayWidth,
700 gdk_colormap_alloc_color(gdk_colormap_get_system(), &gerbvProject->
file[i]->
color, FALSE, TRUE);
701 gdk_gc_set_foreground(gc, &gerbvProject->
file[i]->
color);
705 gdk_gc_set_function(gc, GDK_COPY);
706 gdk_draw_rectangle(colorStamp, gc, TRUE, 0, 0, -1, -1);
709 gdk_gc_set_function(gc, GDK_COPY);
712 gdk_gc_set_function(gc, GDK_XOR);
718 dprintf(
" .... calling image2pixmap on image %d...\n", i);
720 draw_gdk_image_to_pixmap(&clipmask, gerbvProject->
file[i]->
image,
723 DRAW_IMAGE, NULL, renderInfo, gerbvProject->
file[i]->
transform);
730 gdk_gc_set_clip_mask(gc, clipmask);
731 gdk_gc_set_clip_origin(gc, 0, 0);
732 gdk_draw_drawable(pixmap, gc, colorStamp, 0, 0, 0, 0, -1, -1);
733 gdk_gc_set_clip_mask(gc, NULL);
738 if (!selectionColor->pixel)
739 gdk_colormap_alloc_color(gdk_colormap_get_system(), selectionColor, FALSE, TRUE);
741 gdk_gc_set_foreground(gc, selectionColor);
742 gdk_gc_set_function(gc, GDK_COPY);
743 gdk_draw_rectangle(colorStamp, gc, TRUE, 0, 0, -1, -1);
748 if (selectionInfo->selectedNodeArray->len > 0) {
749 gerbv_selection_item_t sItem = g_array_index (selectionInfo->selectedNodeArray,
750 gerbv_selection_item_t, 0);
754 if (gerbvProject->
file[j] && (gerbvProject->
file[j]->
image == matchImage)) {
755 draw_gdk_image_to_pixmap(&clipmask, gerbvProject->
file[j]->
image,
758 DRAW_SELECTIONS, selectionInfo,
762 gdk_gc_set_clip_mask(gc, clipmask);
763 gdk_gc_set_clip_origin(gc, 0, 0);
764 gdk_draw_drawable(pixmap, gc, colorStamp, 0, 0, 0, 0, -1, -1);
765 gdk_gc_set_clip_mask(gc, NULL);
769 gdk_pixmap_unref(colorStamp);
770 gdk_pixmap_unref(clipmask);
776 gerbv_render_all_layers_to_cairo_target_for_vector_output (
gerbv_project_t *gerbvProject,
779 gerbv_render_cairo_set_scale_and_translation(cr, renderInfo);
784 gerbv_render_layer_to_cairo_target_without_transforming(cr, gerbvProject->
file[i], renderInfo, FALSE);
791 gerbv_render_all_layers_to_cairo_target (
gerbv_project_t *gerbvProject, cairo_t *cr,
795 cairo_set_source_rgba (cr, (
double) gerbvProject->
background.red/G_MAXUINT16,
796 (
double) gerbvProject->
background.green/G_MAXUINT16,
797 (
double) gerbvProject->
background.blue/G_MAXUINT16, 1);
801 cairo_push_group (cr);
803 cairo_pop_group_to_source (cr);
804 cairo_paint_with_alpha (cr, (
double) gerbvProject->
file[i]->
alpha/G_MAXUINT16);
813 gerbv_render_cairo_set_scale_and_translation(cr, renderInfo);
814 gerbv_render_layer_to_cairo_target_without_transforming(cr, fileInfo, renderInfo, TRUE);
819 gerbv_render_cairo_set_scale_and_translation(cairo_t *cr,
gerbv_render_info_t *renderInfo){
820 gdouble translateX, translateY;
828 cairo_set_tolerance (cr, 1.0);
829 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
832 cairo_set_tolerance (cr, 0.1);
833 cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
839 cairo_translate (cr, -translateX, translateY + renderInfo->
displayHeight);
848 cairo_set_source_rgba (cr, (
double) fileInfo->
color.red/G_MAXUINT16,
849 (
double) fileInfo->
color.green/G_MAXUINT16,
850 (
double) fileInfo->
color.blue/G_MAXUINT16, 1);
855 draw_image_to_cairo_target (cr, fileInfo->
image,
857 renderInfo, TRUE, fileInfo->
transform, pixelOutput);
862 gerbv_attribute_destroy_HID_attribute (gerbv_HID_Attribute *attributeList,
int n_attr)
867 for (i = 0 ; i < n_attr ; i++) {
868 if ( (attributeList[i].type == HID_String ||
869 attributeList[i].type == HID_Label) &&
870 attributeList[i].default_val.str_value != NULL) {
871 free (attributeList[i].default_val.str_value);
876 if (attributeList != NULL) {
877 free (attributeList);
883 gerbv_HID_Attribute *
884 gerbv_attribute_dup (gerbv_HID_Attribute *attributeList,
int n_attr)
886 gerbv_HID_Attribute *nl;
889 nl = (gerbv_HID_Attribute *) malloc (n_attr *
sizeof (gerbv_HID_Attribute));
891 fprintf (stderr, _(
"%s(): malloc failed\n"), __FUNCTION__);
896 for (i = 0 ; i < n_attr ; i++) {
898 if (attributeList[i].type == HID_String ||
899 attributeList[i].type == HID_Label) {
901 if (attributeList[i].default_val.str_value != NULL) {
902 nl[i].default_val.str_value = strdup (attributeList[i].default_val.str_value);
904 nl[i].default_val.str_value = NULL;
908 nl[i] = attributeList[i];