aboutsummaryrefslogtreecommitdiffstats
path: root/z11.c
blob: 2c2329d551cc5b2e4ce41d90f2810da50e26ec2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*@z11.c:Style Service:EchoStyle()@*******************************************/
/*                                                                           */
/*  THE LOUT DOCUMENT FORMATTING SYSTEM (VERSION 3.27)                       */
/*  COPYRIGHT (C) 1991, 2002 Jeffrey H. Kingston                             */
/*                                                                           */
/*  Jeffrey H. Kingston (jeff@it.usyd.edu.au)                                */
/*  Basser Department of Computer Science                                    */
/*  The University of Sydney 2006                                            */
/*  AUSTRALIA                                                                */
/*                                                                           */
/*  This program is free software; you can redistribute it and/or modify     */
/*  it under the terms of the GNU General Public License as published by     */
/*  the Free Software Foundation; either Version 2, or (at your option)      */
/*  any later version.                                                       */
/*                                                                           */
/*  This program is distributed in the hope that it will be useful,          */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
/*  GNU General Public License for more details.                             */
/*                                                                           */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program; if not, write to the Free Software              */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA   */
/*                                                                           */
/*  FILE:         z11.c                                                      */
/*  MODULE:       Style Service                                              */
/*  EXTERNS:      EchoStyle(), SpaceChange(), BreakChange()                  */
/*                                                                           */
/*****************************************************************************/
#include "externs.h"


#if DEBUG_ON
/*****************************************************************************/
/*                                                                           */
/*  FULL_CHAR *EchoStyle(style)                                              */
/*                                                                           */
/*  Returns a string showing the value of the style.                         */
/*                                                                           */
/*****************************************************************************/

FULL_CHAR *EchoStyle(STYLE *style)
{ static FULL_CHAR res[100];
  static char buff[100];
  static char *hyphwords[] = { "hyph_undef", "hyph_off", "hyph_on" };
  static char *fillwords[] = { "fill_undef", "fill_off", "fill_on" };
  static char *spacewords[] = { "lout", "comp", "troff", "tex" };
  static char *displaywords[] = { "undef", "adjust", "outdent", "oragged",
				  "left", "centre", "right", "do" };

  StringCopy(res, AsciiToFull("["));
  StringCat(res, EchoCatOp(VCAT,mark(line_gap(*style)),join(line_gap(*style))));
  StringCat(res, EchoGap(&line_gap(*style)));
  StringCat(res, AsciiToFull(", "));
  StringCat(res, font(*style) == 0 ?
		   AsciiToFull("nofont") : FontFamilyAndFace(font(*style)));
  StringCat(res, AsciiToFull(" ("));
  StringCat(res, AsciiToFull(spacewords[space_style(*style)]));
  StringCat(res, AsciiToFull(" "));
  StringCat(res, EchoGap(&space_gap(*style)));
  StringCat(res, AsciiToFull("), "));
  StringCat(res, AsciiToFull(hyph_style(*style) < 3 ?
		    hyphwords[hyph_style(*style)] : "?"));
  StringCat(res, AsciiToFull(":"));
  StringCat(res, AsciiToFull(fill_style(*style) < 3 ?
		    fillwords[fill_style(*style)] : "?"));
  StringCat(res, AsciiToFull(":"));
  StringCat(res, AsciiToFull(display_style(*style) < 7 ?
		    displaywords[display_style(*style)] : "?"));
  if( small_caps(*style) > 0 ) StringCat(res, AsciiToFull(":smallcaps"));
  if( vadjust(*style) ) StringCat(res, AsciiToFull(":vadjust"));
  if( hadjust(*style) ) StringCat(res, AsciiToFull(":hadjust"));
  if( padjust(*style) ) StringCat(res, AsciiToFull(":padjust"));
  if( yunit(*style) != 0 )
  { StringCat(res, AsciiToFull(":y="));
    StringCat(res, EchoLength(yunit(*style)));
  }
  if( zunit(*style) != 0 )
  { StringCat(res, AsciiToFull(":z="));
    StringCat(res, EchoLength(zunit(*style)));
  }
  if( nobreakfirst(*style) ) StringCat(res, AsciiToFull(":NBF"));
  if( nobreaklast(*style) ) StringCat(res, AsciiToFull(":NBL"));
  sprintf(buff, ":C%d:P%d", colour(*style), texture(*style));
  StringCat(res, AsciiToFull(buff));
  StringCat(res, AsciiToFull("]"));
  return res;
} /* end EchoStyle */
#endif


/*@::SpaceChange()@***********************************************************/
/*                                                                           */
/*  SpaceChange(style, x)                                                    */
/*                                                                           */
/*  Change the current space style as indicated by object x.                 */
/*                                                                           */
/*****************************************************************************/

static void changespace(STYLE *style, OBJECT x)
{ GAP res_gap;  unsigned gap_inc;
  assert( is_word(type(x)), "changespace: type(x)!" );
  if( beginsbreakstyle(string(x)[0]) )
  {
    /* should be a new space style option */
    if( StringEqual(string(x), STR_SPACE_LOUT) )
	space_style(*style) = SPACE_LOUT;
    else if( StringEqual(string(x), STR_SPACE_COMPRESS) )
	space_style(*style) = SPACE_COMPRESS;
    else if( StringEqual(string(x), STR_SPACE_SEPARATE) )
	space_style(*style) = SPACE_SEPARATE;
    else if( StringEqual(string(x), STR_SPACE_TROFF) )
	space_style(*style) = SPACE_TROFF;
    else if( StringEqual(string(x), STR_SPACE_TEX) )
	space_style(*style) = SPACE_TEX;
    else Error(11, 1, "unknown option to %s symbol (%s)",
	   WARN, &fpos(x), KW_SPACE, string(x));
  }
  else /* should be a new space gap */
  { GetGap(x, style, &res_gap, &gap_inc);
    if( gap_inc != GAP_ABS && units(res_gap) != units(space_gap(*style)) )
    { Error(11, 2, "spacing %s is not compatible with current spacing",
	WARN, &fpos(x), string(x));
    }
    else
    { units(space_gap(*style)) = units(res_gap);
      mode(space_gap(*style))  = mode(res_gap);
      width(space_gap(*style)) = gap_inc == GAP_ABS ? width(res_gap) :
	     gap_inc == GAP_INC ? width(space_gap(*style)) + width(res_gap) :
	     find_max(width(space_gap(*style)) - width(res_gap), 0);
    }
  }
  debug1(DSS, D, "SpaceChange returning %s", EchoStyle(style));
} /* end SpaceChange */


void SpaceChange(STYLE *style, OBJECT x)
{ OBJECT link, y;
  debug2(DSS, D, "SpaceChange(%s, %s)", EchoStyle(style), EchoObject(x));
  switch( type(x) )
  {
    case NULL_CLOS: break;

    case WORD:
    case QWORD:	if( !StringEqual(string(x), STR_EMPTY) )
		  changespace(style, x);
		break;


    case ACAT:	for( link = Down(x);  link != x;  link = NextDown(link) )
		{ Child(y, link);
		  if( type(y) == GAP_OBJ || type(y) == NULL_CLOS )  continue;
		  else if( is_word(type(y)) )
		  { if( !StringEqual(string(y), STR_EMPTY) )
		      changespace(style, y);
		  }
		  else Error(11, 3, "invalid left parameter of %s",
			 WARN, &fpos(x), KW_SPACE);
		}
		break;


    default:	Error(11, 4, "invalid left parameter of %s",
		  WARN, &fpos(x), KW_SPACE);
		break;
  }
  debug1(DSS, D, "SpaceChange returning %s", EchoStyle(style));
} /* end SpaceChange */


/*@::BreakChange()@***********************************************************/
/*                                                                           */
/*  BreakChange(style, x)                                                    */
/*                                                                           */
/*  Change the current break style as indicated by object x.                 */
/*                                                                           */
/*****************************************************************************/

static void changebreak(STYLE *style, OBJECT x)
{ GAP res_gap;  unsigned gap_inc;
  debug0(DSS, D, "[ changebreak");
  if( beginsbreakstyle(string(x)[0]) )
  {
    /* should be a new break style option */
    if( StringEqual(string(x), STR_BREAK_HYPHEN) )
	hyph_style(*style) = HYPH_ON;
    else if( StringEqual(string(x), STR_BREAK_NOHYPHEN) )
	hyph_style(*style) = HYPH_OFF;
    else if( StringEqual(string(x), STR_BREAK_ADJUST) )
	fill_style(*style) = FILL_ON, display_style(*style) = DISPLAY_ADJUST;
    else if( StringEqual(string(x), STR_BREAK_OUTDENT) )
	fill_style(*style) = FILL_ON, display_style(*style) = DISPLAY_OUTDENT;
    else if( StringEqual(string(x), STR_BREAK_RAGGED) )
	fill_style(*style) = FILL_ON, display_style(*style) = DISPLAY_LEFT;
    else if( StringEqual(string(x), STR_BREAK_CRAGGED) )
	fill_style(*style) = FILL_ON, display_style(*style) = DISPLAY_CENTRE;
    else if( StringEqual(string(x), STR_BREAK_RRAGGED) )
	fill_style(*style) = FILL_ON, display_style(*style) = DISPLAY_RIGHT;
    else if( StringEqual(string(x), STR_BREAK_ORAGGED) )
	fill_style(*style) = FILL_ON, display_style(*style) = DISPLAY_ORAGGED;
    else if( StringEqual(string(x), STR_BREAK_LINES) )
	fill_style(*style) = FILL_OFF, display_style(*style) = DISPLAY_LEFT;
    else if( StringEqual(string(x), STR_BREAK_CLINES) )
	fill_style(*style) = FILL_OFF, display_style(*style) = DISPLAY_CENTRE;
    else if( StringEqual(string(x), STR_BREAK_RLINES) )
	fill_style(*style) = FILL_OFF, display_style(*style) = DISPLAY_RIGHT;
    else if( StringEqual(string(x), STR_BREAK_NOFIRST) )
	nobreakfirst(*style) = TRUE;
    else if( StringEqual(string(x), STR_BREAK_FIRST) )
	nobreakfirst(*style) = FALSE;
    else if( StringEqual(string(x), STR_BREAK_NOLAST) )
	nobreaklast(*style) = TRUE;
    else if( StringEqual(string(x), STR_BREAK_LAST) )
	nobreaklast(*style) = FALSE;
    else Error(11, 5, "found unknown option to %s symbol (%s)",
	   WARN, &fpos(x), KW_BREAK, string(x));
  }
  else /* should be a new inter-line gap */
  { GetGap(x, style, &res_gap, &gap_inc);
    if( gap_inc != GAP_ABS && units(res_gap) != units(line_gap(*style)) )
      Error(11, 6, "line spacing %s is not compatible with current spacing",
        WARN, &fpos(x), string(x));
    else
    { units(line_gap(*style)) = units(res_gap);
      mode(line_gap(*style))  = mode(res_gap);
      width(line_gap(*style)) = gap_inc == GAP_ABS ? width(res_gap) :
	gap_inc == GAP_INC ? width(line_gap(*style)) + width(res_gap) :
	find_max(width(line_gap(*style)) - width(res_gap), 0);
    }
  }
  debug0(DSS, D, "] changebreak");
} /* end changebreak */

void BreakChange(STYLE *style, OBJECT x)
{ OBJECT link, y;
  GAP res_gap;  unsigned gap_inc;
  debug3(DSS, D, "BreakChange(%s, %s at %s)", EchoStyle(style),
    EchoObject(x), EchoFilePos(&fpos(x)));
  switch( type(x) )
  {
    case NULL_CLOS: break;

    case WORD:
    case QWORD:	if( !StringEqual(string(x), STR_EMPTY) )
		{
		  debug1(DSS, D, "BreakChange WORD examining %s", (string(x)));
		  if( StringEqual(string(x), STR_BREAK_SETOUTDENT) )
		  {
		    debug1(DSS, D, " found %s", STR_BREAK_SETOUTDENT);
		    Error(11, 11, "width missing after %s in %s",
		      WARN, &fpos(x), STR_BREAK_SETOUTDENT, KW_BREAK);
		  }
		  else
		  {
		    debug1(DSS, D, " not found %s", STR_BREAK_SETOUTDENT);
		    changebreak(style, x);
		  }
		}
		break;


    case ACAT:	for( link = Down(x);  link != x;  link = NextDown(link) )
		{ Child(y, link);
		  if( type(y) == GAP_OBJ || type(y) == NULL_CLOS )  continue;
		  else if( is_word(type(y)) )
		  { if( !StringEqual(string(y), STR_EMPTY) )
		    {
		      debug1(DSS, D, "BreakChange examining %s", (string(y)));
		      if( StringEqual(string(y), STR_BREAK_SETOUTDENT) )
		      {
			debug1(DSS, D, "  found %s", STR_BREAK_SETOUTDENT);
			if( NextDown(link)==x || NextDown(NextDown(link))==x )
			{
			  Error(11, 11, "width missing after %s in %s",
			    WARN, &fpos(x), STR_BREAK_SETOUTDENT, KW_BREAK);
			}
			else
			{
			  link = NextDown(NextDown(link));
			  Child(y, link);
			  GetGap(y, style, &res_gap, &gap_inc);
			  outdent_len(*style) = gap_inc == GAP_ABS ?
			    width(res_gap) : gap_inc == GAP_INC ?
			    outdent_len(*style) + width(res_gap) :
			    find_max(outdent_len(*style) - width(res_gap), 0);
			}
		      }
		      else
		      {
			debug1(DSS, D, "  not found %s", STR_BREAK_SETOUTDENT);
			changebreak(style, y);
		      }
		    }
		  }
		  else Error(11, 7, "invalid left parameter of %s",
			 WARN, &fpos(x), KW_BREAK);
		}
		break;


    default:	Error(11, 8, "invalid left parameter of %s",
		  WARN, &fpos(x), KW_BREAK);
		break;
  }
  debug1(DSS, D, "BreakChange returning %s", EchoStyle(style));
} /* end BreakChange */


/*@::YUnitChange(), ZUnitChange()@********************************************/
/*                                                                           */
/*  YUnitChange(style, x)                                                    */
/*                                                                           */
/*  Change the current value of the y unit as indicated by object x.         */
/*                                                                           */
/*****************************************************************************/

void YUnitChange(STYLE *style, OBJECT x)
{ GAP res_gap; unsigned gap_inc;
  GetGap(x, style, &res_gap, &gap_inc);
  if( units(res_gap) != FIXED_UNIT )
    Error(11, 9, "this unit not allowed with %s symbol",
      WARN, &fpos(x), KW_YUNIT);
  else
  { if( gap_inc == GAP_ABS ) yunit(*style) = width(res_gap);
    else if( gap_inc == GAP_INC ) yunit(*style) += width(res_gap);
    else yunit(*style) = find_max(yunit(*style) - width(res_gap), 0);
  }
} /* end YUnitChange */


/*****************************************************************************/
/*                                                                           */
/*  ZUnitChange(style, x)                                                    */
/*                                                                           */
/*  Change the current value of the z unit as indicated by object x.         */
/*                                                                           */
/*****************************************************************************/

void ZUnitChange(STYLE *style, OBJECT x)
{ GAP res_gap; unsigned gap_inc;
  GetGap(x, style, &res_gap, &gap_inc);
  if( units(res_gap) != FIXED_UNIT )
    Error(11, 10, "this unit not allowed with %s symbol",
      WARN, &fpos(x), KW_ZUNIT);
  else
  { if( gap_inc == GAP_ABS ) zunit(*style) = width(res_gap);
    else if( gap_inc == GAP_INC ) zunit(*style) += width(res_gap);
    else zunit(*style) = find_max(zunit(*style) - width(res_gap), 0);
  }
} /* end ZUnitChange */