2D Structured Graphics on the Web: an Experiment with Amaya

Vincent Quint, W3C
Last revised $Date: 1998/06/12 18:26:42 $ by $Author: clilley $

Abstract

This note presents an experiment that has been carried out in early 1998 to design and implement a markup language for representing simple 2D vector graphics on the Web. The language presented here allows schematic drawings to be seamlessly integrated in HTML documents. It's an XML application and most style properties in graphics are handled by CSS.

The experiment consists in both a language called GraphML and its implementation in Amaya. Pages including graphics represented in GraphML can thus be rendered and authored in a comprehensive interactive environment, what allows an in-depth evaluation of the proposed technology.

Motivations

Needs for an interoperable graphics format

Web authors have currently two ways to include graphics in their Web pages. They can either use an image format or a vector graphics format. Image formats such as PNG, JPEG or GIF are well suited to photographs and paintings, but when it comes to vector graphics, they pose some problems:

The most popular image formats are well supported by browsers, but some others and most vector graphics formats (CGM for instance) are only availble through plug-ins, which also pose some problems:

For these reasons, it seems appropriate to look at other ways to embed structured graphics in Web documents. Graphics considered here are simple schematic 2D drawings for which there is a strong need on the Web. Standards for 3D virtual reality (VRML) and for complex technical drawings (CGM) already exist and are well recognized, but there is a lack in the domain of simple drawings that many authors use to illustrate or explain a point in a report. Most word processors offer tools for drawing such graphics, but there is no common format that provides for interoperability.

An experiment in Amaya

An experiment has been done in Amaya to explore the advantages of integrating seamlessly vector graphics in HTML documents and to design a new format for 2D graphics. For this experiment, MathML has been taken as an example, as the problems of integrating drawings and mathematical expressions in documents have much in common. Hence the name of this experimental format, GraphML, which is an XML application, like MathML.

The experiment is carried out in several phases:

Amaya has been chosen for this experiment, as it has been designed as a framework for experimenting with new Web technologies. Its kernel provides for some graphics primitives and an XML parser is available.

Other aspects: XML and CSS

XML is the language for representing structured data on the Web. It was an easy choice to select XML as the basis for GraphML. An experiment with GraphML was an opportunity to extend Amaya's capabilities in the XML domain. Amaya already supports MathML, and the GraphML experiment allowed us to consider the issue of integrating several XML objects (graphics and mathematical expressions) in the same document, as suggested in the Note about Name Spaces in XML.

CSS is a language for associating style with HTML pages. As graphics objects are intended to be embedded in HTML documents, it was interesting to try using CSS for expressing style in graphics. There is another style language under development at W3C, XSL, which is specifically designed for XML, but in early 1998, XSL is not mature enough.

A model for 2D graphics

The drawings considered here are constituted of a set of basic objects assembled in a structure. Basic objects are:

All these objects are 2-dimensional, but they may overlap. Some primitives represent closed objects (circles, rectangles, polygons, etc.) that can be filled with a color, an image or a pattern. Strokes have different aspects, regarding their color, thickness and style (solid or dashed). Links to any web resource should be available on all objects. Structure allows simple objects to be grouped to form more complex objects that are handled as a whole.

In a drawing, geometry can be considered from two points of view:

  1. Each object has a position and a size that are expressed by absolute Cartesian coordinates.
  2. Objects are related to each other by some geometric constraints which determine their relative position and size. Some basic objects have an intrinsic size (character strings, images) and the structure gives some implicit geometric constraints (nested objects are contained within each other). Explicit constraints associated with some objects allow to supersede default position and size derived from the structure.

The first approach is best suited to graphics created with easy-to-use editors: the user creates objects with a menu or a palette and, using the mouse, she specifies their position and size. The coordinates are then simply saved in the graphics format. Displaying a drawing represented that way is also an easy task. This simple, easy to implement approach must clearly be considered, but it does not correspond to the way many Web authors write documents. A lot of HTML files are produced with text editors and directly composed in the final format. Specifying the coordinates of all objects does not work in this context, where the constraint approach is more appropriate.

Constraints are interesting even when an authoring tool is used, as this approach provides for more flexibility in the resulting drawing. Font change, for instance, can be accommodated by reformatting automatically the whole drawing, taking into account the new character size. The author can also change globally distance between objects or relative size of objects with less pain.

As both approaches have advantages, both are used in GraphML. Experience will allow to decide if both must be kept for a final format or if a single approach is enough.

Whatever the approach, the issue of affine transformations should be considered, as it also allows for flexibility. An author may specify a drawing and then use it again in different contexts after some geometric transformations: translation, scaling or rotation. Though all affine transformations can play an important role in structured graphics, only the translation has been chosen in the initial experiment, for implementation reasons.

GraphML Structure

In defining the structure for GraphML, a lazy approach has been taken. Whenever a feature was already available in some other W3C specification, it has been excluded from GraphML and a provision has been made to allow GraphML to use or include this external feature.

Elements

The following graphic primitives have been included in GraphML:

In addition to these graphical primitives, some other objects are needed to implement the model presented above:

Objects Text and Label contain one or several HTML elements that would be valid children of a DIV element in an HTML document. A single character string is thus accepted, as well as any sequence of elements such as headings, paragraphs, preformatted, lists or tables. Mathematical expressions in MathML can also be included in these HTML fragments.

This use of HTML in elements Text and Label avoids to define additional elements in GraphML to represent images and hypertext links, as they are offered by HTML. In addition, images (possibly with maps) can be used in GraphML drawings.

As mathematical expressions can be included in HTML, such expressions can also be included in the Text and Label elements of a drawing.

Contents

There are several ways to consider the contents of elements presented above. For GraphML, only HTML and MathML elements are considered valid contents. Coordinates, background colors, fill patterns are considered as attributes or properties of a primitive rather than its contents. For example, in the following drawing:

A label in a rectangle

the yellow background is a style property of the rectangle, and its coordinates and size are attributes. Only the character string "A label in a rectangle" constitutes the content of element Label, which is itself the content of element Rectangle. In GraphML, this is written:

<rect position="0, 5" style="background-color: #FFFF00">
   <label>
       A label in a rectangle
   </label>
</rect>

Attributes

Attributes are used in GraphML to specify the geometric properties of objects, as well as their style. As far as possible, style information is represented in CSS, using the style attribute, whenever the corresponding property is available in CSS. Attributes are used only when CSS cannot make it.

Geometric properties

Geometric properties fall in two categories (see the two approaches presented above), constraints and absolute parameters.

Attributes representing absolute geometric parameters (size and position) are:

Attributes representing geometric onstraints are:

The position and size of each object can be specified either absolutely or by constraints. In case of conflict, only the absolute value is considered.

Some objects are not completely defined by their type, size and position: the shape of a polyline, a polygon, a spline and a closed spline are specified by the points attribute:

Lines are considered as a special case of polyline. Their shape is defined by a points attribute that defines the two end points of the line.

If no position is specified (either absolutely or by a constraint), a position "0, 0", is assumed and the points attribute defines the control points in an absolute way. If a position is specified (absolutely or by a constraint), the coordinates of control points are relative to that position.

Style properties

As the content of elements Label and Text is HTML, any CSS property can be used within these elements, with the style attribute. If some style sheets are embedded or linked to the document, they obviously apply to these HTML fragments.

CSS properties are also used for specifying the style of graphic objects, for instance their color or background.

For width and height, it seems more appropriate to use attributes rather than style properties, as these parameters specify the element itself, not its style.

Some style properties useful for GraphML are not available in CSS. They have been added to GraphML in the form of attributes, but they could be considered as possible extensions for a future version of CSS. These attributes are:

An example

The example below shows a drawing that uses only absolute position and sizes. As soon as Amaya handles constraints, another example will be provided.

A drawing showing most GraphML
features
<h1>An example</h1>
<p>
This is a GraphML drawing:</p>
<xmlgraphics>
  <rect position="5, 20" width="95" height="60"
        style="background-color:#FFC0C0; color: #0000FF">
    <label>
      A nice pink rectangle 
    </label>
  </rect>
  <roundrect position="110, 10" width="150" height="120"
             style="background-color: yellow; color: red">
    <label>
      A rectangle with
      <ul>
        <li><em>rounded</em> corners
        <li>a yellow background
        <li>red <strong>HTML</strong> text and border
      </ul>
    </label>
  </roundrect>
  <oval position="270, 5" width="100" height="80" style="color: blue">
    <label style="text-align: center">
      A transparent oval drawn in blue
      <math>
        <msqrt>
          <mrow>
            <msup>
              <mi>x</mi>
              <mn>2</mn>
            </msup>
            <mo>+</mo>
            <mn>1</mn>
          </mrow>
        </msqrt>
      </math>
    </label>
  </oval>
  <circle position="0, 120" width="120"/>
  <line points="100,80 60,180" arrowhead="end"/>
  <polyline arrowhead="start" points="160,180 170,150 220,150 210,180"/>
  <polygon points="230,200 240,150 320,150 310,200"
           style="background-color: #C0FFC0">
    <label>
      A green polygon 
    </label>
  </polygon>
  <spline points="160,250 170,220 220,220 210,250" arrowhead="both"
          style="color: red"/>
  <closedspline points="230,270 240,220 270,220 320,220 310,270"
                style="background-color: #C0C0FF">
    <label style="text-align: center">
      A blue closed curve 
    </label>
  </closedspline>
  <text position="340, 200" width="80" style="text-align: right">
    Some text with a <a href="#target">link</a>
    <span style="color: red">within</span> the Graphics
  </text>
  <math position="280, 90">
    <msqrt>
      <mfrac>
        <mn>1</mn>
        <mrow>
          <msup>
            <mi>x</mi>
            <mn>2</mn>
          </msup>
          <mo>+</mo>
          <mn>1</mn>
        </mrow>
      </mfrac>
    </msqrt>
  </math>
  <group position="380, 20">
    <text position="5, 45" width="100">
      This group contains two overlapping rectangles and this text 
    </text>
    <rect position="26, 11" width="50" height="30"
          style="background-color: black"/>
    <rect position="20, 5" width="50" height="30"
          style="background-color: red"/>
  </group>
</xmlgraphics>

Intermingled Structures

The above example shows three different kinds of structure: text in HTML, drawings in GraphML and mathematical expressions in MathML. Amaya allows text (HTML) to embed mathematical expressions (MathML) and drawings (GraphML). For that purpose, two tags have been added to HTML: <math> and <xmlgraphics>. The first one comes from the MathML specification.

Amaya also allows drawings (GraphML) to embed mathematics (MathML) and text (HTML). For mathematics, GraphML uses the same tag as HTML: <math>. For text, two tags are used, depending on the context: <label> for text surrounded by a graphic shape, and <text> in other cases (see above). Obviously, HTML text in a drawing can embed mathematics and graphics.

Amaya Parsers

In the above example, four different languages are used: HTML, GraphML, CSS and MathML. To support all these languages, Amaya uses three parsers:

  1. A HTML parser
  2. A CSS parser
  3. A XML parser

The HTML parser is very specific, as it tries to parse real life's HTML pages. Although HTML is formally an SGML application, Amaya does not use an SGML parser to process HTML files. It uses an ad hoc parser that accepts many errors and recovers by restructuring ill-structured documents.

Amaya's XML parser is used for both MathML and GraphML, as both are XML applications. It could also be used for other XML objects embedded in HTML documents, as it has been designed as a multi-DTD parser.

To allow structures to be nested, the HTML and XML parsers can call each other and the XML parser can call itself with different DTDs (for example, when a mathematical expression in embedded in a drawing).

GraphML DTD

(To be provided).