Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional DPI spec for svg2png raster. E.g. " --rasterize --dpi=300 " #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion itikz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def parse_args(line):
action='store_true', default=False,
help="Rasterize the svg with cairosvg")

parser.add_argument('--dpi', dest='dpi',
default='',
help="DPI passed to cairosvg.svg2png when 'rasterize' is used")

parser.add_argument('--full-error', dest='full_err',
action='store_true', default=False,
help="Emit the full error message")
Expand Down Expand Up @@ -236,7 +240,9 @@ def itikz(self, line, cell=None):
print("$ pip install cairosvg", file=sys.stderr)
return

png_bytes = cairosvg.svg2png(bytestring=svg.data.encode())
raster_args = {"dpi": float(args.dpi)} if args.dpi else {}

png_bytes = cairosvg.svg2png(bytestring=svg.data.encode(), **raster_args)

return Image(data=png_bytes)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_itikz.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ def test_rasterize_bad_input(itikz_magic):
assert res is None


def test_rasterize_and_dpi_good_input(itikz_magic):
pic = r"\node[draw] at (0,0) {Hello World};"
src = "\\begin{tikzpicture}\n" + pic + "\n\\end{tikzpicture}\n"
cmd = "--implicit-standalone --tex-packages=tikz --temp-dir --rasterize --dpi=200"
res = itikz_magic.itikz(cmd, src)
assert isinstance(res, Image)


def test_rasterize_and_dpi_bad_input(itikz_magic):
cmd = "--temp-dir --rasterize --dpi=200"
res = itikz_magic.itikz(cmd, BAD_TIKZ)
assert res is None


def test_dpi_no_rasterize_implicit_pic(itikz_magic):
src = r"\node[draw] at (0,0) {Hello World};"
res = itikz_magic.itikz("--implicit-pic --dpi=200 --temp-dir", src)
assert isinstance(res, SVG)


def test_rasterize_no_cairo_svg(itikz_magic, monkeypatch, capsys):
monkeypatch.setattr(itikz, 'CAIROSVG_ENABLED', False)

Expand Down