diff --git a/prefig/core/diagram.py b/prefig/core/diagram.py index 2782820..aa36106 100644 --- a/prefig/core/diagram.py +++ b/prefig/core/diagram.py @@ -28,6 +28,7 @@ def __init__(self, self.output = output self.suppress_caption = suppress_caption self.environment = environment + self.caption = "" label.init(self.format, self.environment) @@ -94,6 +95,9 @@ def add_legend(self, legend): def add_label(self, element, group): self.label_group_dict[element] = [group, copy.deepcopy(self.ctm())] + def set_caption(self, text): + self.caption = text + def caption_suppressed(self): return self.suppress_caption @@ -179,33 +183,47 @@ def begin_figure(self): margins = [margins] * 4 self.margins = margins + ctm = CTM.CTM() # tactile diagrams will be embossed on 11.5"x11" paper if self.format == 'tactile': - aspect_ratio = height / width - pagemargin = 1 * 72 - pagewidth = 11.5 * 72 - 2*pagemargin - pageheight = 11 * 72 - 2*pagemargin - if aspect_ratio * pagewidth <= pageheight: - width = pagewidth - height = aspect_ratio * pagewidth - margins = [pagemargin, (pageheight - height)/2 + pagemargin, - pagemargin, (pageheight - height)/2 + pagemargin] + total_width = width + margins[0] + margins[2] + total_height = height + margins[1] + margins[3] + diagram_aspect = total_width / total_height + page_aspect = 10.5 / 8.8 # area available for graphics + + if diagram_aspect >= page_aspect: + s = 756 / total_width + lly = s * total_height + 79.2 else: - height = pageheight - width = pageheight / aspect_ratio - margins = [(pagewidth - width)/2 + pagemargin, pagemargin, - (pagewidth - width)/2 + pagemargin, pagemargin] + s = 633.6 / total_height + lly = 712.8 + ctm.translate(36, lly) + ctm.scale(s, -s) + ctm.translate(margins[0], margins[1]) + self.root.set("width", "828") + self.root.set("height", "792") + + # bounding rectangle + ''' + rect = ET.SubElement(self.root, 'rect') + rect.set('x', '0') + rect.set('y', '0') + rect.set('width', '828') + rect.set('height', '792') + rect.set('stroke', 'black') + rect.set('fill', 'none') + ''' + else: + w = width + margins[0]+margins[2] + h = height + margins[1]+margins[3] + self.root.set("width", str(w)) + self.root.set("height", str(h)) - w = width + margins[0]+margins[2] - h = height + margins[1]+margins[3] - self.root.set("width", str(w)) - self.root.set("height", str(h)) + # initialize the CTM and push it onto the CTM stack + ctm.translate(0, height + margins[1] + margins[3]) + ctm.scale(1,-1) + ctm.translate(margins[0], margins[1]) - # initialize the CTM and push it onto the CTM stack - ctm = CTM.CTM() - ctm.translate(0, height + margins[1] + margins[3]) - ctm.scale(1,-1) - ctm.translate(margins[0], margins[1]) bbox = [0,0,width,height] un.enter_namespace('bbox', bbox) self.ctm_stack = [[ctm, bbox]] @@ -243,6 +261,27 @@ def place_labels(self): for legend in self.legends: legend.place_legend(self) + + if self.format == 'tactile': + # first we'll place the caption + if len(self.caption) == 0: + caption = label.nemeth_on + else: + caption = self.caption + ' ' + label.nemeth_on + text_element = ET.SubElement(self.root, 'text') + text_element.text = caption + text_element.set("x", "36") + text_element.set("y", "50.4") + text_element.set('font-family', "Braille29") + text_element.set('font-size', "29px") + + # add a nemeth off indicator at the bottom + text_element = ET.SubElement(self.root, 'text') + text_element.text = label.nemeth_off + text_element.set('x', '36') + text_element.set('y', '756') + text_element.set('font-family', "Braille29") + text_element.set('font-size', "29px") def end_figure(self): # form the output filenames diff --git a/prefig/core/label.py b/prefig/core/label.py index de5a6cb..b40ac81 100644 --- a/prefig/core/label.py +++ b/prefig/core/label.py @@ -354,6 +354,10 @@ def position_braille_label(element, diagram, ctm, text, typeform ) + if element.get('caption', 'no') == 'yes': + if len(braille_text) == 0: + braille_text = " " + braille_text += nemeth_on if braille_text is None: continue row_text += braille_text @@ -724,16 +728,10 @@ def mk_m_element(m_tag, label_group): # add a caption to a tactile diagram in the upper-left corner # e.g. "Figure 2.3.4" def caption(element, diagram, parent, outline_status): - if diagram.output_format() != "tactile": - return if diagram.caption_suppressed(): return - margins = diagram.get_margins() - element.tag = 'label' - element.set('alignment', 'ne') - element.set('offset', f"(0,{margins[3]+20})") - box = diagram.bbox() - element.set('p', util.pt2str((box[0], box[3]), spacer=",")) - label(element, diagram, parent) + text = element.text + if text is None or len(text) == 0: + return + diagram.set_caption(text.strip()) - diff --git a/prefig/core/label_tools.py b/prefig/core/label_tools.py index b179049..820408f 100644 --- a/prefig/core/label_tools.py +++ b/prefig/core/label_tools.py @@ -196,11 +196,13 @@ def initialized(self): def translate(self, text, typeform): if not self.louis_loaded: return None + if len(text) == 0: + return "" return louis.translateString( ["en-ueb-g2.ctb"], text, typeform=typeform - ) + ).rstrip() class PyodideBrailleTranslator(AbstractBrailleTranslator):