Divide BaseDoc into BaseDoc, TextDoc and DrawDoc. TextDoc and DrawDoc are abstract interfaces that must be implemented by the corresponding document generators.

svn: r8431
This commit is contained in:
Brian Matherly 2007-04-30 01:56:34 +00:00
parent 6544ab67ad
commit d7423fff15
16 changed files with 137 additions and 125 deletions

View File

@ -1,3 +1,23 @@
2007-04-29 Brian Matherly <brian@gramps-project.org>
* src/ReportBase/_Report.py:
* src/BaseDoc.py:
* src/docgen/HtmlDoc.py:
* src/docgen/ODFDoc.py:
* src/docgen/PdfDoc.py:
* src/docgen/AbiWord2Doc.py:
* src/docgen/GtkPrint.py:
* src/docgen/OpenOfficeDoc.py:
* src/docgen/LaTeXDoc.py:
* src/docgen/SvgDrawDoc.py:
* src/docgen/KwordDoc.py:
* src/docgen/AsciiDoc.py:
* src/docgen/RTFDoc.py:
* src/docgen/LPRDoc.py:
* src/docgen/PSDrawDoc.py:
Divide BaseDoc into BaseDoc, TextDoc and DrawDoc. TextDoc and DrawDoc are
abstract interfaces that must be implemented by the corresponding document
generators.
2007-04-28 Benny Malengier <bm@cage.ugent.be> 2007-04-28 Benny Malengier <bm@cage.ugent.be>
* src/images/.../Makefile.am: * src/images/.../Makefile.am:
* src/images/Makefile.am: * src/images/Makefile.am:

View File

@ -1265,7 +1265,7 @@ class IndexMark:
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class BaseDoc: class BaseDoc:
""" """
Base class for text document generators. Different output formats, Base class for document generators. Different output formats,
such as OpenOffice, AbiWord, and LaTeX are derived from this base such as OpenOffice, AbiWord, and LaTeX are derived from this base
class, providing a common interface to all document generators. class, providing a common interface to all document generators.
""" """
@ -1284,61 +1284,24 @@ class BaseDoc:
""" """
self.template = template self.template = template
self.paper = paper_style self.paper = paper_style
self.title = ""
self._style_sheet = styles self._style_sheet = styles
self.name = "" self._creator = ""
self.print_req = 0 self.print_req = 0
self.init_called = False self.init_called = False
def init(self): def init(self):
self.init_called = True self.init_called = True
def start_page(self):
pass
def end_page(self):
pass
def print_requested(self): def print_requested(self):
self.print_req = 1 self.print_req = 1
def set_creator(self, name):
"Sets the owner name"
self._creator = name
def add_media_object(self, name, align, w_cm, h_cm): def get_creator(self):
"""
Adds a photo of the specified width (in centimeters)
@param name: filename of the image to add
@param align: alignment of the image. Valid values are 'left',
'right', 'center', and 'single'
@param w_cm: width in centimeters
@param h_cm: height in centimeters
"""
pass
def get_usable_width(self):
"""
Returns the width of the text area in centimeters. The value is
the page width less the margins.
"""
return self.paper.get_size().get_width() - (self.paper.get_right_margin() + self.paper.get_left_margin())
def get_usable_height(self):
"""
Returns the height of the text area in centimeters. The value is
the page height less the margins.
"""
return self.paper.get_size().get_height() - (self.paper.get_top_margin() + self.paper.get_bottom_margin())
def creator(self, name):
"Returns the owner name" "Returns the owner name"
self.name = name return self._creator
def set_title(self, name):
"""
Sets the title of the document.
@param name: Title of the document
"""
self.title = name
def get_style_sheet(self): def get_style_sheet(self):
""" """
@ -1361,35 +1324,38 @@ class BaseDoc:
@param filename: path name of the file to create @param filename: path name of the file to create
""" """
pass raise NotImplementedError
def close(self): def close(self):
"Closes the document" "Closes the document"
pass raise NotImplementedError
def string_width(self, fontstyle, text):
"Determine the width need for text in given font"
return FontScale.string_width(fontstyle, text)
def line_break(self):
"Forces a line break within a paragraph"
pass
#------------------------------------------------------------------------
#
# TextDoc
#
#------------------------------------------------------------------------
class TextDoc:
"""
Abstract Interface for text document generators. Output formats for
text reports must implment this interface to be used by the report
system.
"""
def page_break(self): def page_break(self):
"Forces a page break, creating a new page" "Forces a page break, creating a new page"
pass raise NotImplementedError
def start_bold(self): def start_bold(self):
pass raise NotImplementedError
def end_bold(self): def end_bold(self):
pass raise NotImplementedError
def start_superscript(self): def start_superscript(self):
pass raise NotImplementedError
def end_superscript(self): def end_superscript(self):
pass raise NotImplementedError
def start_paragraph(self, style_name, leader=None): def start_paragraph(self, style_name, leader=None):
""" """
@ -1400,11 +1366,11 @@ class BaseDoc:
@param leader: Leading text for a paragraph. Typically used @param leader: Leading text for a paragraph. Typically used
for numbering. for numbering.
""" """
pass raise NotImplementedError
def end_paragraph(self): def end_paragraph(self):
"Ends the current parsgraph" "Ends the current parsgraph"
pass raise NotImplementedError
def start_table(self, name, style_name): def start_table(self, name, style_name):
""" """
@ -1413,19 +1379,19 @@ class BaseDoc:
@param name: Unique name of the table. @param name: Unique name of the table.
@param style_name: TableStyle to use for the new table @param style_name: TableStyle to use for the new table
""" """
pass raise NotImplementedError
def end_table(self): def end_table(self):
"Ends the current table" "Ends the current table"
pass raise NotImplementedError
def start_row(self): def start_row(self):
"Starts a new row on the current table" "Starts a new row on the current table"
pass raise NotImplementedError
def end_row(self): def end_row(self):
"Ends the current row on the current table" "Ends the current row on the current table"
pass raise NotImplementedError
def start_cell(self, style_name, span=1): def start_cell(self, style_name, span=1):
""" """
@ -1434,11 +1400,11 @@ class BaseDoc:
@param style_name: TableCellStyle to use for the cell @param style_name: TableCellStyle to use for the cell
@param span: number of columns to span @param span: number of columns to span
""" """
pass raise NotImplementedError
def end_cell(self): def end_cell(self):
"Ends the current table cell" "Ends the current table cell"
pass raise NotImplementedError
def write_note(self, text, format, style_name): def write_note(self, text, format, style_name):
""" """
@ -1449,7 +1415,7 @@ class BaseDoc:
@param format: format to use for writing. True for flowed text, @param format: format to use for writing. True for flowed text,
1 for preformatted text. 1 for preformatted text.
""" """
pass raise NotImplementedError
def write_text(self, text, mark=None): def write_text(self, text, mark=None):
""" """
@ -1459,23 +1425,71 @@ class BaseDoc:
@param text: text to write. @param text: text to write.
@param mark: IndexMark to use for indexing (if supported) @param mark: IndexMark to use for indexing (if supported)
""" """
pass raise NotImplementedError
def add_media_object(self, name, align, w_cm, h_cm):
"""
Adds a photo of the specified width (in centimeters)
@param name: filename of the image to add
@param align: alignment of the image. Valid values are 'left',
'right', 'center', and 'single'
@param w_cm: width in centimeters
@param h_cm: height in centimeters
"""
raise NotImplementedError
#------------------------------------------------------------------------
#
# DrawDoc
#
#------------------------------------------------------------------------
class DrawDoc:
"""
Abstract Interface for graphical document generators. Output formats
for graphical reports must implment this interface to be used by the
report system.
"""
def start_page(self):
raise NotImplementedError
def end_page(self):
raise NotImplementedError
def get_usable_width(self):
"""
Returns the width of the text area in centimeters. The value is
the page width less the margins.
"""
return self.paper.get_size().get_width() - (self.paper.get_right_margin() + self.paper.get_left_margin())
def get_usable_height(self):
"""
Returns the height of the text area in centimeters. The value is
the page height less the margins.
"""
return self.paper.get_size().get_height() - (self.paper.get_top_margin() + self.paper.get_bottom_margin())
def string_width(self, fontstyle, text):
"Determine the width need for text in given font"
return FontScale.string_width(fontstyle, text)
def draw_path(self, style, path): def draw_path(self, style, path):
pass raise NotImplementedError
def draw_box(self, style, text, x, y, w, h): def draw_box(self, style, text, x, y, w, h):
pass raise NotImplementedError
def draw_text(self, style, text, x1, y1): def draw_text(self, style, text, x1, y1):
pass raise NotImplementedError
def center_text(self, style, text, x1, y1): def center_text(self, style, text, x1, y1):
pass raise NotImplementedError
def rotate_text(self, style, text, x, y, angle): def rotate_text(self, style, text, x, y, angle):
pass raise NotImplementedError
def draw_line(self, style, x1, y1, x2, y2): def draw_line(self, style, x1, y1, x2, y2):
pass raise NotImplementedError

View File

@ -57,7 +57,7 @@ class Report:
self.doc = options_class.get_document() self.doc = options_class.get_document()
creator = database.get_researcher().get_name() creator = database.get_researcher().get_name()
self.doc.creator(creator) self.doc.set_creator(creator)
output = options_class.get_output() output = options_class.get_output()
if output: if output:

View File

@ -51,7 +51,7 @@ mime_type = ""
# Class Definitions # Class Definitions
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class AbiWordDoc(BaseDoc.BaseDoc): class AbiWordDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
"""AbiWord document generator. Inherits from the BaseDoc generic """AbiWord document generator. Inherits from the BaseDoc generic
document interface class.""" document interface class."""

View File

@ -121,7 +121,7 @@ def reformat_para(para='',left=0,right=72,just=LEFT,right_pad=0,first=0):
# Ascii # Ascii
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class AsciiDoc(BaseDoc.BaseDoc): class AsciiDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
#-------------------------------------------------------------------- #--------------------------------------------------------------------
# #
@ -169,28 +169,17 @@ class AsciiDoc(BaseDoc.BaseDoc):
def end_page(self): def end_page(self):
self.f.write('\012') self.f.write('\012')
#-------------------------------------------------------------------- def start_bold(self):
# pass
# Force a line break
# def end_bold(self):
#-------------------------------------------------------------------- pass
def line_break(self):
if self.in_cell:
self.cellpars[self.cellnum] = self.cellpars[self.cellnum] + '\n'
else:
self.f.write('\n')
def start_superscript(self): def start_superscript(self):
if self.in_cell: self.text = self.text + '['
self.cellpars[self.cellnum] = self.cellpars[self.cellnum] + '['
else:
self.f.write('[')
def end_superscript(self): def end_superscript(self):
if self.in_cell: self.text = self.text + ']'
self.cellpars[self.cellnum] = self.cellpars[self.cellnum] + ']'
else:
self.f.write(']')
#-------------------------------------------------------------------- #--------------------------------------------------------------------
# #
@ -347,7 +336,7 @@ class AsciiDoc(BaseDoc.BaseDoc):
if self.cell_lines[self.cellnum] > self.maxlines: if self.cell_lines[self.cellnum] > self.maxlines:
self.maxlines = self.cell_lines[self.cellnum] self.maxlines = self.cell_lines[self.cellnum]
def add_photo(self,name,pos,x,y): def add_media_object(self, name, align, w_cm, h_cm):
this_text = '(photo)' this_text = '(photo)'
if self.in_cell: if self.in_cell:
self.cellpars[self.cellnum] = self.cellpars[self.cellnum] + this_text self.cellpars[self.cellnum] = self.cellpars[self.cellnum] + this_text

View File

@ -348,7 +348,7 @@ class CairoJob(object):
# #
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class GtkDoc(BaseDoc.BaseDoc): class GtkDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def open(self,filename): def open(self,filename):
self._job = CairoJob() self._job = CairoJob()

View File

@ -95,7 +95,7 @@ _bottom = [
# HtmlDoc # HtmlDoc
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class HtmlDoc(BaseDoc.BaseDoc): class HtmlDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
def __init__(self,styles,type,template): def __init__(self,styles,type,template):
BaseDoc.BaseDoc.__init__(self,styles,None,template) BaseDoc.BaseDoc.__init__(self,styles,None,template)

View File

@ -57,7 +57,7 @@ def points(val):
# #
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class KwordDoc(BaseDoc.BaseDoc): class KwordDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
def open(self,filename): def open(self,filename):
self.media_list = [] self.media_list = []

View File

@ -492,7 +492,7 @@ class GnomePrintPhoto:
# LPRDoc class # LPRDoc class
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class LPRDoc(BaseDoc.BaseDoc): class LPRDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
"""Gnome-print document interface class. Derived from BaseDoc.""" """Gnome-print document interface class. Derived from BaseDoc."""
#------------------------------------------------------------------------ #------------------------------------------------------------------------
@ -579,17 +579,6 @@ class LPRDoc(BaseDoc.BaseDoc):
"Override generic Fontscale-based width." "Override generic Fontscale-based width."
return get_text_width(text,fontstyle) return get_text_width(text,fontstyle)
def line_break(self):
"Forces a line break within a paragraph."
# Add previously held text to the paragraph,
# then add line break directive,
# then start accumulating further text
append_to_paragraph(self.paragraph,self.paragraph_directive,
self.paragraph_text)
self.paragraph.add_piece(_LINE_BREAK,"")
self.paragraph_text = ""
self.brand_new_page = 0
def start_paragraph(self,style_name,leader=None): def start_paragraph(self,style_name,leader=None):
"""Paragraphs handling - A Gramps paragraph is any """Paragraphs handling - A Gramps paragraph is any
single body of text, from a single word, to several sentences. single body of text, from a single word, to several sentences.

View File

@ -128,7 +128,7 @@ class TexFont:
# LaTeXDon # LaTeXDon
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class LaTeXDoc(BaseDoc.BaseDoc): class LaTeXDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
"""LaTeX document interface class. Derived from BaseDoc""" """LaTeX document interface class. Derived from BaseDoc"""
def open(self,filename): def open(self,filename):

View File

@ -74,7 +74,7 @@ _esc_map = {
# ODFDoc # ODFDoc
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class ODFDoc(BaseDoc.BaseDoc): class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def __init__(self,styles,type,template): def __init__(self,styles,type,template):
BaseDoc.BaseDoc.__init__(self,styles,type,template) BaseDoc.BaseDoc.__init__(self,styles,type,template)
@ -945,13 +945,13 @@ class ODFDoc(BaseDoc.BaseDoc):
self.meta.write('<dc:description>') self.meta.write('<dc:description>')
self.meta.write('</dc:description>\n') self.meta.write('</dc:description>\n')
self.meta.write('<meta:initial-creator>') self.meta.write('<meta:initial-creator>')
self.meta.write(self.name) self.meta.write(self.get_creator())
self.meta.write('</meta:initial-creator>\n') self.meta.write('</meta:initial-creator>\n')
self.meta.write('<meta:creation-date>') self.meta.write('<meta:creation-date>')
self.meta.write(self.time) self.meta.write(self.time)
self.meta.write('</meta:creation-date>\n') self.meta.write('</meta:creation-date>\n')
self.meta.write('<dc:creator>') self.meta.write('<dc:creator>')
self.meta.write(self.name) self.meta.write(self.get_creator())
self.meta.write('</dc:creator>\n') self.meta.write('</dc:creator>\n')
self.meta.write('<dc:date>') self.meta.write('<dc:date>')
self.meta.write(self.time) self.meta.write(self.time)

View File

@ -69,7 +69,7 @@ _esc_map = {
# OpenOfficeDoc # OpenOfficeDoc
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class OpenOfficeDoc(BaseDoc.BaseDoc): class OpenOfficeDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def __init__(self,styles,type,template): def __init__(self,styles,type,template):
BaseDoc.BaseDoc.__init__(self,styles,type,template) BaseDoc.BaseDoc.__init__(self,styles,type,template)
@ -800,13 +800,13 @@ class OpenOfficeDoc(BaseDoc.BaseDoc):
self.meta.write(const.program_name + ' ' + const.version) self.meta.write(const.program_name + ' ' + const.version)
self.meta.write('</meta:generator>\n') self.meta.write('</meta:generator>\n')
self.meta.write('<meta:initial-creator>') self.meta.write('<meta:initial-creator>')
self.meta.write(self.name) self.meta.write(self.get_creator())
self.meta.write('</meta:initial-creator>\n') self.meta.write('</meta:initial-creator>\n')
self.meta.write('<meta:creation-date>') self.meta.write('<meta:creation-date>')
self.meta.write(self.time) self.meta.write(self.time)
self.meta.write('</meta:creation-date>\n') self.meta.write('</meta:creation-date>\n')
self.meta.write('<dc:creator>') self.meta.write('<dc:creator>')
self.meta.write(self.name) self.meta.write(self.get_creator())
self.meta.write('</dc:creator>\n') self.meta.write('</dc:creator>\n')
self.meta.write('<dc:date>') self.meta.write('<dc:date>')
self.meta.write(self.time) self.meta.write(self.time)

View File

@ -69,7 +69,7 @@ if print_label == None:
# PSDrawDoc # PSDrawDoc
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class PSDrawDoc(BaseDoc.BaseDoc): class PSDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
def __init__(self,styles,type,template): def __init__(self,styles,type,template):
BaseDoc.BaseDoc.__init__(self,styles,type,template) BaseDoc.BaseDoc.__init__(self,styles,type,template)

View File

@ -119,7 +119,7 @@ class GrampsDocTemplate(BaseDocTemplate):
# #
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class PdfDoc(BaseDoc.BaseDoc): class PdfDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def open(self,filename): def open(self,filename):
if filename[-4:] != ".pdf": if filename[-4:] != ".pdf":

View File

@ -60,7 +60,7 @@ def twips(cm):
# use style sheets. Instead it writes raw formatting. # use style sheets. Instead it writes raw formatting.
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class RTFDoc(BaseDoc.BaseDoc): class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
#-------------------------------------------------------------------- #--------------------------------------------------------------------
# #
@ -336,7 +336,7 @@ class RTFDoc(BaseDoc.BaseDoc):
self.f.write('\\clbrdrl\\brdrs\\brdrw10\n') self.f.write('\\clbrdrl\\brdrs\\brdrw10\n')
if s.get_right_border(): if s.get_right_border():
self.f.write('\\clbrdrr\\brdrs\\brdrw10\n') self.f.write('\\clbrdrr\\brdrs\\brdrw10\n')
table_width = float(self.get_usable_width()) table_width = float(self.paper.get_usable_width())
for cell in range(self.cell,self.cell+span): for cell in range(self.cell,self.cell+span):
self.cell_percent = self.cell_percent + float(self.tbl_style.get_column_width(cell)) self.cell_percent = self.cell_percent + float(self.tbl_style.get_column_width(cell))
cell_width = twips((table_width * self.cell_percent)/100.0) cell_width = twips((table_width * self.cell_percent)/100.0)

View File

@ -42,7 +42,7 @@ import Errors
# SvgDrawDoc # SvgDrawDoc
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class SvgDrawDoc(BaseDoc.BaseDoc): class SvgDrawDoc(BaseDoc.BaseDoc,BaseDoc.DrawDoc):
def __init__(self,styles,type,template): def __init__(self,styles,type,template):
BaseDoc.BaseDoc.__init__(self,styles,type,template) BaseDoc.BaseDoc.__init__(self,styles,type,template)