Esempio n. 1
0
def drawBagel(text: str,
              text2: str,
              fontSize: int = 13,
              textColor: str = '#FFF',
              leftColor: str = '#555',
              rightColor: str = '#08C',
              spacing: int = 10,
              height: int = 20,
              fontName: str = None) -> str:
    spacing = max(spacing, 3)
    height = max(20, height)
    fontSize = max(round(height * 0.5), max(round(height * 0.75), fontSize))
    s = settings()
    s.ReadSettings()
    if fontName is None:
        fontName = s.defaultSvgFont
        fontFamily = 'sans-serif'
    else:
        fontFamily = fontName
    t1w = textwidth(text, fontSize, fontName)
    t2w = textwidth(text2, fontSize, fontName)
    zw = 4 * spacing + t1w + t2w
    d = Drawing(zw, height)
    if fontForgeSupported and 'sans-serif' != fontFamily:
        css = generateFontCSS(fontName, text + text2, genFlags=ASCII_SUPPORT)
        if css is not None:
            d.append(Style(css))
    m = Mask(id="m")
    d.append(m)
    rx = round(height * 0.15)
    m.append(Rectangle(0, 0, zw, height, fill='#FFF', rx=rx, ry=rx))
    g1 = Group(mask="url(#m)")
    g1.append(Rectangle(0, 0, 2 * spacing + t1w, height, fill=leftColor))
    g1.append(Rectangle(2 * spacing + t1w, 0, zw, height, fill=rightColor))
    d.append(g1)
    g2 = Group(aria_hidden="true",
               fill=textColor,
               text_anchor='start',
               font_family=fontFamily)
    g2.append(Text(text, fontSize, spacing, height - fontSize, textLength=t1w))
    g2.append(
        Text(text2,
             fontSize,
             3 * spacing + t1w,
             height - fontSize,
             textLength=t2w))
    d.append(g2)
    return d.asSvg().replace('\n', '')
Esempio n. 2
0
    def assert_equal(self, svg_actual: Drawing, svg_expected: Drawing,
                     name: str):
        png_actual = drawing_to_image(svg_actual)
        png_expected = drawing_to_image(svg_expected)
        w = max(png_actual.width, png_expected.width)
        h = max(png_actual.height, png_expected.height)

        png_actual_padded = Image.new(png_actual.mode, (w, h))
        png_expected_padded = Image.new(png_expected.mode, (w, h))
        png_actual_padded.paste(png_actual)
        png_expected_padded.paste(png_expected)
        png_diff = Image.new(png_actual.mode, (w, h))
        self.mismatch_found = False
        png_diff.putdata([
            self.diff_pixel(actual_pixel, expected_pixel)
            for actual_pixel, expected_pixel in zip(
                png_actual_padded.getdata(), png_expected_padded.getdata())
        ])

        # Display image when in live turtle mode.
        display_image = getattr(Turtle, 'display_image', None)
        if display_image is not None:
            t = Turtle()
            try:
                # noinspection PyUnresolvedReferences
                w = t.screen.cv.cget('width')
                # noinspection PyUnresolvedReferences
                h = t.screen.cv.cget('height')
                ox, oy = w / 2, h / 2
                text_height = 20
                t.penup()
                t.goto(-ox, oy)
                t.right(90)
                t.forward(text_height)
                t.write(f'Actual')
                display_image(ox + t.xcor(),
                              oy - t.ycor(),
                              image=encode_image(png_actual))
                t.forward(png_actual.height)
                t.forward(text_height)
                t.write(f'Diff')
                display_image(ox + t.xcor(),
                              oy - t.ycor(),
                              image=encode_image(png_diff))
                t.forward(png_diff.height)
                t.forward(text_height)
                t.write('Expected')
                display_image(ox + t.xcor(),
                              oy - t.ycor(),
                              image=encode_image(png_expected))
                t.forward(png_expected.height)
            except Exception as ex:
                t.write(str(ex))

        if not self.mismatch_found:
            return
        text_actual = svg_actual.asSvg()
        (self.work_dir / (name + '_actual.svg')).write_text(text_actual)
        text_expected = svg_expected.asSvg()
        (self.work_dir / (name + '_expected.svg')).write_text(text_expected)
        with (self.work_dir / (name + '_diff.png')) as f:
            png_diff.save(f)
        assert text_actual == text_expected