1 /*
2  * Copyright (C) 1991,1992 NEC Corporation.
3  */
4 #ifndef lint
5 static char rcsid[] =
6 	"$Id: example.c,v 2.10 1994/04/19 10:16:20 uchida Exp $ (NEC)";
7 #endif
8 
9 #include <stdio.h>
10 #include "plain2.h"
11 /*
12  * Example Block
13  * 	"non-fill" , "constant-width"
14  */
markIfExample(begin,end)15 markIfExample(begin, end)
16 int	begin;
17 int	end;
18 {
19 	int	lengthSum = 0, spaceSum = 0, jcodeSum = 0;
20 	short	aveLength, spacePercent, jcodePercent;
21 	int	l, totalLines;
22 	int	indent;
23 	DBG2(7,"markIfExample (%d-%d)\n", begin, end);
24 	indent	   = minIndent(begin, end);
25 	if ((totalLines = end - begin) == 0)
26 		return 1;
27 	for (l= begin; l < end; l++) {
28 		if (texts[l]->block != NULL)
29 			return 0;
30 		if (!texts[l]->blank){
31 			lengthSum += texts[l]->length - indent;
32 			spaceSum  += texts[l]->spaces;
33 			jcodeSum  += texts[l]->japanese;
34 		}
35 		else
36 			totalLines--;
37 	}
38 	if (totalLines == 0)
39 		return 1;
40 	aveLength    = lengthSum      / totalLines ;
41 	spacePercent = spaceSum * 100 / lengthSum;
42 	jcodePercent = jcodeSum * 100 / totalLines;
43 	if (ExampleCheck(begin, end, aveLength, spacePercent,
44 			 jcodePercent, totalLines)) {
45 		struct	textBlock *tbp;
46 		MSG2("%d-%d ",begin, end - 1);
47 		tbp = newTextBlock(begin, end, TB_EXAMPLE);
48 		for (l = begin; l < end; l++)
49 			texts[l]->block = tbp;
50 		return 1;
51 		}
52 	return 0;
53 }
54 /*
55  * Judge if this region is example or not.
56  */
ExampleCheck(begin,end,length,spaces,jcode,lines)57 ExampleCheck(begin, end, length, spaces, jcode, lines)
58 int	begin;
59 int	end;
60 int	length;		/* average of text length	*/
61 int	spaces;		/* percentage of space characters	*/
62 int	jcode;		/* percentage of Japanese text lines	*/
63 int	lines;		/* number of lines		*/
64 {
65 	int	eval;
66 	if (japaneseText) {
67 		if (lines == 1 && texts[begin]->japanese)
68 			/* Ignore text length if lines == 1.
69 			 */
70 			eval = spaces * 4 - jcode/2;
71 		else
72 			eval = 2000/length + spaces * 4 - jcode/2;
73 	}
74 	else {
75 		/* English texts tends to have many spaces	 */
76 		if (lines == 1)
77 			eval = 1000/length + spaces * 4 - 30;
78 		else
79 			eval = 2400/length + spaces * 4 - 30;
80 	}
81 	DBG1(7, "Example=%d", eval);
82 	DBG3(7, "(l:%d, s:%d, j:%d)\n", length, spaces, jcode);
83 	return ((max(eval, 0) + examFactor - 50) * examFactor >= 5000);
84 }
85