
Object subclass: #A4PaperArc
  instanceVariableNames: 
    'index theNames thePieces theGraph theTexts theCurves classMacro ownMacro '
  classVariableNames: ''
  poolDictionaries: 
    'FunctionKeys ' !

!A4PaperArc class methods !

postScriptFonts
  ^#('Times-Roman'
             'Times-Bold'
             'Times-Italic'
             'Times-BoldItalic'
             'Symbol'
             'Helvetica'
             'Helvetica-Bold'
             'Helvetica-Oblique'
             'Helvetica-BoldOblique'
             'Courier'
             'Courier-Bold'
             'Courier-Oblique'
             'Courier-BoldOblique').! !


!A4PaperArc methods !

addNewCurve
    | aStream  temporaries |
    CurveStackC isNil
     ifTrue:
       [CurveStackC:=OrderedCollection new].
    CurveStackC isEmpty
    ifFalse:
     [ self addNewCurve: CurveStackC removeLast.
    self giveItaName:thePieces last.
   "  index isNil
         ifTrue:[index:=0.theNames:=OrderedCollection new].
      aStream:=WriteStream on:(String new).
     'Curve'  printOn:aStream.
     index printOn:aStream.
      temporaries:=aStream contents.
      theNames addLast:temporaries.
      index:=(index +1).
      thePieces last name: temporaries "
      ].!

addNewCurve:curves
    | aStream  temporaries |


      thePieces addLast:( A4CurvePiece new
                            rectangle:(self newPlace)
                            theCurves: curves).!

addNewPiece
  | temporaries aChar oldOffset center aPen upperRight aStream|
 PlasmaFormFifo isEmpty ifTrue: [^nil].
 self addNewPiece:  PlasmaFormFifo removeLast.
 self giveItaName: thePieces last.
  "  index isNil
         ifTrue:[index:=0.theNames:=OrderedCollection new].
     aStream:=WriteStream on:(String new).
     'Pieces'  printOn:aStream.
     index printOn:aStream.
      temporaries:=aStream contents.
      theNames addLast:temporaries.
      index:=(index +1).
    thePieces last name:temporaries."!

addNewPiece:aForm
  | temporaries aChar oldOffset center aPen upperRight aStream|


    thePieces addLast:
         (
          A4FormPiece new
           rectangle: (self newPlace)
           form: aForm
         ).!

addNewText
  | aStream  temporaries aChar oldOffset center aPen upperRight |

 self addNewText:
 '....!!....1....!!....2....!!....3....!!....4....!!....5....!!....6....!!....7....!!....8'.
 self giveItaName:thePieces last.!

addNewText:aText
  | numS  aLine  i  theBytewidth   aFile aString|
 " aFile:=ReadStream on:aText.
  aFile isNil
      ifFalse:[
         theBytewidth:= aFile nextLine size.

          i:=1.theBytewidth:=1.
          [aFile atEnd]
             whileFalse:[ aLine:=aFile nextLine .
                aLine isNil
                   ifFalse:[ numS:=0.
                         aLine do:[:ch | ch=$# ifTrue:[numS:=numS+2].
                                         ch=$& ifTrue:[numS:=numS+1]
                                  ].
                         theBytewidth:=
                           (aLine size -numS)
                             max:theBytewidth
                           ].
                          i:=i+1
                        ].
              ].
     aString:=String new:theBytewidth.
     1 to:theBytewidth do:[:aPos  | aString at:aPos put:$.].
     aString:=aFile contents."
    aString:=''.
    thePieces addLast:
         (
          A4TextPiece new
           rectangle: (self newPlace )
           text: (aString,aText)
         ).!

addNewText:aText place:aRect
  |aString|
    aString:=''.
    thePieces addLast:
         (
          A4TextPiece new
           rectangle:aRect
           text: (aString,aText)
         ).
  self giveItaName:thePieces last.!

addNewText:aText place:aRect font:aChar
  |aString|
    aString:=''.
    thePieces addLast:
         (
          A4TextPiece new
           rectangle:aRect
           text: (aString,aText)
         ).
  self giveItaName:thePieces last.
  thePieces last  font:
     (A4PaperArc postScriptFonts at:((aChar asciiValue)+(-64)))!

addNewVGAPiece
  | temporaries aChar oldOffset center aPen upperRight aStream|
 PlasmaFormFifo isEmpty ifTrue: [^nil].
 self addNewPiece:  PlasmaFormFifo removeLast.
 self giveItaName: thePieces last.
 thePieces last bitsPerPixel:4.!

animatedCurves
        "comment"
    | temporaries curves|
  1 to: 4 do:[:i |
       1 to:5 do: [:j |
                  curves:=CurveStackC  removeLast.
                  thePieces addLast:( A4CurvePiece new
                            rectangle:((i*50)@(j*50) extent:40@40)
                            theCurves: curves).
                  ]
              ].
   self upDate!

changeFont
    |  aText aName|
  thePieces isNil ifTrue: [^nil].
  aText:=self locate: thePieces.
  aText isNil ifTrue: [^nil].
  aText class==A4TextPiece
  ifTrue:
 [
  [Terminal read == EndSelectFunction]
     whileFalse:[].
  aName:=Menu oneOfMany:
          A4PaperArc postScriptFonts.
  aName isNil
   ifFalse:[ aText font: aName]
  ]!

changeFromFifo
   | aPiece aRectangle|
    aPiece := self locate:thePieces.

    aPiece isNil ifTrue:[^nil].
    aPiece rectangle:
              (aPiece rectangle) form:(   PlasmaFormFifo removeLast  )!

display
    | aPen  pos oldOffset xpos ypos aForm aRectangle
      savedRectangle aPiece aText aCurvePiece aName curve aType p|
  aPen:=Pen new.
 aPiece := self locate: thePieces.
 aPiece isNil ifFalse:
[  p:=aPiece.
  (Form width: (p extent x) height:(p extent y))
                 displayAt:(p origin x)@(p origin y).
               aPen rectangleDraw:(p origin) corner: (p origin + p extent).
   aPiece class == A4FormPiece
 ifTrue:
[
 aForm:=aPiece form.
 aRectangle:=aPiece  rectangle.
     pos:= 0@0."aRectangle origin."
     Cursor offset:pos.
  [Terminal read == SelectFunction]
     whileFalse:[
                Cursor offset = oldOffset
                ifFalse: [
                    oldOffset := Cursor offset.
                xpos:=((oldOffset x) - (pos  x)) .
                ypos:=((oldOffset y) - (pos  y)) .
                xpos >  (aForm width - (aRectangle extent x))
                     ifTrue: [ xpos:=aForm width - (aRectangle extent x)].
                ypos > ( aForm height - (aRectangle extent y))
                     ifTrue: [ ypos:=aForm height - (aRectangle extent y)].
                xpos>=0
                     ifFalse:[xpos:=0].
                ypos>=0
                     ifFalse:[ypos:=0].
         Display copy:((xpos@ypos)
                    extent:((aRectangle  extent))-(1@1))
           from: aForm
                to:( ( aRectangle origin) +(1@1))
                rule:Form over.
                         ]
                ] .

   ^nil
 ].
 aPiece class == A4TextPiece
   ifTrue: [aPiece editCh ].
 aPiece class == A4CurvePiece
 ifTrue:
 [ aCurvePiece := aPiece .
                          [Terminal read == EndSelectFunction]
                           whileFalse:[].
                         aType:=Menu confirm:'smothCurve?'.
                         aType ifFalse:[aType:=nil].
                         1 to:(aCurvePiece size)
                           do:[: c |
                              curve := aCurvePiece aCurve .
                               savedRectangle:=curve baseRectangle.
                               curve baseRectangle:aCurvePiece rectangle.
                               curve scale:
                                  ((aCurvePiece rectangle extent x asFloat)/
                                    (savedRectangle extent x asFloat))@
                                   ((aCurvePiece rectangle extent y asFloat)/
                                    (savedRectangle extent y asFloat)).
                               curve draw:(Form reverse).
                               curve scale:(1.0@1.0).
                               curve baseRectangle:savedRectangle;type:aType.
                              ].
                            [Terminal read == SelectFunction]
                                whileFalse:[ ]
 ]
 ].!

displayAll
   | aPen  aForm aRectangle |

aPen:=Pen new.
thePieces do:
[ :aPiece |
  aPiece class == A4FormPiece
 ifTrue:
 [
 aForm:=aPiece form.
 aRectangle:=aPiece  rectangle.


  Display copy:((0@0)
                    extent:((aRectangle  extent))-(1@1))
           from: aForm
                to:( ( aRectangle origin) +(1@1))
                rule:Form over.
 ]

]!

duplicate
    | aPiece  |
  thePieces isEmpty ifFalse:
      [ aPiece:=self locate:thePieces].
     aPiece isNil ifFalse:
 [
 aPiece class == A4FormPiece
 ifTrue:
    [
     PlasmaFormFifo addLast:  aPiece form.
    ].
   aPiece class == A4TextPiece
    ifTrue:
     [
     StackCh addLast:aPiece text.
     ].
   aPiece class == A4CurvePiece
    ifTrue:
      [
       CurveStackC addLast:aPiece theCurves.
      ]
]!

editOwnMacro
   ownMacro  editCh!

garbage!

giveItaName:aPiece
    | temporaries aStream|

  index isNil
         ifTrue:[index:=0.theNames:=OrderedCollection new].
     aStream:=WriteStream on:(String new).
     aPiece class == A4FormPiece
       ifTrue:   ['Pieces'  printOn:aStream].
     aPiece class == A4TextPiece
        ifTrue:   [ 'Text'  printOn:aStream].
      aPiece class == A4CurvePiece
        ifTrue:   [ 'Curve'  printOn:aStream.  ].
     index printOn:aStream.
      temporaries:=aStream contents.
      theNames addLast:temporaries.
      index:=(index +1).
    aPiece name:temporaries.!

graph: aRect
        "comment"
     | aForm |

    aForm:=Form width: aRect width
                height: aRect height.

    aForm displayAt: aRect origin.
  ^aForm!

initWindowSize
        "comment"
    | temporaries |
   ^((0@0) extent:(310@399))!

lastPiece

  ^thePieces last!

locate: piecesOrTexts
    | pos oldOffset xpos ypos aForm aRectangle aname result|
    piecesOrTexts isEmpty ifTrue: [^nil].
    [Terminal read == SelectFunction]
     whileFalse:[].
    pos:=Cursor offset.
    1 to:piecesOrTexts size do:
     [ :i |
        (( piecesOrTexts at:i) rectangle containsPoint:pos )
          ifTrue:[ ^piecesOrTexts at:i].
     ].
      [Terminal read == EndSelectFunction]
     whileFalse:[].
      aname:=Menu oneOfMany:
              theNames asArray.
    1 to:piecesOrTexts size do:
      [:i |
        aname=((piecesOrTexts at:i) name)
         ifTrue:[result:=(piecesOrTexts at:i)]
      ].
     ^result!

menu
        "Private - Answer the hierarchy pane menu."
    ^Menu
        labels:
     'testFonts\program\upDate\displayAll\addNewPiece\addNewText\addNewCurve\changeFromFifo\placeBefore\PaintBrushPict\scannedPict\scannedPictFromMW\display\changeFont\print\printToFile\move\duplicate\remove\rotate\ownMacro\vgaPict' withCrs
        lines: Array new
        selectors:
     #(testFonts program upDate displayAll addNewPiece addNewText addNewCurve changeFromFifo placeBefore PaintBrushPict scannedPict scannedPictFromMW display changeFont print printall move duplicate remove rotate editOwnMacro vgaPict)!

move
   | aPiece aRectangle|
    aPiece := self locate:thePieces.

    aPiece isNil ifTrue:[^nil].
    aRectangle :=aPiece rectangle.
      (Form width:(aRectangle extent x+1) height:(aRectangle extent y+1))
           displayAt:(aRectangle origin).

     aRectangle := self newPlace:aRectangle.
     aPiece class == A4FormPiece
     ifTrue:[ aPiece rectangle: aRectangle form:(aPiece form)  ].
    aPiece class == A4TextPiece
     ifTrue:[aPiece rectangle:aRectangle text:(aPiece text)].
    aPiece class == A4CurvePiece
     ifTrue:[aPiece rectangle:aRectangle theCurves:(aPiece theCurves)].
    self upDate!

newPlace
  | temporaries aChar oldOffset center aPen upperRight |
 aPen:=Pen new.
    aPen defaultNib: 1@1;
        mask: Form white;
        combinationRule: Form reverse;
        place: (center := Cursor offset).

     aPen rectangleDraw:center corner:(upperRight:= (center+(20@(-20)))).

     [Terminal read == SelectFunction]
     whileFalse:[
                Cursor offset = oldOffset
                ifFalse: [
                    oldOffset := Cursor offset.
                    aPen rectangleDraw:center corner: upperRight.
                    center:=oldOffset.
                    upperRight:=center+(20@(-20)).
                    aPen rectangleDraw:center corner: upperRight.
                         ]
                ] .
       Cursor offset: upperRight.
       [Terminal read == SelectFunction]
     whileFalse:[
                Cursor offset = oldOffset
                ifFalse: [
                    oldOffset := Cursor offset.
                    aPen rectangleDraw:center corner: upperRight.
                    upperRight:=oldOffset.
                    aPen rectangleDraw:center corner: upperRight.
                         ].
               ] .
     ( Form width: (upperRight x -(center x)+(-1))
            height:(((center y)-(upperRight y))+(-1)) )
       displayAt:(center x+1)@(upperRight y+1).
   " aPen mask: (Form gray);fillAt:(center+(1@(-1))). "


            ^(
             Rectangle origin:((center x))@(((upperRight y)))
               extent:
                ((upperRight x -(center x)))@(((center y)-(upperRight y)))
             )!

newPlace:aRectangle
  | temporaries aChar oldOffset center aPen upperRight origExtent|
 origExtent:=(aRectangle extent x)@((aRectangle extent y) negated).
 center:=(aRectangle origin x)@(aRectangle origin y+(aRectangle extent y)).
 Cursor offset: center.
 upperRight:=aRectangle corner.
 aPen:=Pen new.
    aPen defaultNib: 1@1;
        mask: Form white;
        combinationRule: Form reverse;
        place: (center ).

     aPen rectangleDraw:center corner:upperRight.

     [Terminal read == SelectFunction]
     whileFalse:[
                Cursor offset = oldOffset
                ifFalse: [
                    oldOffset := Cursor offset.
                    aPen rectangleDraw:center corner: upperRight.
                    center:=oldOffset.
                    upperRight:=center+origExtent.
                    aPen rectangleDraw:center corner: upperRight.
                         ]
                ] .
       Cursor offset: upperRight.
       [Terminal read == SelectFunction]
     whileFalse:[
                Cursor offset = oldOffset
                ifFalse: [
                    oldOffset := Cursor offset.
                    aPen rectangleDraw:center corner: upperRight.
                    upperRight:=oldOffset.
                    aPen rectangleDraw:center corner: upperRight.
                         ].
               ] .
     ( Form width: (upperRight x -(center x)+(-1))
            height:(((center y)-(upperRight y))+(-1)) )
       displayAt:(center x+1)@(upperRight y+1).
   " aPen mask: (Form gray);fillAt:(center+(1@(-1))). "


            ^(
             Rectangle origin:((center x))@(((upperRight y)))
               extent:
                ((upperRight x -(center x)))@(((center y)-(upperRight y)))
             )!

open
        "comment"
    | temporaries aChar oldOffset center aPen upperRight aTopPane|
 thePieces:=OrderedCollection new.
 theTexts:=OrderedCollection new.
 theCurves:=OrderedCollection new.
 classMacro:=A4TextPiece new.
 ownMacro:=A4TextPiece new font:'';name:'ownMacro'.
aTopPane := TopPane new
         model:self;
        label: 'a A4-paper arc';
        yourself.

 aTopPane addSubpane:
        (theGraph:=GraphPane new
            model: self;
            name: #graph:;
            menu:#menu;
            framingRatio: ((0 @ 0)
                        extent: (1 @ 1 ))
        ).
  aTopPane dispatcher open scheduleWindow  .!

openCh
        "comment"
    | temporaries aChar oldOffset center aPen upperRight aTopPane|
 thePieces:=OrderedCollection new.
 theTexts:=OrderedCollection new.
 theCurves:=OrderedCollection new.
 classMacro:=A4TextPiece new.
 ownMacro:=A4TextPiece new font:'';name:'ownMacro'.!

openCj
        "comment"
    | temporaries aChar oldOffset center aPen upperRight aTopPane|
aTopPane := TopPane new
         model:self;
        label: 'a A4-paper arc';
        yourself.

 aTopPane addSubpane:
        (theGraph:=GraphPane new
            model: self;
            name: #graph:;
            menu:#menu;
            framingRatio: ((0 @ 0)
                        extent: (1 @ 1 ))
        ).
  aTopPane dispatcher open scheduleWindow  .!

ownMacro

    ownMacro editCh!

PaintBrushPict
        "comment"
    | scannedStack aName aFile|

 aName := Prompter prompt: 'PaintBrush  filename'
            default: 'd:\pbrush\cutout.pcc'.
 aFile:=(File pathName: aName  ).
 scannedStack:=aFile fillItsBitmapFromPCX.
 [scannedStack isEmpty]
   whileFalse:[
                PlasmaFormFifo addLast: scannedStack removeFirst.
                self addNewPiece
              ].!

placeBefore
   | aPiece anOtherPiece|
    aPiece := self locate:thePieces.
    aPiece isNil ifTrue:[^nil].

    anOtherPiece := self locate:thePieces.
    anOtherPiece isNil ifTrue:[^nil].
    aPiece=anOtherPiece ifTrue:[^nil].
    aPiece:=thePieces remove:aPiece ifAbsent:[^nil].
    thePieces add:aPiece before:anOtherPiece.
    self upDate!

print
    |  aName |
  [
    (PrintStream new initialize)  takeItsEveryThing: self
  ] forkAt:2!

printall
    |  aName aFile|
   theTexts isNil ifTrue: [^nil].
   aName := Prompter prompt: 'Postscript file name'
            default: 'plasmapi'.

    "  [Terminal read == SelectFunction]  whileTrue: []."
    File remove:aName.
    (aFile:=(File pathName:   aName)) takeItsEveryThing: self.
    aFile close!

printAndWait
    |  aName |
    (PrintStream new initialize)  takeItsEveryThing: self!

printFirst
    |  aName aFile|
   theTexts isNil ifTrue: [^nil].
   aName := Prompter prompt: 'Postscript file name'
            default: 'e:\postscri\plasmapi'.


    File remove:aName.
    aFile:=(File pathName:   aName).
    aFile takeItsEveryThing: self.
    ^aFile!

printMore:aFile
    |  aName |
   aFile takeItsThing: self!

program

" self animatedCurves.
  false ifFalse:[^nil]. "

0 to:5 do:
[: j |
0 to: 1 do:
[: i  |
 PlasmaFormFifo  isEmpty
 ifFalse:
 [
 thePieces addLast:
         (
          A4FormPiece new
           rectangle: ( (i*120+40)@(j*55+50) extent:110@50)

           form:
            PlasmaFormFifo removeLast
         )."PlasmaFormFifo removeLast."
  self giveItaName:thePieces last."

  (thePieces at:(j*6+i+3)) rectangle: ((thePieces at:(j*6+i+3))
                  rectangle) form:(   PlasmaFormFifo removeLast  )"

 "aStream:=WriteStream on:(String new).
 ' height: '  printOn:aStream.
 (2-j*5+(5-i)) printOn:aStream.
 thePieces addLast:
         (
          A4TextPiece new
           rectangle: ( (2-j*100+20)@(5-i*50+64) extent:  80@6 )
           text:
            aStream collection
         ).
  self giveItaName:thePieces last. "
 ]
].
].


self upDate!

remove
    | pos oldOffset xpos ypos aForm aRectangle aPiece aText aCurve|
    thePieces isEmpty ifFalse:
      [ aPiece:=self locate:thePieces].
     aPiece isNil ifFalse:
 [    thePieces remove:aPiece.
       theNames remove:aPiece name.
      aRectangle :=aPiece rectangle.
      (Form width:(aRectangle extent x+1) height:(aRectangle extent y+1))
           displayAt:(aRectangle origin).
     self  upDate.
      aPiece class == A4FormPiece
 ifTrue:
    [

     aForm:=aPiece form.
     PlasmaFormFifo addLast: aForm.


     ^aPiece
    ].
     aPiece class == A4TextPiece
    ifTrue:
     [
     StackCh addLast:aPiece text.


     ^aPiece
     ].
       aPiece class == A4CurvePiece
    ifTrue:
      [
       CurveStackC addLast:aPiece theCurves.

      ^aPiece
      ].
].
     ^nil!

rotate
   | pos oldOffset xpos ypos aForm aRectangle aPiece aText aName|
 aPiece := self locate: thePieces.
 aPiece isNil ifFalse:
[
  PlasmaFormFifo addLast:
        aPiece form rotate.
]!

scannedPict
        "comment"
    | scanned aName aFile|

 aName := Prompter prompt: 'Scanned pictures filename'
            default: '\smaltalk\scanpic'.
 aFile:=(File pathName: aName  ).

 scanned:=Form width:484 height:194.
 aFile fillItsBitmap: scanned.
 PlasmaFormFifo addLast: scanned .
 self addNewPiece!

scannedPictFromMW
        "comment"
    | scannedStack aName aFile|

 aName := Prompter prompt: 'Scanned pictures filename'
            default: 'd:\scan\mwtot.msp'.
 aFile:=(File pathName: aName  ).
 scannedStack:=aFile fillItsBitmapFromMW.
 [scannedStack isEmpty]
   whileFalse:[
                PlasmaFormFifo addLast: scannedStack removeFirst.
                self addNewPiece
              ].!

size
    ^thePieces size!

testFonts
  |   aStream ch temporaries|
aStream:=WriteStream on:('                                   ').

 32 to: 127 do: [: i |
                         aStream nextPutAll:'\\'.
                         aStream nextPut:( (i // 64 +48+2) asCharacter).
                         aStream nextPut:( (i \\ 64 // 8+48 )asCharacter).
                         aStream nextPut:(( i \\  8+48 )asCharacter).

                         aStream nextPutAll:'#A#4#*'.
                         aStream nextPutAll:'\\'.
                         aStream nextPut:( (i // 64 +48) asCharacter).
                         aStream nextPut:( (i \\ 64 // 8+48 )asCharacter).
                         aStream nextPut:(( i \\  8+48 )asCharacter).

                         aStream nextPutAll:'  #$'.

                   (i \\ 8 )=7
                   ifTrue:
                    [aStream nextPut:(13 asCharacter);nextPut:(10 asCharacter)].
                ].
           aStream nextPut:(13 asCharacter);nextPut:(10 asCharacter).



 thePieces addLast:
         (
          (A4TextPiece new )
           rectangle: ( 50@50 extent: 200 @ 300)
           text:   aStream collection ;name:'program'

         ).
 theNames addLast:'program'.

self upDate!

theCurves
   ^theCurves!

thePieces
   ^thePieces!

theTexts
   ^theTexts!

upDate
   | aPen |
   aPen:=Pen new.
   (Form width: (300) height:(374))
                 displayAt:(2)@(22).

    thePieces do:[:p |
             (Form width: (p extent x) height:(p extent y))
                 displayAt:(p origin x)@(p origin y).
               aPen rectangleDraw:(p origin) corner: (p origin + p extent).
                 ].!

vgaPict
        "comment"
    | aForm aName aFile|

 aName := Prompter prompt: 'VGApictures filename'
            default: 'a:\picture'.
 aFile:=(File pathName: aName  ).


 aForm:=aFile formFromSelf.
 PlasmaFormFifo addLast: aForm .
 self addNewVGAPiece.
 aForm:=aFile formFromSelf.
 PlasmaFormFifo addLast: aForm .
 self addNewVGAPiece! !
