[docbook-css] Different image resources for HTML & FO output

David Holroyd dave at badgers-in-foil.co.uk
Sat, 22 Jan 2005 01:11:24 +0000


--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Tue, Oct 19, 2004 at 08:08:00PM +0000, David Holroyd wrote:
> ...
> 
> I was thinking about changing the XBL such that, rather than simply
> binding to all the <imageobject> elements, a binding is made to the
> containing <mediaobject>.  That binding would then, somehow, make a
> reasonable selection from the <imageobject> children.

I went ahead and implemented something along these lines.  The attached
patch causes the XBL to use the following image selection process for
<mediaobject> and <inlinemediaobject>:

1) Look through all the <imageobject> children for one with an
   acceptable 'role' attribute.  We will accept the values 'html', 'HTML',
   'xhtml' or 'XHTML'.

2) If step 1 doesn't select and image, look though all the <imagedata>
   descendants for on with an acceptable 'format' attribute.  We will
   accept the values 'PNG', 'GIF', 'GIF87a', 'GIF89a', 'JPG' or 'JPEG'.

3) If either step above has now selected an <imagedata> element with a
   non-empty 'fileref' attribute, use that attribute's value as the
   image path, otherwise, use the Gecko built-in broken image
   "resource://gre/res/broken-image.gif".


This should prevent multiple images from appearing, and also gives a
visual indication that 'some' image should be appearing, even if the
stylesheet considers none of the options suitable.


dave

-- 
http://david.holroyd.me.uk/

--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="improved_image_selection-patch1.diff"

Index: db-bindings.xml
===================================================================
RCS file: /var/lib/cvs/docbook-css/db-bindings.xml,v
retrieving revision 1.6
diff -u -r1.6 db-bindings.xml
--- db-bindings.xml	3 Jan 2005 01:30:22 -0000	1.6
+++ db-bindings.xml	22 Jan 2005 00:49:51 -0000
@@ -81,20 +81,58 @@
     </implementation>
   </binding>
 
-  <binding id="image">
+  <binding id="mediaobj" inheritstyle="false">
+    <!--
+      We must have <children/> here, because Moz will reject this binding if
+      any of the bound node's children aren't matched.
+      Because of this, the stylesheet hides most of [inline]mediaobject's
+      children with display:none;
+    -->
     <content>
-      <html:img xbl:inherits="src=fileref"/>
+      <html:img/>
+      <children/>
     </content>
 
     <implementation>
       <constructor>
 	var img = document.getAnonymousNodes(this)[0];
-	var file = this.getAttribute("fileref");
+	var file = this.selectImage();
 	// HACK: using img.src=file 'inline' doesn't seem to work
 	//       but it does when called from a setTimeout()
 	var f = function() { img.src = file; }
 	setTimeout(f, 0);
       </constructor>
+
+      <method name="selectImage">
+	<body><![CDATA[
+	  var src = this.selectImageByRole();
+	  if (src == null) {
+	    src = this.selectImageByFormat()
+	  }
+	  if (src == null) {
+	    src = "resource://gre/res/broken-image.gif";
+	  }
+	  return src;
+	]]></body>
+      </method>
+      <method name="selectImageByRole">
+	<body><![CDATA[
+	  var fileref = document.evaluate("imageobject[@role='html' or @role='HTML' or @role='xhtml' or @role='XHTML']/imagedata/@fileref", this, null, XPathResult.STRING_TYPE, null).stringValue;
+	  if (fileref != "") {
+	    return fileref;
+	  }
+	  return null;
+	]]></body>
+      </method>
+      <method name="selectImageByFormat">
+	<body><![CDATA[
+	  var fileref = document.evaluate("imageobject/imagedata[@format='PNG' or @format='GIF' or @format='GIF87a' or @format='GIF89a' or @format='JPG' or @format='JPEG']/@fileref", this, null, XPathResult.STRING_TYPE, null).stringValue;
+	  if (fileref != "") {
+	    return fileref;
+	  }
+	  return null;
+	]]></body>
+      </method>
     </implementation>
   </binding>
 </bindings>
Index: mozilla.css
===================================================================
RCS file: /var/lib/cvs/docbook-css/mozilla.css,v
retrieving revision 1.7
diff -u -r1.7 mozilla.css
--- mozilla.css	3 Jan 2005 01:30:22 -0000	1.7
+++ mozilla.css	22 Jan 2005 00:49:51 -0000
@@ -34,10 +34,9 @@
 	-moz-outline: 1px dotted invert;
 }
 
-imagedata {
-	-moz-binding:url('db-bindings.xml#image');
+mediaobject, inlinemediaobject {
+	-moz-binding:url('db-bindings.xml#mediaobj');
 }
-
 
 guimenu, guimenuitem, guisubmenu {
 	font: menu;
Index: styles.css
===================================================================
RCS file: /var/lib/cvs/docbook-css/styles.css,v
retrieving revision 1.15
diff -u -r1.15 styles.css
--- styles.css	22 Jan 2005 00:43:59 -0000	1.15
+++ styles.css	22 Jan 2005 00:49:51 -0000
@@ -657,6 +657,16 @@
 	text-align: center;
 }
 
-mediaobject>textobject {
-	font-size: smaller;
+/* hide content; we will use XML in mozilla to display it */
+mediaobject>*, inlinemediaobject>* {
+	display: none;
+}
+mediaobject>caption {
+	display: block;
+}
+mediaobject {
+	text-align: center;
+}
+caption>para {
+	text-align: inherit;  /* to override 'justify' and inherit 'centre' */
 }

--BXVAT5kNtrzKuDFl--