Пример #1
0
int main(int argn, char *argv[])
{
    int ret;
    char buf[256];
    int numfonts = 0;
    int t;
    char t1searchpath[1024];
    int nup_pos = 0;
    int x,y;
    int one_file_per_page = 0;
    
    initLog(0,-1,0,0,-1,loglevel);

    /* not needed anymore since fonts are embedded
       if(installPath) {
	fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
    }*/

#ifdef HAVE_SRAND48
    srand48(time(0)*getpid());
#else
#ifdef HAVE_SRAND
    srand(time(0)*getpid());
#endif
#endif

    processargs(argn, argv);
    
    driver = gfxsource_pdf_create();
    
    /* pass global parameters to PDF driver*/
    parameter_t*p = device_config;
    while(p) {
	driver->setparameter(driver, p->name, p->value);
	p = p->next;
    }

    if(!filename)
    {
	fprintf(stderr, "Please specify an input file\n");
	exit(1);
    }

    if (!info_only) {
        if(!outputname)
        {
            if(filename) {
                outputname = stripFilename(filename, ".swf");
                msg("<notice> Output filename not given. Writing to %s", outputname);
            } 
        }
            
        if(!outputname)
        {
            fprintf(stderr, "Please use -o to specify an output file\n");
            exit(1);
        }
    }

    // test if the page range is o.k.
    is_in_range(0x7fffffff, pagerange);

    if (!filename) {
	args_callback_usage(argv[0]);
	exit(0);
    }
    
    char fullname[256];
    if(password && *password) {
	sprintf(fullname, "%s|%s", filename, password);
	filename = fullname;
    }
    
    if(pagerange)
	driver->setparameter(driver, "pages", pagerange);

    /* add fonts */
    for(t=0;t<fontpathpos;t++) {
	driver->setparameter(driver, "fontdir", fontpaths[t]);
    }

    if(info_only) {
	show_info(driver, filename);
	return 0;
    }

    char*u = 0;
    if((u = strchr(outputname, '%'))) {
	if(strchr(u+1, '%') || 
	   strchr(outputname, '%')!=u)  {
	    msg("<error> only one %% allowed in filename\n");
	    return 1;
	}
	if(preloader || viewer) {
	    msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
	    return 1;
	}
	msg("<notice> outputting one file per page");
	one_file_per_page = 1;
	char*pattern = (char*)malloc(strlen(outputname)+2);
	/* convert % to %d */
	int l = u-outputname+1;
	memcpy(pattern, outputname, l);
	pattern[l]='d';
	strcpy(pattern+l+1, outputname+l);
	outputname = pattern;
    }

    gfxdocument_t* pdf = driver->open(driver, filename);
    if(!pdf) {
        msg("<error> Couldn't open %s", filename);
        exit(1);
    }
    /* pass global parameters document */
    p = device_config;
    while(p) {
	pdf->setparameter(pdf, p->name, p->value);
	p = p->next;
    }

    struct mypage_t {
	int x;
	int y;
	gfxpage_t*page;
    } pages[4];

    int pagenum = 0;
    int frame = 1;
    int pagenr;
    
    for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
    {
	if(is_in_range(pagenr, pagerange)) {
	    char mapping[80];
	    sprintf(mapping, "%d:%d", pagenr, frame);
	    pdf->setparameter(pdf, "pagemap", mapping);
	    pagenum++;
	}
	if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
	    pagenum = 0;
	    frame++;
	}
    }
    if(pagerange && !pagenum && frame==1) {
	fprintf(stderr, "No pages in range %s", pagerange);
	exit(1);
    }

    pagenum = 0;

    gfxdevice_t*out = create_output_device();;
    pdf->prepare(pdf, out);

    for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
    {
	if(is_in_range(pagenr, pagerange)) {
	    gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
	    pages[pagenum].x = 0;
	    pages[pagenum].y = 0;
	    pages[pagenum].page = page;
	    pagenum++;
	}
	if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {

	    int t;
	    int xmax[xnup], ymax[xnup];
	    int x,y;
	    int width=0, height=0;

	    memset(xmax, 0, xnup*sizeof(int));
	    memset(ymax, 0, ynup*sizeof(int));

	    for(y=0;y<ynup;y++)
	    for(x=0;x<xnup;x++) {
		int t = y*xnup + x;

		if(pages[t].page->width > xmax[x])
		    xmax[x] = (int)pages[t].page->width;
		if(pages[t].page->height > ymax[y])
		    ymax[y] = (int)pages[t].page->height;
	    }
	    for(x=0;x<xnup;x++) {
		width += xmax[x];
		xmax[x] = width;
	    }
	    for(y=0;y<ynup;y++) {
		height += ymax[y];
		ymax[y] = height;
	    }
	    if(custom_clip) {
		out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
	    } else {
		out->startpage(out,width,height);
	    }
	    for(t=0;t<pagenum;t++) {
		int x = t%xnup;
		int y = t/xnup;
		int xpos = x>0?xmax[x-1]:0;
		int ypos = y>0?ymax[y-1]:0;
		msg("<verbose> Render (%d,%d) move:%d/%d\n",
			(int)(pages[t].page->width + xpos),
			(int)(pages[t].page->height + ypos), xpos, ypos);
		pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos, 
			                                   custom_move? move_y : ypos,
							   custom_clip? clip_x1 : 0 + xpos, 
							   custom_clip? clip_y1 : 0 + ypos, 
							   custom_clip? clip_x2 : pages[t].page->width + xpos, 
							   custom_clip? clip_y2 : pages[t].page->height + ypos);
	    }
	    out->endpage(out);
	    for(t=0;t<pagenum;t++)  {
		pages[t].page->destroy(pages[t].page);
	    }
	    pagenum = 0;

	    if(one_file_per_page) {
		gfxresult_t*result = out->finish(out);out=0;
		char buf[1024];
		sprintf(buf, outputname, pagenr);
		if(result->save(result, buf) < 0) {
		    return 1;
		}
		result->destroy(result);result=0;
		out = create_output_device();;
                pdf->prepare(pdf, out);
		msg("<notice> Writing SWF file %s", buf);
	    }
	}
    }
   
    if(one_file_per_page) {
	// remove empty device
	gfxresult_t*result = out->finish(out);out=0;
	result->destroy(result);result=0;
    } else {
	gfxresult_t*result = out->finish(out);
	msg("<notice> Writing SWF file %s", outputname);
	if(result->save(result, outputname) < 0) {
	    exit(1);
	}
	int width = (int)(ptroff_t)result->get(result, "width");
	int height = (int)(ptroff_t)result->get(result, "height");
	result->destroy(result);result=0;

	if(preloader || viewer) {
	    const char*zip = "";
	    if(zlib) {
		zip = "-z";
	    }
	    if(!preloader && viewer) {
		systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
			viewer, outputname, outputname);
		if(!system_quiet)
		    printf("\n");
	    }
	    if(preloader && !viewer) {
		msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
		ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
			SWFDIR, preloader, outputname, outputname);
		if(!system_quiet)
		    printf("\n");
	    }
	    if(preloader && viewer) {
#ifdef HAVE_MKSTEMP
		char tmpname[] = "__swf__XXXXXX";
		mkstemp(tmpname);
#else 
		char*tmpname = "__tmp__.swf";
#endif
		systemf("swfcombine \"%s\" viewport=%s -o %s",
			viewer, outputname, tmpname);
		systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
			getRate(preloader), SWFDIR, preloader, tmpname, outputname);
		systemf("rm %s", tmpname);
	    }
	}
    }

    pdf->destroy(pdf);
    driver->destroy(driver);

   
    /* free global parameters */
    p = device_config;
    while(p) {
	parameter_t*next = p->next;
	if(p->name) free((void*)p->name);p->name = 0;
	if(p->value) free((void*)p->value);p->value =0;
	p->next = 0;free(p);
	p = next;
    }
    if(filters) {
	free(filters);
    }

    return 0;
}
Пример #2
0
int mainPDF2SWF(int argn, char *argv[], void *stream, void *pdfDoc, ExportSWFParams *exportParams)
#endif
{
	char buf[256];
	int numfonts = 0;
	int t;
	char t1searchpath[1024];
	int nup_pos = 0;
	int x,y;
	int one_file_per_page = 0;

	int ret = 0;

	initLog(0,-1,0,0,-1,loglevel);

	srand(time(0));

	processargs(argn, argv);

	driver = gfxsource_pdf_create();

	/* pass global parameters to PDF driver*/
	parameter_t*p = device_config;
	while(p) {
		driver->setparameter(driver, p->name, p->value);
		p = p->next;
	}
#ifndef _CONSOLE
	if(openStream)
		filename = "stream";
#endif
	if(!filename)
	{
		fprintf(stderr, "Please specify an input file\n");
		RETERROR(1);
	}

	if (!info_only) {
		if(!outputname)
		{
			if(filename) {
				outputname = stripFilename(filename, ".swf");
				msg("<notice> Output filename not given. Writing to %s", outputname);
			} 
		}

		if(!outputname)
		{
			fprintf(stderr, "Please use -o to specify an output file\n");
			RETERROR(2);
		}
	}

	// test if the page range is o.k.
	is_in_range(0x7fffffff, pagerange);

	if (!filename) {
		args_callback_usage(argv[0]);
		RETERROR(1,0);
	}

	char fullname[256];
	if(password && *password) {
		sprintf(fullname, "%s|%s", filename, password);
		filename = fullname;
	}

	if(pagerange)
		driver->setparameter(driver, "pages", pagerange);

	/* add fonts */
	for(t=0;t<fontpathpos;t++) {
		driver->setparameter(driver, "fontdir", fontpaths[t]);
	}
#ifdef _CONSOLE
	if(info_only) {
		show_info(driver, filename);
		return 0;
	}
#endif
	char*u = 0;
	if((u = strchr(outputname, '%'))) {
		if(strchr(u+1, '%') || 
			strchr(outputname, '%')!=u)  {
				msg("<error> only one %% allowed in filename\n");
				RETERROR(3);
		}
		if(preloader || viewer) {
			msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
			RETERROR(4);
		}
		//msg("<notice> outputting one file per page");
		one_file_per_page = 1;
		char*pattern = (char*)malloc(strlen(outputname)+2);
		/* convert % to %d */
		int l = u-outputname+1;
		memcpy(pattern, outputname, l);
		pattern[l]='d';
		strcpy(pattern+l+1, outputname+l);
		outputname = pattern;
	}
#ifdef _CONSOLE
	gfxdocument_t* pdf = driver->open(driver, filename);
#else
	gfxdocument_t* pdf;
	if(openStream)
		pdf = driver->open_stream(driver, stream, pdfDoc);
	else
		pdf = driver->open(driver, filename);
#endif

	if(!pdf) {
		msg("<error> Couldn't open %s", filename);
		RETERROR(5);
	}
	/* pass global parameters document */
	p = device_config;
	while(p) {
		pdf->setparameter(pdf, p->name, p->value);
		p = p->next;
	}

	struct mypage_t {
		int x;
		int y;
		gfxpage_t*page;
	} pages[4];

	int pagenum = 0;
	int frame = 1;
	int pagenr;

	for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
	{
		if(is_in_range(pagenr, pagerange)) {
			char mapping[80];
			sprintf(mapping, "%d:%d", pagenr, frame);
			pdf->setparameter(pdf, "pagemap", mapping);
			pagenum++;
		}
		if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
			pagenum = 0;
			frame++;
		}
	}
	if(pagerange && !pagenum && frame==1) {
		fprintf(stderr, "No pages in range %s", pagerange);
		RETERROR(6);
	}

	pagenum = 0;

	gfxdevice_t*out = create_output_device();;
	pdf->prepare(pdf, out);

	for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
	{
		DWORD waitRes = WaitForSingleObject(exportParams->hExportSwfCancel,0);
		if(waitRes ==  WAIT_OBJECT_0){
			SetEvent(exportParams->hExportSwfCancelled);
			ret = 1;
			goto clean;
		}else{
			if(exportParams->m_ExportSwfProgressHandle != 0){
				if(!exportParams->m_ExportSwfProgressHandle(pdf->num_pages,pagenr) !=0){
					SetEvent(exportParams->hExportSwfCancelled);
					ret = 4;
					goto clean;
				}
			}
		}
				
		if(is_in_range(pagenr, pagerange)) {
			gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
			pages[pagenum].x = 0;
			pages[pagenum].y = 0;
			pages[pagenum].page = page;
			pagenum++;
		}
		if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) 
		{
			int t;
			int *xmax = new int[xnup];
			//[xnup]
			int *ymax = new int[xnup];
			//[xnup];
			int x,y;
			int width=0, height=0;

			memset(xmax, 0, xnup*sizeof(int));
			memset(ymax, 0, ynup*sizeof(int));

			for(y=0;y<ynup;y++)
				for(x=0;x<xnup;x++) {
					int t = y*xnup + x;

					if(pages[t].page->width > xmax[x])
						xmax[x] = (int)pages[t].page->width;
					if(pages[t].page->height > ymax[y])
						ymax[y] = (int)pages[t].page->height;
				}
				for(x=0;x<xnup;x++) {
					width += xmax[x];
					xmax[x] = width;
				}
				for(y=0;y<ynup;y++) {
					height += ymax[y];
					ymax[y] = height;
				}
				if(custom_clip) {
					out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
				} else {
					out->startpage(out,width,height);
				}
				for(t=0;t<pagenum;t++) 
				{
					int x = t%xnup;
					int y = t/xnup;
					int xpos = x>0?xmax[x-1]:0;
					int ypos = y>0?ymax[y-1]:0;
					msg("<verbose> Render (%d,%d) move:%d/%d\n",
						(int)(pages[t].page->width + xpos),
						(int)(pages[t].page->height + ypos), xpos, ypos);
					
					pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos, 
						custom_move? move_y : ypos,
						custom_clip? clip_x1 : 0 + xpos, 
						custom_clip? clip_y1 : 0 + ypos, 
						custom_clip? clip_x2 : pages[t].page->width + xpos, 
						custom_clip? clip_y2 : pages[t].page->height + ypos);
				}
				out->endpage(out);
				for(t=0;t<pagenum;t++)  {
					pages[t].page->destroy(pages[t].page);
				}
				pagenum = 0;

				if(one_file_per_page) {
					gfxresult_t*result = out->finish(out);out=0;
					char buf[1024];
					sprintf(buf, outputname, pagenr);
					if(result->save(result, buf) < 0) {
						return 1;
					}
					result->destroy(result);result=0;
					out = create_output_device();;
					msg("<notice> Writing SWF file %s", buf);
				}
				delete xmax;
				delete ymax;
		}
	}
	
	if(one_file_per_page) {
		// remove empty device
		gfxresult_t*result = out->finish(out);out=0;
		result->destroy(result);result=0;
	} else {
		gfxresult_t*result = out->finish(out);
		msg("<notice> Writing SWF file %s", outputname);
		if(result->save(result, outputname) < 0) 
			ret = 7;
		
		int width = (int)(ptroff_t)result->get(result, "width");
		int height = (int)(ptroff_t)result->get(result, "height");
		result->destroy(result);result=0;

		if(preloader && viewer && ret == 0) 
		{

#ifdef HAVE_MKSTEMP
			char tmpname[] = "__swf__XXXXXX";
			int fileTmp = mkstemp(tmpname);
			close(fileTmp);
#else 
			char*tmpname = "__tmp__.swf";
#endif
			
			char buf[1024];	char buf2[1024];char buf3[1024];char buf4[1024];char buf5[50];	char buf6[50]; char buf7[50];

			sprintf(buf,"viewport=%s", outputname);
			sprintf(buf2, "-o%s", tmpname);

			char *argvCombine[4] = {"swfcombine", viewer, buf, buf2 };
			mainCombine(4, argvCombine);

			sprintf(buf, "%s/PreLoaderTemplate.swf", SWFDIR);
			sprintf(buf2, "loader=%s", preloader);
			sprintf(buf3, "movie=%s", tmpname);
			sprintf(buf4, "-o%s", outputname);
			sprintf(buf5, "-X %d", width);
			sprintf(buf6, "-Y %d", height);
			sprintf(buf7, "-r %f", getRate(preloader));
			if(zlib)
			{
				char *argvCombine2[9] = {"swfcombine","-z", buf5, buf6, buf7, buf, buf2, buf3, buf4 };
				ret = mainCombine(9, argvCombine2);
			}
			else
			{
				char *argvCombine2[8] = {"swfcombine", buf5, buf6, buf7, buf, buf2, buf3, buf4 };
				ret = mainCombine(8, argvCombine2);
			}

			remove(tmpname);
		}
	}
clean:
	pdf->destroy(pdf);
	driver->destroy(driver);

	SetEvent(exportParams->hExportSwfFinished); 
	if(exportParams->m_ExportSwfFinishHandle !=0)
		exportParams->m_ExportSwfFinishHandle();
	/* free global parameters */

	p = device_config;
	while(p) {
		parameter_t*next = p->next;
		if(p->name) free((void*)p->name);p->name = 0;
		if(p->value) free((void*)p->value);p->value =0;
		p->next = 0;
		free(p);
		p = next;
	}
	device_config=0;
	if(filters) {
		free(filters);
	}
	filters=0;
	return ret;
}