-- color screen drawing initialization module of silicon (pretty picture) program
-- last modified by E. McCreight, December 17, 1982  12:20 PM
DIRECTORY
  StreamDefs,
  InlineDefs,
  IODefs,
  AltoDefs,
  DirectoryDefs,
  AltoFileDefs,
  StringDefs,
  multiGraphicsDefs,
  ProcessDefs,
  ppdddefs,ppddefs,
  ppdefs;

ppdrawCommon: PROGRAM
  IMPORTS
    ppdefs, ppddefs, StreamDefs, InlineDefs, multiGraphicsDefs, ProcessDefs,
    DirectoryDefs, ppdddefs,StringDefs
  EXPORTS ppdefs, ppddefs =
  BEGIN
  OPEN ppdefs, ppddefs, ppdddefs,StreamDefs, StringDefs, InlineDefs, multiGraphicsDefs,
    DirectoryDefs, IODefs;

  fp: AltoFileDefs.FP;
  fHandle: DiskHandle;
  i: INTEGER;

  CTName: STRING ← [50];

  colorsttt: ARRAY [0..48) OF CARDINAL;
  colorHandle: POINTER;

  markPnt, markDPnt, selMarkPnt: PUBLIC Point ← [0, 0];

  cursorBox: PUBLIC Bitmap;
  insideCBox: PUBLIC Bitmap;

  cursorSize: INTEGER = 16;
  cursNwds: CARDINAL = (cursorSize + 3)/4;
  curAr: ARRAY [0..cursorSize*cursNwds) OF CARDINAL ← ALL[0];
  curArF: ARRAY [0..cursorSize*cursNwds) OF CARDINAL ← ALL[0];
  curArI: ARRAY [0..cursorSize*cursNwds) OF CARDINAL ← ALL[0];


  initAll: PUBLIC PROCEDURE =
    BEGIN
    i, j, k: CARDINAL;
    q: INTEGER;
    lv:level;
    fHandle ← NewByteStream["chipColors.name", Read];
    CTName.length ← 0;
    k ← fHandle.get[fHandle];
    UNTIL k = 15B DO
      AppendChar[CTName, LOOPHOLE[k]]; k ← fHandle.get[fHandle]; ENDLOOP;
    fHandle.destroy[fHandle];

    fHandle ← NewWordStream[CTName, Read];
    cTabCnt ← fHandle.get[fHandle];
    FOR k IN [0..cTabCnt) DO
      FOR i IN [0..49) DO colorTabs[k][i] ← fHandle.get[fHandle]; ENDLOOP;
      ENDLOOP;
    fHandle.destroy[fHandle];
    currentCTab ← 0;

    IF DirectoryLookup[@fp, "chipPatterns.color", FALSE] THEN
      BEGIN
	fHandle ← NewWordStream["chipPatterns.color", Read];
	k←fHandle.get[fHandle]; -- get table count
	FOR i IN [0..MIN[k,colPatNum]) DO
	  colPatternBits[i]←fHandle.get[fHandle];
	  FOR lv IN level DO
	    colPatternTabs[i][lv]←fHandle.get[fHandle];
	  ENDLOOP;
	ENDLOOP;
	fHandle.destroy[fHandle];
	IF colPatternBits[0]#0 THEN orLtab←colPatternTabs[0];
      END
     ELSE {colPatternBits[0]←1;colPatternTabs[0]←orLtab;};

    [colorHandle] ← TurnOnColor[bitsPerPixel: 4, portraitMode: csHor,
      initColor: 0];
    [] ← TurnOnGraphics[];
    restoreColors[];
    cursorBox ← [
      bank: 0, nWords: cursNwds, nBits: cursorSize, nLines: cursorSize,
      nBitsPerPixel: 4, portraitMode: csHor, scaleFactor: 1,
      bits: LOOPHOLE[@curAr]];
    insideCBox ← [
      bank: 0, nWords: cursNwds, nBits: cursorSize, nLines: cursorSize,
      nBitsPerPixel: 4, portraitMode: csHor, scaleFactor: 1,
      bits: LOOPHOLE[@curArI]];
    SetGrayMap[1, 42104B, 10421B, 42104B, 10421B];  -- set green bit pattern
    SetGrayMap[2, 104210B, 21042B, 0, 0];  -- set blue bit pattern
    SetGrayMap[4, 104210B, 42104B, 21042B, 10421B];  -- set red bit pattern
    SetGrayMap[10, 177777B, 177777B, 177777B, 177777B];  --set black bit pattern
    SetGrayMap[14, 21042B, 0B, 21042B, 0B];  --set yellow bit pattern
    SetGrayMap[8, 42104B, 167356B, 42104B, 0B];  --set ovg bit pattern
    SetGrayMap[11, 104210B, 104210B, 21042B, 21042B];  -- set metal2 pattern
    SetGrayMap[13, 42104B, 0B, 0B, 0B];  --set nwell bit pattern
    j ← 177600B;
    k ← 100000B;
    FOR i IN [0..15] DO
      curse: POINTER TO ARRAY [0..15] OF CARDINAL = LOOPHOLE[431B];
        -- make the B/W cursor all white for now
      curse↑[i] ← 0;
      ENDLOOP;
    FOR q IN [0..cursorSize) DO
      setColorCursorBits[y: q, w1: bwCurs[q], w2: 0, bm: @cursorBox, color: 15];
      setColorCursorBits[y: q, w1: bwCursI[q], w2: 0, bm: @insideCBox, color: 15];
      ENDLOOP;
    CursorTrack[FALSE];

    initkbd[@bwCurs, @cursorBox];

  initColors[];
  cellList ← NIL;
  cellStack ← NIL;
  unDelPnt ← NIL;
  unDelGrpCnt ← unDelItemCnt ← 0;
  initRects[];
  ProcessDefs.Detach[FORK updateColor[]];
  setCscale[10];
  ProcessDefs.Detach[FORK updateBW[]];
  setBWscale[8];

  doParmInit[];

    END;
  setColorCursorBits: PROCEDURE [
    y: CARDINAL, w1, w2: CARDINAL, bm: POINTER TO Bitmap,
    color: Color] =
    BEGIN
    i: CARDINAL;
    FOR i IN [0..cursorSize) DO
      IF BITAND[w1, 100000B] # 0 THEN PutColorPoint[i, y, color, bm];
      IF i = 16 THEN w1 ← w2 ELSE w1 ← BITSHIFT[w1, 1];
      ENDLOOP;
    END;

  writeCTab: PUBLIC PROCEDURE =
    BEGIN
    bb: BOOLEAN ← FALSE;
    i, j: CARDINAL;
    lv:level;
    IF NOT anyCTChanges THEN RETURN;
    anyCTChanges ← FALSE;
    WHILE NOT bb DO strInc[CTName]; bb ← gentlyOpenFile[CTName]; ENDLOOP;
    fHandle.put[fHandle, cTabCnt];
    FOR i IN [0..cTabCnt) DO
      FOR j IN [0..49) DO fHandle.put[fHandle, colorTabs[i][j]]; ENDLOOP; ENDLOOP;
    fHandle.destroy[fHandle];
    fHandle ← NewByteStream["chipColors.name", Write + Append];
    FOR i IN [0..CTName.length) DO fHandle.put[fHandle, CTName[i]]; ENDLOOP;
    fHandle.put[fHandle, 15B];
    fHandle.destroy[fHandle];

    fHandle ← NewWordStream["chipPatterns.color", Write + Append];
    fHandle.put[fHandle, colPatNum];
    FOR i IN [0..colPatNum) DO
      fHandle.put[fHandle, colPatternBits[i]];
      FOR lv IN level DO
        fHandle.put[fHandle, colPatternTabs[i][lv]];
      ENDLOOP;
    ENDLOOP;
    fHandle.destroy[fHandle];
    END;
  strInc: PROCEDURE [s: STRING] =
    BEGIN
    i: CARDINAL ← 0;
    IF s.length = 0 THEN RETURN;
    WHILE i < s.length AND s[i] # '. DO i ← i + 1; ENDLOOP;
    i ← i - 1;
    cInc[s, i];
    END;
  cInc: PROCEDURE [s: STRING, i: CARDINAL] =
    BEGIN
    IF s[i] IN ['0..'8] THEN s[i] ← s[i] + 1
    ELSE
      IF s[i] = '9 THEN BEGIN s[i] ← '0; cInc[s, i - 1]; END
      ELSE
        BEGIN
        s.length ← s.length + 1;
        FOR j: CARDINAL DECREASING IN [i + 2..s.length) DO
          s[j] ← s[j - 1]; ENDLOOP;
        s[i + 1] ← '1;
        END;
    END;
  gentlyOpenFile: PROCEDURE [fname: STRING] RETURNS [BOOLEAN] =
    BEGIN
    IF DirectoryLookup[@fp, fname, FALSE] THEN RETURN[FALSE];
    fHandle ← NewWordStream[fname, Write + Append];
    RETURN[TRUE];
    END;

  setGlev: PUBLIC PROCEDURE [c, cc, v: INTEGER] =
    BEGIN colorTabs[currentCTab][c*3 + cc + 1] ← v; restoreColors[]; END;
  invertColors: PUBLIC PROCEDURE =
    BEGIN
    FOR i IN [0..48) DO
      colorsttt[i] ← 255 - colorTabs[currentCTab][i + 1]; ENDLOOP;
    SetColorTable[LOOPHOLE[@colorsttt]];
    END;
  restoreColors: PUBLIC PROCEDURE =
    BEGIN
    FOR i IN [0..48) DO colorsttt[i] ← colorTabs[currentCTab][i + 1]; ENDLOOP;
    SetColorTable[LOOPHOLE[@colorsttt]];
    END;
  setColorTable: PUBLIC PROCEDURE [a: CARDINAL] =
    BEGIN currentCTab ← a; restoreColors[]; END;
  getCurrenColor: PUBLIC PROCEDURE [c, cc: INTEGER] RETURNS [INTEGER] =
    BEGIN RETURN[colorTabs[currentCTab][c*3 + cc + 1]]; END;

  deScaledCursor: PUBLIC PROCEDURE [x, y: INTEGER]
    RETURNS [BOOLEAN, INTEGER, INTEGER] =
    BEGIN
    IF x > xColorMax THEN
      BEGIN
      IF y <= bwFeedTop THEN
        RETURN[
          TRUE, ((x - xColorMax)*bwScaleD)/bwScaleN - bwxoff,
            (y*bwScaleD)/bwScaleN - bwyoff]
      ELSE RETURN[FALSE, x - xColorMax, y - bwFeedTop];
      END
    ELSE
      BEGIN
      RETURN[TRUE, (x*cScaleD)/cScaleN - cxoff, (y*cScaleD)/cScaleN - cyoff];
      END;
    END;


  END.