1 /*
2  * Copyright (C) 1991,1992 NEC Corporation.
3  */
4 #ifndef lint
5 static char rcsid[]=
6 	"$Id: ftitle.c,v 2.12 1994/04/19 10:16:21 uchida Exp $ (NEC)";
7 #endif
8 
9 #include <stdio.h>
10 #include "plain2.h"
11 
12 extern struct strVal kdot[];
13 extern struct	strVal	*kstrMatch();
14 /*
15  * Figure/Table title strings
16  */
17 #define	MARK_FIG	1
18 #define	MARK_TBL	2
19 struct strVal figTblMark[] = {
20 #ifdef	KANJI
21 	{"��",		FT_FIGURE },
22 	{"ɽ",		FT_TABLE },
23 #endif
24 	{"Figure",	FT_FIGURE },
25 	{"Fig.",	FT_FIGURE },
26 	{"fig. ",	FT_FIGURE },
27 	{"Table",	FT_TABLE },
28 	{ "",  -1}
29 };
30 unsigned char	firstByteFtitle[256];
ftitleInit()31 ftitleInit()
32 {
33 	bzero((char *)firstByteFtitle, sizeof(firstByteFtitle));
34 	byteRegister(firstByteFtitle, figTblMark);
35 }
lineAtrFtitle(textp)36 lineAtrFtitle(textp)
37 register struct	text	*textp;
38 {
39 	struct	strVal	*svp;
40 	int	ofst, len, len2, num, fnum;
41 	char	*str;
42 
43 	if (textp->pListHead || textp->indent == 0)
44 		return;
45 	if (firstByteFtitle[(unsigned char)*(textp->body + textp->indent)]
46 	    == 0)
47 		return;
48 	str = textp->body + textp->indent;
49 	if (svp = kstrMatch(str, figTblMark)) {
50 		textp->fTitle = svp->value;
51 	}
52 	else return ;
53 
54 	len = strlen(svp->pattern);
55 	while (*(str + len) == ' ')
56 		len++;
57 	str  += len;
58 	if (*str == '\0')
59 		goto undo;
60 	ofst = 0;
61 	fnum = 0;
62 	while (len2 = checkIfNumber(str + ofst, &num)){
63 		fnum = fnum * 10 + num;
64 		ofst += len2;
65 	}
66 	if (ofst == 0)
67 		goto undo;
68 	textp->listNum = fnum;
69 	if (kstrMatch(str + ofst, kdot))
70 		ofst += 2;
71 	else if (*(str + ofst) == '.')
72 		ofst++;
73 	while (*(str + ofst) == ' ')
74 		ofst++;
75 	textp->headLen = ofst + len;
76 	return;
77     undo:
78 	textp->fTitle = 0;
79 	return;
80 }
figTitle(begin,end)81 figTitle(begin, end)
82 int	begin;
83 int	end;
84 {
85 	int	i;
86 	for (i = begin; i < end; i++) {
87 		if (texts[i]->block == NULL
88 		    && texts[i]->fTitle) {
89 			texts[i]->block = newTextBlock(i, i + 1, TB_FTITLE);
90 		}
91 	}
92 }
93 /*
94  * connect figure/picture block with its title
95  */
96 titledBlock(tbp1, tbp2)
97 struct	textBlock	*tbp1;
98 struct	textBlock	*tbp2;
99 {
100 	struct	textBlock	*newTbp;
101 
102 	newTbp = newTextBlock(tbp1->rbegin, tbp2->rend, TB_CAPSULE);
103 	texts[tbp1->rbegin]->block = newTbp;
104 
105 	newTbp->nextBlock = tbp1;
106 	tbp1->nextBlock = tbp2;
107 	tbp2->nextBlock = NULL;
108 
109 	tbp1->superBlock = tbp2->superBlock = newTbp;
110 }
111 joinIfTitled(titlep, begin, end)
112 struct	text		*titlep;
113 int	begin;
114 int	end;
115 {
116 	struct	textBlock	*tbpPrev   = NULL;
117 	struct	textBlock	*tbpFollow = NULL;
118 	struct	textBlock	*titleTbp  = titlep->block;
119 	DBG1(7, "joinIfTitled (%d)\n", titleTbp->rbegin);
120 
121 	tbpPrev   = prevBlock(titleTbp->rbegin, begin);
122 	tbpFollow = nextBlock(titleTbp->rend, end);
123 
124 	if (tbpPrev && ((tbpPrev->type != TB_TABLE
125 			&& tbpPrev->type != TB_PICTURE)
126 			|| tbpPrev->superBlock))
127 		tbpPrev = NULL;
128 	if (tbpFollow && ((tbpFollow->type != TB_TABLE
129 			  && tbpFollow->type != TB_PICTURE)
130 			  || tbpFollow->superBlock))
131 		tbpFollow = NULL;
132 
133 	DBG2(4, "joinIfTitled (p:%d,f:%d)\n", tbpPrev, tbpFollow);
134 
135 	if ((tbpPrev && !tbpFollow)
136 	    || ((tbpPrev && tbpFollow)
137 		&& ((titleTbp->rbegin - tbpPrev->rend)
138 		    < (tbpFollow->rbegin - titleTbp->rbegin)))) {
139 		titledBlock(tbpPrev, titleTbp);
140 		return titleTbp->rend;
141 	}
142 	else if (tbpFollow) {
143 		titledBlock(titleTbp, tbpFollow);
144 		return tbpFollow->rend;
145 	}
146 	return titleTbp->rend;
147 }
makeTitledBlock(begin,end)148 makeTitledBlock(begin, end)
149 int	begin;
150 int	end;
151 {
152 	int	l;
153 	for (l = begin; l < end;) {
154 		if (texts[l]->block && texts[l]->block->type == TB_FTITLE) {
155 			l = joinIfTitled(texts[l], begin, end);
156 		}
157 		else
158 			l++;
159 	}
160 }
161