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

Rewite indentation engine #53

Merged
Merged
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
1 change: 1 addition & 0 deletions doc/indentation_logic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pages
11 changes: 11 additions & 0 deletions doc/indentation_logic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pages.pdf: pages/page_000.pdf
pdfunite pages/page_*.pdf pages.pdf

pages/page_000.pdf: pages/page_000.svg
for i in pages/page_*.svg ; do inkscape --export-pdf=pages/"$$( basename "$${i}" .svg )".pdf "$${i}" ; done

pages/page_000.svg: src/pages.svg src/split_pages.kts
kotlinc-jvm -script src/split_pages.kts src/pages.svg

clean:
rm -rf pages.pdf pages
Binary file added doc/indentation_logic/pages.pdf
Binary file not shown.
3,322 changes: 3,322 additions & 0 deletions doc/indentation_logic/src/pages.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions doc/indentation_logic/src/split_pages.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Write each layer of Inkscape SVG file into separate SVG files.
*/
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import org.w3c.dom.Element

val INKSCAPE_NS = "http://www.inkscape.org/namespaces/inkscape"

if (args.size < 1) {
System.err.println("Usage: kotlinc-jvm -script split_pages.kts pages.svg")
System.exit(-1)
}

val transformer = TransformerFactory.newInstance().newTransformer()

val inputFile = File(args[0])
val document = DocumentBuilderFactory
.newDefaultInstance()
.also { it.setNamespaceAware(true) }
.newDocumentBuilder()
.parse(inputFile)

val svgElement = document.documentElement
val childNodes = svgElement.childNodes
val layers = 0.until(childNodes.length)
.map { childNodes.item(it) }
.filter { child ->
child is Element &&
child.localName == "g" &&
child.getAttributeNS(INKSCAPE_NS, "groupmode") == "layer"
}

for (layer in layers) {
svgElement.removeChild(layer)
}

val outputDirectory = File("pages")

outputDirectory.mkdirs()

for ((index, layer) in layers.withIndex()) {
svgElement.appendChild(layer)

// Assuming `style="display:none"`
layer.attributes.removeNamedItem("style")

val outputFile = File(outputDirectory, "page_%03d.svg".format(index))

transformer.transform(DOMSource(document), StreamResult(outputFile))

svgElement.removeChild(layer)
}
Loading