Tkhtml 3.0 - HTML and CSS for Tcl/Tk
Total Page:16
File Type:pdf, Size:1020Kb
Untitled 10/12/06 9:23 AM Tkhtml 3.0 - HTML and CSS for Tcl/Tk Dan Kennedy, Tcl Conference 2006. Abstract Tkhtml is a Tk widget written in C that displays documents formatted according to the HTML and CSS standards. This paper reports on the recent work on Tkhtml 3.0 sponsored by Eolas Technologies. The motivations for creating a standards compliant HTML/CSS widget for Tcl/Tk are examined, expected uses of the widget identified and the scope of functionality provided defined. Also discussed is the web browser hv3, developed as part of the Tkhtml project to test the HTML widget. Future extensions and enhancements to Tkhtml are contemplated. Introduction To Tkhtml 3.0 Adding a widget to display documents formatted using the Hyper-Text Markup Language (HTML) [1] to Tk is not a new idea. There are currently at least three widely used widgets for just this purpose, Tkhtml 2 [2], htmllib [3] and the scrolledhtml widget included in the Iwidgets package [4]. The most common use is to embed hyper-linked documentation in an application ("help" documentation), although there exists at least one fully-functional web browser application built on top of Tkhtml 2, browsex [5]. Both htmllib and scrolledhtml are written in pure Tcl, making them particularly easy to deploy as part of Tcl applications. Tkhtml 2 is written in the C language, and has been used by applications written in Tcl, Ruby, Python and possibly others. Figure 1 - Role of Tkhtml All existing HTML renderers for Tcl/Tk have one thing in common: A significant subset (possibly even a majority) of documents found on todays Internet are not rendered as the author intended. This is not due to major technical flaws in existing packages, but simply because the web has moved on from pure HTML. In 2006, most web documents use a combination of HTML and Cascading Style Sheets (CSS) [6] to define the file://localhost/Users/mom/Desktop/tcl2006/Dan_Kennedy/tkhtml3_tcl2006/tkhtml3_tcl2006.html Page 1 of 16 Untitled 10/12/06 9:23 AM presentation of documents. The requirement to support CSS is the reason Tkhtml 3 was created instead of channeling effort into improving Tkhtml 2 or another existing package. In many ways, it now seems that "TkCSS" would have been a more descriptive name than "Tkhtml". Figure 1 roughly depicts the functionality provided by the Tkhtml 3 package. The application configures a Tkhtml widget by specifying: A single HTML document. Zero or more CSS documents. Zero or more images. Images are passed from the application to Tkhtml using Tk image handles. Zero or more instances of "replaced content". Replaced content is a concept defined by CSS to mean boxes of content generated by external means that are mapped into the document viewport. Examples of replaced content in an HTML document include form controls, the Tcl browser Plugin and embedded video frames. The widget displays a scrollable viewport that may be used to view the rendered document. Various introspection interfaces to query the document tree, CSS property values and document layout are also available. Introduction To HTML/CSS The role of HTML has changed slightly since it's inception. Last century, HTML was used to define both the content and presentation of documents for display. More recently, the task of specifying presentation has been abrogated to CSS and HTML constructs solely concerned with presentation have been deprecated by the World Wide Web Consortium (WC3) [7]. Content and structure are still specified using HTML. Of course, many documents do not conform to these guidelines and so any HTML widget is required to support both CSS and the older, deprecated HTML presentational attributes. As of version 2.1, CSS consists of 108 separate properties, around 90 of which are applicable to a visual display agent like Tkhtml (other properties concern aural or fixed-page document renderings). Based on rules specified in various CSS documents (including the default stylesheet, see below), each document element is assigned a value for each applicable property. Rules may specify that certain property values be assigned to elements based on their type, attributes or their position in the document tree relative to other elements, The values of the assigned properties determine the presentation of the document element. For example: The 'display' property determines whether the element generates a block box (similar to the default behavior of a <p> element), an inline box (similar to the default behavior of an <i> element), or even a table box (similar to the default behavior of a <table>). The 'float' property determines whether the element is added to the normal document flow or "floated" to the left or right of the document (similar to an <img> element with the "align" attribute set to "left" or "right"). Text wraps around floated boxes. The 'color' property determines the color of text contained by the element. Internally in Tkhtml, document layout is determined entirely by CSS properties. The section entitled "The Default Stylesheet" below clarifies the approach to handling legacy document that use deprecated HTML presentation attributes. Project Motivations and Goals file://localhost/Users/mom/Desktop/tcl2006/Dan_Kennedy/tkhtml3_tcl2006/tkhtml3_tcl2006.html Page 2 of 16 Untitled 10/12/06 9:23 AM Despite lack of recognition as such, Tcl/Tk remains one of the most portable and complete application platforms available today. Complex graphical applications move seamlessly between embedded devices and many different personal computer configurations. However, it is not currently possible to create a web browser application. The primary purpose of Tkhtml is to facilitate the development of web browser applications using Tcl/Tk. The above paragraph brazenly states that it is not possible to create a web browser application using Tcl/Tk, which is obviously not a true statement, if only because the introduction of this paper refers to a "fully-fledged web browser", browsex. A casual inspection of browsex reveals a stable cross-platform application, with similar functionality to all but the latest generation of popular web browsers [20,21] Performance is an order of magnitude better than that of popular browsers, both in terms of initialization time and resource consumption. Despite this, browsex has been all but abandoned (no updates since January 2003). The reason for this is simple: because it uses an older generation of HTML widget, a large number of web pages are rendered incorrectly. Tkhtml aims to address this by supporting modern versions of the relevant standards. Therefore: Although targeted at web browser applications, other uses are possible. It is anticipated that applications may use Tkhtml for the following broad categories of purpose. All categories of application are taken into consideration when design or API decisions are taken, although in practice it is fair to say that the other application pattern appear to demand a subset of the functionality of the web browser. Label widget: Using Tkhtml as a label widget capable of displaying rich text (formatted using HTML). Geometry manager: Tk application windows where an HTML widget is used in place of a geometry manager. Tkhtml widgets can manage other windows in similar manner to the Tk text and Figure 2 - Shameless gimmickry: the Acid 2 test [8] canvas widgets. Some application windows, for rendered by a (partially) standards compliant Tcl/Tk example those that resemble forms or "wizard" web browser [9]. dialogs, may be more easily created by specifying an HTML document than by using existing methods. Internal document viewer: Using Tkhtml to view application supplied documents formatted using HTML/CSS, either packaged by the application author (i.e. application help systems) or generated dynamically by the application itself (i.e. reports). Web browser: Using Tkhtml to create a modern web browser application or component. Interface Concepts and Design This section contains a high level conceptual description of the API provided by Tkhtml and in so doing communicates the scope and flexibility of the provided functionality. The goal of this section is not to provide a programming tutorial, but to clarify the role of Tkhtml in the Tcl/Tk software ecosystem. file://localhost/Users/mom/Desktop/tcl2006/Dan_Kennedy/tkhtml3_tcl2006/tkhtml3_tcl2006.html Page 3 of 16 Untitled 10/12/06 9:23 AM Along with the application patterns in the preceding sections, the API was designed with the following general principles in mind: Tkhtml should provide sufficient functionality and/or sufficiently flexible interfaces to facilitate development of a modern web browser application. Tkhtml should include only functionality defined by the relevant portions of the HTML or CSS standards. Functionality not defined by these standards is a matter of policy and therefore must be delegated to the application. Whenever standardized web browser functionality can be realistically delegated to the application level, this should be done. This is both to keep the widget as lightweight as possible for applications other than web browsers and for the pragmatic reason that project resources are scarce. Basics Using a Tkhtml widget is similar to using any other Tk widget. An instance must be created and handed off to a geometry manager for display. # Create a shrink-wrapped HTML "label" Usually, the widgets requested height and width are set html [html $pathName -shrink true] pack $html set by the -width or -height options in the same way as they are for built-in widgets. The exception # Create a document frame (default size 800x600) is if the boolean -shrink option is set to true. In this set html [html $pathName] case the widgets requested width and height are the pack $html -fill both -expand true size of the currently rendered document. The current widget width, or value of the -width option if the widget is unmapped, is used as the desired width when calculating the layout in this mode.