![]() |
Graphical markup and its typesetting | ![]() |
A picture is a graphics tag. Its children are drawn in order, so that later children cover earlier ones (as in SVG; see the comment at the top of
Typeset/Concat/concat_graphics.cpp
). A freshly inserted picture, as created by make-graphics in
graphics/graphics-utils.scm
, looks like this:
(with "gr-mode" "point"
"gr-frame" (tuple "scale" "1cm" (tuple "0.5gw" "0.5gh"))
"gr-geometry" (tuple "geometry" "1par" "0.6par")
(graphics "")) |
After drawing a red line and a label it might become
(with "gr-mode" (tuple "edit" "line") "gr-color" "red" …
(graphics ""
(with "color" "red"
(line (point "-2" "0") (point "1.5" "0.5")))
(with "text-at-halign" "center"
(text-at "A" (point "0" "1"))))) |
The children of a graphics are the following kinds of trees. Their DRD properties are declared in
Data/Drd/drd_std.cpp
, their semantics is described in graphics primitives.
A point with two (or three) coordinates. The coordinates are numbers in the units of the current frame, but lengths such as 1cm are also accepted: in that case edit_env_rep::as_point (
Typeset/Env/env_length.cpp
) converts them through the inverse frame.
line, cline, spline, cspline, bezier, cbezier, smooth, csmooth, arc and carc. Their arguments are points; the c variants are closed. arc and carc have exactly three points.
text-at, math-at and document-at, whose first argument is the (editable) content and whose second argument is the anchor point.
penscript and calligraphy, with four arguments: start point, end point, an ink-meta tag (identifier and pixel size at creation time) and a tuple of samples (tuple x y time pressure).
gr-group (a group of objects which behaves as a single object), superpose, gr-transform and gr-effect.
with tags around an object, setting variables like color, line-width, fill-color, point-style, arrow-end, text-at-halign or magnify.
The variable gid gives an object a graphical identifier; constraint tags like is-equal relate identified objects (see below).
Any user macro which expands to graphics, for instance rectangle, circle or arrow-with-text from
packages/standard/std-graphics.ts
.
An empty string child (the "" in the examples above) is ignored by the typesetter (typeset_graphical skips atomic children); it gives the cursor a place to stand in an empty picture.
There are two sets of graphical variables, with a different purpose. This is the single most important point to understand about the data model.
The typesetting variables, like color, line-width, point-style, fill-color or text-at-halign, are read by the typesetter when it draws an object. They are set on individual objects by with tags. Their built-in defaults are in initialize_default_env (
Typeset/Env/env_default.cpp
).
The gr- prefixed variables, like gr-color, gr-line-width or gr-point-style, are not used by the typesetter. They store the properties which the editor gives to new objects, and they live in a with around the graphics tag. Their default value is default. The menus and toolbars modify them through graphics-set-property, and the editor copies them onto new objects with graphics-enrich.
A few gr- variables do concern the picture as a whole and are read by the typesetter: gr-geometry (size and vertical alignment), gr-frame (coordinate system), gr-grid, gr-grid-aspect, gr-edit-grid, gr-edit-grid-aspect, gr-auto-crop, gr-crop-padding and gr-transformation (the 3D view). Other ones only matter to the editor: gr-mode (the current editing mode), gr-snap-distance and gr-snap (snapping parameters; the latter is not a built-in variable and is only known to Scheme).
On the Scheme side, the list of per-object attributes, their defaults and the attributes which make sense for each tag are in
graphics/graphics-drd.scm
:
The keys of attribute-default-table: gid, anim-id, proviso, magnify, color, opacity, the point, line, dash, arrow, fill, text-at-*, doc-at-* and pen attributes, without gr- prefix.
Overloaded with :require clauses: points get the point attributes, curves (and user defined graphical macros) the line, dash, arrow and fill attributes, text tags the alignment attributes, and so on. Only these attributes are copied to a new object of this kind.
Looks up attr (with or without gr- prefix) in attribute-default-table. graphics-set-property removes a gr- property instead of setting it when the new value equals this default.
The tag groups (define-group) in the same file are used throughout the editor: graphical-curve-tag (with graphical-open-curve-tag and graphical-closed-curve-tag), graphical-text-tag (text-at, math-at, document-at), graphical-pen-tag, graphical-group-tag (gr-group) and graphical-over-under-tag (draw-over, draw-under). The lists gr-tags-all, gr-tags-curves, gr-tags-noncurves and gr-tags-user are derived from them; the last one is filled by define-graphics (see the chapter on extensions).
A point is just an array<double> (
Graphics/Types/point.hpp
); the empty array is used as an invalid point, so many routines test N(p) == 0. A frame (
Graphics/Types/frame.hpp
) is an abstract invertible map between two coordinate systems. Its operators are
point frame::operator () (point p); // direct: local -> parent
point frame::operator [] (point p); // inverse: parent -> local |
and the same operators are defined on rectangles and on curves (the transformation of a curve is again a curve). Concrete frames are constructed by scaling, shift_2D, rotation_2D, slanting, linear_2D and affine_2D; they can be composed with operator * and inverted with invert. A frame also knows its Jacobian and bounds which are used when computing intersections of transformed curves.
When the typesetter enters a graphics, the environment field edit_env_rep::fr holds the frame which maps graphical coordinates (the numbers stored in point tags) to typesetter coordinates (SI units relative to the origin of the box being built). It is recomputed by edit_env_rep::update_frame (
Typeset/Env/env_semantics.cpp
) whenever gr-frame or gr-geometry changes:
gr-geometry has the form (tuple "geometry" w h [valign]); update_geometry stores the width and height in gw and gh (these define the length units gw and gh) and the alignment in gvalign.
gr-frame has the form (tuple "scale" unit (tuple x y)), for instance (tuple "scale" "1cm" (tuple "0.5gw" "0.5gh")): one graphical unit is one centimeter, and the origin is at the center of the picture. The resulting frame is scaling (magn, point (x, y + yinc)), where yinc depends on the vertical alignment. Without a valid gr-frame, a default of one centimeter with origin at (0.5par, 1yfrac) is used.
The visible rectangle of the picture, in graphical coordinates, is stored in clip_lim1 and clip_lim2.
Because the unit and the origin are lengths, zooming a picture (graphics-zoom in
graphics/graphics-main.scm
) is implemented by rewriting gr-frame and the magnify property of the picture, and scrolling (graphics-move-origin) by rewriting the origin. The objects themselves are unchanged.
The environment exposes the frame to markup through the primitives frame-direct and frame-inverse (exec_frame_direct and exec_frame_inverse in
Typeset/Env/env_exec.cpp
); the Scheme functions frame-direct and frame-inverse in
graphics/graphics-utils.scm
evaluate these primitives at the cursor, and are used by the editor to compute distances in physical units, for instance in points-dist<.
A grid (
Graphics/Types/grid.hpp
) is an abstract object which produces the lines to be drawn (grid_rep::get_curves) and which can find the nearest grid point (find_point_around) and nearby grid lines (get_curves_around). The concrete grids are empty_grid, cartesian, polar and logarithmic. The function as_grid parses trees like (tuple "cartesian" center step) or (tuple "polar" center step astep), and grid_rep::set_aspect applies the subdivisions and colors from a *-grid-aspect variable.
Each picture has two grids. The visual grid gr-grid is drawn (as the first child of the graphics_box) but plays no role in editing. The edit grid gr-edit-grid is invisible and is stored in the graphics_box; it is the grid used for snapping. By default the Scheme code keeps the edit grid synchronized with the visual one (egrid-as-vgrid?, update-edit-grid and graphics-set-edit-grid in
graphics/graphics-main.scm
).
The concater dispatches each graphical tag to a method in
Typeset/Concat/concat_graphics.cpp
(see the big switch in
concater.cpp
). For graphics this is
void
concater_rep::typeset_graphics (tree t, path ip) {
BEGIN_MAGNIFY
env->update_color ();
env->update_dash_style_unit ();
grid gr= as_grid (env->read (GR_GRID));
array<box> bs;
gr->set_aspect (env->read (GR_GRID_ASPECT));
bs << grid_box (ip, gr, env->fr, env->as_length ("2ln"),
env->clip_lim1, env->clip_lim2);
…
typeset_graphical (bs, t, ip);
…
gr= as_grid (env->read (GR_EDIT_GRID));
gr->set_aspect (env->read (GR_EDIT_GRID_ASPECT));
box b= graphics_box (ip, bs, env->fr, gr, lim1, lim2);
…
print (b);
notify_graphics_extents (t, lim1, lim2);
END_MAGNIFY
} |
The omitted parts handle gr-auto-crop: when it is true, the limits are computed from the extents of the typeset children, enlarged by gr-crop-padding, instead of clip_lim1, clip_lim2. The macros BEGIN_MAGNIFY and END_MAGNIFY multiply magnification by the current magnify and reset magnify to 1; they surround all graphical primitives, which is how a magnify attribute on an object or on a group scales line widths, point sizes and text. notify_graphics_extents calls the Scheme hook graphics-notify-extents (defined in
dynamic/scripts-edit.scm
) for identified pictures, which is used by some plug-ins.
typeset_graphical typesets the children:
It first records the subtrees of all objects carrying a gid (set_graphical_values) and applies the constraints among the children (only is-equal is implemented; the other constraint tags print a message).
Every non-atomic, non-constraint child is then typeset by typeset_gr_item. For a with, this function evaluates the variables, returns an empty box when proviso is false, and otherwise typesets the body with the variables temporarily set. Other children are typeset with typeset_as_atomic.
The result of all this is a graphics_box whose first child is the grid_box of the visual grid.
Evaluates the coordinates, maps them through env->fr and produces a point_box with the current point-style, point-size, pen and fill brush.
Evaluate the points, build a curve with poly_segment, spline, arc or poly_bezier (
Graphics/Types/curve.cpp
), transform it with env->fr and produce a curve_box. Each curve is built with the array of inverse paths of its points (cip), which allows the curve box to report which source point corresponds to which control point. A one point curve is doubled, an arc through three collinear points falls back to a line, and a spline or Bezier curve with less than three points falls back to a polygon. Before the box is made, adjust_extremities cuts the curve where it enters the white_zones: rectangles around text-at boxes with a non-negative text-at-repulse, so that lines do not cross labels. Arrow heads are obtained by typesetting the macros named by arrow-begin and arrow-end (typeset_line_arrows).
Typeset the content (respectively as text, as math and inside a paragraph-box), position it with respect to the anchor according to text-at-halign and text-at-valign (or doc-at-valign), produce a text_at_box, and register a white zone if requested.
Used for penscript and calligraphy. The samples are first adapted to the possibly modified end points, then smoothed according to pen-enhance (refine and smoothen, or bezier_fit and rectify_bezier, in
Graphics/Types/curve_extras.cpp
). For calligraphy, the curve is replaced by the outline of an oval pen (oval_profile, calligraphy), which is filled.
Typesets the children with typeset_graphical into a graphics_group_box.
Wrap the typeset body in a transformed_box (the transformation is one of the tuples recognized by is_transformation: rotation, scaling, slanting or a linear map) or an effect_box.
Used for the experimental tags object-3d, triangle-3d, transform-3d and light-3d. The tree is converted into a spacial (
Graphics/Spacial/spacial.hpp
), transformed by the composition of the current frame and the 4\(\times\)4 matrix in gr-transformation, and drawn by a spacial_box. The keyboard shortcuts ⌃←, ⌃→, ⌃↑ and ⌃↓ rotate the view by modifying gr-transformation (graphics-rotate-xz, graphics-rotate-yz).
The tags spline* (typeset_var_spline) and fill (typeset_fill) are placeholders which only print a test_box.
The variable gid and the global table graphical_values implement a rudimentary constraint system. While typesetting a picture, every subtree (with "gid" id body) registers body under id, and (is-equal id1 id2) copies the value of id2 to id1. When edit_env_rep::as_point evaluates (with "gid" id (point …)) and the registered value differs, it calls graphics_require_update and still returns the old point. After the editor has committed an object, the Scheme function graphics-update-constraints (in
graphics/graphics-single.scm
) asks graphics-needs-update?, and rewrites the outdated points in the document using graphics-ref and graphics-notify-update. The corresponding glue functions are graphics-set, graphics-has?, graphics-ref, graphics-needs-update? and graphics-notify-update.
A composite box which stores the frame f, the edit grid g and the limits lim1, lim2. Its logical extents are the image of the limits; its ink extents are clipped to them, and pre_display installs a clipping rectangle, so that objects outside the picture are not drawn. It overrides get_frame, get_grid and get_limits, and its find_child prefers text-at children so that clicking on a label puts the cursor into the label.
The box of a gr-group. It is not accessible, so that the cursor cannot enter a group, and its graphical_select reports the whole group as a single selection of type group.
Draws a point. The polygonal styles (square, diamond, triangle, star, plus, cross) are produced by get_contour; any other style is drawn as a circle, filled for disk; the style none draws nothing.
Draws a rectified curve with a pencil, dash style and motif, an optional fill brush and arrow boxes. Its tree representation is the string curve and that of a point box is point; draw_graphical_object relies on this.
Draws a grid. The grid lines are computed lazily at the first display (and again when the pixel size changes). Grid boxes are never selected.
A move_box_rep which remembers the position of its anchor (hx, hy), its axis and its snapping padding.
The editor needs the frame of the picture which contains the cursor. Since boxes are positioned relative to their parent, the frame is obtained by walking down the box tree and composing the translations with the innermost frame found (
Typeset/Boxes/Basic/boxes.cpp
):
frame
box_rep::find_frame (path bp, bool last) {
SI x= 0;
SI y= 0;
box b= this;
frame f= get_frame ();
while (!is_nil (bp)) {
x += b->sx (bp->item);
y += b->sy (bp->item);
b = b->subbox (bp->item);
bp = bp->next;
frame g= b->get_frame ();
if (!is_nil (g)) {
if (last)
f= g;
else
f= scaling (1.0, point (x, y)) * g;
}
}
return f;
} |
With last= false the result maps graphical coordinates to absolute document coordinates (those of the mouse); with last= true it maps them to the local coordinates of the graphics box, which are the coordinates of the points returned by graphical_select. find_grid and find_limits similarly return the innermost grid and limits.
The virtual method
virtual gr_selections box_rep::graphical_select (SI x, SI y, SI dist); |
returns all the parts of a box within distance dist of (x, y), and the variant with a rectangle returns the parts inside the rectangle. A gr_selection (
Typeset/boxes.hpp
) has the fields
A string describing what was found (see the table below).
One or two paths in the source tree. For a control point, this is the path of the point subtree; for a point on a curve segment, the paths of the two control points which bound the segment.
The corresponding control points.
The point found and its distance to the mouse.
For selections lying on a curve, the curve itself; it is used to compute intersections when snapping.
| Type | Produced by | Meaning |
| point | point_box_rep | a point object |
| curve-handle | curve_box_rep | a control point of a curve |
| curve-point | curve_box_rep | a point on a curve segment (two paths in cp) |
| curve | curve_box_rep | a curve inside a rectangle |
| text | text_at_box_rep | the inside of a text box |
| text-handle | text_at_box_rep | the anchor point of a text box |
| text-border, text-border-point | text_at_box_rep | the border of a text box (enlarged by text-at-snapping) and special points on it |
| group | graphics_group_box_rep | a whole group |
| box | box_rep | any other box (default implementation) |
| grid-point, grid-curve-point | edit_graphics_rep::adjust | added by the editor when snapping to the edit grid |
A curve box first looks for control points within the distance and only returns curve points if none is found. The composite graphics_box_rep::graphical_select simply collects the selections of all children, from the last one to the first one. Finally as_tree (gr_selections) sorts the selections by distance and converts them into a tuple of tuples of paths, which is what Scheme receives.