Ideally you should not simply create new key/value pairs in whatever the current dictionary happens to be when you run the BeginPage or EndPage procedures. The reason is that you can't rely on exactly which dictionary happens to be on the top of the dictionary stack at the time these are executed, and it can be different.
This can lead to the keys you expect to be present not being defined, and can (potentially) lead to you overwriting keys/value pairs already in the dictionary.
In fact it's usually regarded as better practice, in PostScript, not to define transient variables in a dictionary at all, but simply to store them on the stack and access them from there. This negates the possibility of overwriting values, and stack operations are usually faster than dictionary operations, so there's a (slight) performance benefit as well.
Your EndPage procedure performs all the calculations and executes the stroke, even when it's not going to transmit the page to the device, which is clearly wasteful, you should use an if instead. You are using gsave and grestore together as a pair, that's pointless and very expensive performance wise. That's a save and restore of the graphics state, if you don't do anything in between then it has no effect.
Finally your BeginPage procedure defines /count but you don't appear to actually do anything with it!
If I were you I would rewrite these as:
/BeginPage {
userdict /MyDict known %% is MyDict alreadly present in userdict ?
{
userdict /MyDict get %% If it is, then get it
}
{
userdict begin %% start userdict (makes it top element on dict stack)
/MyDict %% Put key /MyDict on stack
%% stack - /MyDict
5 dict %% make a 5 element dictionary
%% stack - /MyDict -dict-
dup %% make a copy (duiplicates a pointer)
%% stack - /MyDict -dict -dict-
3 1 roll %% rotate the stack
%% stack -dict /MyDict -dict
put %% Put the top element on the stack in the current
%% dictionary, using the key second top on the stack
%% stack -dict-
end %% close userdict
} ifelse
begin %% make the top element on the stack the current
%% dictionary.
/count exch def %% store the count ot pages in the currnt dictionary
%% using the key /count.
end %% close the current dictionary
}
Obviously you can drop all the comments, they are purely explanatory. From this point on you can find the value associated with count by doing:
userdict /MyDict get /count get
Then:
/EndPage {
%% We enter with the reason code on the stack
0 eq { %% We only want to take action for 0 (showpage)
gsave
currentpagedevice %% Get the current page device dictionary
%% stack -dict-
/PageSize get %% get the value associated with the key /PageSize
%% from that dictionary.
%% stack -array-
dup dup %% make some copies of the array
%% stack -array- -array- -array-
0 get %% get element 0 from the array
%% stack -array- -array- 'x'
9 sub %% subtract 9 from the value
0 put %% put that into array element 0
%% stack -array-
dup dup 1 get %% repaeat for element 1
9 sub 1 put
newpath
-1 -1 moveto
0 9 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
dup 0 get %% duplicate the array, get the 0th element
-1 moveto
0 9 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
dup 0 get %% duplicate the array, get the 0th element
%% stack -array- 'x'
1 index %% copy the element one down the stack to the top
%% stack -array- 'x' -array-
1 get %% get element 1 of the array
%% stack -array- 'x' 'y'
moveto
0 10 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
1 get %% get element 1 from the array
-1 exch moveto
0 10 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
1 setlinewdith
stroke
grestore
true
}
{
false
}ifelse
}
Note that I have not tested the code, it's just there for illustration.
Now the real point of this is that the reason your strokes are misplaced is because you have scaled the page.
-dFitPage
works by adjusting the Current Transformation Matrix to scale the page contents so that they fit into the new page dimensions. Everything after that point will be scaled, and this includes the path construction in your EndPage procedure.
To demonstrate; try executing the command line without -dFIXEDMEDIA
etc and you should see that the page comes out the original size and with the strokes in the correct place.
In order to place the strokes correctly you need to know what scale factor has been applied and multiply the co-ordinate values by the inverse of that amount. That is, if the scale factor was 0.5 (ie half size) you would need to multiply the positions by 2.