[dviout] Patch for dvispc.c and related docs
Hironobu YAMASHITA
h.y.acetaminophen at gmail.com
Thu Jun 8 12:34:46 CEST 2017
Hello,
I wrote a patch for "dvispc.c" and its document files ("dvispc.txt" and
"dvispce.txt") to extend its functions. Included are:
[1] Support for page independence of \special's in reverse order too.
[2] Support \special{pdf:bcolor ...}, \special{pdf:ecolor} and
\special{pdf:bgcolor ...}.
[3] New option -z to append blank page, if necessary to have multiple
of four output pages.
I will explain the detail in the latter half of this mail.
And, I'd like to propose adding "dvispc" to TeX Live Unix.
I cannot find any program in TeX Live to convert DVI into
page-independent DVI in \special's.
Of course it is already included as part of dviout previewer, but
it's true on win32 only. However, I think "dvispc.c" can be easily
compiled on unix systems and very useful.
(dviselect and co. from seetex is a good pioneer, I think)
I'm willing to provide more information if needed.
The following is the details of my patch:
[1] Current "dvispc" is not enough for e.g.
% test1.tex
\documentclass{article}
\usepackage{color}
\begin{document}
white\newpage % page 1 should be white
\pagecolor{red}
red\newpage % page 2 should be red
red % page 3 should be red
\end{document}
$ latex test1.tex
$ dvispc test1.dvi % <= fails (no change)
$ dvidvi -r test1.dvi
This makes page 1 (which comes at last) red. My patch resolves this
problem.
[2] These pdf:... specials are used in dvipdfmx.def of graphics, and
equivalent of \special{color push ...}, \special{color pop} and
\special{background ...}. My patch adds support for such specials.
[3] This is meant for use with dvibook; dvibook adds extra blank page,
but it does nothing about \special's. By using -z option, "dvispc" can
add page-independent white page.
$ dvispc -z in.dvi out.dvi
Best,
Hironobu Yamashita
-------------- next part --------------
diff --git a/dvispc.c b/dvispc.c
index ee3600f..2fd0209 100644
--- a/dvispc.c
+++ b/dvispc.c
@@ -4,7 +4,8 @@
* A program to modify DVI file to be page-independent
* Support the following specials
* color specials: push, pop, background
- * tpic specials : sh, pn
+ * pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor
+ * tpic specials: pn
*
* Written by SHIMA
* January 2003
@@ -228,21 +231,30 @@ int f_sjis = 0; /* -j */
int f_sjis = 1;
#endif
int f_pos = 0; /* position */
+int f_book = 0; /* multiple of four pages */
int f_background = 0;
+int f_pdf_bgcolor = 0;
int f_pn = 0;
int f_backup = 0; /* output=input */
int f_ptex = 0;
+int f_prescan = 0;
int max_stack;
char *out_pages ="T-L";
+int total_book_page;
int color_depth;
int color_depth_max;
int color_under;
+int pdf_color_depth;
+int pdf_color_depth_max;
+int pdf_color_under;
int f_color;
char *color_pt[MAX_COLOR];
+char *pdf_color_pt[MAX_COLOR];
char color_buf[COLOR_BUF_SIZE];
char background[MAX_LEN];
+char pdf_bgcolor[MAX_LEN];
char tpic_pn[MAX_LEN];
char tmp_buf[COMMON_SIZE];
FILE *fp_in;
@@ -285,6 +297,8 @@ uint work(FILE *);
uint s_work(FILE *);
int strsubcmp(char *s, char *t);
void sp_color(char *sp);
+void sp_pdf_bcolor(char *sp);
+void sp_pdf_ecolor(char *sp);
void read_post(DVIFILE_INFO *dvi);
uint interpret(FILE *);
void make_page_index(DVIFILE_INFO *dvi, DIMENSION *dim);
@@ -361,28 +375,51 @@ Long read_long(FILE *fp)
}
+void write_word(int x, FILE *fp)
+{
+ write_byte((x >> 8) & 0xff, fp);
+ write_byte(x & 0xff, fp);
+}
+
+
+Long read_word(FILE *fp)
+{
+ int i;
+
+ i = read_byte(fp) << 8;
+ return i + read_byte(fp);
+}
+
+
void usage(void)
{
fprintf(stderr,
"\t Modify a DVI file to be page-independent in specials\n"
"\t Translation between DVI file <-> Text file\n"
"\t Ver.0.3 written by SHIMA, Jan. 2003\n\n"
- "Usage: dvispc [-c] [-bv] input_dvi_file [output_dvi_file]\n"
+ "Usage: dvispc [-c] [-bvz] input_dvi_file [output_dvi_file]\n"
" dvispc -d input_dvi_file\n"
" dvispc -s [-p..] input_dvi_file [output_text_file]\n"
" dvispc -a [-jltv][-p..][-r..] input_dvi_file [output_text_file]\n"
- " dvispc -x[..] [-dltv][-r..] [input_text_file] output_dvi_file\n"
- " -c: make page-indepent DVI in specials for color push/pop, background, pn\n"
+ " dvispc -x[..] [-ltv][-r..] [input_text_file] output_dvi_file\n"
+ " -c: make page-indepent DVI in specials (default)\n"
" -d: check page-independence\n"
" -b: backup original even if output_dvi_file is not given\n"
" -s: show specials\n"
" -a: translate DVI to Text\n"
" -x: translate Text to DVI (-x0:str0 1:chkfnt 2:variety)\n"
" -v: verbose -j: Japanese characters -l: location\n"
+ " -z: append empty pages if necessary to have multiple of 4 pages for book\n"
" -r: replace (-rorg_1=new_1/org_2=new_2... eg. -rxxx=special/fnt=font)\n"
" -p: T:preamble L:postamble pages with - (eg: -pT-L -pT2/4-8L -p-4 etc.)\n"
- " -t: comaptible to DLT (the followings are suboptions if necessary eg. -t02)\n"
+ " -t: compatible to DTL (the followings are suboptions if necessary eg. -t02)\n"
" 0:str 1:ch 2:ch2 3:cmd 4:c-sum 5:dir/name 6:err 7:page 8:oct 9:str0\n"
+ " output_text_file : stdout if it is not specified.\n"
+ " input_text_file : stdin if it is not specified.\n\n"
+ "Supported specials:\n"
+ " color specials: push, pop, background\n"
+ " pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor\n"
+ " tpic specials: pn\n"
);
exit(1);
}
@@ -490,6 +527,10 @@ error: fprintf(stderr, "Error in parameter %s\n", argv[i]);
f_pos = 1;
break;
+ case 'z':
+ f_book = 1;
+ break;
+
default:
fprintf(stderr, "Unknown option:%s\n", argv[i]);
exit(1);
@@ -729,10 +770,24 @@ lastpage: if(isdigit(*++out_pages)){
fclose(fp_out);
dvi->file_ptr = fp_out = NULL;
return;
+ } /* if(f_mode == EXE2TEXT || f_mode == EXE2SPECIAL) */
+
+ /* Prior scanning. This ensures page independence in reverse order too,
+ by checking whether non-stack specials appears somewhere in DVI.
+ Specials with paired syntax (push/pop, bcolor/ecolor) are already safe
+ without pre-scanning, so these are skipped due to f_prescan = 1.
+ Other specials (background, pdf_bgcolor, pn) are handled in this scanning. */
+ if(f_mode == EXE2INDEP || f_mode == EXE2CHECK){
+ f_prescan = 1; /* change behavior of interpret(dvi) */
+ for(page = 1; page <= dim->total_page; page++){
+ fseek(dvi->file_ptr, dim->page_index[page], SEEK_SET);
+ interpret(dvi->file_ptr);
+ }
+ f_prescan = 0; /* restore interpret(dvi) */
}
former = current = -1;
- if(fp){
+ if(fp){ /* f_mode == EXE2INDEP and can be opened */
fseek(dvi->file_ptr, 0, SEEK_SET);
for(size = dim->page_index[1]; size > 0; size--)
write_byte(read_byte(dvi->file_ptr), fp); /* Write preamble */
@@ -745,17 +800,25 @@ lastpage: if(isdigit(*++out_pages)){
for(page = 1; page <= dim->total_page; page++){
fseek(dvi->file_ptr, dim->page_index[page], SEEK_SET);
f_background = 0;
+ f_pdf_bgcolor = 0;
pos = interpret(dvi->file_ptr); /* pos = position of EOP + 1 */
if(f_debug){
fprintf(fp_out, "[%d]", page);
if(page <= dim->total_page){
flag = color_depth;
+ flag += pdf_color_depth;
if(background[0] && !f_background){
fprintf(fp_out, "\n%s", background);
flag++;
}
+ if(pdf_bgcolor[0] && !f_pdf_bgcolor){
+ fprintf(fp_out, "\n%s", pdf_bgcolor);
+ flag++;
+ }
for(count = 0; count < color_depth; count++)
fprintf(fp_out, "\n%d:%s", count+1, color_pt[count]);
+ for(count = 0; count < pdf_color_depth; count++)
+ fprintf(fp_out, "\n%d:%s", count+1, pdf_color_pt[count]);
if(tpic_pn[0] && f_pn < 0){
fprintf(fp_out, "\n%s", tpic_pn);
flag++;
@@ -767,17 +830,26 @@ lastpage: if(isdigit(*++out_pages)){
}
}
if(f_mode != EXE2INDEP)
- continue;
+ continue; /* skip loop if(f_mode == EXE2CHECK || f_mode == EXE2DVI) */
while(color_under > 0){ /* recover underflow of color stack */
write_sp(fp, "color push Black");
f_color++;
color_under--;
}
+ while(pdf_color_under > 0){ /* recover underflow of pdf:bcolor ... pdf:ecolor stack */
+ write_sp(fp, "pdf:bcolor [0]");
+ f_color++;
+ pdf_color_under--;
+ }
if(background[0] && !f_background){ /* no background in this page */
write_sp(fp, background);
f_color++;
}
+ if(pdf_bgcolor[0] && !f_pdf_bgcolor){ /* no pdf:bgcolor in this page */
+ write_sp(fp, pdf_bgcolor);
+ f_color++;
+ }
fseek(dvi->file_ptr, dim->page_index[page]+45, SEEK_SET);
for(size = pos - dim->page_index[page] - 46; size > 0; size--)
write_byte(read_byte(dvi->file_ptr), fp); /* write contents of the current page */
@@ -786,6 +858,10 @@ lastpage: if(isdigit(*++out_pages)){
write_sp(fp, "color pop");
f_color++;
}
+ for(count = 0; count < pdf_color_depth; count++){
+ write_sp(fp, "pdf:ecolor");
+ f_color++;
+ }
write_byte((uchar)EOP, fp); /* write EOP */
former = current;
current = ftell(fp); /* get position of BOP/POST */
@@ -797,17 +873,22 @@ lastpage: if(isdigit(*++out_pages)){
write_long(former, fp); /* position of BOP of the former page */
for(count = 0; count < color_depth; count++)
write_sp(fp, color_pt[count]);
+ for(count = 0; count < pdf_color_depth; count++)
+ write_sp(fp, pdf_color_pt[count]);
if(tpic_pn[0]){
write_sp(fp, tpic_pn);
f_color++;
}
f_color += color_depth;
+ f_color += pdf_color_depth;
if(tpic_pn[0])
f_color++;
}
}
if(f_debug && color_depth_max)
fprintf(fp_out, "\nMaximal depth of color stack:%d", color_depth_max);
+ if(f_debug && pdf_color_depth_max)
+ fprintf(fp_out, "\nMaximal depth of pdf:bcolor ... pdf:ecolor stack:%d", pdf_color_depth_max);
if(f_mode != EXE2INDEP){
fclose(dvi->file_ptr);
fprintf(fp_out, f_color?"\nSome corrections are necessary!\n":
@@ -816,12 +897,49 @@ lastpage: if(isdigit(*++out_pages)){
dvi->file_ptr = fp_out = NULL;
return;
}
+
+ /* if -z option is given, add empty pages to make multiple of 4 pages */
+ if(f_book && dim->total_page%4 == 0)
+ f_book = 0; /* modification unnecessary */
+ if(f_book){
+ total_book_page = dim->total_page + (4 - dim->total_page%4)%4;
+ for(page = dim->total_page; page < total_book_page; page++){
+ write_byte((uchar)BOP,fp);
+ write_long(-1, fp);
+ for (count = 1; count < 10; count++) /* set all sub counts to 0 */
+ write_long(0, fp);
+ write_long(former, fp);
+ /* always white page */
+ if(background[0]) /* background is used somewhere */
+ write_sp(fp, "background gray 1");
+ if(pdf_bgcolor[0]) /* pdf:bgcolor is used somewhere */
+ write_sp(fp, "pdf:bgcolor [1]");
+ write_byte((uchar)EOP,fp);
+ former = current;
+ current = ftell(fp); /* get position of BOP/POST */
+ }
+ }
+
write_byte((uchar)POST,fp); /* write POST */
write_long(former, fp); /* position of the last BOP */
fseek(dvi->file_ptr, dvi->post + 5, SEEK_SET);
- for(size = dvi->pt_post - dvi->post - 5; size-- > 0; )
- write_byte(read_byte(dvi->file_ptr), fp); /* write postamble upto post_post */
+ if(f_book){
+ write_long(read_long(dvi->file_ptr), fp); /* numerator */
+ write_long(read_long(dvi->file_ptr), fp); /* denominator */
+ write_long(read_long(dvi->file_ptr), fp); /* magnification */
+ write_long(read_long(dvi->file_ptr), fp); /* tallest page height */
+ write_long(read_long(dvi->file_ptr), fp); /* widest page width */
+ write_word(read_word(dvi->file_ptr), fp); /* DVI stack size */
+ read_word(dvi->file_ptr); /* skip original number of pages */
+ write_word(total_book_page, fp); /* new number of pages */
+ for(size = dvi->pt_post - dvi->post - 29; size-- > 0; )
+ write_byte(read_byte(dvi->file_ptr), fp); /* write postamble upto post_post */
+ }
+ else{
+ for(size = dvi->pt_post - dvi->post - 5; size-- > 0; )
+ write_byte(read_byte(dvi->file_ptr), fp); /* write postamble upto post_post */
+ }
write_long(current, fp); /* position of POST */
read_long(dvi->file_ptr); /* skip old position of POST */
write_byte(read_byte(dvi->file_ptr), fp); /* write id = 2/3 */
@@ -834,7 +952,7 @@ lastpage: if(isdigit(*++out_pages)){
fclose(fp);
fclose(dvi->file_ptr);
fp = dvi->file_ptr = NULL;
- if(!f_color){
+ if(!f_color && !f_book){
unlink(outfile);
fprintf(stderr, "\nNo correction is done.\n");
return;
@@ -1066,12 +1184,20 @@ skip: while (tmp--)
!strsubcmp(special, "ar") || /* ar: draw circle */
!strsubcmp(special, "ia")) ) /* ia: fill */
f_pn = -1;
- else if(!strsubcmp(special, "color")) /* color push/pop */
+ else if(!strsubcmp(special, "color") && !f_prescan) /* color push/pop */
sp_color(special);
- else if(!strsubcmp(special, "background")){ /* background */
+ else if(!strsubcmp(special, "pdf:bcolor") && !f_prescan) /* pdf:bcolor */
+ sp_pdf_bcolor(special);
+ else if(!strsubcmp(special, "pdf:ecolor") && !f_prescan) /* pdf:ecolor */
+ sp_pdf_ecolor(special);
+ else if(!strsubcmp(special, "background")){ /* background */
strncpy(background, special, MAX_LEN);
f_background = 1;
}
+ else if(!strsubcmp(special, "pdf:bgcolor")){ /* pdf:bgcolor */
+ strncpy(pdf_bgcolor, special, MAX_LEN);
+ f_pdf_bgcolor = 1;
+ }
break;
}
goto skip;
@@ -1116,6 +1242,45 @@ void sp_color(char *sp)
}
}
+/* pdf:bcolor special */
+void sp_pdf_bcolor(char *sp)
+{
+ char *s;
+
+ /* copied from "color push" routine of sp_color */
+ if(pdf_color_depth >= MAX_COLOR)
+ error("Too many pdf:bcolor > 512");
+ if(pdf_color_depth){
+ s = pdf_color_pt[pdf_color_depth-1];
+ s += strlen(s) + 1;
+ }
+ else
+ s = color_buf;
+ if(s - color_buf + strlen(sp) >= COLOR_BUF_SIZE - 2)
+ error("Too much color definitions");
+ else{
+ strcpy(s, sp);
+ pdf_color_pt[pdf_color_depth++] = s;
+ }
+ if(pdf_color_depth > pdf_color_depth_max)
+ pdf_color_depth_max = pdf_color_depth;
+}
+
+/* pdf:ecolor special */
+void sp_pdf_ecolor(char *sp)
+{
+ char *s;
+
+ /* copied from "color pop" routine of sp_color */
+ if(--pdf_color_depth < 0){
+ fprintf(stderr, "pdf:bcolor ... pdf:ecolor stack underflow\n");
+ pdf_color_under++;
+ f_color++;
+ pdf_color_depth = 0;
+ }
+ return;
+}
+
int num_add;
int top_add;
diff --git a/dvispc.txt b/dvispc.txt
index a1396cd..2ba1c81 100644
--- a/dvispc.txt
+++ b/dvispc.txt
@@ -1,26 +1,32 @@
-[2] Modify a DVI file to be page-independent in specials
-[1] Translation between DVI file <-> Text file
- Ver.0.2 written by SHIMA, Jan. 2003
+[2] Modify a DVI file to be page-independent in specials
+[1] Translation between DVI file <-> Text file
+ Ver.0.3 written by SHIMA, Jan. 2003
-Usage: dvispc [-c] [-bv] input_dvi_file [output_dvi_file]
+Usage: dvispc [-c] [-bvz] input_dvi_file [output_dvi_file]
dvispc -d input_dvi_file
dvispc -s [-p..] input_dvi_file [output_text_file]
dvispc -a [-jltv][-p..][-r..] input_dvi_file [output_text_file]
- dvispc -x[..] [-dltv][-r..] [input_text_file] output_dvi_file
- -c: make page-indepent DVI in specials for color push/pop, background, pn
+ dvispc -x[..] [-ltv][-r..] [input_text_file] output_dvi_file
+ -c: make page-indepent DVI in specials (default)
-d: check page-independence
-b: backup original even if output_dvi_file is not given
-s: show specials
-a: translate DVI to Text
-x: translate Text to DVI (-x0:str0 1:chkfnt 2:variety)
-v: verbose -j: Japanese characters -l: location
+ -z: append empty pages if necessary to have multiple of 4 pages for book
-r: replace (-rorg_1=new_1/org_2=new_2... eg. -rxxx=special/fnt=font)
-p: T:preamble L:postamble pages with - (eg: -pT-L -pT2/4-8L -p-4 etc.)
- -t: comaptible to DLT (the followings are suboptions if necessary eg. -t02)
+ -t: compatible to DTL (the followings are suboptions if necessary eg. -t02)
0:str 1:ch 2:ch2 3:cmd 4:c-sum 5:dir/name 6:err 7:page 8:oct 9:str0
output_text_file : stdout if it is not specified.
input_text_file : stdin if it is not specified.
+Supported specials:
+ color specials: push, pop, background
+ pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor
+ tpic specials: pn
+
*****************************************************
[1] ** DVI t@CÆeLXgt@CÆÌÔÌÝÏ· **
@@ -182,10 +188,11 @@ dvips
dvispc ÍA±Ìæ¤ÈDVIt@CðAy[WƧÈDVIt@CÉC³µÜ·B
-εĢé specials ÍAdvips/dviout ÈÇpÌàÌÅ
+εĢé specials ÍAdvips/dvipdfmx/dviout ÈÇpÌàÌÅ
-color specials: color push/pop, background
-tpic specials : pn
+ color specials: push, pop, background
+ pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor
+ tpic specials: pn
--- dl ---
-c: ãLÌC³ðs¤B
@@ -207,11 +214,15 @@ color specials: push/pop
background : wiFªè`³êĨèA»ÌãÌy[WÅÄè`ªÈ¢y[WÉÍA
y[WÌæªÉwiFÌè`ðiXPÂj¨B
+pdf:bcolor, pdf:color : color push/pop ƯB
+
+pdf:bgcolor : background ƯB
+
tpic specials : pn ªè`³ê½çA»ÌãÌy[WÉ pn Ìè`ðuiX^bNÅ
ÍÈ¢ÌÅAXPÂj
y[WÌæªÉu©êéÍA
-color push -> background -> pn (tpic special)
+color push -> pdf:bcolor -> background -> pdf:bgcolor -> pn (tpic special)
ÆÈÁÄ¢éB
diff --git a/dvispce.txt b/dvispce.txt
index 7dfd3bc..c0fb251 100644
--- a/dvispce.txt
+++ b/dvispce.txt
@@ -1,26 +1,31 @@
-[2] Modify a DVI file to be page-independent in specials
-[1] Translation between DVI file <-> Text file
- Ver.0.2 written by SHIMA, Jan. 2003
+[2] Modify a DVI file to be page-independent in specials
+[1] Translation between DVI file <-> Text file
+ Ver.0.3 written by SHIMA, Jan. 2003
-Usage: dvispc [-c] [-bv] input_dvi_file [output_dvi_file]
+Usage: dvispc [-c] [-bvz] input_dvi_file [output_dvi_file]
dvispc -d input_dvi_file
dvispc -s [-p..] input_dvi_file [output_text_file]
dvispc -a [-jltv][-p..][-r..] input_dvi_file [output_text_file]
- dvispc -x[..] [-dltv][-r..] [input_text_file] output_dvi_file
- -c: make page-indepent DVI in specials for color push/pop, background, pn
+ dvispc -x[..] [-ltv][-r..] [input_text_file] output_dvi_file
+ -c: make page-indepent DVI in specials (default)
-d: check page-independence
-b: backup original even if output_dvi_file is not given
-s: show specials
-a: translate DVI to Text
-x: translate Text to DVI (-x0:str0 1:chkfnt 2:variety)
-v: verbose -j: Japanese characters -l: location
+ -z: append empty pages if necessary to have multiple of 4 pages for book
-r: replace (-rorg_1=new_1/org_2=new_2... eg. -rxxx=special/fnt=font)
-p: T:preamble L:postamble pages with - (eg: -pT-L -pT2/4-8L -p-4 etc.)
- -t: comaptible to DLT (the followings are suboptions if necessary eg. -t02)
+ -t: compatible to DTL (the followings are suboptions if necessary eg. -t02)
0:str 1:ch 2:ch2 3:cmd 4:c-sum 5:dir/name 6:err 7:page 8:oct 9:str0
+ output_text_file : stdout if it is not specified.
+ input_text_file : stdin if it is not specified.
-color specials: color push/pop, background
-tpic specials: pn
+Supported specials:
+ color specials: push, pop, background
+ pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor
+ tpic specials: pn
*****************************************************
@@ -196,9 +201,9 @@ occurs for some DVI tools such as dvidvi.
dvispc modifies such a DVI file to be page-independent. Supporting specials are
- color specials: color push/pop, background
- tpic specials : sh, pn
-
+ color specials: push, pop, background
+ pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor
+ tpic specials: pn
--- specifications ---
-c: the above modification will be done
@@ -220,9 +225,13 @@ background : If the "background" color is defined in a page and the
definition is not written in another page where the definition should be
valid, then the definition is inserted at the top of the page.
+pdf:bcolor, pdf:ecolor : Similar to color push/pop.
+
+pdf:bgcolor : Similar to background.
+
tpic specials : If tpic special "pn" is defined, then the same special will be
inserted at the top of the following pages.
The order to be inserted at the top of each page is as follows
-color push -> background -> pn (tpic special)
+color push -> pdf:bcolor -> background -> pdf:bgcolor -> pn (tpic special)
More information about the dviout
mailing list