summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/window/amiga.c
blob: ec6867950666a2dc701a5bf461d63b37bf7bfbb0 (plain)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * AMIGA.C: This is the Graphics Window interface to MetaFont for online
 * displays on the Commodore Amiga.  This file was written by Edmund Mergl
 * for his Amiga port of TeX 3.1 and MetaFont 2.7 from the original Unix
 * sources (see Fish Disks 611-616).
 *
 * Some modifications and improvements were made since 1993 by Andreas and
 * Stefan Scherer, Abt-Wolf-Strasse 17, 96215 Lichtenfels, Germany.  The
 * most recent entries from top to bottom always superseed their ancestors,
 * just read this as a bit of history:
 *
 *   August 12, 1993:
 *
 *   - We use the `memory hack', so the values for `screenwidth'
 *     and `screendepth' are to be found in `mfmemory.config'.  The
 *     display height should be set to an InterLace value, this will
 *     be dealt with by `lacefactor' (see below).
 *
 *   - The display window is no longer resizeable, because no
 *     `screenupdate' is called and the picture is destroyed.
 *
 *   - Based on ideas by Stefan Becker used in his famous MetaFont port
 *     to the Amiga of 1991 (see Fish Disk 486) there is a variable
 *     `scalefactor', which can be set to a (small) positiv integer in
 *     `mfmemory.config'.  This results in a reduced size of the displayed
 *     characters, so that large font pictures can be drawn on
 *     small-sized screens (although some distortions are possible).
 *
 *   - By replacing `Move' and `Draw' with `WritePixelLine' and friends
 *     the speed of the display was heavily increased.
 *
 *   - The display screen is totally controlled by `screenwidth' and
 *     `screendepth' from `mfmemory.config', when you set `screen_rows'
 *     and `screen_cols' both to the maximum value `4095' in your local
 *     device modes file.  MetaFont takes the smaller of these pairs.
 *
 *   - `blankrectangle' *always* set the complete display window to the
 *     background color, not only the area specified by `left', `right',
 *     `top', and `bottom'.  These MetaFont coordinates are now obeyed.
 *
 *   September 11, 1993:
 *
 *   - The changes for `Computers and Typesetting -- Volume A-E' provided
 *     by Addison-Wesley Publishing Company resulted in MetaFont 2.71.
 *     This caused a change in the window title.
 *
 *   October 22, 1993:
 *
 *   - The official version 2.71 has the same window title.  :-)
 *
 *   January 28, 1994:
 *
 *   - Although SAS/C has an AutoOpen feature for Amiga libraries,
 *     it is better to define the library bases explicitly.
 *
 *   February 15, 1995:
 *
 *   - Here's my new address:
 *     Andreas Scherer, Rolandstraße 16, 52070 Aachen, Germany.
 *     <scherer@genesis.informatik.rwth-aachen.de> (Internet).
 *   - Make `amiga.c' compliant with the syntax of the other modules
 *     in the Web2C 6.1 distribution.
 *
 *   February 19, 1995:
 *
 *   - `close_all()' must not have arguments, because we want to
 *     link it into the `atexit()' function list.
 *   - To distinguish this version from the old distribution, the
 *     window title is modified.
 *
 *   March 23, 1995:
 *
 *   - MetaFont has the new version number 2.718.
 *
 *   April 9, 1995:
 *
 *   - rename `screenheight' to `screendepth' for consistency.
 *
 *   October 10, 1995:
 *
 *   - Yet another change in my address, now it's a UNIX CIP pool:
 *     <scherer@physik.rwth-aachen.de>
 *   - Cleanup for the new web2c release.
 */

#define EXTERN extern

#include "../mfd.h"

#ifdef AMIGAWIN /* Whole file */

#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;

struct Screen *Scr;
struct Window *Win;

struct BitMap tempbm;
struct RastPort temprp;

UBYTE *linearray;

int scaledwidth  = 0;
int scaledheight = 0;

#ifndef scalefactor
#define scalefactor 1
#endif

/*
 * The configuration `mf.mcf' of AmiWeb2C holds values for `screenwidth'
 * and `screendepth' that always refer to an InterLace screen.  In case
 * you open the display on a non-InterLaced screen, only half the lines
 * are shown to you.  This is automatically done by reading the screen
 * information and setting `lacefactor'.
 */
int lacefactor;

#define BACKGROUNDCOLOR 0
#define PENCOLOR        1

#define PUBSCREEN 0

/*
 * Allocating memory for the array used in `WritePixelLine' needs
 * WORD-aligned arguments.
 */
#define ALIGN_SIXTEEN_BIT(A) ((((A)+15)/16)*16)
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif

/*
 * CLOSE_ALL: Clean up on MetaFont termination.  This routine has to be
 * called just before leaving the program, which is done automatically
 * when it gets entered into the `atexit' function list.  (See below.)
 */
void close_all(void)
{
  if (linearray)        free(linearray);
  if (tempbm.Planes[0]) FreeRaster(tempbm.Planes[0], scaledwidth, 1);
  if (Win)              CloseWindow(Win);
  if (GfxBase)          CloseLibrary((struct Library *)GfxBase);
  if (IntuitionBase)    CloseLibrary((struct Library *)IntuitionBase);

} /* close_all() */

/*
 * INITSCREEN: Initialize the physical display window on the WorkBench
 * screen of the Commodore Amiga.  Open the necessary libraries and open
 * the window according to `screenwidth' and `screendepth' as given in
 * the configuration file `mf.mcf'.  For this purpose, the
 * `OpenWindowTagList' routine was replaced with `OpenWindowTags'.
 * The `WFLG_SIZEGADGET' window flag has been removed; resizing the
 * window kills the picture and never triggered an `updatescreen'.
 */
boolean mf_amiga_initscreen(void)
{
#ifdef DEBUG
  printf("\ninitscreen()\n");
#endif

  /*
   * Make sure the resources are correctly returned to the system
   * when leaving the program.
   */
  if(atexit(&close_all)) { /* Can't happen. */
    fprintf(stderr, "Exit trap failure! (close_all)\n");
    uexit(1);
    }

  if (!(IntuitionBase = (struct IntuitionBase *)
    OpenLibrary((unsigned char *)"intuition.library",(unsigned long)37L)))
  {
    fprintf(stderr,"\nCan't open intuition library. V37 required.\n");
    close_all();
    return(FALSE);
  }

  if (!(GfxBase = (struct GfxBase *)
    OpenLibrary((unsigned char *)"graphics.library",(unsigned long)37L)))
  {
    fprintf(stderr,"\nCan't open intuition library. V37 required.\n");
    close_all();
    return(FALSE);
  }

  if (!(Scr = LockPubScreen(PUBSCREEN)))
  {
    fprintf(stderr,
      "\nCan't get lock on public screen (%d).\n", PUBSCREEN);
    close_all();
    return(FALSE);
  }

  scaledwidth  = screenwidth/scalefactor;
  scaledheight = screendepth/scalefactor;

  lacefactor  = ((Scr->ViewPort.Modes & LACE) ? 1 : 2);

  if (!(Win = (struct Window *)OpenWindowTags( NULL,
    WA_InnerWidth,  scaledwidth,
    WA_InnerHeight, scaledheight/lacefactor,
    WA_Flags,       WFLG_DRAGBAR|WFLG_DEPTHGADGET,
    WA_AutoAdjust,  FALSE,
    WA_Title,       (ULONG)" MetaFont V2.718 Online Display",
    WA_PubScreen,   (ULONG)Scr,
    TAG_DONE)))
  {

    fprintf(stderr,
       "\nCan't open online display window at size %d times %d.\n"
#ifdef VARMEM
       "Change your configuration file.\n",
#else
       "If you really absolutely need more capacity,\n"
       "you can ask a wizard to enlarge me.\n",
#endif
    screenwidth,screendepth);

    close_all();
    return(FALSE);
  }

  InitBitMap(&tempbm, 1, scaledwidth, 1);

  tempbm.Planes[0] = NULL;

  if(!(tempbm.Planes[0] = (PLANEPTR)AllocRaster(scaledwidth, 1)))
     return(FALSE);

  InitRastPort(&temprp);

  temprp.BitMap = &tempbm;

  if(!(linearray = (UBYTE *)
    calloc(ALIGN_SIXTEEN_BIT(scaledwidth),sizeof(UBYTE))))
    return(FALSE);

  UnlockPubScreen(NULL, Scr);

#ifdef DEBUG
   printf("initscreen() ok\n");
#endif

   return(TRUE);

} /* mf_amiga_initscreen() */

/*
 * UPDATESCREEN: I really don't know what this function is supposed
 * to do.
 */
void mf_amiga_updatescreen(void)
{
#ifdef DEBUG
  printf("updatescreen()\n");
#endif
} /* mf_amiga_update_screen() */

/*
 * BLANKRECTANGLE: Reset the drawing rectangle bounded by
 * ([left,right],[top,bottom]) to the background color.
 */
void mf_amiga_blankrectangle(screencol left, screencol right,
  screenrow top, screenrow bottom)
{
#ifdef DEBUG
  printf("blankrectangle() - left: %d right: %d top: %d bottom: %d\n",
    left, right, top, bottom);
#endif

  SetAPen (Win->RPort, BACKGROUNDCOLOR);
  RectFill(Win->RPort,
    (SHORT)(Win->BorderLeft + left/scalefactor),
    (SHORT)(Win->BorderTop + top/(scalefactor*lacefactor)),
    (SHORT)(Win->BorderLeft + right/scalefactor - 1),
    (SHORT)(Win->BorderTop + bottom/(scalefactor*lacefactor) - 1));
} /* mf_amiga_blankrectangle() */

/*
 * PAINTROW: Paint a `row' starting with color `init_color', up to the
 * next transition specified by `transition_vector', switch colors, and
 * continue for `vector_size' transitions.  It now only
 * draws into the display window when a new raster line will be affected.
 * A lot of pre-drawing and inter-drawing calculations and conditionals
 * are evaluated to prepare a fast and clean drawing under the most
 * circumstances to be expected on the Amiga.  InterLace/Lace is taken
 * care of and the sizes are set appropriately not to clobber the borders.
 * (Don't ask me why this works; at least on my machine with my config.)
 */
void mf_amiga_paintrow(screenrow row, pixelcolor init_color,
  transspec transition_vector, screencol vector_size)
{
  int start, scaledstart, scaledrow, scaledlaced;
  register int i=0, j=0, col, color;

#ifdef DEBUG
    printf("paintrow() - vector size: %d\n", vector_size);
#endif

  color       = ((0 == init_color) ? BACKGROUNDCOLOR : PENCOLOR);
  scaledlaced = scalefactor * lacefactor;
  start       = *transition_vector++;
  scaledstart = start / scalefactor;
  scaledrow   = row / scaledlaced;

  ReadPixelLine8(Win->RPort, Win->BorderLeft + scaledstart,
    Win->BorderTop + scaledrow, scaledwidth - scaledstart,
    linearray, &temprp);

  if(scalefactor>1) {

    do {
      for(col = *transition_vector++; i<col-start; i++) {
        linearray[j] |= color;
        if((i%scalefactor)==0) j++;
      }

      color = PENCOLOR - color;

#ifdef DEBUG
      printf("move col: %d row: %d ", col, row);
      printf("draw col: %d row: %d\n", (*transition_vector)-1, row);
#endif

    } while (--vector_size);

    if(i >= col-start) linearray[++j] |= color;

  } else {

    do {
      for(col = *transition_vector++; j<col-start; j++) {
         linearray[j] |= color;
         }

      color = PENCOLOR - color;

#ifdef DEBUG
      printf("move col: %d row: %d ", col, row);
      printf("draw col: %d row: %d\n", (*transition_vector)-1, row);
#endif

    } while (--vector_size);
  }

  if( (  Win->BorderTop + scaledrow ) <
      (  Win->Height - Win->BorderBottom ) )
    WritePixelLine8(Win->RPort, Win->BorderLeft + scaledstart,
      Win->BorderTop + scaledrow,
      min(Win->Width - Win->BorderLeft - Win->BorderRight - scaledstart,j),
      linearray, &temprp);

} /* mf_amiga_paintrow() */

#else
int amiga_dummy;
#endif /* AMIGAWIN */