-- GriffinToTAPrivateImpl.mesa
-- Beach,
June 27, 1982 12:54 pm

DIRECTORY
 GFileFormatDefs,
 GriffinToTA,
 GriffinToTAPrivate,
 IO,
 NameSymbolTable USING [MakeName, Name],
 PairList,
 Rope,
 -- RTTypesBasic USING [NarrowFault],
 SplineDefs,
 StyleDefs,
 TextNode USING [FirstChild, LastChild, NewTextNode, Parent, Previous, Ref, RefTextNode];


GriffinToTAPrivateImpl: PROGRAM
IMPORTS GriffinToTA, IO, NameSymbolTable, Rope, TextNode
EXPORTS GriffinToTAPrivate = {
 OPEN GriffinToTA;

ROPE: TYPE = Rope.ROPE;

FindColor: PUBLIC PROCEDURE[h, s, b: REAL] RETURNS[ColorRef] = {
 c: ColorRef ← colors;
MatchColor: PROCEDURE[c: ColorRef, h, s, b: REAL] RETURNS[BOOLEAN] = INLINE {
  RETURN[c#NIL AND c.h=h AND c.s=s AND c.b=b]};
MakeColorName: PROCEDURE[n: CARDINAL] RETURNS[ROPE] = INLINE {
  RETURN[IO.PutFToRope["Color%d", IO.card[n]]]};
WHILE c#NIL DO
  IF MatchColor[c, h, s, b] THEN RETURN[c];
  c ← c.next;
  ENDLOOP;
 colorNumber ← colorNumber+1;
 colors ← NEW[ColorRec ← [colors, MakeColorName[colorNumber], h, s, b]];
RETURN[colors];
 };

FindPathStyle: PUBLIC PROCEDURE[areaColor, outlineColor: ColorRef, pathType: ROPE, lineWeight: REAL] RETURNS[PathStyleRef] = {
 p: PathStyleRef ← pathStyles;
MatchPathStyle: PROCEDURE[p: PathStyleRef, areaColor, outlineColor: ColorRef, pathType: ROPE, lineWeight: REAL] RETURNS[match: BOOLEAN] = {
  match ← p#NIL;
  IF match AND areaColor#NIL THEN match ← p.areaColor=areaColor;
  IF match AND outlineColor#NIL THEN match ← p.outlineColor=outlineColor;
  IF match THEN match ← Rope.Equal[p.pathType,pathType];
  IF match AND lineWeight#0 THEN match ← p.lineWeight=lineWeight;
  RETURN[match];
  };
MakePathStyleName: PROCEDURE[n: CARDINAL] RETURNS[NameSymbolTable.Name] = INLINE {
  RETURN[NameSymbolTable.MakeName[
   LOOPHOLE[Rope.Flatten[
    IO.PutFToRope["PathStyle%d", IO.card[n]]]]]];
  };
WHILE p#NIL DO
  IF MatchPathStyle[p, areaColor, outlineColor, pathType, lineWeight] THEN RETURN[p];
  p ← p.next;
  ENDLOOP;
 pathStyleNumber ← pathStyleNumber+1;
 pathStyles ← NEW[PathStyleRec ← [pathStyles, MakePathStyleName[pathStyleNumber], areaColor, outlineColor, pathType, lineWeight]];
RETURN[pathStyles];
 };

FindCaptionStyle: PUBLIC PROCEDURE[family: ROPE, size: REAL, face: ROPE, charRotation: ROPE, lineFormatting: ROPE] RETURNS[CaptionStyleRef] = {
 p: CaptionStyleRef ← captionStyles;
MatchCaptionStyle: PROCEDURE[p: CaptionStyleRef, family: ROPE, size: REAL, face: ROPE, charRotation: ROPE, lineFormatting: ROPE] RETURNS[BOOLEAN] = INLINE {
  RETURN[p#NIL AND
   Rope.Equal[p.family, family] AND
   p.size=size AND
   Rope.Equal[p.face, face] AND
   Rope.Equal[p.charRotation, charRotation] AND
   Rope.Equal[p.lineFormatting, lineFormatting]];
  };
MakeCaptionStyleName: PROCEDURE[n: CARDINAL] RETURNS[NameSymbolTable.Name] = INLINE {
  RETURN[NameSymbolTable.MakeName[
   LOOPHOLE[Rope.Flatten[
    IO.PutFToRope["CaptionStyle%d", IO.card[n]]]]]];
  };
WHILE p#NIL DO
  IF MatchCaptionStyle[p, family, size, face, charRotation, lineFormatting] THEN RETURN[p];
  p ← p.next;
  ENDLOOP;
 captionStyleNumber ← captionStyleNumber+1;
 captionStyles ← NEW[CaptionStyleRec ← [captionStyles, MakeCaptionStyleName[captionStyleNumber], family, size, face, charRotation, lineFormatting]];
RETURN[captionStyles];
 };

EqualCardinals: PUBLIC PairList.EqualProc = {
 l,r: CARDINAL;
 return ← FALSE;
 --if not a cardinal, will signal out
 l ← NARROW[left,REF CARDINAL]^; r ← NARROW[right,REF CARDINAL]^;
 IF l=r THEN return ← TRUE;
 };

-- procedures to create Tioga node structures

InsertNodeAfterChildrenOf: PUBLIC PROCEDURE[node: TextNode.RefTextNode, newnode: TextNode.RefTextNode ← NIL] RETURNS[TextNode.RefTextNode] = {
 p: TextNode.Ref;
 IF newnode = NIL THEN -- create a new node
  newnode ← TextNode.NewTextNode[]
 ELSE {-- unhook newnode from the tree
  p ← TextNode.Parent[newnode];
  IF TextNode.FirstChild[p] = newnode THEN { -- newnode is a first sibling
   IF newnode.last THEN -- it was an only child
    p.child ← NIL
   ELSE -- its sibling is now the parents first child
    p.child ← newnode.next}
  ELSE { -- unhook newnode from the middle of a chain
   p ← TextNode.Previous[newnode, p];
   p.next ← newnode.next;
   p.last ← newnode.last}};
 -- newnode is unattached
 IF TextNode.FirstChild[node]=NIL THEN { -- add newnode as a first child
  node.child ← newnode;
  newnode.next ← node;
  newnode.last ← TRUE}
 ELSE { -- finally, add newnode as sibling of last sibling
  p ← TextNode.LastChild[node];
  newnode.next ← p.next;
  newnode.last ← p.last;
  p.next ← newnode;
  p.last ← FALSE};
 RETURN[newnode];
 };

InitColors: PUBLIC PROCEDURE[] RETURNS[ColorRef] = {
-- create the Griffin color table: "name",   h, s, b
 colors ← NEW[ColorRec ← [NIL, "black",   0, 0, 0]];
 colors ← NEW[ColorRec ← [colors, "darkBrown", 7, 255, 59]];
 colors ← NEW[ColorRec ← [colors, "brown",  7, 255, 118]];
 colors ← NEW[ColorRec ← [colors, "tan",   0, 131, 217]];
 colors ← NEW[ColorRec ← [colors, "maroon",  234, 255, 79]];
 colors ← NEW[ColorRec ← [colors, "darkRed", 0, 255, 160]];
 colors ← NEW[ColorRec ← [colors, "red",   0, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "orange",  10, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "darkYellow", 25, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "yellow",  40, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "lightYellow", 40, 190, 255]];
 colors ← NEW[ColorRec ← [colors, "darkGreen", 71, 255, 59]];
 colors ← NEW[ColorRec ← [colors, "green",  76, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "lightGreen", 71, 193, 255]];
 colors ← NEW[ColorRec ← [colors, "darkBlue", 150, 255, 170]];
 colors ← NEW[ColorRec ← [colors, "blue",   148, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "lightBlue", 141, 150, 255]];
 colors ← NEW[ColorRec ← [colors, "darkAqua", 107, 255, 98]];
 colors ← NEW[ColorRec ← [colors, "aqua",  107, 224, 255]];
 colors ← NEW[ColorRec ← [colors, "cyan",  120, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "darkPurple", 178, 255, 178]];
 colors ← NEW[ColorRec ← [colors, "purple",  170, 224, 255]];
 colors ← NEW[ColorRec ← [colors, "violet",  170, 131, 255]];
 colors ← NEW[ColorRec ← [colors, "magenta", 200, 255, 255]];
 colors ← NEW[ColorRec ← [colors, "pink",  206, 170, 255]];
 colors ← NEW[ColorRec ← [colors, "darkGrey", 0, 0, 40]];
 colors ← NEW[ColorRec ← [colors, "grey",  0, 0, 120]];
 colors ← NEW[ColorRec ← [colors, "lightGrey", 0, 0, 200]];
 colors ← NEW[ColorRec ← [colors, "paleGrey", 0, 0, 230]];
 colors ← NEW[ColorRec ← [colors, "white",  0, 0, 255]];
 RETURN[colors];
 };

}.