コード例 #1
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
char * exists_with_extension(char *s, char *ext)
/******************************************************************************
 purpose   : return s.ext or s.EXT if it exists otherwise return NULL
 ******************************************************************************/
{
	char *t,*x;
	FILE *fp;
	
	t=strdup_together(s,ext);
	fp=fopen(t,"r");
	diagnostics(4,"trying to open %s, result = %0x",t,fp);
	if (fp) {
		fclose(fp);
		return t;
	}
	free(t);
	
/* now try upper case version of ext */
	x=upper_case_string(ext);
	t=strdup_together(s,x);
	free(x);
	
	fp=fopen(t,"r");
	diagnostics(4,"trying to open %s, result = %0x",t,fp);
	if (fp) {
		fclose(fp);
		return t;
	}
	free(t);
	return NULL;
}
コード例 #2
0
ファイル: utils.c プロジェクト: blipvert/latex2rtf
/******************************************************************************
 purpose:  returns a new string consisting of s+t+u
******************************************************************************/
char *strdup_together3(const char *s, const char *t, const char *u)
{
    char *two, *three;
    two = strdup_together(s,t);
    three = strdup_together(two,u);
    free(two);
    return three;
}
コード例 #3
0
static char *
SaveEquationAsFile(char *pre, char *eq, char *post)
{	
	FILE * f;
	char name[15];
	char *tmp_dir, *fullname, *texname;
	static int file_number = 0;
	
	/* create needed file names */
	file_number++;
	tmp_dir = getTmpPath();
#ifdef INLINE_EQ_ALIGN
        if(g_inline_eq_align&&!strcmp(pre,"$"))
       	   sprintf(name, "l2r-%04d", file_number);
        else 
#endif
	sprintf(name, "l2r_%04d", file_number);
	fullname = strdup_together(tmp_dir, name);	
	texname = strdup_together(fullname,".tex");

	diagnostics(4, "SaveEquationAsFile =%s", texname);
	
	f = fopen(texname,"w");
	while (eq && (*eq == '\n' || *eq == ' ')) eq++;  /* skip whitespace */
	if (f) {
		fprintf(f, "%s", g_preamble);
		fprintf(f, "\\thispagestyle{empty}\n");
		fprintf(f, "\\begin{document}\n");
		fprintf(f, "\\setcounter{equation}{%d}\n",getCounter("equation"));
		if (strstr(pre, "equation"))
			fprintf(f, "$$%s$$", eq);
#ifdef INLINE_EQ_ALIGN
                else if(g_inline_eq_align&&!strcmp(pre,"$"))
			fprintf(f, "%s.\\quad %s%s", pre, eq, post);
#endif
		else
			fprintf(f, "%s%s%s", pre, eq, post);
		fprintf(f, "\n\\end{document}");
		fclose(f);
	} else {
		free(fullname);
		fullname = NULL;
	}
	
	free(tmp_dir);
	free(texname);
	return(fullname);
}
コード例 #4
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static void
PutGifFile(char *s, double scale, double baseline, int full_path)
/******************************************************************************
 purpose   : Insert GIF file (from g_home_dir) into RTF file as a PNG image
 ******************************************************************************/
{
	char *cmd, *gif, *png, *tmp_png;
	size_t cmd_len;
	
	diagnostics(1, "filename = <%s>", s);
	png = strdup_new_extension(s, ".gif", ".png");
	if (png == NULL) {
		png = strdup_new_extension(s, ".GIF", ".png");
		if (png == NULL) return;
	}
	
	tmp_png = strdup_tmp_path(png);
	gif = strdup_together(g_home_dir,s);
	
	cmd_len = strlen(gif)+strlen(tmp_png)+10;
	cmd = (char *) malloc(cmd_len);
	snprintf(cmd, cmd_len, "convert %s %s", gif, tmp_png);	
	diagnostics(2, "system graphics command = [%s]", cmd);
	system(cmd);
	
	PutPngFile(tmp_png, scale, baseline, TRUE);
	my_unlink(tmp_png);

	free(tmp_png);
	free(cmd);
	free(gif);
	free(png);
}
コード例 #5
0
ファイル: utils.c プロジェクト: blipvert/latex2rtf
/******************************************************************************
 purpose:  returns a new string consisting of s+t+u+v
******************************************************************************/
char *strdup_together4(const char *s, const char *t, const char *u, const char *v)
{
    char *four, *three;
    three = strdup_together3(s,t,u);
    four = strdup_together(three,v);
    free(three);
    return four;
}
コード例 #6
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static void 
PutPictFile(char * s, double scale, double baseline, int full_path)
/******************************************************************************
     purpose : Include .pict file in RTF
 ******************************************************************************/
{
FILE *fp;
char *pict;
short buffer[5];
short top, left, bottom, right;
int width, height;

	if (full_path)
		pict = strdup(s);
	else
		pict = strdup_together(g_home_dir, s);		
	diagnostics(1, "PutPictFile <%s>", pict);

	fp = fopen(pict, "rb");
	free(pict);
	if (fp == NULL) return;
	
	if (fseek(fp, 514L, SEEK_SET) || fread(buffer, 2, 4, fp) != 4) {
		diagnostics (WARNING, "Cannot read graphics file <%s>", s);
		fclose(fp);
		return;
	}
	
	top    = buffer[0];
	left   = buffer[1];
	bottom = buffer[2];
	right  = buffer[3];

	width  = right - left;
	height = bottom - top;
	
	if (g_little_endian) {
		top    = LETONS(top);
		bottom = LETONS(bottom);
		left   = LETONS(left);
		right  = LETONS(right);
	}

	diagnostics(4,"top = %d, bottom = %d", top, bottom);
	diagnostics(4,"left = %d, right = %d", left, right);
	diagnostics(4,"width = %d, height = %d", width, height);
	fprintRTF("\n{\\pict\\macpict\\picw%d\\pich%d\n", width, height);
	if (scale != 1.0) {
		int iscale = (int) (scale * 100);
		fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
	}

	fseek(fp, -10L, SEEK_CUR);
	PutHexFile(fp);
	fprintRTF("}\n");
	fclose(fp);
}
コード例 #7
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static char *
get_latex2png_name()
{
#ifdef MSDOS
	return strdup("command.com /e:2048 /c latex2pn");
#else
	return strdup_together(g_script_path, "latex2png");
#endif
}
コード例 #8
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
void
PutLatexFile(char *s, double scale, char *pre)
/******************************************************************************
 purpose   : Convert LaTeX to Bitmap and insert in RTF file
 ******************************************************************************/
{
	char *png, *cmd, *l2p;
	int err, baseline,second_pass;
	size_t cmd_len;
	unsigned long width, height, rw, rh;
	unsigned long maxsize=(unsigned long) (32767.0/20.0);
	int resolution = g_dots_per_inch; /*points per inch */
	
	diagnostics(4, "Entering PutLatexFile");

	png = strdup_together(s,".png");
	l2p = get_latex2png_name();

	cmd_len = strlen(l2p)+strlen(s)+25;
	if (g_home_dir)
		cmd_len += strlen(g_home_dir);

	cmd = (char *) malloc(cmd_len);

	do {
		second_pass = FALSE; 	/* only needed if png is too large for Word */
		if (g_home_dir==NULL)
			snprintf(cmd, cmd_len, "%s -d %d %s", l2p, resolution, s);	
		else
			snprintf(cmd, cmd_len, "%s -d %d -H \"%s\" %s", l2p, resolution, g_home_dir, s);	

		diagnostics(2, "system graphics command = [%s]", cmd);
		err=system(cmd);
		if (err) break;

		GetPngSize(png, &width, &height);
		baseline=GetBaseline(s, pre);
		diagnostics(4, "png size height=%d baseline=%d width=%d", height, baseline, width);
		
		if( (width>maxsize && height!=0) || (height>maxsize && width!=0) ){  
			second_pass = TRUE;
			rw=(unsigned long) ((resolution*maxsize)/width);
		 	rh=(unsigned long) ((resolution*maxsize)/height); 
			resolution=rw<rh?(int)rw:(int)rh;
		}
	} while (resolution>10 && ( (width>maxsize) || (height>maxsize)) );
	
	if (err==0)
		PutPngFile(png, scale*72.0/resolution, (double) baseline, TRUE);
	
	free(l2p);
	free(png);
	free(cmd);
}
コード例 #9
0
ファイル: definitions.c プロジェクト: ra3xdh/latex2rtf-ex
void
newEnvironment(char *name, char *opt_param, char *begdef, char *enddef, int params)
/**************************************************************************
     purpose: allocates and initializes a \newenvironment 
              name should not begin with a '\' 
**************************************************************************/
{
	if (iNewEnvironmentCount==MAX_ENVIRONMENTS){
		diagnostics(WARNING,"Too many newenvironments, ignoring %s", name);
		return;
	}
	
	NewEnvironments[iNewEnvironmentCount].name=strdup(name); 
	NewEnvironments[iNewEnvironmentCount].begname=strdup_together("\\begin{",name); 
	NewEnvironments[iNewEnvironmentCount].endname=strdup_together("\\end{",name); 
	NewEnvironments[iNewEnvironmentCount].begdef=strdup(begdef); 
	NewEnvironments[iNewEnvironmentCount].enddef=strdup(enddef); 
	NewEnvironments[iNewEnvironmentCount].params=params; 

	if (opt_param) {
		NewEnvironments[iNewEnvironmentCount].opt_param=strdup(opt_param); 

		if (NewEnvironments[iNewEnvironmentCount].opt_param==NULL) {
			diagnostics(ERROR, "\nCannot allocate opt_param for \\newenvironment{%s}", name);
		}
	}
	else {
	  NewEnvironments[iNewEnvironmentCount].opt_param=NULL;
	}


	if (NewEnvironments[iNewEnvironmentCount].name   ==NULL ||
		NewEnvironments[iNewEnvironmentCount].begdef ==NULL ||
		NewEnvironments[iNewEnvironmentCount].begname==NULL ||
		NewEnvironments[iNewEnvironmentCount].endname==NULL ||
	    NewEnvironments[iNewEnvironmentCount].enddef ==NULL) {
		diagnostics(ERROR, "Cannot allocate memory for \\newenvironment{%s}", name);
	}
	
	iNewEnvironmentCount++;
}
コード例 #10
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static char *
strdup_new_extension(char *s, char *old_ext, char *new_ext)
{
	char *new_name, *p;
	
	p=strstr(s,old_ext);
	if (p==NULL) return NULL;
	
	new_name = strdup_together(s,new_ext);
	p=strstr(new_name,old_ext);
	strcpy(p,new_ext);
	return new_name;
}
コード例 #11
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static char *
strdup_tmp_path(char *s)
/******************************************************************************
     purpose : create a tmp file name using only the end of the filename
 ******************************************************************************/
{
	char *tmp, *p, *fullname, c;
	
	if (s==NULL) return NULL;

	tmp = getTmpPath();
	
	c = PATHSEP;
	p=strrchr(s,c);
	
	if (!p)
		fullname = strdup_together(tmp,s);
	else
		fullname = strdup_together(tmp,p+1);

	free(tmp);
	return fullname;
}
コード例 #12
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
void 
PutPngFile(char * s, double scale, double baseline, int full_path)
/******************************************************************************
     purpose : Include .png file in RTF
 ******************************************************************************/
{
	FILE *fp;
	char *png;
	unsigned long width, height, w, h, b;
	int iscale;

	if (full_path)
		png = strdup(s);
	else
		png = strdup_together(g_home_dir, s);		
	diagnostics(2, "PutPngFile <%s>",png);

	GetPngSize(png, &width, &height);

	diagnostics(4,"width = %ld, height = %ld, baseline = %g", width, height, baseline);

	if (width==0 || height==0) return;
	
	fp = fopen(png, "rb");
	free(png);
	if (fp == NULL) return;

	w = (unsigned long)( 100000.0*width   )/ ( 20* POINTS_PER_M );
	h = (unsigned long)( 100000.0*height  )/ ( 20* POINTS_PER_M );
	b = (unsigned long)( 100000.0*baseline*scale)/ ( 20* POINTS_PER_M );

	diagnostics(4,"width = %ld, height = %ld, baseline = %ld", w, h, b);
	
	fprintRTF("\n{");
	if (b) fprintRTF("\\dn%ld",b);
	fprintRTF("\\pict\\pngblip\\picw%ld\\pich%ld", w, h);
	fprintRTF("\\picwgoal%ld\\pichgoal%ld", width*20, height*20);
	if (scale != 1.0) {
		iscale = (int) (scale * 100);
		fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
	}
	fprintRTF("\n");
	rewind(fp);
	PutHexFile(fp);
	fprintRTF("}\n");
	fclose(fp);
}
コード例 #13
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static char *
strdup_absolute_path(char *s)
/******************************************************************************
     purpose : return a string containing an absolute path
 ******************************************************************************/
{
	char 	c = PATHSEP;
	char *abs_path=NULL;
	
	if (s) {
		if (*s==c || g_home_dir==NULL)
			abs_path = strdup(s);
		else 
			abs_path = strdup_together(g_home_dir, s);
	}
	
	return abs_path;
}
コード例 #14
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static char *
eps_to_pict(char *s)
/******************************************************************************
     purpose : create a pict file from an EPS file and return file name for
               the pict file.  Ideally this file will contain both the bitmap
               and the original EPS embedded in the PICT file as comments.  If a
               bitmap cannot be created, then the EPS is still embedded in the PICT
               file so that at least the printed version will be good.
 ******************************************************************************/
{
	char *cmd, *p, buffer[560];
	size_t cmd_len;
	long ii, pict_bitmap_size, eps_size;
	short err,handle_size;
	unsigned char byte;
	short PostScriptBegin 	= 190;
	short PostScriptEnd   	= 191;
	short PostScriptHandle	= 192;
	char *pict_bitmap		=NULL;
	char *pict_eps			=NULL;
	char *eps				=NULL;
	char *return_value		=NULL;
	FILE *fp_eps			=NULL;
	FILE *fp_pict_bitmap	=NULL;
	FILE *fp_pict_eps		=NULL;
	
	diagnostics(2, "eps_to_pict filename = <%s>", s);

	/* Create filename for bitmap */
	p = strdup_new_extension(s, ".eps", "a.pict");
	if (p == NULL) {
		p = strdup_new_extension(s, ".EPS", "a.pict");
		if (p == NULL) goto Exit;
	}
	pict_bitmap = strdup_tmp_path(p);
	free(p);
	
	/* Create filename for eps file */
	p = strdup_new_extension(s, ".eps", ".pict");
	if (p == NULL) {
		p = strdup_new_extension(s, ".EPS", ".pict");
		if (p == NULL) goto Exit;
	}
	pict_eps = strdup_tmp_path(p);
	free(p);

	eps = strdup_together(g_home_dir,s);

	/* create a bitmap version of the eps file */
	cmd_len = strlen(eps)+strlen(pict_bitmap)+strlen("convert -crop 0x0 -density ")+40;
	cmd = (char *) malloc(cmd_len);
	snprintf(cmd, cmd_len, "convert -crop 0x0 -density %d %s %s", g_dots_per_inch, eps, pict_bitmap);	
	diagnostics(2, "system graphics command = [%s]", cmd);
	err = system(cmd);
	free(cmd);
	
	if (err!=0) 
		diagnostics(WARNING, "problem creating bitmap from %s", eps);
	else
		return_value = pict_bitmap;
		
	/* open the eps file and make sure that it is less than 32k */
 	fp_eps = fopen (eps, "rb");
	if (fp_eps==NULL) goto Exit;
	fseek(fp_eps, 0, SEEK_END);
  	eps_size = ftell (fp_eps);
  	if (eps_size > 32000) {
  		diagnostics(WARNING, "EPS file >32K ... using bitmap only");
  		goto Exit;
  	}
  	rewind (fp_eps);
  	diagnostics(WARNING, "eps size is 0x%X bytes", eps_size);
  
	/*open bitmap pict file and get file size */
	fp_pict_bitmap = fopen(pict_bitmap, "rb");
	if (fp_pict_bitmap == NULL) goto Exit;
	fseek(fp_pict_bitmap, 0, SEEK_END);
  	pict_bitmap_size = ftell(fp_pict_bitmap);
  	rewind(fp_pict_bitmap);

	/*open new pict file */
	fp_pict_eps = fopen(pict_eps, "w");
	if (fp_pict_eps == NULL) goto Exit;

	/*copy header 512 buffer + 40 byte header*/
	if (fread( &buffer,1,512+40,fp_pict_bitmap)!=512+40) goto Exit;
	if (fwrite(&buffer,1,512+40,fp_pict_eps)!=512+40) goto Exit;
	
	/* insert comment that allows embedding postscript */
	PicComment(PostScriptBegin,0,fp_pict_eps);
	
	/*copy bitmap 512+40 bytes of header + 2 bytes at end */
	for (ii=512+40+2; ii<pict_bitmap_size; ii++) {
		if(fread (&byte,1,1,fp_pict_bitmap)!=1) goto Exit;
		if(fwrite(&byte,1,1,fp_pict_eps   )!=1) goto Exit;
	}
	
	/*copy eps graphic (write an even number of bytes) */
	handle_size = eps_size;   
	if (odd(eps_size)) handle_size ++;	
	
	PicComment(PostScriptHandle,handle_size,fp_pict_eps);
	for (ii=0; ii<eps_size; ii++) {
		if(fread(&byte,1,1,fp_eps)!=1) goto Exit;
		if(fwrite(&byte,1,1,fp_pict_eps)!=1) goto Exit;
	}
	if (odd(eps_size)) {
		byte = ' ';
		if(fwrite(&byte,1,1,fp_pict_eps) !=1) goto Exit;
	}		
	
	/*close file*/
	PicComment(PostScriptEnd,0,fp_pict_eps);
	byte = 0x00;
	if (fwrite(&byte,1,1,fp_pict_eps)!=1) goto Exit;
	byte = 0xFF;
	if (fwrite(&byte,1,1,fp_pict_eps)!=1) goto Exit;

	return_value= pict_eps;
	
Exit:
	if (eps)           free(eps);
	if (pict_eps)      free(pict_eps);
	if (pict_bitmap)   free(pict_bitmap);

  	if (fp_eps)         fclose(fp_eps); 
	if (fp_pict_eps)    fclose(fp_pict_eps); 
	if (fp_pict_bitmap) fclose(fp_pict_bitmap); 
	return return_value;
}
コード例 #15
0
ファイル: tikz.c プロジェクト: ra3xdh/latex2rtf-ex
void TikzToPng(char *tikzcode,char *exts)
{
        char *fullname, *tmp_dir, *texname, *auxname, *logname,
	        *pdfname, *pngname, *destname;

        FILE *f;
	static int file_number = 0;

        char name[15];

	file_number++;

        tmp_dir = getTmpPath();
	snprintf(name,15,"t2p_%04d",file_number);
	fullname = strdup_together(tmp_dir,name);

	texname = strdup_together(fullname,".tex");
	pdfname = strdup_together(fullname,".pdf");
	auxname = strdup_together(fullname,".aux");
	logname = strdup_together(fullname,".log");


	f = fopen(texname, "w");

        
        fprintf(f,"\\documentclass[varwidth=true,border=10pt]{standalone}\n");
        fprintf(f,"\\usepackage{mathtext}\n");
        fprintf(f,"\\usepackage[T2A]{fontenc}\n");
        fprintf(f,"\\usepackage[%s]{inputenc}\n",g_charset_encoding_name);
        fprintf(f,"\\usepackage[russian]{babel}\n");

        fprintf(f,"\\usepackage{tikz}\n");
	fprintf(f,"\\usepackage{gnuplot-lua-tikz}\n");

        fprintf(f,"\\begin{document}\n");

        int i;
	for (i=0;i<tikzlibsnum;i++) {
	    fprintf(f,"\\usetikzlibrary{%s}\n",tikzlibs[i]);
	}


        if (exts!=NULL)
        fprintf(f,"\\begin{tikzpicture}[%s]\n",exts);
	else 
        fprintf(f,"\\begin{tikzpicture}\n");
        
	fprintf(f,"\n%s\n",tikzcode);

	fprintf(f,"\\end{tikzpicture}\n");
	fprintf(f,"\\end{document}\n");

        fclose(f);


        int cmd_len = strlen("pdflatex") + strlen(texname)+32+strlen(" >/dev/null");
	char *cmd = (char *)malloc(cmd_len);

        snprintf(cmd, cmd_len, "pdflatex %s >/dev/null",texname);

        char *oldcwd = (char *)malloc(1024);
	getcwd(oldcwd,1024);
	chdir(tmp_dir);
	int err = system(cmd);
	chdir(oldcwd);
	free(cmd);

        if (!err) PutPdfFile(pdfname,g_png_figure_scale,0,TRUE);

        if (g_figs_extract && g_processing_figure) {
	    struct stat st ={0};
	    if (stat(g_figsdir, &st) == -1) {
	       mkdir(g_figsdir,0755);
	    }

	    g_fignum++;
            
	    char *figname = (char *)malloc(15*sizeof(char));
            snprintf(figname,15,"Ris%d.png",g_fignum);

	    destname = strdup_together(g_figsdir,"/");
	    pngname = strdup_together(destname,figname);

	    cmd_len = strlen("convert -alpha off -density 300x300 ")+strlen(destname)+strlen(pdfname)+32;
	    cmd = (char *) malloc(cmd_len);
	    snprintf(cmd,cmd_len,"convert -density 300x300 %s -alpha off %s ",pdfname,pngname);
	    printf(cmd);
	    system(cmd);
	    free(cmd);
	    free(destname);
	    free(pngname);
	}

        remove(texname);
	remove(auxname);
	remove(pdfname);
	remove(logname);

        free(oldcwd);
        free(fullname);
	free(texname);
	free(auxname);
	free(pdfname);
	free(logname);
	free(tmp_dir);
}
コード例 #16
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
long 
GetBaseline(char *s, char *pre)
/****************************************************************************
purpose: reads a .pbm file to determine the baseline for an equation
		 the .pbm file should have dimensions of 1xheight
 ****************************************************************************/
{
	FILE *fp;
	int thechar;
	char *pbm;
	char magic[250];
	long baseline, width, height, items, top, bottom;
	
	/* baseline=0 if not an inline image */
	if ((strcmp(pre,"$")!=0) && (strcmp(pre,"\\begin{math}")!=0) && (strcmp(pre,"\\(")!=0)) 
		return 0;
		
	pbm = strdup_together(s,".pbm");
	baseline = 4;
		
	diagnostics(4, "GetBaseline opening=<%s>",pbm);

	fp = fopen(pbm, "rb");
	if (fp == NULL) {free(pbm); return baseline;}

	items = fscanf(fp,"%2s", magic);			/* ensure that file begins with "P4" */
	if ((items!=1) || (strcmp(magic,"P4")!=0)) goto Exit;

	items = fscanf(fp," %s", magic);
	while ((items==1) && (magic[0]=='#')) {		/* skip any comment lines in pbm file */
		if (!ReadLine(fp)) goto Exit;
		items = fscanf(fp,"%s", magic);
	}
	
	items = sscanf(magic, "%ld", &width);		/* make sure image width is 1 */
	if ((items != 1) || (width != 1)) goto Exit;
	
	items=fscanf(fp," %ld", &height);  /* read height */
	if (items != 1) goto Exit;

	diagnostics(4, "width=%ld height=%ld", width, height);
			
	if (!ReadLine(fp)) goto Exit;				/* pixel map should start on next line */

	for (top=height; top>0; top--) {			/* seek first black pixel (0x00) */
		thechar = getc(fp);
		if (thechar == EOF) goto Exit;		
		if (thechar != 0  ) break;
	}

	for (bottom=top-1; bottom>0; bottom--) {	/* seek first black pixel (0x00) */
		thechar = getc(fp);
		if (thechar == EOF) goto Exit;		
		if (thechar == 0  ) break;
	}
		
	baseline = (bottom+top)/2;

	diagnostics(4, "top=%ld bottom=%ld baseline=%ld", top, bottom, baseline);
	
Exit:
	free(pbm);
	fclose(fp);
	return baseline;	
}
コード例 #17
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static void
PutWmfFile(char *s, double scale, double baseline, int full_path)
/******************************************************************************
 purpose   : Insert WMF file (from g_home_dir) into RTF file
 ******************************************************************************/
{
	FILE *fp;
	char *wmf;
	unsigned long	Key;			/* Magic number (always 0x9AC6CDD7) */
	unsigned short	FileType;		/* Type of metafile (0=memory, 1=disk) */
	unsigned short	HeaderSize;		/* Size of header in WORDS (always 9) */
	unsigned short	Handle;			/* Metafile HANDLE number (always 0) */
	short			Left;			/* Left coordinate in twips */
	short			Top;			/* Top coordinate in twips */
	short			Right;			/* Right coordinate in twips */
	short			Bottom;			/* Bottom coordinate in twips */
	int 			width, height;
	unsigned long int magic_number = (unsigned long int) 0x9AC6CDD7;
	
	/* open the proper file */
	wmf = strdup_together(g_home_dir,s);
	diagnostics(1, "PutWmfFile <%s>", wmf);
	fp = fopen(wmf, "rb");
	free(wmf);
	if (fp == NULL) return;

	/* verify file is actually WMF and get size */
	if (fread(&Key,4,1,fp) != 1) goto out;
	if (!g_little_endian) Key  = LETONL(Key);

	if (Key == magic_number) {		/* file is placeable metafile */
		if (fread(&Handle,2,1,fp) != 1) goto out;
		if (fread(&Left,2,1,fp)   != 1) goto out;
		if (fread(&Top,2,1,fp)    != 1) goto out;
		if (fread(&Right,2,1,fp)  != 1) goto out;
		if (fread(&Bottom,2,1,fp) != 1) goto out;

		if (!g_little_endian) {
			Left   = LETONS(Left);
			Top    = LETONS(Top);
			Right  = LETONS(Right);
			Bottom = LETONS(Bottom);
		}
		
		width  = abs(Right - Left);
		height = abs(Top-Bottom);

	} else {					/* file may be old wmf file with no size */

		rewind(fp);
		if (fread(&FileType,2,1,fp) != 1) goto out;
		if (fread(&HeaderSize,2,1,fp) != 1) goto out;
		
		if (!g_little_endian) {
			FileType  = (unsigned short) LETONS(FileType);
			HeaderSize = (unsigned short) LETONS(HeaderSize);
		}
	
		if (FileType != 0 && FileType != 1) goto out;
		if (HeaderSize != 9) goto out;
		
		/* real wmf file ... just assume size */
		width = 200;
		height = 200;
	}

	diagnostics(4,"width = %d, height = %d", width, height);
	fprintRTF("\n{\\pict\\wmetafile1\\picw%d\\pich%d\n", width, height);
	if (scale != 1.0) {
		int iscale = (int) (scale * 100);
		fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
	}

	rewind(fp);
	PutHexFile(fp);
	fprintRTF("}\n");
	fclose(fp);
	return;

out:
	diagnostics(WARNING,"Problem with file %s --- not included",s);
	fclose(fp);
}
コード例 #18
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static void
PutEmfFile(char *s, double scale, double baseline, int full_path)
{
	FILE *fp;
	char *emf;
	unsigned long	RecordType;		/* Record type (always 0x00000001)*/
	unsigned long	RecordSize;		/* Size of the record in bytes */
	long			BoundsLeft;		/* Left inclusive bounds */
	long			BoundsRight;	/* Right inclusive bounds */
	long			BoundsTop;		/* Top inclusive bounds */
	long			BoundsBottom;	/* Bottom inclusive bounds */
	long			FrameLeft;		/* Left side of inclusive picture frame */
	long			FrameRight;		/* Right side of inclusive picture frame */
	long			FrameTop;		/* Top side of inclusive picture frame */
	long			FrameBottom;	/* Bottom side of inclusive picture frame */
	unsigned long	Signature;		/* Signature ID (always 0x464D4520) */
	unsigned long	w,h,width,height;
	
	if (full_path)
		emf = strdup(s);
	else
		emf = strdup_together(g_home_dir, s);		
	diagnostics(1, "PutEmfFile <%s>",emf);
	fp = fopen(emf,"rb");
	free(emf);
	if (fp == NULL) return;

/* extract size information*/
	if (fread(&RecordType,4,1,fp)  != 1) goto out;
	if (fread(&RecordSize,4,1,fp)  != 1) goto out;
	if (fread(&BoundsLeft,4,1,fp)  != 1) goto out;
	if (fread(&BoundsTop,4,1,fp)   != 1) goto out;
	if (fread(&BoundsRight,4,1,fp) != 1) goto out;
	if (fread(&BoundsBottom,4,1,fp)!= 1) goto out;
	if (fread(&FrameLeft,4,1,fp)   != 1) goto out;
	if (fread(&FrameRight,4,1,fp)  != 1) goto out;
	if (fread(&FrameTop,4,1,fp)    != 1) goto out;
	if (fread(&FrameBottom,4,1,fp) != 1) goto out;
	if (fread(&Signature,4,1,fp)   != 1) goto out;

	if (!g_little_endian) {
		RecordType   = LETONL(RecordType);
		RecordSize   = LETONL(RecordSize);
		BoundsLeft   = LETONL(BoundsLeft);
		BoundsTop    = LETONL(BoundsTop);
		BoundsRight  = LETONL(BoundsRight);
		BoundsBottom = LETONL(BoundsBottom);
		FrameLeft    = LETONL(FrameLeft);
		FrameRight   = LETONL(FrameRight);
		FrameTop     = LETONL(FrameTop);
		FrameBottom  = LETONL(FrameBottom);
		Signature    = LETONL(Signature);
	}

	if (RecordType != 1 || Signature != 0x464D4520) goto out;
	height = (unsigned long) (BoundsBottom-BoundsTop);
	width  = (unsigned long) (BoundsRight-BoundsLeft);
	
	w = (unsigned long)(( 100000.0*width  )/ ( 20* POINTS_PER_M ));
	h = (unsigned long)(( 100000.0*height )/ ( 20* POINTS_PER_M ));
	diagnostics(4,"width = %ld, height = %ld", width, height);
	fprintRTF("\n{\\pict\\emfblip\\picw%ld\\pich%ld", w, h);
	fprintRTF("\\picwgoal%ld\\pichgoal%ld\n", width*20, height*20);
	if (scale != 1.0) {
		int iscale = (int) (scale * 100);
		fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
	}

/* write file */
	rewind(fp);
	PutHexFile(fp);
	fprintRTF("}\n");
	fclose(fp);
	return;

out:
	diagnostics(WARNING,"Problem with file %s --- not included",s);
	fclose(fp);
}
コード例 #19
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
static void 
PutJpegFile(char * s, double scale, double baseline, int full_path)
/******************************************************************************
     purpose : Include .jpeg file in RTF
 ******************************************************************************/
{
	FILE *fp;
	char *jpg;
	unsigned short buffer[2];
	int m,c;
	unsigned short width, height;
	unsigned long w, h;

	jpg = strdup_together(g_home_dir,s);
	fp = fopen(jpg, "rb");
	free(jpg);
	if (fp == NULL) return;

	if ((c=fgetc(fp)) != 0xFF && (c=fgetc(fp)) != 0xD8) {
		fclose(fp);
		diagnostics(WARNING, "<%s> is not really a JPEG file --- skipping");
		return;
	}
	
	do {  /* Look for SOFn tag */
	
		  while (!feof(fp) && fgetc(fp) != 0xFF){}   		/* Find 0xFF byte */
		  
		  while (!feof(fp) && (m=fgetc(fp)) == 0xFF){}  	/* Skip multiple 0xFFs */
		  
	} while (!feof(fp) && m!=0xC0 && m!=0xC1 && m!=0xC2 && m!=0xC3 && m!=0xC5 && m!=0xC6 && m!=0xC7 &&
					      m!=0xC9 && m!=0xCA && m!=0xCB && m!=0xCD && m!=0xCE && m!=0xCF );    
	
	if (fseek(fp, 3, SEEK_CUR) || fread(buffer,2,2,fp) != 2) {
		diagnostics (WARNING, "Cannot read graphics file <%s>", s);
		fclose(fp);
		return;
	}

	width = buffer[1];
	height = buffer[0];

	if (g_little_endian) {
		width  = (unsigned short) LETONS(width);
		height = (unsigned short) LETONS(height);
	}

	diagnostics(4,"width = %d, height = %d", width, height);

	w = (unsigned long)( 100000.0*width  )/ ( 20* POINTS_PER_M );
	h = (unsigned long)( 100000.0*height )/ ( 20* POINTS_PER_M );
	fprintRTF("\n{\\pict\\jpegblip\\picw%ld\\pich%ld", w, h);
	fprintRTF("\\picwgoal%ld\\pichgoal%ld\n", width*20, height*20);
	if (scale != 1.0) {
		int iscale = (int) (scale * 100);
		fprintRTF("\\picscalex%d\\picscaley%d", iscale, iscale);
	}

	rewind(fp);
	PutHexFile(fp);
	fprintRTF("}\n");
	fclose(fp);
}
コード例 #20
0
ファイル: graphics.c プロジェクト: ra3xdh/latex2rtf-ex
void 
CmdGraphics(int code)
/*
\includegraphics[parameters]{filename}

where parameters is a comma-separated list of any of the following: 
bb=llx lly urx ury (bounding box),
width=h_length,
height=v_length,
angle=angle,
scale=factor,
clip=true/false,
draft=true/false.

code=0 => includegraphics
code=1 => epsffile
code=2 => epsfbox
code=3 => \BoxedSPSF
code=4 => psfig
*/
{
	char           *options,*options2;
	char           *filename,*fullpathname, *fullname;
	double			scale=1.0;
	double			baseline=0.0;
	double 			x;
	char			*p;
	
	if (code==0) { /* could be \includegraphics*[0,0][5,5]{file.pict} */
		options = getBracketParam();
		options2 = getBracketParam();
		if (options2) free(options2);

		if (options) {						/* \includegraphics[scale=0.5]{file.png} */
			p = strstr(options,"scale");
			if (p) {
				p = strchr(p,'=');
				if (p && (sscanf(p+1,"%lf", &x) == 1)) scale = x;
			}
			free(options);
		}
		filename = getBraceParam();
		diagnostics(1, "image scale = %g", scale);


                if (g_figs_extract && g_processing_figure) {
		   

	           struct stat st ={0};
	           if (stat(g_figsdir, &st) == -1) {
	           mkdir(g_figsdir,0755);
	           }
                   
		   g_fignum++;

                   char *name = (char *)malloc(15*sizeof(char));
		   snprintf(name,15,"Ris%d.png",g_fignum);

	           char *destname = strdup_together(g_figsdir,"/");
		   char *pngname = strdup_together(destname,name);

	           int cmd_len = strlen("convert -alpha off -density 300x300 ")
		                                   + strlen(destname) + strlen(filename)+32;
	           char *cmd = (char *) malloc(cmd_len);

	           snprintf(cmd,cmd_len,"convert -density 300x300 %s -alpha off %s ",filename,pngname);
	           system(cmd);

	           free(cmd);
	           free(destname);
	           free(pngname);
		   free(name);
	        }



	}
	
	if (code==1) { /* \epsffile{filename.eps} */
		filename = getBraceParam();
	}

	if (code==2) { /* \epsfbox[0 0 30 50]{filename.ps} */
		options = getBracketParam();
		if (options) free(options);
		filename = getBraceParam();
	}
	
	if (code==3) {		/* \BoxedEPSF{filename [scaled nnn]} */
		char *s;
		filename = getBraceParam();
		s= strchr(filename,' ');
		if (s) *s='\0';
	}
		
	if (code==4) {		/* \psfig{figure=filename,height=hhh,width=www} */
		char *s, *t;
		filename = getBraceParam();
		s = strstr(filename,"figure=");
		if (!s) return;
		s += strlen("figure=");
		t = strchr(s,',');
		if (t) *t='\0';
		t = strdup(s);
		free(filename);
		filename = t;
	}
	
	SetTexMode(MODE_HORIZONTAL);

	fullname=strdup_absolute_path(filename);
	fullpathname=append_graphic_extension(fullname);
	free(fullname);
	
	if (has_extension(fullpathname, ".pict"))
		PutPictFile(fullpathname, scale, baseline, TRUE);
		
	else if (has_extension(fullpathname, ".png"))
		PutPngFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".gif"))
		PutGifFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".emf"))
		PutEmfFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".wmf"))
		PutWmfFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".eps"))
		PutEpsFile(fullpathname, scale, baseline, TRUE);
                
	else if (has_extension(fullpathname, ".pdf"))
		PutPdfFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".ps"))
		PutEpsFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".tiff"))
		PutTiffFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".tif"))
		PutTiffFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".jpg"))
		PutJpegFile(fullpathname, scale, baseline, TRUE);

	else if (has_extension(fullpathname, ".jpeg"))
		PutJpegFile(fullpathname, scale, baseline, TRUE);

	else 
		diagnostics(WARNING, "Conversion of '%s' not supported", filename);
	
	free(filename);
	free(fullpathname);
}