Skip to content

Instantly share code, notes, and snippets.

@manu-mannattil
Last active June 10, 2020 01:04
Show Gist options
  • Select an option

  • Save manu-mannattil/eebe61eb68f184295eeb6848d9be8d08 to your computer and use it in GitHub Desktop.

Select an option

Save manu-mannattil/eebe61eb68f184295eeb6848d9be8d08 to your computer and use it in GitHub Desktop.
A script to set the stroke width in a Xournal++ document as a percentage of the current width
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# xoppwidth.py is a simple Python script to set the stroke width in
# a Xournal++ .xopp document as a percentage of the current stroke
# width. For instance, to double the stroke width, run
#
# xoppwidth.py -w 200 -o output.xopp input.xopp
#
# For a detailed description of the options, run
#
# xoppwidth.py --help
#
# This script was written because Xournal++ currently doesn't provide
# a way to set the stroke width [1, 2] as a floating point.
#
# [1]: https://github.com/xournalpp/xournalpp/issues/1361
# [2]: https://github.com/xournalpp/xournalpp/issues/1998
#
from __future__ import print_function
import argparse
import gzip
import os
import sys
import xml.etree.ElementTree as ET
def stroke_width(name, output=None, percent=100.0):
"""Set the stroke width to the given percentage."""
with gzip.open(name, "rb") as fd:
tree = ET.fromstring(fd.read())
for stroke in tree.iter("stroke"):
if stroke.attrib.get("tool", "") in ("pen", "highlighter"):
width = list()
for w in stroke.attrib["width"].split(" "):
width.append("{:.8f}".format(float(w) * 0.01 * percent))
stroke.set("width", " ".join(width))
if output is None:
output = os.path.splitext(name)[0] + "_w{:d}.xopp".format(int(percent))
with gzip.open(output, "wb") as fd:
fd.write(ET.tostring(tree, encoding="UTF-8"))
print("output written to {}".format(output), file=sys.stderr)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="xoppwidth", description="set the stroke width in a Xournal++ document"
)
parser.add_argument("name", help="input files", metavar="FILE")
parser.add_argument(
"-o", "--output", default=None, help="output file", metavar="FILE"
)
parser.add_argument(
"-w",
"--width",
default=100.0,
help="width as percentage of current width",
metavar="PERCENT",
type=float,
)
args = parser.parse_args()
sys.exit(stroke_width(args.name, args.output, args.width))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment