コード例 #1
0
    def test_control_file_to_control_buffer(self, tmpdir):
        control_file = tmpdir / "ta_ctrl.txt"
        data = u"abcd"
        control_file.write_text(data, encoding="utf-8")

        # 'control_file' is a file object opened in text mode
        with control_file.open(mode="rt", encoding="utf-8") as f:
            kwargs = {'in_buffer': b"\0", 'control_file': f}
            options = validate_options(kwargs)
        assert options["control_buffer"] == data.encode("utf-8")
        assert "control_file" not in options
        assert options["control_buffer_len"] == len(data)
        assert options["control_name"] == str(control_file)

        # 'control_file' is a path string
        kwargs = {'in_buffer': b"\0", 'control_file': str(control_file)}
        options = validate_options(kwargs)
        assert options["control_buffer"] == data.encode("utf-8")
        assert "control_file" not in options
        assert options["control_buffer_len"] == len(data)
        assert options["control_name"] == str(control_file)

        # 'control_file' is a file-like stream
        kwargs = {'in_buffer': b"\0", 'control_file': StringIO(data)}
        options = validate_options(kwargs)
        assert options["control_buffer"] == data.encode("utf-8")
        assert "control_file" not in options
        assert options["control_buffer_len"] == len(data)
        # the stream doesn't have a 'name' attribute; using fallback
        assert options["control_name"] == u"<control-instructions>"
コード例 #2
0
    def test_reference_file_to_reference_buffer(self, tmpdir):
        reference_file = tmpdir / "font.ttf"
        data = b"\0\1\0\0"
        reference_file.write_binary(data)
        encoded_filename = ensure_binary(
            str(reference_file), encoding=sys.getfilesystemencoding())

        # 'reference_file' is a file object opened in binary mode
        with reference_file.open(mode="rb") as f:
            kwargs = {'in_buffer': b"\0", 'reference_file': f}
            options = validate_options(kwargs)
        assert options["reference_buffer"] == data
        assert "reference_file" not in options
        assert options["reference_buffer_len"] == len(data)
        assert options["reference_name"] == encoded_filename

        # 'reference_file' is a path string
        kwargs = {'in_buffer': b"\0", 'reference_file': str(reference_file)}
        options = validate_options(kwargs)
        assert options["reference_buffer"] == data
        assert "reference_file" not in options
        assert options["reference_buffer_len"] == len(data)
        assert options["reference_name"] == encoded_filename

        # 'reference_file' is a file-like stream
        kwargs = {'in_buffer': b"\0", 'reference_file': BytesIO(data)}
        options = validate_options(kwargs)
        assert options["reference_buffer"] == data
        assert "reference_file" not in options
        assert options["reference_buffer_len"] == len(data)
        # the stream doesn't have a 'name' attribute, no reference_name
        assert options["reference_name"] is None
コード例 #3
0
 def test_reference_file_or_reference_buffer(self, tmpdir):
     msg = "reference_file and reference_buffer are mutually exclusive"
     reference_file = (tmpdir / "ref.ttf").ensure()
     kwargs = dict(in_buffer=b"\0\1\0\0",
                   reference_file=reference_file,
                   reference_buffer=b"\x00\x01\x00\x00")
     with pytest.raises(ValueError, match=msg):
         validate_options(kwargs)
コード例 #4
0
 def test_control_file_or_control_buffer(self, tmpdir):
     msg = "control_file and control_buffer are mutually exclusive"
     control_file = (tmpdir / "ta_ctrl.txt").ensure()
     kwargs = dict(in_buffer=b"\0\1\0\0",
                   control_file=control_file,
                   control_buffer=b"abcd")
     with pytest.raises(ValueError, match=msg):
         validate_options(kwargs)
コード例 #5
0
    def test_unknown_keyword(self):
        kwargs = dict(foo="bar")
        with pytest.raises(TypeError, match="unknown keyword argument: 'foo'"):
            validate_options(kwargs)

        # 's' for plural
        kwargs = dict(foo="bar", baz=False)
        with pytest.raises(TypeError,
                           match="unknown keyword arguments: 'foo', 'baz'"):
            validate_options(kwargs)
コード例 #6
0
    def test_in_file_to_in_buffer(self, tmpdir):
        in_file = tmpdir / "file1.ttf"
        data = b"\0\1\0\0"
        in_file.write_binary(data)

        # 'in_file' is a file-like object
        options = validate_options({'in_file': in_file.open(mode="rb")})
        assert options["in_buffer"] == data
        assert "in_file" not in options
        assert options["in_buffer_len"] == len(data)

        # 'in_file' is a path string
        options = validate_options({"in_file": str(in_file)})
        assert options["in_buffer"] == data
        assert "in_file" not in options
        assert options["in_buffer_len"] == len(data)
コード例 #7
0
    def test_custom_reference_name(self, tmpdir):
        reference_file = tmpdir / "font.ttf"
        data = b"\0\1\0\0"
        reference_file.write_binary(data)
        expected = u"Some Font".encode(sys.getfilesystemencoding())

        with reference_file.open(mode="rb") as f:
            kwargs = {'in_buffer': b"\0",
                      'reference_file': f,
                      'reference_name': u"Some Font"}
            options = validate_options(kwargs)

        assert options["reference_name"] == expected

        kwargs = {'in_buffer': b"\0",
                  'reference_file': str(reference_file),
                  'reference_name': u"Some Font"}
        options = validate_options(kwargs)

        assert options["reference_name"] == expected
コード例 #8
0
 def test_in_buffer_is_bytes(self, tmpdir):
     with pytest.raises(TypeError, match="in_buffer type must be bytes"):
         validate_options({"in_buffer": u"abcd"})
コード例 #9
0
 def test_in_file_or_in_buffer(self, tmpdir):
     msg = "in_file and in_buffer are mutually exclusive"
     in_file = (tmpdir / "file1.ttf").ensure()
     kwargs = dict(in_file=str(in_file), in_buffer=b"\x00\x01\x00\x00")
     with pytest.raises(ValueError, match=msg):
         validate_options(kwargs)
コード例 #10
0
 def test_no_info_or_detailed_info(self, tmpdir):
     msg = "no_info and detailed_info are mutually exclusive"
     kwargs = dict(no_info=True, detailed_info=True)
     with pytest.raises(ValueError, match=msg):
         validate_options(kwargs)
コード例 #11
0
 def test_no_input(self):
     with pytest.raises(ValueError, match="No input file"):
         validate_options({})
コード例 #12
0
 def test_family_suffix(self):
     options = validate_options({"in_buffer": b"\0",
                                 "family_suffix": b"-TA"})
     assert isinstance(options["family_suffix"], text_type)
     assert options["family_suffix"] == u"-TA"
コード例 #13
0
 def test_epoch(self):
     options = validate_options({"in_buffer": b"\0", "epoch": 0})
     assert isinstance(options["epoch"], c_ulonglong)
     assert options["epoch"].value == 0
コード例 #14
0
 def test_reference_buffer_is_bytes(self, tmpdir):
     with pytest.raises(TypeError,
                        match="reference_buffer type must be bytes"):
         validate_options({"in_buffer": b"\0", "reference_buffer": u""})
コード例 #15
0
 def test_control_buffer_name(self, tmpdir):
     kwargs = {"in_buffer": b"\0", "control_buffer": b"abcd"}
     options = validate_options(kwargs)
     assert options["control_name"] == u"<control-instructions>"
コード例 #16
0
    def ttfautohint(self, **kwargs):
        options = validate_options(kwargs)

        info_data = self._build_info_data(options)

        if info_data.family_suffix:
            info_post_callback = info.info_post_callback
        else:
            info_post_callback = None

        if options.pop("verbose"):
            # by default, it prints to stderr like ttfautohint.exe
            # TODO: figure out a way to implement progress using logging?
            printer = progress.ProgressPrinter()
            progress_callback = printer.callback
        else:
            progress_callback = None
        progress_callback_data = progress.ProgressData()

        error_callback = errors.error_callback
        control_name = options.pop("control_name", None)
        error_callback_data = errors.ErrorData(control_name)

        # pop 'out_file' from options dict since we use 'out_buffer'
        out_file = options.pop('out_file')

        out_buffer_p = POINTER(c_char)()
        out_buffer_len = c_size_t(0)

        option_keys, option_values = format_varargs(
            out_buffer=byref(out_buffer_p),
            out_buffer_len=byref(out_buffer_len),
            alloc_func=memory.alloc_callback,
            free_func=memory.free_callback,
            info_callback=info.info_callback,
            info_post_callback=info_post_callback,
            info_callback_data=byref(info_data),
            progress_callback=progress_callback,
            progress_callback_data=byref(progress_callback_data),
            error_callback=error_callback,
            error_callback_data=byref(error_callback_data),
            **options)

        rv = self.lib.TTF_autohint(option_keys, *option_values)
        if rv:
            raise TAError(rv, **error_callback_data.kwargs)

        assert out_buffer_len.value

        data = out_buffer_p[:out_buffer_len.value]
        assert len(data) == out_buffer_len.value

        if out_buffer_p:
            memory.free(out_buffer_p)
            out_buffer_p = None

        if out_file is not None:
            try:
                return out_file.write(data)
            except AttributeError:
                with open(out_file, 'wb') as f:
                    return f.write(data)
        else:
            return data