Exemplo n.º 1
0
def extract_text_font(root, metadata_file):
    glyphcodes = get_text_names_to_codes(metadata_file)
    # (2) Output bounding box svg
    for glyph in glyphs:
        if glyph.attrib["glyph-name"] not in glyphcodes:
            continue
        # set glyph id
        g_element = ET.SubElement(root, "g")
        name = glyph.attrib["glyph-name"]
        glyph_code = glyphcodes[name]
        #glyph_code = name.split("uni")[-1]
        g_element.set("c", glyph_code)
        g_element.set("n", name)

        # set bounding box values if present
        if "d" in glyph.attrib:
            path = Path(glyph.attrib["d"])
            xmin, xmax, ymin, ymax = path.bbox()
            g_element.set("x", str(round(xmin, 2)))
            g_element.set("y", str(round(ymin, 2)))
            g_element.set("w", str(round(xmax - xmin, 2)))
            g_element.set("h", str(round(ymax - ymin, 2)))
        else:
            g_element.set("x", str(0.0))
            g_element.set("y", str(0.0))
            g_element.set("w", str(0.0))
            g_element.set("h", str(0.0))

        # set set horiz-av-x
        horiz_adv_x = glyph.attrib[
            "horiz-adv-x"] if "horiz-adv-x" in glyph.attrib else ""
        if horiz_adv_x:
            g_element.set("h-a-x", horiz_adv_x)

    return root
Exemplo n.º 2
0
 def boundingRect(self, k=None):
     if k is not None:
         return self[k].bbox()
     else:
         Ls = []
         for path in self.paths:
             if path:
                 Ls.extend(path._segments)
         P = SVGPath(*Ls)
         return P.bbox()
Exemplo n.º 3
0
def extract_smufl_font(root, metadata_file):
    glyphnames = get_supported_glyph_codes()
    metadata = get_json_content(metadata_file)
    glyph_anchors = metadata[
        "glyphsWithAnchors"] if "glyphsWithAnchors" in metadata else ""
    # extract alternate glyphs and append them if any
    alternate_glyphs = get_alternate_glyphs(glyphnames, metadata)
    if bool(alternate_glyphs):
        glyphnames.update(alternate_glyphs)

    # (1) Create xml file for each glyph
    write_xml_glyphs(glyphnames)

    # (2) Output bounding box svg
    for glyph in glyphs:
        # set glyph id
        glyph_code = glyph.attrib["glyph-name"][-4:]
        if glyph_code not in glyphnames:
            continue
        g_element = ET.SubElement(root, "g")
        g_element.set("c", glyph_code)

        # set bounding box values if present
        if "d" in glyph.attrib:
            path = Path(glyph.attrib["d"])
            xmin, xmax, ymin, ymax = path.bbox()
            g_element.set("x", str(round(xmin, 2)))
            g_element.set("y", str(round(ymin, 2)))
            g_element.set("w", str(round(xmax - xmin, 2)))
            g_element.set("h", str(round(ymax - ymin, 2)))
        else:
            g_element.set("x", str(0.0))
            g_element.set("y", str(0.0))
            g_element.set("w", str(0.0))
            g_element.set("h", str(0.0))

        # set set horiz-av-x
        if "horiz-adv-x" in glyph.attrib:
            g_element.set("h-a-x", glyph.get("horiz-adv-x"))
            if not float(g_element.get("w")):
                g_element.set("w", glyph.get("horiz-adv-x"))

        # add glyph anchors if present for current glyph
        current_glyphname = glyphnames[
            glyph_code] if glyph_code in glyphnames else ""
        if current_glyphname:
            g_element.set("n", current_glyphname)
            if current_glyphname in glyph_anchors:
                for key, value in glyph_anchors[current_glyphname].items():
                    a_element = ET.SubElement(g_element, "a")
                    a_element.set("n", key)
                    a_element.set("x", str(round(value[0], 2)))
                    a_element.set("y", str(round(value[1], 2)))

    return root