#!/usr/local/bin/pythonw
# coding: iso-8859-1
# Copyright © 2009 by Amos Newcombe
# 
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''classes derived from xmllib, specialized to SVG

To suppress the Doctype (as discussed in STUB where?), include 'isNoDoctype=True' in the constructor arguments
'''

from xmllib.xmllib import Document, Element, Namespace

class SVGNamespace(Namespace):
	
	def __init__(self, **d):
		Namespace.__init__(self, **d)
		self['uri'] = 'http://www.w3.org/2000/svg'

class SVGDocument(Document):
	
	def __init__(self, **d):
		# Incude a doctype if d['isNoDoctype'] is missing or false.
		Document.__init__(self, sStandalone = 'no', dDoctype = {
			'sElem'    : 'svg',
			'uriSystem': 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd',
			'idPublic' : '-//W3C//DTD SVG 1.1//EN',
		}, **d)

if __name__ == '__main__':
	
	sMsg = ''
	
	nsSVG = SVGNamespace(sPrefix='svg', suri='svg')

	svgHello = Element(sTag = 'svg', ltpAttr = [
		('width' ,  '4.5cm'),
		('height',  '1cm'),
		('version', '1.1'),
	], lContents = [
		Element(sTag='title', lContents = ['"Hello, World!" example for SVG']),
		Element(sTag='text' , lContents = ['Hello, SVG!'],
			ltpAttr = [('x', '1.25cm'), ('y', '0.625cm')], 
		),
		Element(sTag='rect' , ltpAttr = [
			('x'    ,  '.01cm'), ('y'     ,  '.01cm'),
			('width', '4.48cm'), ('height', '0.98cm'),
			('fill' , 'none'), ('stroke', 'blue'), ('stroke-width', '.01cm'),
		]),
	], nsApplied = nsSVG,
	)
	
# 	open('HelloWorld.svg', 'w').write(str(SVGDocument(
# 		xmlElem = svgHello,
# 		lnsNamespaces = [SVGNamespace(sPrefix='', isRoot=True)],
# 	))) # KLUGE: Uncommenting this block of text will screw up svgHello for the next part.
# 	
# 	print svgHello.OpenTag()
	
	from xmllib.Xhtml import XhtmlDocument
	
	xhtmlHello = XhtmlDocument(
		dDecl = {'sStandalone': 'no'},
		sDoctype = 'xhtmlmathsvg',
		lHead = [
			Element(sTag = 'title', lContents = ['"Hello, World!" example for SVG']),
		],
		lnsNamepaces = [nsSVG],
		lBody = [
			Element(sTag = 'h1'   , lContents = ['Hello, World!']),
			svgHello,
		],
	)
	xhtmlHello.PropagateNamespace()
	
	open('HelloWorld.svg.xhtml', 'w').write(str(xhtmlHello))
