SQR Print Positioning And The Report Grid | KEVIN RESCHENBERG 08-07-2014 |
In the last post on SQR report basics, we saw how the sections of a report
(the heading, body and footing) are defined and when they are produced. Today we'll look more carefully at how
to specify where elements are printed.
By default, the report uses a fixed-pitch font, where every letter takes the same amount of space. Think of this space as
being a cell in a grid. In addition to just printing one thing after another, SQR allows us to jump around
in this grid and print wherever we want.
The PRINT statement includes a positioning specification that consists of up to three parameters, all optional:
print $x (1, 2, 3)
The parameters are:
- The row as an absolute or relative number (1 in this example)
- The column as an absolute or relative number (2 in this example)
- The length (3 in this example)
So in this example we are printing the value of $x at row 1, column 2, for a length of 3 characters (padded or truncated).
The simplest positioning specification is () . Since all of the parameters are missing, this just means
that the value will print at the current position, for whatever length it happens to be.
Relative positions can be used:
print $x (+1, -2)
This prints starting at 1 row down and 2 columns to the left of the current position. After it prints, the next space becomes the new position
(sort of—more later). Note that the "+" and "-" indicate relative positioning and not positive or negative row/column numbers. "+1" is not the same as "1".
You can use absolute or relative row and column numbers in numeric variables, but any variable containing a relative number must
be preceded by the "+" (whether you are moving up or down, left or right):
let #PositiveOne = 1
let #NegativeOne = -1
print 'Absolute position at row 1, column 1' (#PositiveOne, #PositiveOne)
print 'Relative: Down 1 row, right 1 column' (+#PositiveOne, +#PositiveOne)
print 'Relative: Up 1 row, left 1 column' (+#NegativeOne, +#NegativeOne)
If you forget to include the "+" then SQR will consider any positive value to be an absolute row or column number and any
negative value to be invalid (since you can't position to a negative row or column).
An example of positioning and the result:
print 'First' ()
print 'Second' ()
print 'Third' ()
print 'Row 5 column 3 with truncation' (5, 3, 25)
print 'Down 2 and left 15' (+2, -15)
print 'Up 3 and current column' (-3) ! Could also be (-3, 0) or (-3,)
print 'Same row right 3' (, +3) ! Could also be (0, +3)
"First", "Second" and "Third" each print at the current position, so each just follows the previous one. Then we print a line
at an absolute position (row 5, column 3). Next we move down 2 rows and left 15 columns from the current position, which was the
space immediately after the last line printed. The final two lines are also positioned relatively.
Now look at this:
print 'XXXXX' (1, 1)
print 'yyyyy' (1, 2)
print 'This is in column 1' (2, 1)
print 'This is in column 2' (2, 2)
This prints "XXXXX" in column 1 and "yyyyy" in column 2. Now, you might expect the "yyyyy" to replace the last four X's, but instead
they just overprint and seem to coexist in the same cells. Here's why, and it's important for understanding the whole theory of the grid.
Each string you print, no matter how large the font is or how long the string, just starts at a particular position, and that's all we
know about it. SQR does not fill an array with data—in other words, if you printed two strings starting in the same position, both would appear (overlapped).
This is a grid of coordinates, not a storage area for the text.
But what about that earlier example where we printed three strings in sequence?
Surely "First" occupies the first five cells and then "Second" prints after that? No, not really. After SQR prints "First", it estimates
how much space it took (by simply counting the characters) and then sets the current position to be the next available position. It works perfectly
in this case, but only because we are using a fixed-pitch font. This is an estimate only, and if we used a proportional font (where "i"
takes a little space and "w" takes a lot), it wouldn't work that way at all:
SQR estimates that "First" takes five positions in the coordinate system, but that is true only for the fixed-pitch font (in the specific default size).
The other two lines show that the estimate is wrong—"First" does not always take five "characters" of space. So now that's kind of a mess.
Next time we'll find ways around this and look at SQR proportional font positioning.
|