/* ** Allocates new space for text buffer. */ void ocrwords_add_word(OCRWORDS *words,OCRWORD *word) { static char *funcname="ocrwords_add_word"; int i; if (words->n>=words->na) { int newsize; newsize = words->na<512 ? 1024 : words->na*2; willus_mem_realloc_robust_warn((void **)&words->word,newsize*sizeof(OCRWORD), words->na*sizeof(OCRWORD),funcname,10); for (i=words->na;i<newsize;i++) ocrword_init(&words->word[i]); words->na=newsize; } words->word[words->n]=(*word); words->word[words->n].text=NULL; willus_mem_alloc_warn((void **)&words->word[words->n].text,strlen(word->text)+1,funcname,10); strcpy(words->word[words->n].text,word->text); /* Copy char positions */ words->word[words->n].n=utf8_to_unicode(NULL,word->text,1000000); if (word->cpos!=NULL) { willus_mem_alloc_warn((void **)&words->word[words->n].cpos, words->word[words->n].n*sizeof(double),funcname,10); for (i=0;i<words->word[words->n].n;i++) words->word[words->n].cpos[i] = word->cpos[i]; } else words->word[words->n].cpos=NULL; words->n++; }
void strbuf_dsprintf_no_space(STRBUF *sbuf,STRBUF *sbuf2,char *fmt,...) { static char *funcname="strbuf_sprintf"; if (sbuf!=NULL || sbuf2!=NULL) { va_list args; char *buf; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); va_start(args,fmt); vsprintf(buf,fmt,args); va_end(args); if (buf[0]!='\0') { if (sbuf!=NULL && sbuf2==NULL) { strbuf_ensure(sbuf,(sbuf->s==NULL?0:strlen(sbuf->s))+strlen(buf)+2); strcat(sbuf->s,buf); } if (sbuf2!=NULL) { strbuf_ensure(sbuf2,(sbuf2->s==NULL?0:strlen(sbuf2->s))+strlen(buf)+2); strcat(sbuf2->s,buf); } } willus_mem_free((double **)&buf,funcname); } }
/* ** allocates new memory */ void ocrword_copy(OCRWORD *dst,OCRWORD *src) { int memsize; static char *funcname="ocrword_copy"; (*dst)=(*src); dst->text=NULL; dst->cpos=NULL; memsize=sizeof(double)*src->n; willus_mem_alloc_warn((void **)&dst->text,strlen(src->text)+1,funcname,10); strcpy(dst->text,src->text); if (src->cpos!=NULL) { willus_mem_alloc_warn((void **)&dst->cpos,memsize,funcname,10); memcpy(dst->cpos,src->cpos,memsize); } }
int utf16_to_utf8_alloc(void **d,short *s) { int len; char *d8; static char *funcname="utf16_to_utf8_alloc"; (*d)=NULL; len=utf16_to_utf8(NULL,s,-1); willus_mem_alloc_warn(d,len+1,funcname,10); d8=(char *)(*d); utf16_to_utf8(d8,s,len); return(len); }
int utf8_to_utf16_alloc(void **d,char *s) { int len; short *dw; static char *funcname="utf8_to_utf16_alloc"; (*d)=NULL; len=utf8_to_utf16(NULL,s,-1); willus_mem_alloc_warn(d,sizeof(short)*(len+1),funcname,10); dw=(short *)(*d); utf8_to_utf16(dw,s,len); return(len); }
void k2pdfopt_files_add_file(K2PDFOPT_FILES *k2files,char *filename) { static char *funcname="k2pdfopt_files_add_file"; if (k2files->n >= k2files->na) { int newsize; newsize = k2files->na<128 ? 256 : k2files->na*2; willus_mem_realloc_robust_warn((void **)&k2files->file,sizeof(char *)*newsize, sizeof(char *)*k2files->na,funcname,10); k2files->na=newsize; } willus_mem_alloc_warn((void **)&k2files->file[k2files->n],strlen(filename)+1,funcname,10); strcpy(k2files->file[k2files->n],filename); k2files->n++; }
/* ** Truncate word to chars from text[i1..i2] */ void ocrword_truncate(OCRWORD *word,int i1,int i2) { int i,n; int *u; double w0,scale; static char *funcname="ocrword_truncate"; n=word->n; if (i1<0) i1=0; if (i2>n-1) i2=n-1; if (i2<i1) { word->n=0; word->w0=0.; word->w=0; return; } w0=word->w0; /* Fix text */ willus_mem_alloc_warn((void **)&u,n*sizeof(int),funcname,10); utf8_to_unicode(u,word->text,n); unicode_to_utf8(word->text,&u[i1],i2-i1+1); willus_mem_free((double **)&u,funcname); /* Fix length */ word->n=i2-i1+1; if (word->cpos==NULL) return; /* Fix width */ word->w0 = word->cpos[i2]-(i1==0?0.:word->cpos[i1-1]); scale = word->w/w0; word->w = (int)(scale*word->w0+.5); /* Fix left position and subsequent positions */ if (i1>0) { double x0; x0=word->cpos[i1-1]; word->x0 += x0; word->c += (int)(x0*scale+.5); for (i=i1;i<=i2;i++) word->cpos[i-i1]=word->cpos[i]-x0; } }
int k2gui_cbox_vprintf(FILE *f,char *fmt,va_list args) { static char *funcname="k2gui_cbox_vprintf"; char prbuf[1024]; char *buf; int i,j,status,nlines; if (k2gui_cbox==NULL || k2gui_cbox->control[0].handle==NULL || k2gui_cbox->ncontrols<1) return(-1); status=vsprintf(prbuf,fmt,args); willus_mem_alloc_warn((void **)&buf,strlen(prbuf)*2,funcname,10); for (i=j=0;prbuf[i]!='\0';i++) { /* Insert CRs before LFs */ if (prbuf[i]=='\r' && prbuf[i+1]=='\n') { buf[j++]=prbuf[i++]; buf[j++]=prbuf[i]; continue; } if (prbuf[i]=='\n') { buf[j++]='\r'; buf[j++]=prbuf[i]; continue; } /* Remove any ANSI color code */ if (prbuf[i]=='\x1b' && prbuf[i+1]=='[') { for (i+=2;prbuf[i]!='\0' && (tolower(prbuf[i])<'a' || tolower(prbuf[i])>'z');i++); if (prbuf[i]=='\0') break; continue; } buf[j++]=prbuf[i]; } buf[j]='\0'; strbuf_sprintf(&k2gui_cbox->buf,"%s",buf); nlines=willusgui_control_nlines(&k2gui_cbox->control[0]); willusgui_control_set_text(&k2gui_cbox->control[0],strbuf_lineno(&k2gui_cbox->buf,-nlines)); willusgui_control_scroll_to_bottom(&k2gui_cbox->control[0]); willus_mem_free((double **)&buf,funcname); return(status); }
short *char_to_wide(short *d,char *s) { static char *funcname="char_to_wide"; unsigned short *x; unsigned char *p; int i; if (d==NULL) willus_mem_alloc_warn((void **)&x,sizeof(short)*(strlen(s)+1),funcname,10); else x=(unsigned short *)d; p=(unsigned char *)s; for (i=0; p[i]!='\0'; i++) x[i]=p[i]; x[i]=0; return((short *)x); }
char *wide_to_char(char *d,short *s) { static char *funcname="wide_to_char"; unsigned char *x; unsigned short *p; int i; if (d==NULL) willus_mem_alloc_warn((void **)&x,sizeof(short)*(wide_strlen(s)+1),funcname,10); else x=(unsigned char *)d; p=(unsigned short *)s; for (i=0; p[i]!=0; i++) x[i]=(unsigned char)p[i]; x[i]='\0'; return((char *)x); }
void strbuf_sprintf(STRBUF *sbuf,char *fmt,...) { static char *funcname="strbuf_sprintf"; if (sbuf!=NULL) { va_list args; char *buf; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); va_start(args,fmt); vsprintf(buf,fmt,args); va_end(args); strbuf_cat(sbuf,buf); willus_mem_free((double **)&buf,funcname); } }
compress_handle compress_start(FILE *f,int level) { compress_handle_p h; int ret; static char *funcname="compress_start"; willus_mem_alloc_warn((void **)&h,sizeof(compress_handle_t),funcname,10); h->strm.zalloc = Z_NULL; h->strm.zfree = Z_NULL; h->strm.opaque = Z_NULL; h->strm.total_in = 0; h->strm.total_out = 0; h->strm.avail_in = 0; h->strm.next_in = &h->in[0]; ret = deflateInit2(&h->strm,level,Z_DEFLATED,MAX_WBITS,8,Z_DEFAULT_STRATEGY); /* memory level 8 (default) = 128K */ if (ret != Z_OK) /* Error */ return NULL; return ((compress_handle)h); }
double string_atof(char *s) { #ifdef WIN32 return(atof(s)); #else char *p; int i; double x; static char *funcname="string_atof"; willus_mem_alloc_warn((void **)&p,strlen(s)+1,funcname,10); if (p==NULL) return(atof(s)); strcpy(p,s); for (i=0; p[i]!='\0'; i++) if (tolower(p[i])=='d') p[i]=p[i]+1; x=atof(p); willus_mem_free((double **)&p,funcname); return(x); #endif }
static int wmupdf_pdfdoc_newpages(pdf_document *xref,fz_context *ctx,WPDFPAGEINFO *pageinfo, int use_forms,FILE *out) { static char *funcname="wmupdf_pdfdoc_newpages"; pdf_obj *root,*oldroot,*pages,*kids,*countobj,*parent,*olddests; pdf_obj *srcpageobj,*srcpagecontents; pdf_obj *destpageobj,*destpagecontents,*destpageresources; double srcx0,srcy0; int qref,i,i0,pagecount,srccount,destpageref,nbb; int *srcpageused; char *bigbuf; double srcpagerot; /* Avoid compiler warning */ destpageref = 0; destpageobj = NULL; srcx0=srcy0=0.; /* Keep only pages/type and (reduced) dest entries to avoid references to unretained pages */ pagecount = pdf_count_pages(xref); if (use_forms) { willus_mem_alloc_warn((void **)&srcpageused,sizeof(int)*(pagecount+1),funcname,10); /* Mark all source pages as "not done" */ for (i=0;i<=pagecount;i++) srcpageused[i]=0; nbb=4096; willus_mem_alloc_warn((void **)&bigbuf,nbb,funcname,10); bigbuf[0]='\0'; } oldroot = pdf_dict_gets(xref->trailer,"Root"); /* ** pages points to /Pages object in PDF file. ** Has: /Type /Pages, /Count <numpages>, /Kids [ obj obj obj obj ] */ pages = pdf_dict_gets(oldroot,"Pages"); olddests = pdf_load_name_tree(xref,"Dests"); /* ** Create new root object with only /Pages and /Type (and reduced dest entries) ** to avoid references to unretained pages. */ root = pdf_new_dict(ctx,4); pdf_dict_puts(root,"Type",pdf_dict_gets(oldroot,"Type")); pdf_dict_puts(root,"Pages",pages); pdf_update_object(xref,pdf_to_num(oldroot),root); pdf_drop_obj(root); /* Parent indirectly references the /Pages object in the file */ /* (Each new page we create has to point to this.) */ parent = pdf_new_indirect(ctx, pdf_to_num(pages), pdf_to_gen(pages), xref); /* Create a new kids array with only the pages we want to keep */ kids = pdf_new_array(ctx, 1); qref=0; /* Avoid compiler warnings */ destpageresources=NULL; destpagecontents=NULL; srcpagecontents=NULL; srcpagerot=0.; for (i=0;i<=pageinfo->boxes.n;i++) if (pageinfo->boxes.box[i].dstpage>0) break; if (i>0) { if (i<pageinfo->boxes.n) memmove(&pageinfo->boxes.box[0],&pageinfo->boxes.box[i],sizeof(WPDFBOX)*pageinfo->boxes.n-i); pageinfo->boxes.n -= i; } /* Walk through PFDBOXES array */ for (i=srccount=i0=0;i<=pageinfo->boxes.n;i++) { WPDFBOX *box; int j,k,newsrc; static char buf[512]; pdf_obj *s1indirect,*qindirect,*rotobj; static double cpm[3][3],m[3][3],m1[3][3]; static double xclip[4],yclip[4]; /* printf("box[%d]\n",i); if (i<pageinfo->boxes.n) { box=&pageinfo->boxes.box[i]; printf(" srcpage=%d, dstpage=%d\n",box->srcbox.pageno,box->dstpage); printf(" x0=%g, y0=%g\n",box->x0,box->y0); printf(" w=%g, h=%g\n",box->w,box->h); printf(" x1=%g, y1=%g\n",box->x1,box->y1); printf(" sr=%g, dr=%g\n",box->srcrot_deg,box->dstrot_deg); printf(" scale=%g\n",box->scale); } */ /* Check to see if we are done with an output page */ if (srccount>0 && (i==pageinfo->boxes.n || (i>0 && pageinfo->boxes.box[i].dstpage!=pageinfo->boxes.box[i-1].dstpage))) { pdf_obj *newpageref; /* ** Store destination page into document structure */ /* printf(" ADDING NEW PAGE. (srccount=%d)\n",srccount); */ if (use_forms) { pdf_obj *dest_stream; /* Create new object in document for destination page stream */ dest_stream = pdf_new_indirect(ctx,new_stream_object(xref,ctx,bigbuf), 0,(void *)xref); /* Store this into the destination page contents array */ pdf_array_push(destpagecontents,dest_stream); pdf_drop_obj(dest_stream); } newpageref=pdf_new_indirect(ctx,destpageref,0,(void *)xref); /* Reference parent list of pages */ pdf_dict_puts(destpageobj,"Parent",parent); pdf_dict_puts(destpageobj,"Contents",destpagecontents); pdf_dict_puts(destpageobj,"Resources",destpageresources); /* Store page object in document's kids array */ pdf_array_push(kids,newpageref); /* Update document with new page */ pdf_update_object(xref,destpageref,destpageobj); /* Clean up */ pdf_drop_obj(newpageref); pdf_drop_obj(destpageresources); pdf_drop_obj(destpagecontents); pdf_drop_obj(destpageobj); /* Reset source page and index to start of new destination page */ i0=i; srccount=0; } /* Quit loop if beyond last box */ if (i>=pageinfo->boxes.n) break; box=&pageinfo->boxes.box[i]; if (box->srcbox.pageno<1 || box->srcbox.pageno>pagecount) continue; /* Is this a source page we haven't processed yet (for this destination page)? */ for (newsrc=1,j=i0;j<i;j++) { if (pageinfo->boxes.box[j].srcbox.pageno==box->srcbox.pageno) { newsrc=0; break; } } if (newsrc) { double v[4]; srccount++; if (use_forms) srcpageused[box->srcbox.pageno]=1; /* printf(" NEW SOURCE PAGE (srccount=%d)\n",srccount); */ if (srccount==1) { /* ** Start a new destination page. ** ** Each new page object is a dict type with: ** /Type /Page ** /Contents (array of objects) ** /Resources (dict) ** /MediaBox [0 0 612 792] ** /Parent <PagesObj> ** [Can have /Rotate 90, for example.] ** */ /* printf(" (STARTING NEW DEST. PAGE)\n"); */ destpageobj=start_new_destpage(ctx,box->dst_width_pts,box->dst_height_pts); destpageresources=pdf_new_dict(ctx,1); if (use_forms) pdf_dict_puts(destpageresources,"XObject",pdf_new_dict(ctx,1)); destpageref=pdf_create_object(xref); destpagecontents=pdf_new_array(ctx,1); /* Init the destination page stream for forms */ if (use_forms) bigbuf[0]='\0'; } /* New source page, so get the source page objects */ srcpageobj = xref->page_objs[box->srcbox.pageno-1]; wmupdf_page_bbox(srcpageobj,v); srcx0=v[0]; srcy0=v[1]; /* printf("SRCX0=%g, SRCY0=%g\n",srcx0,srcy0); */ rotobj=pdf_dict_gets(srcpageobj,"Rotate"); srcpagerot = rotobj!=NULL ? pdf_to_real(rotobj) : 0.; /* printf("Page rotation = %g\n",srcpagerot); */ srcpagecontents=pdf_dict_gets(srcpageobj,"Contents"); /* if (pdf_is_array(srcpagecontents)) { int k; printf(" source page contents = array.\n"); for (k=0;k<pdf_array_len(srcpagecontents);k++) { pdf_obj *obj; obj=pdf_array_get(srcpagecontents,k); if (pdf_is_indirect(obj)) { printf(" contents[%d] = indirect (%d)\n",k,pdf_to_num(obj)); pdf_resolve_indirect(obj); } } } */ if (use_forms) { pdf_obj *xobjdict; int pageno; xobjdict=pdf_dict_gets(destpageresources,"XObject"); pageno=box->srcbox.pageno; pdf_dict_puts(xobjdict,xobject_name(pageno),xref->page_refs[pageno-1]); pdf_dict_puts(destpageresources,"XObject",xobjdict); } else { pdf_obj *srcpageresources; /* Merge source page resources into destination page resources */ srcpageresources=pdf_dict_gets(srcpageobj,"Resources"); /* printf("box->dstpage=%d, srcpage=%d (ind.#=%d)\n",box->dstpage,box->srcbox.pageno,pdf_to_num(xref->page_refs[box->srcbox.pageno-1])); */ wmupdf_dict_merge(ctx,"Resources",destpageresources,srcpageresources); } } /* ** Process this source box: ** ** Create a tranformation matrix and clipping path to only show the ** desired part of the source page at the appropriate place on the ** destination page. ** ** How the tranformation matrix works: ** - Translations shall be specified as [ 1 0 0 1 tx ty ], where tx and ty ** shall be the distances to translate the origin of the coordinate system ** in the horizontal and vertical dimensions, respectively. ** ** - Scaling shall be obtained by [ sx 0 0 sy 0 0 ]. This scales the coordinates ** so that 1 unit in the horizontal and vertical dimensions of the new coordinate ** system is the same size as sx and sy units, respectively, in the previous ** coordinate system. ** ** - Rotations shall be produced by [ cos q sin q -sin q cos q 0 0 ], which has the ** effect of rotating the coordinate system axes by an angle q counter-clockwise. ** ** - Skew shall be specified by [ 1 tan a tan b 1 0 0 ], which skews the x axis by ** an angle a and the y axis by an angle b. ** */ wpdfbox_determine_original_source_position(box); /* printf("Before unrotate.\n"); printf("box->srcrot=%g\n",box->srcrot_deg); printf("box->x0=%g, box->y0=%g\n",box->x0,box->y0); printf("box->w=%g, box->h=%g\n",box->w,box->h); printf("box->pw=%g, box->ph=%g\n",box->src_width_pts,box->src_height_pts); */ if (fabs(srcpagerot) > 1.0e-4) wpdfbox_unrotate(box,srcpagerot); /* printf("box->srcrot=%g\n",box->srcrot_deg); printf("box->x0=%g, box->y0=%g\n",box->x0,box->y0); printf("box->w=%g, box->h=%g\n",box->w,box->h); printf("box->pw=%g, box->ph=%g\n",box->src_width_pts,box->src_height_pts); */ matrix_unity(m,1.); /* printf("xfmatrix = [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n", m[0][0],m[0][1],m[0][2], m[1][0],m[1][1],m[1][2], m[2][0],m[2][1],m[2][2]); */ matrix_translate(m1,-box->x0-srcx0,-box->y0-srcy0); matrix_mul(m,m1); matrix_rotate(m1,-box->srcrot_deg+box->dstrot_deg); matrix_mul(m,m1); matrix_unity(m1,box->scale); matrix_mul(m,m1); matrix_translate(m1,box->x1,box->y1); matrix_mul(m,m1); matrix_zero_round(m); matrix_rotate(cpm,box->srcrot_deg); matrix_translate(m1,box->x0+srcx0,box->y0+srcy0); matrix_mul(cpm,m1); /* printf("Clip matrix:\n"); printf("xfmatrix = [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n", cpm[0][0],cpm[0][1],cpm[0][2], cpm[1][0],cpm[1][1],cpm[1][2], cpm[2][0],cpm[2][1],cpm[2][2]); */ set_clip_array(xclip,yclip,box->srcrot_deg,box->w,box->h); for (k=0;k<4;k++) matrix_xymul(cpm,&xclip[k],&yclip[k]); /* printf("Clip path:\n %7.2f %7.2f\n %7.2f,%7.2f\n %7.2f,%7.2f\n" " %7.2f %7.2f\n %7.2f,%7.2f\n", xclip[0],yclip[0],xclip[1],yclip[1],xclip[2],yclip[2], xclip[3],yclip[3],xclip[0],yclip[0]); */ strcpy(buf,"q"); for (k=0;k<=2;k++) { cat_pdf_double(buf,m[k][0]); cat_pdf_double(buf,m[k][1]); } strcat(buf," cm"); for (k=0;k<=4;k++) { cat_pdf_double(buf,xclip[k&3]); cat_pdf_double(buf,yclip[k&3]); strcat(buf,k==0 ? " m" : " l"); } strcat(buf," W n"); if (use_forms) { /* FORM METHOD */ sprintf(&buf[strlen(buf)]," /%s Do Q\n",xobject_name(box->srcbox.pageno)); if (strlen(bigbuf)+strlen(buf) > nbb) { int newsize; newsize=nbb*2; willus_mem_realloc_robust_warn((void **)&bigbuf,newsize,nbb,funcname,10); nbb=newsize; } strcat(bigbuf,buf); } else { /* NO-FORMS METHOD */ strcat(buf,"\n"); /* Create new objects in document for tx matrix and restore matrix */ s1indirect = pdf_new_indirect(ctx,new_stream_object(xref,ctx,buf),0,(void *)xref); if (qref==0) qref=new_stream_object(xref,ctx,"Q\n"); qindirect = pdf_new_indirect(ctx,qref,0,(void *)xref); /* Store this region into the destination page contents array */ pdf_array_push(destpagecontents,s1indirect); if (pdf_is_array(srcpagecontents)) { int k; for (k=0;k<pdf_array_len(srcpagecontents);k++) pdf_array_push(destpagecontents,pdf_array_get(srcpagecontents,k)); } else pdf_array_push(destpagecontents,srcpagecontents); pdf_array_push(destpagecontents,qindirect); pdf_drop_obj(s1indirect); pdf_drop_obj(qindirect); } } pdf_drop_obj(parent); /* For forms, convert all original source pages to XObject Forms */ if (use_forms) wmupdf_convert_pages_to_forms(xref,ctx,srcpageused); /* Update page count and kids array */ countobj = pdf_new_int(ctx, pdf_array_len(kids)); pdf_dict_puts(pages, "Count", countobj); pdf_drop_obj(countobj); pdf_dict_puts(pages, "Kids", kids); pdf_drop_obj(kids); /* Also preserve the (partial) Dests name tree */ if (olddests) wmupdf_preserve_old_dests(olddests,ctx,xref,pages); if (use_forms) { /* Free memory */ willus_mem_free((double **)&bigbuf,funcname); willus_mem_free((double **)&srcpageused,funcname); } return(0); }
/* ** Open file using file association if necessary. This can be necessary if ** the file is being used by another process. */ int willusgui_open_file_ex(char *filename) { char ext[512]; short *extw; short exew[512]; char *exename; char pwd[512]; char cmdargs[512]; int maxsize,status,procnum; HRESULT hr; static char *funcname="willusgui_open_file_ex"; status=willusgui_open_file(filename); if (status!=-1) return(status); strncpy(&ext[1],wfile_ext(filename),510); ext[511]='\0'; ext[0]='.'; utf8_to_utf16_alloc((void **)&extw,ext); maxsize=511; hr=AssocQueryStringW(0,ASSOCSTR_EXECUTABLE,(LPCWSTR)extw,NULL,(LPWSTR)exew,(DWORD *)&maxsize); willus_mem_free((double **)&extw,funcname); if (hr==S_OK) utf16_to_utf8_alloc((void **)&exename,exew); if (hr!=S_OK || wfile_status(exename)!=1) { char *message; int len; static char *funcname="willusgui_open_file_ex"; static int bcolors[3]= {0x6060b0,0xf0f0f0,0xf0f0f0}; if (hr==S_OK) willus_mem_free((double **)&exename,funcname); len=strlen(filename); willus_mem_alloc_warn((void **)&message,len+80,funcname,10); sprintf(message,"Cannot find application to open %s!",filename); willusgui_message_box(NULL,"Error",message,"*&OK",NULL,NULL, NULL,0,24,640,0xe0e0e0,bcolors,NULL,1); willus_mem_free((double **)&message,funcname); return(0); } /* Put quotes around filename -> cmdargs */ strncpy(&cmdargs[1],filename,509); cmdargs[510]='\0'; cmdargs[0]='\"'; maxsize=strlen(cmdargs); cmdargs[maxsize]='\"'; cmdargs[maxsize+1]='\0'; /* Get working directory */ strncpy(pwd,wfile_get_wd(),511); pwd[511]='\0'; /* Make sure PWD isn't UNC -- if it is, default C:\ */ if (pwd[0]=='\\' && pwd[1]=='\\') strcpy(pwd,"C:\\"); process_launch_ex(exename,cmdargs,1,1,pwd,1,&procnum); willus_mem_free((double **)&exename,funcname); return(1); /* if (procnum>=0) { int status,exitcode; while ((status=process_done_ex(procnum,&exitcode))==0) win_sleep(10); if (exitcode==0) return(1); } */ }
int win_clipboard_to_bmp(WILLUSBITMAP *bmp,FILE *out) { static char *funcname="win_clipboard_to_bmp"; HBITMAP hbitmap; HWND dtwin; HDC hDC,hMemDC; dtwin=GetDesktopWindow(); if (!OpenClipboard(dtwin)) { nprintf(out,"win_clipboard_to_bmp: Error opening clipboard.\n"); return(-1); } hbitmap=(HBITMAP)GetClipboardData(CF_BITMAP); if (hbitmap==NULL) { nprintf(out,"win_clipboard_to_bmp: Clipboard does not have a bitmap.\n"); CloseClipboard(); return(-2); } // This is kludgy, but works for 24-bit desktops hDC=GetDC(dtwin); if (hDC==NULL) { nprintf(out,"win_clipboard_to_bmp: Could not get DC.\n"); CloseClipboard(); return(-3); } // Create a device context for the windows bitmap hMemDC=CreateCompatibleDC(hDC); if (hMemDC==NULL) { nprintf(out,"win_clipboard_to_bmp: Could not create hMemDC.\n"); // DeleteObject(hbitmap); ReleaseDC(dtwin,hDC); return(-4); } // Select the windows bitmap into the new device context SelectObject(hMemDC,hbitmap); /* Kludgey way to get BMP dims, but GetBitmapDimensionsEx() doesn't work. */ { int step; for (step=2048,bmp->width=0;1;bmp->width+=step) { if (GetPixel(hMemDC,bmp->width,0)==CLR_INVALID) { if (step==1 || bmp->width==0) break; bmp->width -= step; step >>= 1; } } for (step=1024,bmp->height=0;1;bmp->height+=step) { if (GetPixel(hMemDC,0,bmp->height)==CLR_INVALID) { if (step==1 || bmp->height==0) break; bmp->height -= step; step >>= 1; } } } if (bmp->height==0 || bmp->width==0) { nprintf(out,"win_clipboard_to_bmp: Zero sized bitmap.\n"); DeleteDC(hMemDC); ReleaseDC(dtwin,hDC); return(-5); } bmp->bpp=24; bmp->type=WILLUSBITMAP_TYPE_WIN32; bmp_alloc(bmp); // Set up the device independent bitmap { BITMAPINFO *bmi; willus_mem_alloc_warn((void **)&bmi,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD),funcname,10); bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi->bmiHeader.biWidth = bmp->width; bmi->bmiHeader.biHeight = bmp->height; bmi->bmiHeader.biPlanes = 1; bmi->bmiHeader.biBitCount = 24; bmi->bmiHeader.biCompression = BI_RGB; bmi->bmiHeader.biSizeImage = 0; bmi->bmiHeader.biXPelsPerMeter = (int)(72.0*100./2.54); bmi->bmiHeader.biYPelsPerMeter = (int)(72.0*100./2.54); bmi->bmiHeader.biClrUsed = 0; bmi->bmiHeader.biClrImportant = 0; GetDIBits(hMemDC,hbitmap,0,bmp->height,bmp->data,bmi,DIB_RGB_COLORS); /* Old way--pretty slow */ /* for (row=0;row<bmp->height;row++) { unsigned char *p; int col; p=bmp_rowptr_from_top(bmp,row); for (col=0;col<bmp->width;col++,p+=3) { int x; x=(int)GetPixel(hMemDC,col,row); p[0]=x&0xff; p[1]=(x>>8)&0xff; p[2]=(x>>16)&0xff; } } */ willus_mem_free((double **)&bmi,funcname); } DeleteDC(hMemDC); ReleaseDC(dtwin,hDC); return(0); }
/* ** Conversion thread. This is launched as a separate thread to do the ** conversions while the dialog box displays the progress. */ static void k2gui_cbox_start_conversion(void *data) { void **ptrs; int i; K2PDFOPT_CONVERSION *k2conv; K2PDFOPT_SETTINGS *k2settings; K2PDFOPT_FILELIST_PROCESS k2listproc; static char *funcname="k2gui_cbox_start_conversion"; ptrs=(void **)data; k2conv=(K2PDFOPT_CONVERSION *)ptrs[0]; k2settings=&k2conv->k2settings; /* ** Count files */ k2gui_cbox_set_num_files(1); k2gui_cbox_set_files_completed(0,"Counting files..."); overwrite_set(1); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { k2listproc.bmp=NULL; k2listproc.outname=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_GET_FILECOUNT; k2pdfopt_proc_wildarg(k2settings,k2conv->k2files.file[i],&k2listproc); willus_mem_free((double **)&k2listproc.outname,funcname); } if (k2listproc.filecount==0) { k2gui_cbox_set_files_completed(0,"No files"); k2gui_alertbox(0,"No files","No files can be opened for conversion."); k2gui_cbox_conversion_thread_cleanup(); return; } k2gui_cbox_set_num_files(k2listproc.filecount); k2gui_cbox_set_files_completed(0,NULL); /* ** Process files */ k2gui_cbox_set_error_count(0); k2gui_cbox_freelist(); overwrite_set(0); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { char *buf; K2PDFOPT_CONVERSION *k2c; STRBUF *cmdline,_cmdline; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); k2gui_cbox_set_num_pages(1); sprintf(buf,"Starting conversion of %s...",k2gui_short_name(k2conv->k2files.file[i])); k2gui_cbox_set_pages_completed(0,buf); willus_mem_alloc_warn((void **)&k2c,sizeof(K2PDFOPT_CONVERSION),funcname,10); k2pdfopt_conversion_init(k2c); cmdline=&_cmdline; strbuf_init(cmdline); k2gui_settings_to_cmdline(cmdline,&k2conv->k2settings); parse_cmd_args(k2c,k2gui->env,cmdline,&k2gui->cmdxtra,1,1); k2listproc.outname=NULL; k2listproc.bmp=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_CONVERT_FILES; k2pdfopt_proc_wildarg(&k2c->k2settings,k2conv->k2files.file[i],&k2listproc); #if (WILLUSDEBUGX & 0x2000) printf("\n\nDone conversion...\n\n"); #endif str0_addstring(&k2gui_cbox->filelist,&k2gui_cbox->filelist_na,k2listproc.outname); willus_mem_free((double **)&k2listproc.outname,funcname); k2pdfopt_conversion_close(k2c); willus_mem_free((double **)&k2c,funcname); willus_mem_free((double **)&buf,funcname); } k2gui_cbox_set_files_completed(k2listproc.filecount,NULL); if (k2listproc.filecount==k2conv->k2files.n && k2gui_cbox->error_count==0) k2gui_cbox->successful=1; k2gui_cbox_conversion_thread_cleanup(); }