Exemple #1
0
 def test_eq_none_none_2(self):
     a = SVGLength('1')
     b = SVGLength('1.0000000001')  # -> 1
     self.assertNotEqual(a.value(), b.value())
     self.assertAlmostEqual(a.value(a.unit), b.value(a.unit))
     self.assertEqual(a.tostring(), b.tostring(a.unit))  # '1' == '1'
     self.assertTrue(a == b)  # 1 == 1
Exemple #2
0
    def test_imul(self):
        a = SVGLength('1cm')
        a *= 2
        self.assertEqual('2cm', a.tostring())

        a *= SVGLength('11mm')
        self.assertEqual('2.2cm', a.tostring())
Exemple #3
0
    def test_negative_zero(self):
        a = SVGLength(-0.0)  # -0.0 -> 0
        self.assertEqual('-0.0', str(a.value()))
        self.assertEqual('0', a.tostring())

        a = SVGLength('-0.0001')
        self.assertEqual('-0.0001', str(a.value()))
        self.assertEqual('0', a.tostring())
Exemple #4
0
 def test_eq_cm_mm(self):
     # 1(cm) == 10(mm)
     a = SVGLength('1cm')  # 37.79527559055118
     b = SVGLength('10mm')  # 37.795275590551185
     self.assertNotEqual(a.value(), b.value())
     self.assertAlmostEqual(a.value(), b.value())
     self.assertAlmostEqual(a.value(a.unit), b.value(a.unit))
     self.assertEqual(a.tostring(), b.tostring(a.unit))
     self.assertTrue(a == b)
Exemple #5
0
    def test_precision(self):
        a = SVGLength('1cm')  # 37.79527559055118
        self.assertEqual('37.795', a.tostring(SVGLength.TYPE_NUMBER))

        formatter.precision = 4
        self.assertEqual('37.7953', a.tostring(SVGLength.TYPE_NUMBER))

        formatter.precision = 2
        # '37.80' -> '37.8'
        self.assertEqual('37.8', a.tostring(SVGLength.TYPE_NUMBER))
Exemple #6
0
 def test_add_cm_in(self):
     # 10.2(cm) + 1.8(in)
     a = SVGLength('10.2cm')  # 10.2 * 96 / 2.54 (px) = 385.511811023622(px)
     a = a + SVGLength(
         '1.8in')  # 1.8in = 1.8 * 2.54cm = 4.572cm = 172.8(px)
     # 10.2(cm) + 2.54(cm) = 14.771999999999998(cm)
     # 385.511811(px) + 172.8(px) = 558.311811(px)
     self.assertEqual('14.772cm', a.tostring())
     self.assertEqual('558.312px', a.tostring(SVGLength.TYPE_PX))
     self.assertEqual('cm', a.unit)
     self.assertEqual(SVGLength('558.311811px'), a)
Exemple #7
0
    def test_iadd(self):
        a = SVGLength('0.1mm')
        a += SVGLength('0.1mm')
        a += SVGLength('0.1mm')  # 0.30000000000000004mm
        self.assertEqual('0.3mm', a.tostring())
        self.assertTrue(a == SVGLength('0.3mm'), msg=a.value(a.unit))

        a = SVGLength('10.2cm')  # 10.2 * 96 / 2.54 (px) = 385.511811...px
        a += SVGLength('1.8in')  # 1.8in = 1.8 * 2.54cm = 4.572cm = 172.8px
        self.assertEqual('14.772cm', a.tostring())  # 10.2 + 4.572 (cm)
        self.assertTrue(SVGLength('558.311811px') == a)  # 385.511811 + 172.8
Exemple #8
0
    def test_sub(self):
        a = SVGLength('0.1mm')
        a = a - SVGLength('0.1mm')
        a = a - SVGLength('0.1mm')  # -0.1mm
        self.assertEqual('-0.1mm', a.tostring())
        self.assertTrue(SVGLength('-0.1mm') == a, msg=a.value(a.unit))

        a = SVGLength('10.2cm')  # 10.2 * 96 / 2.54 (px) = 385.511811...px
        a = a - SVGLength('1.8in')  # 1.8in = 1.8 * 2.54cm = 4.572cm = 172.8px
        self.assertEqual('5.628cm', a.tostring())  # 10.2 - 4.572 (cm)
        self.assertTrue(SVGLength('212.711811px') == a)  # 385.511811 - 172.8
Exemple #9
0
 def test_add_mm_mm(self):
     # 0.1(mm) + 0.1(mm) + 0.1(mm)
     a = SVGLength('0.1mm')
     a = a + SVGLength('0.1mm')
     a = a + SVGLength('0.1mm')
     self.assertEqual('0.3mm', a.tostring())
     self.assertEqual('mm', a.unit)
     self.assertEqual(0.3, a.value(SVGLength.TYPE_MM))
Exemple #10
0
    def test_new_value_percentage2(self):
        parser = SVGParser()
        root = parser.create_element('svg')
        group = root.create_sub_element('g')
        group.attributes.update({
            'font-size': '16',
        })

        a = SVGLength(context=group)
        a.new_value(92.00042345, SVGLength.TYPE_PERCENTAGE)
        self.assertEqual(92.00042345, a.value(SVGLength.TYPE_PERCENTAGE))
        self.assertEqual('%', a.unit)
        self.assertEqual('92%', a.tostring())
Exemple #11
0
    def test_eq_none_none(self):
        # 1 == 1.000000001?
        SVGLength.rel_tol = 1e-9
        a = SVGLength('1')
        b = SVGLength('1.000000001')
        self.assertNotEqual(a.value(), b.value())
        self.assertAlmostEqual(a.value(a.unit), b.value(a.unit))
        self.assertEqual(a.tostring(), b.tostring(a.unit))  # '1' == '1'
        self.assertFalse(a == b)  # 1 != 1.000000001

        SVGLength.rel_tol = 1e-8
        self.assertTrue(a == b)  # 1 == 1.00000000

        SVGLength.rel_tol = 1e-9
        SVGLength.abs_tol = 1e-9
        a = SVGLength('0')
        b = SVGLength('-0.000000001')
        self.assertNotEqual(a.value(), b.value())
        self.assertAlmostEqual(a.value(a.unit), b.value(a.unit))
        self.assertEqual(a.tostring(), b.tostring(a.unit))  # '1' == '1'
        self.assertTrue(a == b)

        SVGLength.abs_tol = 1e-10
        self.assertFalse(a == b)
Exemple #12
0
    def test_ne(self):
        a = SVGLength('1cm')  # 37.79527559055118
        b = SVGLength('10mm')  # 37.795275590551185
        self.assertNotEqual(a.value(), b.value())
        self.assertEqual(a.tostring(), b.tostring(a.unit))
        self.assertFalse(a != b)

        a = SVGLength('1')
        b = SVGLength('1.000000001')
        self.assertNotEqual(a.value(), b.value())
        self.assertEqual(a.tostring(), b.tostring(a.unit))  # '1' == '1'
        self.assertTrue(a != b)

        a = SVGLength('1')
        b = SVGLength('1.0000000001')  # -> 1
        self.assertNotEqual(a.value(), b.value())
        self.assertEqual(a.tostring(), b.tostring(a.unit))  # '1' == '1'
        self.assertFalse(a != b)
Exemple #13
0
    def test_convert03(self):
        # without context
        Font.default_font_size = 20

        a = SVGLength('1em')
        self.assertEqual('em', a.unit)
        self.assertEqual('1em', a.tostring())

        # 1(em) = 2(ex)
        a.convert(SVGLength.TYPE_EXS)
        self.assertEqual('ex', a.unit)
        self.assertEqual('2ex', a.tostring())

        # 1(em) = default font size(px)
        a.convert(SVGLength.TYPE_PX)
        self.assertEqual('px', a.unit)
        self.assertEqual('20px', a.tostring())

        # 1(rem) = default font size(px)
        a.convert(SVGLength.TYPE_REMS)
        self.assertEqual('rem', a.unit)
        self.assertEqual('1rem', a.tostring())

        # 1(ch) = default font size / 2(px)
        a.convert(SVGLength.TYPE_CHS)
        self.assertEqual('ch', a.unit)
        self.assertEqual('2ch', a.tostring())

        # 100(%) = default font size(px)
        a.convert(SVGLength.TYPE_PERCENTAGE)
        self.assertEqual('%', a.unit)
        self.assertEqual('100%', a.tostring())

        # 1(ex) = 0.5(em)
        a = SVGLength('1ex')
        a.convert(SVGLength.TYPE_EMS)
        self.assertEqual('em', a.unit)
        self.assertEqual('0.5em', a.tostring())
Exemple #14
0
 def test_new_value_percentage(self):
     a = SVGLength()
     a.new_value(92.00042345, SVGLength.TYPE_PERCENTAGE)
     self.assertEqual(92.00042345, a.value(SVGLength.TYPE_PERCENTAGE))
     self.assertEqual('%', a.unit)
     self.assertEqual('92%', a.tostring())
Exemple #15
0
 def test_new_value_number(self):
     a = SVGLength()
     a.new_value(1.0001, SVGLength.TYPE_NUMBER)
     self.assertEqual(1.0001, a.value())
     self.assertIsNone(a.unit)
     self.assertEqual('1', a.tostring())
Exemple #16
0
 def test_init(self):
     a = SVGLength()
     self.assertEqual(0, a.value())
     self.assertIsNone(a.unit)
     self.assertEqual('0', a.tostring())
Exemple #17
0
    def test_convert01(self):
        # with context
        parser = SVGParser()
        root = parser.create_element('svg')
        group = root.create_sub_element('g')
        group.attributes.update({
            'font-family': 'DejaVu Sans, sans-serif',
            'font-size': '16',
        })
        # 1(rem) = 16(px)
        text = group.create_sub_element('text')

        a = SVGLength('1in', context=text)
        self.assertEqual('in', a.unit)
        self.assertEqual('1in', a.tostring())

        a.convert(SVGLength.TYPE_CM)  # 1(in) = 2.54(cm)
        self.assertEqual('cm', a.unit)
        self.assertEqual('2.54cm', a.tostring())

        a.convert(SVGLength.TYPE_PX)  # 2.54(cm) = 96(px)
        self.assertEqual('px', a.unit)
        self.assertEqual('96px', a.tostring())

        a.convert(SVGLength.TYPE_NUMBER)  # 96(px) = 96
        self.assertIsNone(a.unit)
        self.assertEqual('96', a.tostring())

        a.convert(SVGLength.TYPE_MM)  # 96(px) = 25.4(mm)
        self.assertEqual('mm', a.unit)
        self.assertEqual('25.4mm', a.tostring())

        # 100(%) = 16(px)
        a.convert(SVGLength.TYPE_PERCENTAGE)  # 96(px) = 600(%)
        self.assertEqual('%', a.unit)
        self.assertEqual('600%', a.tostring())

        # 100(%) = 1(em)
        a.convert(SVGLength.TYPE_EMS)  # 600(%) = 6(em)
        self.assertEqual('em', a.unit)
        self.assertEqual('6em', a.tostring())

        # 1(ex) = x-height(px)
        # 96(px) -> x-height: 53(px)
        # 96(px) / 53(px) = 1.8113...
        a.convert(SVGLength.TYPE_EXS)
        self.assertEqual('ex', a.unit)
        self.assertEqual('1.811ex', a.tostring())

        # font-family: "DejaVu Sans", sans-serif
        # element font size: 16(px) -> 1(ex) = 9(px)
        # 1.8113(ex) = 1.8113 * 9 = 16.3017...(px)
        # 1(px) = 0.75(pt)
        # 16.3017 * 0.75 = 12.2263...(pt)
        a.convert(SVGLength.TYPE_PT)
        self.assertEqual('pt', a.unit)
        self.assertEqual('12.226pt', a.tostring())

        # 1(pt) = 1 / 12(pc)
        # 12.2263(pt) = 1.0189...(pc)
        a.convert(SVGLength.TYPE_PC)
        self.assertEqual('pc', a.unit)
        self.assertEqual('1.019pc', a.tostring())