Exemple #1
0
gboolean
PyDia_export_data(DiagramData *data, DiaContext *ctx,
		  const gchar *filename, const gchar *diafilename, void* user_data)
{
  DiaPyRenderer *renderer;

  {
    FILE *file;
    file = g_fopen(filename, "w"); /* "wb" for binary! */

    if (file == NULL) {
      dia_context_add_message_with_errno(ctx, errno, _("Couldn't open '%s' for writing.\n"), 
				         dia_context_get_filename(ctx));
      return FALSE;
    }
    else
      fclose (file);
  }

  renderer = g_object_new (DIA_TYPE_PY_RENDERER, NULL);

  renderer->filename = g_strdup (filename);
  renderer->diagram_data = PyDiaDiagramData_New(data);

  /* The Python Renderer object was created at PyDia_Register */
  renderer->self = (PyObject*)user_data;

  /* this will call the required callback functions above */
  data_render(data, DIA_RENDERER(renderer), NULL, NULL, NULL);

  g_object_unref(renderer);

  return TRUE;
}
Exemple #2
0
Fichier : wpg.c Projet : mpuels/dia
/* dia export funtion */
static gboolean
export_data(DiagramData *data, DiaContext *ctx,
	    const gchar *filename, const gchar *diafilename,
	    void* user_data)
{
  WpgRenderer *renderer;
  FILE *file;
  Rectangle *extent;
  real width, height;

  file = g_fopen(filename, "wb"); /* "wb" for binary! */

  if (file == NULL) {
    dia_context_add_message_with_errno (ctx, errno, _("Can't open output file %s"), 
					dia_context_get_filename(ctx));
    return FALSE;
  }

  renderer = g_object_new (WPG_TYPE_RENDERER, NULL);

  renderer->file = file;
  renderer->ctx = ctx;

  extent = &data->extents;

  /* use extents */
  DIAG_NOTE(g_message("export_data extents %f,%f -> %f,%f", 
            extent->left, extent->top, extent->right, extent->bottom));

  width  = extent->right - extent->left;
  height = extent->bottom - extent->top;
#if 0
  /* extend to use full range */
  renderer->Scale = 0.001;
  if (width > height)
    while (renderer->Scale * width < 3276.7) renderer->Scale *= 10.0;
  else
    while (renderer->Scale * height < 3276.7) renderer->Scale *= 10.0;
#else
  /* scale from Dia's cm to WPU (1/1200 inch) */
  renderer->Scale = WPU_PER_DCM;
  /* avoid int16 overflow */
  if (width > height)
    while (renderer->Scale * width > 32767) renderer->Scale /= 10.0;
  else
    while (renderer->Scale * height > 32767) renderer->Scale /= 10.0;
  renderer->XOffset = - extent->left;
  renderer->YOffset =   extent->bottom;
#endif
  renderer->Box.Width  = width * renderer->Scale;
  renderer->Box.Height = height * renderer->Scale;
  renderer->Box.Flag   = 0;
  renderer->Box.Version = 0;

  data_render(data, DIA_RENDERER(renderer), NULL, NULL, NULL);

  g_object_unref(renderer);

  return TRUE;
}
Exemple #3
0
static gboolean
export_render_eps(DiaPsRenderer *renderer, 
		  DiagramData *data, DiaContext *ctx,
		  const gchar *filename, const gchar *diafilename,
		  void* user_data)
{
  FILE *outfile;

  outfile = g_fopen(filename, "w");
  if (outfile == NULL) {
    dia_context_add_message_with_errno (ctx, errno, _("Can't open output file %s"), 
					dia_context_get_filename(ctx));
    return FALSE;
  }
  renderer->file = outfile;
  renderer->scale = 28.346 * data->paper.scaling;
  renderer->extent = data->extents;
  renderer->pstype = (guint)user_data;
  if (renderer->pstype & PSTYPE_EPSI) {
    /* Must store the diagram for making a preview */
    renderer->diagram = data;
  }

  if (renderer->file) {
    renderer->title = g_strdup (diafilename);

    data_render(data, DIA_RENDERER(renderer), NULL, NULL, NULL);
  }
  fclose(outfile);

  return TRUE;
}
Exemple #4
0
static void
end_render(DiaRenderer *self)
{
    WmfRenderer *renderer = WMF_RENDERER (self);
    W32::HENHMETAFILE hEmf;
    W32::UINT nSize;
    W32::BYTE* pData = NULL;
    FILE* f;

    DIAG_NOTE(renderer, "end_render\n");
    hEmf = W32::CloseEnhMetaFile(renderer->hFileDC);

#if !defined DIRECT_WMF && defined G_OS_WIN32 /* the later offers both */
    /* Don't do it when printing */
    if (renderer->sFileName && strlen (renderer->sFileName)) {

        W32::HDC hdc = W32::GetDC(NULL);

        f = g_fopen(renderer->sFileName, "wb");

	if (renderer->target_emf) {
	  nSize = W32::GetEnhMetaFileBits (hEmf, 0, NULL);
	  pData = g_new(W32::BYTE, nSize);
	  nSize = W32::GetEnhMetaFileBits (hEmf, nSize, pData);
	} else {
	  /* write the placeable header */
	  fwrite(&renderer->pmh, 1, 22 /* NOT: sizeof(PLACEABLEMETAHEADER) */, f);
	  /* get size */
	  nSize = W32::GetWinMetaFileBits(hEmf, 0, NULL, MM_ANISOTROPIC, hdc);
	  pData = g_new(W32::BYTE, nSize);
	  /* get data */
	  nSize = W32::GetWinMetaFileBits(hEmf, nSize, pData, MM_ANISOTROPIC, hdc);
	}

	/* write file */
	if (fwrite(pData,1,nSize,f) != nSize)
	  dia_context_add_message_with_errno(renderer->ctx, errno, _("Couldn't write file %s\n%s"),
					     dia_context_get_filename (renderer->ctx)); 
	fclose(f);
    
	g_free(pData);

        W32::ReleaseDC(NULL, hdc);
    } else {
        W32::PlayEnhMetaFile (renderer->hPrintDC, hEmf, &renderer->margins);
    }
#endif
    g_free(renderer->sFileName);

    if (hEmf)
	W32::DeleteEnhMetaFile(hEmf);
    if (renderer->hFont)
	W32::DeleteObject(renderer->hFont);
    if (renderer->pango_context)
        g_object_unref (renderer->pango_context);
}
Exemple #5
0
/* plug-in interface : export function */
static gboolean
export_data(DiagramData *data, DiaContext *ctx,
	    const gchar *filename, const gchar *diafilename,
	    void* user_data)
{
    HpglRenderer *renderer;
    FILE *file;
    Rectangle *extent;
    real width, height;

    file = g_fopen(filename, "w"); /* "wb" for binary! */

    if (!file) {
	dia_context_add_message_with_errno(ctx, errno, _("Can't open output file %s."), 
					   dia_context_get_filename(ctx));
	return FALSE;
    }

    renderer = g_object_new(HPGL_TYPE_RENDERER, NULL);

    renderer->file = file;

    extent = &data->extents;

    /* use extents */
    DIAG_NOTE(g_message("export_data extents %f,%f -> %f,%f", 
              extent->left, extent->top, extent->right, extent->bottom));

    width  = extent->right - extent->left;
    height = extent->bottom - extent->top;
    renderer->scale = 0.001;
    if (width > height)
        while (renderer->scale * width < 3276.7) renderer->scale *= 10.0;
    else
        while (renderer->scale * height < 3276.7) renderer->scale *= 10.0;
    renderer->offset = 0.0; /* just to have one */

    renderer->size.x = width * renderer->scale;
    renderer->size.y = height * renderer->scale;
#if 0
    /* OR: set page size and scale */
    fprintf(renderer->file, "PS0;SC%d,%d,%d,%d;\n",
            hpgl_scale(renderer, extent->left),
            hpgl_scale(renderer, extent->right),
            hpgl_scale(renderer, extent->bottom),
            hpgl_scale(renderer, extent->top));
#endif
    data_render(data, DIA_RENDERER(renderer), NULL, NULL, NULL);

    g_object_unref(renderer);

    return TRUE;
}
Exemple #6
0
static gboolean
import_data (const gchar *filename, DiagramData *data, DiaContext *ctx, void* user_data)
{
  DiaObjectType *otype = object_get_type("Standard - Image");
  gint width, height;

  if (!otype) /* this would be really broken */
    return FALSE;

  if (!user_data) {
    dia_context_add_message(ctx, _("Calling error, missing user_data."));
    return FALSE;
  }

  if (gdk_pixbuf_get_file_info (filename, &width, &height))
    {
      DiaObject *obj;
      Handle *h1, *h2;
      Point point;
      point.x = point.y = 0.0;

      obj = otype->ops->create(&point, otype->default_user_data, &h1, &h2);
      if (obj)
        {
          GPtrArray *plist = g_ptr_array_new ();

          prop_list_add_filename (plist, "image_file", filename);
          prop_list_add_real (plist, "elem_width", width / 20.0);
          prop_list_add_real (plist, "elem_height", height / 20.0);

          obj->ops->set_props(obj, plist);
          prop_list_free (plist);

          layer_add_object(data->active_layer, obj);
          return TRUE;
        }
    }
  else
    {
      dia_context_add_message(ctx, _("Pixbuf[%s] can't load:\n%s"), 
			      (gchar*)user_data, dia_context_get_filename(ctx));
    }

  return FALSE;
}
Exemple #7
0
gboolean
import_pdf(const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data)
{
  PDFDoc *doc;
  GooString *fileName = new GooString(filename);
  // no passwords yet
  GooString *ownerPW = NULL;
  GooString *userPW = NULL;
  gboolean ret = FALSE;

  // without this we will get strange crashes (at least with /O2 build)
  globalParams = new GlobalParams();

  doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
  if (!doc->isOk()) {
    dia_context_add_message (ctx, _("PDF document not OK.\n%s"),
			     dia_context_get_filename (ctx));
  } else {
    DiaOutputDev *diaOut = new DiaOutputDev(dia, doc->getNumPages());

    for (int pg = 1; pg <= doc->getNumPages(); ++pg) {
      Page *page = doc->getPage (pg);
      if (!page || !page->isOk())
        continue;
      doc->displayPage(diaOut, pg,
		       72.0, 72.0, /* DPI, scaling elsewhere */
		       0, /* rotate */
		       gTrue, /* useMediaBox */
		       gTrue, /* Crop */
		       gFalse /* printing */
		       );
    }
    delete diaOut;
    ret = TRUE;
  }
  delete doc;
  delete globalParams;
  delete fileName;

  return ret;
}
static DiaObject *
_dae_load (ObjectNode obj_node, int version, DiaContext *ctx)
{
  DiaObject *obj;
  DiagramAsElement *dae;
 
  obj = object_load_using_properties (&diagram_as_element_type,
                                       obj_node, version, ctx);
  /* filename de-normalization */
  dae = (DiagramAsElement*)obj;
  if (strlen(dae->filename) && !g_path_is_absolute (dae->filename)) {
    gchar *dirname = g_path_get_dirname (dia_context_get_filename(ctx));
    gchar *fname = g_build_filename (dirname, dae->filename, NULL);
    g_free (dae->filename);
    dae->filename = fname;
    g_free (dirname);

    /* need to update again with new filenames */
    _dae_update_data(dae);
  }
  return obj;
}
Exemple #9
0
static void
_dae_save (DiaObject *obj, ObjectNode obj_node, DiaContext *ctx)
{
  DiagramAsElement *dae;
  /* filename normalization */
  gchar *saved_path = NULL;

  dae = (DiagramAsElement*)obj;
  if (strlen(dae->filename) && g_path_is_absolute (dae->filename)) {
    gchar *dirname = g_path_get_dirname (dia_context_get_filename (ctx));
    if (strstr (dae->filename, dirname) == dae->filename) {
      saved_path = dae->filename;
      dae->filename += (strlen (dirname) + g_str_has_suffix (dirname, G_DIR_SEPARATOR_S) ? 0 : 1);
    }
    g_free (dirname);
  }
  object_save_using_properties (obj, obj_node, ctx);

  if (saved_path) {
    dae->filename = saved_path;
  }
}
Exemple #10
0
static gboolean
export_data(DiagramData *data, DiaContext *ctx,
	    const gchar *filename, const gchar *diafilename,
	    void* user_data)
{
  DiaCairoRenderer *renderer;
  GdkColor color;
  int width, height;
  GdkPixbuf* pixbuf = NULL;
  GError* error = NULL;
  Rectangle rect;
  real zoom = 1.0;
  cairo_t *cctx;
  const char* format = (const char*)user_data;

  rect.left = data->extents.left;
  rect.top = data->extents.top;
  rect.right = data->extents.right;
  rect.bottom = data->extents.bottom;

  /* quite arbitrary */
  zoom = 20.0 * data->paper.scaling; 
  /* Adding a bit of padding to account for rounding errors.  Better to
   * pad than to clip.  See bug #413275 */
  width = ceil((rect.right - rect.left) * zoom) + 1;
  height = ceil((rect.bottom - rect.top) * zoom) + 1;

  renderer = g_object_new (dia_cairo_renderer_get_type(), NULL);
  renderer->scale = zoom;
  renderer->surface = cairo_surface_reference (cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                                  width, height));

  cctx = cairo_create (renderer->surface);

  /* draw background */
  color_convert (&data->bg_color, &color);
  gdk_cairo_set_source_color (cctx, &color);
  cairo_rectangle (cctx, 0, 0, width, height);
  cairo_fill (cctx);

  data_render (data, DIA_RENDERER (renderer), NULL, NULL, NULL);

  #if GTK_CHECK_VERSION(3,0,0)
  pixbuf = gdk_pixbuf_get_from_surface (renderer->surface, 0, 0,
                                        width, height);
  #else
  {
    GdkPixmap *pixmap;
    cairo_t *cr;

    pixmap = gdk_pixmap_new (NULL, width, height, 24);
    cr = gdk_cairo_create (pixmap);

    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_surface (cr, renderer->surface, 0, 0);
    cairo_paint (cr);
    pixbuf = gdk_pixbuf_get_from_drawable (NULL,
                                           pixmap,
                                           gdk_colormap_get_system (),
                                           0, 0, 0, 0, width, height);
  }
  #endif
  if (pixbuf)
    {
      gdk_pixbuf_save (pixbuf, filename, format, &error, NULL);
      g_object_unref (pixbuf);
    }
  else
    {
      dia_context_add_message(ctx, _("Failed to create pixbuf from drawable."));
    }

  if (error)
    {
      dia_context_add_message(ctx, _("Could not save file:\n%s\n%s"),
		              dia_context_get_filename(ctx),
			      error->message);
      g_error_free (error);
    }

  g_object_unref (renderer);

  return TRUE;
}
Exemple #11
0
/* imports the given fig-file, returns TRUE if successful */
static gboolean 
import_fig(const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data)
{
    FILE *figfile;
    char buf[BUFLEN];
    int figmajor, figminor;	
    int i;

    for (i = 0; i < FIG_MAX_USER_COLORS; i++) {
	fig_colors[i] = color_black;
    }
    for (i = 0; i < FIG_MAX_DEPTHS; i++) {
	depths[i] = NULL;
    }

    figfile = g_fopen(filename,"r");
    if (figfile == NULL) {
	dia_context_add_message_with_errno(ctx, errno, _("Couldn't open: '%s' for reading.\n"), 
		                dia_context_get_filename(ctx));
	return FALSE;
    }
  
    /* First check magic bytes */
    if (fgets(buf, BUFLEN, figfile) == NULL ||
        sscanf(buf, "#FIG %d.%d\n", &figmajor, &figminor) != 2)
    {
	dia_context_add_message_with_errno(ctx, errno, _("Doesn't look like a Fig file"));
	fclose(figfile);
	return FALSE;
    }
	
    if (figmajor != 3 || figminor != 2) {
	dia_context_add_message(ctx, _("This is a Fig version %d.%d file.\n It may not be importable."), 
				figmajor, figminor);
    }

    figversion = figmajor*100+figminor;

    if (!skip_comments(figfile)) {
	if (!feof(figfile)) {
	    dia_context_add_message_with_errno(ctx, errno, _("Error reading Fig file."));
	} else {
	    dia_context_add_message(ctx, _("Premature end of Fig file"));
	}
	fclose(figfile);
	return FALSE;
    }

    if (!fig_read_meta_data(figfile, dia, ctx)) {
	fclose(figfile);
	return FALSE;
    }
  
    compound_stack = NULL;

    do {
	if (!skip_comments(figfile)) {
	    if (!feof(figfile)) {
		dia_context_add_message_with_errno(ctx, errno, _("Error reading Fig file."));
	    } else {
		break;
	    }
	}
	if (! fig_read_object(figfile, ctx)) {
	    fclose(figfile);
	    break;
	}
    } while (TRUE);

    /* Now we can reorder for the depth fields */
    for (i = 0; i < FIG_MAX_DEPTHS; i++) {
	if (depths[i] != NULL)
	    layer_add_objects_first(dia->active_layer, depths[i]);
    }
    return TRUE;
}
Exemple #12
0
static DiaObject *
image_load(ObjectNode obj_node, int version, DiaContext *ctx)
{
  Image *image;
  Element *elem;
  DiaObject *obj;
  int i;
  AttributeNode attr;
  char *diafile_dir;
  
  image = g_malloc0(sizeof(Image));
  elem = &image->element;
  obj = &elem->object;
  
  obj->type = &image_type;
  obj->ops = &image_ops;

  element_load(elem, obj_node, ctx);
  
  image->border_width = 0.1;
  attr = object_find_attribute(obj_node, "border_width");
  if (attr != NULL)
    image->border_width =  data_real(attribute_first_data(attr), ctx);

  image->border_color = color_black;
  attr = object_find_attribute(obj_node, "border_color");
  if (attr != NULL)
    data_color(attribute_first_data(attr), &image->border_color, ctx);
  
  image->line_style = LINESTYLE_SOLID;
  attr = object_find_attribute(obj_node, "line_style");
  if (attr != NULL)
    image->line_style =  data_enum(attribute_first_data(attr), ctx);

  image->dashlength = DEFAULT_LINESTYLE_DASHLEN;
  attr = object_find_attribute(obj_node, "dashlength");
  if (attr != NULL)
    image->dashlength = data_real(attribute_first_data(attr), ctx);

  image->draw_border = TRUE;
  attr = object_find_attribute(obj_node, "draw_border");
  if (attr != NULL)
    image->draw_border =  data_boolean(attribute_first_data(attr), ctx);

  image->keep_aspect = TRUE;
  attr = object_find_attribute(obj_node, "keep_aspect");
  if (attr != NULL)
    image->keep_aspect =  data_boolean(attribute_first_data(attr), ctx);

  attr = object_find_attribute(obj_node, "file");
  if (attr != NULL) {
    image->file =  data_filename(attribute_first_data(attr), ctx);
  } else {
    image->file = g_strdup("");
  }

  element_init(elem, 8, NUM_CONNECTIONS);

  for (i=0;i<NUM_CONNECTIONS;i++) {
    obj->connections[i] = &image->connections[i];
    image->connections[i].object = obj;
    image->connections[i].connected = NULL;
  }
  image->connections[8].flags = CP_FLAGS_MAIN;

  image->image = NULL;
  
  if (strcmp(image->file, "")!=0) {
    diafile_dir = get_directory(dia_context_get_filename(ctx));

    if (g_path_is_absolute(image->file)) { /* Absolute pathname */
      image->image = dia_image_load(image->file);
      if (image->image == NULL) {
	/* Not found as abs path, try in same dir as diagram. */
	char *temp_string;
	const char *image_file_name = image->file;
	const char *psep;

	psep = strrchr(image->file, G_DIR_SEPARATOR);
	/* try the other G_OS as well */
	if (!psep)
	  psep =  strrchr(image->file, G_DIR_SEPARATOR == '/' ? '\\' : '/');
	if (psep)
	  image_file_name = psep + 1;

	temp_string = g_build_filename(diafile_dir, image_file_name, NULL);

	image->image = dia_image_load(temp_string);

	if (image->image != NULL) {
	  /* Found file in same dir as diagram. */
	  message_warning(_("The image file '%s' was not found in the specified directory.\n"
			  "Using the file '%s' instead.\n"), image->file, temp_string);
	  g_free(image->file);
	  image->file = temp_string;
	} else {
	  g_free(temp_string);
	  
	  image->image = dia_image_load((char *)image_file_name);
	  if (image->image != NULL) {
	    char *tmp;
	    /* Found file in current dir. */
	    message_warning(_("The image file '%s' was not found in the specified directory.\n"
			    "Using the file '%s' instead.\n"), image->file, image_file_name);
	    tmp = image->file;
	    image->file = g_strdup(image_file_name);
	    g_free(tmp);
	  } else {
	    message_warning(_("The image file '%s' was not found.\n"),
			    image_file_name);
	  }
	}
      }
    } else { /* Relative pathname: */
      char *temp_string;

      temp_string = g_build_filename (diafile_dir, image->file, NULL);

      image->image = dia_image_load(temp_string);

      if (image->image != NULL) {
	/* Found file in same dir as diagram. */
	g_free(image->file);
	image->file = temp_string;
      } else {
	g_free(temp_string);
	  
	image->image = dia_image_load(image->file);
	if (image->image == NULL) {
	  /* Didn't find file in current dir. */
	  message_warning(_("The image file '%s' was not found.\n"),
			  image->file);
	}
      }
    }
    g_free(diafile_dir);
  }
  /* if we don't have an image yet try to recover it from inlined data */
  if (!image->image) {
    attr = object_find_attribute(obj_node, "pixbuf");
    if (attr != NULL) {
      GdkPixbuf *pixbuf = data_pixbuf (attribute_first_data(attr));

      if (pixbuf) {
	image->image = dia_image_new_from_pixbuf (pixbuf);
	image->inline_data = TRUE; /* avoid loosing it */
	/* FIXME: should we reset the filename? */
	g_object_unref (pixbuf);
      }
    }
  }

  /* update mtime */
  {
    struct stat st;
    if (g_stat (image->file, &st) != 0)
      st.st_mtime = 0;

    image->mtime = st.st_mtime;
  }
  image_update_data(image);

  return &image->element.object;
}
Exemple #13
0
/*!
 * \brief Fallback implementation for not well-formed diagram files
 *
 * If all files produced by dia were good XML files, we wouldn't have to do 
 *  this little gymnastic. Alas, during the libxml1 days, we were outputting 
 *  files with no encoding specification (which means UTF-8 if we're in an
 *  asciish encoding) and strings encoded in local charset (so, we wrote
 *  broken files). 
 *
 *  The following logic finds if we have a broken file, and attempts to fix 
 *  it if it's possible. If the file is correct or is unrecognisable, we pass
 *  it untouched to libxml2.
 * @param filename The name of the file to check.
 * @param default_enc The default encoding to use if none is given.
 * @param ctx The context in which this function is called
 * @return The filename given if it seems ok, or the name of a new file
 *          with fixed contents, or NULL if we couldn't read the file.  The
 *          caller should free this string and unlink the file if it is not
 *          the same as `filename'.
 * @bug The many gzclose-g_free-return sequences should be refactored into
 *       an "exception handle" (goto+label). At least for people who think goto is
 *       better than this. I dont. --hb
 * \ingroup DiagramXmlIo
 */
static const gchar *
xml_file_check_encoding(const gchar *filename, const gchar *default_enc, DiaContext *ctx)
{
  int fd = g_open (filename, O_RDONLY, 0);
  /* If the next call exits the program (without any message) check if
   * you are loading an incompatible version of zlib*.dll, e.g. one
   * built against a newer version of msvcrt*.dll
   */
  gzFile zf = gzdopen(fd,"rb");  
  gchar *buf;
  gchar *p,*pmax;
  int len;
  gchar *tmp,*res;
  int uf;
  gboolean well_formed_utf8;
  int write_ok;

  static char magic_xml[] = 
  {0x3c,0x3f,0x78,0x6d,0x6c,0x00}; /* "<?xml" in ASCII */

  if (!zf) {
    dia_log_message("%s can not be opened for encoding check (%s)", filename, fd > 0 ? "gzdopen" : "g_open");
    /* XXX perhaps we can just chicken out to libxml ? -- CC */
    return filename;
  }
  p = buf = g_malloc0(BUFLEN);  
  len = gzread(zf,buf,BUFLEN);
  pmax = p + len;

  /* first, we expect the magic <?xml string */
  if ((0 != strncmp(p,magic_xml,5)) || (len < 5)) {
    gzclose(zf);
    g_free(buf);
    return filename; /* let libxml figure out what this is. */
  }
  /* now, we're sure we have some asciish XML file. */
  p += 5;
  while (((*p == 0x20)||(*p == 0x09)||(*p == 0x0d)||(*p == 0x0a))
         && (p<pmax)) p++;
  if (p>=pmax) { /* whoops ? */
    gzclose(zf);
    g_free(buf);
    return filename;
  }
  if (0 != strncmp(p,"version=\"",9)) {
    gzclose(zf); /* chicken out. */
    g_free(buf);
    return filename;
  }
  p += 9;
  /* The header is rather well formed. */
  if (p>=pmax) { /* whoops ? */
    gzclose(zf);
    g_free(buf);
    return filename;
  }
  while ((*p != '"') && (p < pmax)) p++;
  p++;
  while (((*p == 0x20)||(*p == 0x09)||(*p == 0x0d)||(*p == 0x0a))
         && (p<pmax)) p++;
  if (p>=pmax) { /* whoops ? */
    gzclose(zf);
    g_free(buf);
    return filename;
  }
  if (0 == strncmp(p,"encoding=\"",10)) {
    gzclose(zf); /* this file has an encoding string. Good. */
    g_free(buf);
    return filename;
  }
  /* now let's read the whole file, to see if there are offending bits.
   * We can call it well formed UTF-8 if the highest isn't used
   */
  well_formed_utf8 = TRUE;
  do {
    int i;
    for (i = 0; i < len; i++)
      if (buf[i] & 0x80 || buf[i] == '&')
        well_formed_utf8 = FALSE;
    len = gzread(zf,buf,BUFLEN);
  } while (len > 0 && well_formed_utf8);
  if (well_formed_utf8) {
    gzclose(zf); /* this file is utf-8 compatible  */
    g_free(buf);
    return filename;
  } else {
    gzclose(zf); /* poor man's fseek */
    fd = g_open (filename, O_RDONLY, 0);
    zf = gzdopen(fd,"rb"); 
    len = gzread(zf,buf,BUFLEN);
  }

  if (0 != strcmp(default_enc,"UTF-8")) {
    dia_context_add_message (ctx, 
                             _("The file %s has no encoding specification;\n"
			     "assuming it is encoded in %s"),
			     dia_context_get_filename(ctx), default_enc);
  } else {
    gzclose(zf); /* we apply the standard here. */
    g_free(buf);
    return filename;
  }

  tmp = getenv("TMP"); 
  if (!tmp) tmp = getenv("TEMP");
  if (!tmp) tmp = "/tmp";

  res = g_strconcat(tmp,G_DIR_SEPARATOR_S,"dia-xml-fix-encodingXXXXXX",NULL);
  uf = g_mkstemp(res);
  write_ok = (uf > 0);
  write_ok = write_ok && (write(uf,buf,p-buf) > 0);
  write_ok = write_ok && (write(uf," encoding=\"",11) > 0);
  write_ok = write_ok && (write(uf,default_enc,strlen(default_enc)) > 0);
  write_ok = write_ok && (write(uf,"\" ",2) > 0);
  write_ok = write_ok && (write(uf,p,pmax - p) > 0);

  while (write_ok) {
    len = gzread(zf,buf,BUFLEN);
    if (len <= 0) break;
    write_ok = write_ok && (write(uf,buf,len) > 0);
  }
  gzclose(zf);
  if (uf > 0)
    close(uf);
  g_free(buf);
  if (!write_ok) {
    g_free(res);
    res = NULL;
  }
  return res; /* caller frees the name and unlinks the file. */
}
Exemple #14
0
static gboolean
export_dxf(DiagramData *data, DiaContext *ctx,
	   const gchar *filename, const gchar *diafilename,
           void* user_data)
{
    DxfRenderer *renderer;
    FILE *file;
    int i;
    Layer *layer;
    gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
    gchar buf2[G_ASCII_DTOSTR_BUF_SIZE];

    file = g_fopen(filename, "w");

    if (file == NULL) {
	dia_context_add_message_with_errno (ctx, errno, _("Can't open output file %s"),
					    dia_context_get_filename(ctx));
	return FALSE;
    }

    renderer = g_object_new(DXF_TYPE_RENDERER, NULL);

    renderer->file = file;

    /* drawing limits */
    fprintf(file, "  0\nSECTION\n  2\nHEADER\n");
    fprintf(file, "  9\n$EXTMIN\n 10\n%s\n 20\n%s\n",
      g_ascii_formatd (buf, sizeof(buf), "%g", data->extents.left),
      g_ascii_formatd (buf2, sizeof(buf2), "%g", -data->extents.bottom));
    fprintf(file, "  9\n$EXTMAX\n 10\n%s\n 20\n%s\n",
      g_ascii_formatd (buf, sizeof(buf), "%g", data->extents.right),
      g_ascii_formatd (buf2, sizeof(buf2), "%g", -data->extents.top));
    fprintf(file, "  0\nENDSEC\n");

    /* write layer description */
    fprintf(file,"  0\nSECTION\n  2\nTABLES\n  0\nTABLE\n");
    /* some dummy entry to make it work for more DXF viewers */
    fprintf(file,"  2\nLAYER\n 70\n255\n");

    for (i=0; i<data->layers->len; i++) {
      layer = (Layer *) g_ptr_array_index(data->layers, i);
      fprintf(file,"  0\nLAYER\n  2\n%s\n",layer->name);
      if(layer->visible)
	fprintf(file," 62\n%d\n",i+1);
      else
        fprintf(file," 62\n%d\n",(-1)*(i+1));
    }
    fprintf(file, "  0\nENDTAB\n  0\nENDSEC\n");

    /* write graphics */
    fprintf(file,"  0\nSECTION\n  2\nENTITIES\n");

    init_attributes(renderer);

    DIA_RENDERER_GET_CLASS(renderer)->begin_render(DIA_RENDERER(renderer), NULL);

    for (i=0; i<data->layers->len; i++) {
        layer = (Layer *) g_ptr_array_index(data->layers, i);
	    renderer->layername = layer->name;
        layer_render(layer, DIA_RENDERER(renderer), NULL, NULL, data, 0);
    }

    DIA_RENDERER_GET_CLASS(renderer)->end_render(DIA_RENDERER(renderer));

    g_object_unref(renderer);

    return TRUE;
}
Exemple #15
0
/* imports the given dxf-file, returns TRUE if successful */
static gboolean
import_dxf(const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data)
{
    FILE *filedxf;
    DxfData *data;
    
    filedxf = g_fopen(filename,"r");
    if(filedxf == NULL){
        dia_context_add_message(ctx, _("Couldn't open: '%s' for reading.\n"),
				dia_context_get_filename (ctx));
        return FALSE;
    }
    
    data = g_new(DxfData, 1);
    
    do {
        if(read_dxf_codes(filedxf, data) == FALSE) {
            g_free(data);
	    dia_context_add_message(ctx, _("read_dxf_codes failed on '%s'"),
			            dia_context_get_filename(ctx) );
            return FALSE;
        }
        else {
            if (0 == data->code && strstr(data->codeline, "AutoCAD Binary DXF")) {
                g_free(data);
	        dia_context_add_message(ctx, _("Binary DXF from '%s' not supported"),
			                dia_context_get_filename(ctx));
                return FALSE;
            }
	    if (0 == data->code) {
                if(strcmp(data->value, "SECTION") == 0) {
		  /* don't think we need to do anything */
                } else if(strcmp(data->value, "ENDSEC") == 0) {
		  /* don't think we need to do anything */
                } else if(strcmp(data->value, "EOF") == 0) {
		  /* handled below */
		} else {
		  g_print ("DXF 0:%s not handled\n", data->value);
		}
            } else if(data->code == 2) {
                if(strcmp(data->value, "ENTITIES") == 0) {
		   /*printf( "reading section entities\n" );*/
                    read_section_entities_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "BLOCKS") == 0) {
		   /*printf( "reading section BLOCKS\n" );*/
                    read_section_blocks_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "CLASSES") == 0) {
		   /*printf( "reading section CLASSES\n" );*/
                    read_section_classes_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "HEADER") == 0) {
		   /*printf( "reading section HEADER\n" );*/
                    read_section_header_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "TABLES") == 0) {
		  /*printf( "reading section tables\n" );*/
                    read_section_tables_dxf(filedxf, data, dia);
                }
	        else if(strcmp(data->value, "OBJECTS") == 0) {
		  /*printf( "reading section objects\n" );*/
                    read_section_entities_dxf(filedxf, data, dia);
		}
            } else if(data->code == 999) {
	      /* Don't complain on comments, but silently ignore */
	    }
	   else
	     g_warning("Unknown dxf code %d", data->code);
        }
    }while((data->code != 0) || (strcmp(data->value, "EOF") != 0));
    
    g_free(data);
    if (_color_by_layer_ht) {
        g_hash_table_destroy (_color_by_layer_ht);
        _color_by_layer_ht = NULL;
    }
    return TRUE;
}