[cairo-commit] svgslides/src svgslides-4suite,NONE,1.1

Carl Worth commit at pdx.freedesktop.org
Sat Feb 12 10:21:20 PST 2005


Committed by: cworth

Update of /cvs/cairo/svgslides/src
In directory gabe:/tmp/cvs-serv29629/src

Added Files:
	svgslides-4suite 
Log Message:

        * src/svgslides-4suite: New implementation of svgslides in python
        rather than XSLT. It's nearly as far along as svgslides.xsl, but
        isn't ready to be the default implementation yet.


--- NEW FILE: svgslides-4suite ---
#! /usr/bin/python

# XXX: This should be a command-line argument
basename='test'

SS  = u'http://www.svgslides.org/svgslides0.1'
SSI = u'http://www.svgslides.org/svgslides-index0.1'
SVG = u'http://www.w3.org/2000/svg'

from Ft.Xml.XPath.Context import Context
from Ft.Xml import XPath
from Ft.Xml import Domlette as Dom
from Ft.Lib import Uri

NSS = {
	u'ss': SS,
	u'ssi': SSI,
	u'svg': SVG
}

def slide_file_name (slide_num):
    return basename+'-'+str(slide_num+1).zfill(3)+'.svg'

def do_xpath (context_node, expr):
    ctx = Context (context_node, processorNss = NSS)
    return XPath.Evaluate (expr, ctx)

###
### Main program
###

file_uri = Uri.OsPathToUri ('test.xml', attemptAbsolute=1)
slides_doc = Dom.NonvalidatingReader.parseUri (file_uri) 

slides = do_xpath (slides_doc, u'//ss:slides/ss:slide')

index_doc = Dom.implementation.createDocument (SSI, None, None)
index = index_doc.createElementNS (SSI, 'svgslides-index')
index_doc.appendChild (index)

# Generate the index file

slide_num = 0
for slide in slides:
    si = index_doc.createElementNS (SSI, 'slide')
    si.setAttributeNS (None, 'title', slide.getAttributeNS (None, 'title'))
    si.setAttributeNS (None, 'filename', slide_file_name (slide_num))
    index.appendChild (si)
    slide_num += 1

file = open (basename + '-index.xml', 'w')
Dom.PrettyPrint (index_doc, file)
file.close ()

def slide_generate_svg_from_template (slide, theme, variant = None):

    def substitute_variable (variable):
	variable_name = variable.getAttributeNS (SS, 'variable')
	value = slide.getAttributeNS (None, u''+variable_name)
	variable.replaceChild (doc.createTextNode (value), variable.firstChild)

    def substitute_region (region):

	# XXX: Need to finish the layout functions
	def layout_ul (ul):
	    pass

	def layout_li (li):
	    pass

	def layout_content_node (node):
	    if node.nodeName == u'ul':
		layout_ul (node)
	    elif node.nodeName == u'li':
		layout_li (node)

	def layout_content_nodes (nodes):
	    for node in nodes:
		layout_content_node (node)

	def transform_ul (ul, root):
	    group = doc.createElementNS (SVG, 'g')
	    root.appendChild (group)
	    return group

	def transform_li (li, root):
	    values = do_xpath (li, u'text()')
	    text = doc.createElementNS (SVG, 'text')
	    root.appendChild (text)
	    for v in values:
		text.appendChild (doc.createTextNode (v.nodeValue))
	    # XXX: Need to get x/y from the layout stuff
	    text.setAttributeNS (None, u'x', u'0')
	    text.setAttributeNS (None, u'y', u'0')
	    return root

	def transform_content_node (node, root):
	    if node.nodeName == u'ul':
		return transform_ul (node, root)
	    if node.nodeName == u'li':
		return transform_li (node, root)
	    return root

	def transform_content_nodes (nodes, root):
	    for node in nodes:
		child = transform_content_node (node, root)
		transform_content_nodes (node.childNodes, child)

	region_name = region.getAttributeNS (SS, 'region')

	# Copy text style up from a mandatory text element
	text_sample = do_xpath (region, u'svg:text[1]')[0]
	region.setAttributeNS (None, 'font-family', text_sample.getAttributeNS (None, 'font-family'))
	region.setAttributeNS (None, 'font-size', text_sample.getAttributeNS (None, 'font-size'))
	region.setAttributeNS (None, 'fill', text_sample.getAttributeNS (None, 'fill'))

	# Get bounds information from a mandatory rect within the region
	rect = do_xpath (region, u'svg:rect[1]')[0]
	region_x = rect.getAttributeNS (None, 'x')
	region_y = rect.getAttributeNS (None, 'y')
	region_width = rect.getAttributeNS (None, 'width')
	region_height = rect.getAttributeNS (None, 'height')

	# Remove sample content from region
	while len(region.childNodes):
	    region.removeChild (region.firstChild)

	# Add a new group with a transform based on the rect
	g = doc.createElementNS (SVG, 'g')
	region.appendChild (g)
	g.setAttributeNS (None, 'transform', 'translate('+region_x+','+region_y+')')

	if (region_name == 'default'):
	    content = do_xpath (slide, u'*[local-name() != "region"]')
	else:
	    content = do_xpath (slide, u'ss:region[@name = '+region_name+']/*')

	layout_content_nodes (content)
	transform_content_nodes (content, g)

    if variant:
	theme_filename = theme + '-' + variant + '.svg'
    else:
	theme_filename = theme + '.svg'
    theme_uri = Uri.OsPathToUri (theme_filename, attemptAbsolute=1)
    doc = Dom.NonvalidatingReader.parseUri (theme_uri)
    
    variables = do_xpath (doc, u'//svg:text[@ss:variable]')
    for variable in variables:
	substitute_variable (variable)

    regions = do_xpath (doc, u'//svg:g[@ss:region]')
    for region in regions:
	substitute_region (region)
	
    return doc

# Generate per-slide SVG output

# Loop through each slide element, find it's theme and variant, then generate SVG
slide_num = 0
for slide in slides:
    file = open (slide_file_name (slide_num), 'w')
    theme = do_xpath (slide, u'ancestor-or-self::*[@theme][1]/@theme')
    variant = do_xpath (slide, u'ancestor-or-self::*[@variant][1]/@variant')
    if len(variant):
	svg = slide_generate_svg_from_template (slide, theme[0].nodeValue, variant[0].nodeValue)
    else:
	svg = slide_generate_svg_from_template (slide, theme[0].nodeValue)
    Dom.PrettyPrint (svg, file)
    file.close ()
    slide_num += 1




More information about the cairo-commit mailing list