class TextItem(DocItem):
    """Prints some form of text"""
    def __init__(self, text = '', style = 'body'):
        DocItem.__init__(self)
        self.text = text
        self.style = style

    def writePDF(self, pdf = None):
        """Write itself to a FPDF object.
        
        Args:
            pdf (FPDF): the FPDF object to write to.
        """
        return self.getText()
    
    def resizePDF(self, pdf, x = 0, y = 0):
        """Resize internal Rect according to current settings of pdf"""
        width = pdf.get_string_width( self.getText() )
        height = pdf.font_size_pt / pdf.k
        self.rect = Rect( x, y, x + width, y + height )
    
    def cellPDF(self, pdf, r = None):
        if r:
            x_shift = r.x0()
            y_shift = r.y0()
        else:
            x_shift = 0.0
            y_shift = 0.0
        pdf.set_y( self.rect.y0() - y_shift )
        pdf.set_x( self.rect.x0() - x_shift )
        pdf.cell( self.rect.width(), self.rect.height(), self.getText() )
        
    def refit(self):
        """Doesn't need to do anything as cellPDF uses self.rect to output the content"""
        pass
class MathFrac(MultiItem):
    """Container for inline maths"""
    def __init__(self, num = None, denom = None):
        MultiItem.__init__(self)
        self.style = 'math-var', 1
        if num:
            if not denom:
                denom = MathNumber('1')
            self.appendItem(num)
            self.appendItem(denom)
        
    def resizePDF(self, pdf, x = 0, y = 0):
        if len(self.items) < 2 or not self.items[0] or not self.items[1]:
            raise Exception('MathFrac must have two items.')

        self.rect = Rect(x,y,x,y)
        dx = pdf.get_string_width(' ') * self.style[1]
        self.margins.set(dx, 0.0)
        setFontPDF(pdf, self.style, self.styles)
        lineHeight = pdf.font_size_pt / pdf.k
        
        numerator = self.items[0] 
        if hasattr(numerator,'style'):
            setFontPDF(pdf, numerator.style, self.styles)
        numerator.resizePDF(pdf,x + dx, y - lineHeight * 0.5)

        denominator = self.items[1] 
        if hasattr(denominator,'style'):
            setFontPDF(pdf, denominator.style, self.styles)
        denominator.resizePDF(pdf, x + dx, numerator.rect.y1())
        
        if numerator.rect.width() > denominator.rect.width():
            denominator.rect.alignXCenter(numerator.rect)
        else:
            numerator.rect.alignXCenter(denominator.rect)

        self.rect.unite(numerator.rect)
        self.rect.unite(denominator.rect)
        self.rect.adjust(rect.Point(0,0),rect.Point(dx,0))

    def cellPDF(self, pdf, r = None):
        MultiItem.cellPDF(self, pdf, r)
        y = self.items[0].rect.y1()
        pdf.set_line_width(0.2)
        if r:
            x_shift = r.x0()
            y_shift = r.y0()
        else:
            x_shift = 0.0
            y_shift = 0.0
        pdf.line(self.rect.x0() - x_shift, y - y_shift, self.rect.x1() - x_shift, y - y_shift)
Beispiel #3
0
def process_file(path, images):
    annotation = ET.Element("annotation")
    tree = ElementTree(annotation)
    filename = ET.SubElement(annotation, "filename")
    name = os.path.splitext(os.path.split(path)[1])[0]
    filename.text = name + ".jpeg"
    size = ET.SubElement(annotation, "size")
    width = ET.SubElement(size, "width")
    width.text = str(images[name].get("width"))
    height = ET.SubElement(size, "height")
    height.text = str(images[name].get("height"))
    depth = ET.SubElement(size, "depth")
    depth.text = str(3)
    segmented = ET.SubElement(annotation, "segmented")
    segmented.text = str(0)
    marmot_tree = ET.parse(path)
    marmot_root = marmot_tree.getroot()

    tables = marmot_root.findall("*/Composites/*[@Label='TableBody']")

    if not tables:
        return None

    for table in tables:
        obj = ET.SubElement(annotation, "object")
        obj_name = ET.SubElement(obj, "name")
        obj_name.text = "table"
        bndbox = ET.SubElement(obj, "bndbox")
        hexs = table.get("BBox").split(" ")
        bbox_array = [hex_to_double(x) for x in hexs]
        re = Rect(bbox_array[0], bbox_array[3], bbox_array[2], bbox_array[1])
        xmin = ET.SubElement(bndbox, "xmin")
        xmin.text = str(re.x0())
        ymin = ET.SubElement(bndbox, "ymin")
        fymin = images[name].get("height") - re.y1()
        ymin.text = str(fymin)
        xmax = ET.SubElement(bndbox, "xmax")
        xmax.text = str(re.x1())
        fymax = images[name].get("height") - re.y0()
        ymax = ET.SubElement(bndbox, "ymax")
        ymax.text = str(fymax)

    return tree