Skip to content
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
23 changes: 18 additions & 5 deletions GenerateReferencesCLI/cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ArgumentParser
#if os(macOS)
@main
struct cli: ParsableCommand {
@Argument(help: "Path to a folder that contains 1.1F2/ and 1.2T/")
@Argument(help: "Path to a folder that contains 1.1F2/, 1.2T/ and Custom/")
var input: String

static let v11Refs: [String] = [
Expand Down Expand Up @@ -144,7 +144,12 @@ struct cli: ParsableCommand {
"struct-frag-01-t",
"struct-use-03-t",
]


static let customRefs: [String] = [
"viewport-01",
"viewport-02",
]

mutating func run() throws {
let inputURL = URL(fileURLWithPath: input)

Expand All @@ -154,9 +159,10 @@ struct cli: ParsableCommand {

let v11FolderURL = inputURL.appendingPathComponent("1.1F2")
let v12FolderURL = inputURL.appendingPathComponent("1.2T")

guard FileManager.default.fileExists(atPath: v11FolderURL.path) || FileManager.default.fileExists(atPath: v12FolderURL.path) else {
throw ValidationError("1.1F2/ or 1.2T/ folder does not exist in '\(input)'")
let customFolderURL = inputURL.appendingPathComponent("Custom")

guard FileManager.default.fileExists(atPath: v11FolderURL.path) || FileManager.default.fileExists(atPath: v12FolderURL.path) || FileManager.default.fileExists(atPath: customFolderURL.path) else {
throw ValidationError("1.1F2/, 1.2T/ or Custom/ folder does not exist in '\(input)'")
}

for ref in Self.v11Refs {
Expand All @@ -172,6 +178,13 @@ struct cli: ParsableCommand {
let refURL = v12FolderURL.appending(path: "refs/\(ref).ref")
try svgContent.write(to: refURL, atomically: true, encoding: .utf8)
}

for ref in Self.customRefs {
let svgURL = customFolderURL.appending(path: "svg/\(ref).svg")
let svgContent = try serialize(inputURL: svgURL)
let refURL = customFolderURL.appending(path: "refs/\(ref).ref")
try svgContent.write(to: refURL, atomically: true, encoding: .utf8)
}
}

private func serialize(inputURL: URL) throws -> String {
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ generate-test-cases: # Generate test cases from w3c reference files
printf "}" >> ../SVGViewTests/$$class.swift; \
}; \
generateTest "1.1F2" "SVG11Tests"; \
generateTest "1.2T" "SVG12Tests"
generateTest "1.2T" "SVG12Tests"; \
generateTest "Custom" "SVGCustomTests"

update-references-snapshots: # Update .ref from .svg files
swift run GenerateReferencesCLI Tests/SVGViewTests/w3c/
Expand Down
4 changes: 3 additions & 1 deletion Source/Parser/SVG/SVGContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class SVGNodeContext: SVGContext {

private static func replaceRoot(element: XMLElement, context: SVGContext) -> SVGRootContext {
if element.name == "svg" {
if let viewBox = SVGHelper.parseViewBox(element.attributes, context: context) {
let viewPort = SVGHelper.parseViewPort(element.attributes, context: context)
// Each SVG viewport generates a viewport coordinate system and a user coordinate system, initially identical. Providing a ‘viewBox’ on a viewport's element transforms the user coordinate system relative to the viewport coordinate system
if let viewBox = SVGHelper.parseViewBox(element.attributes, context: context) ?? viewPort {
let screen = SVGScreen(ppi: context.screen.ppi, width: viewBox.width, height: viewBox.height)
return SVGRootContext(logger: context.logger, linker: context.linker, screen: screen, index: context.index, defaultFontSize: context.defaultFontSize)
}
Expand Down
10 changes: 10 additions & 0 deletions Source/Parser/SVG/SVGParserPrimitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ public class SVGHelper: NSObject {
return scale.concatenating(move)
}

static func parseViewPort(_ attributes: [String: String], context: SVGContext) -> CGRect? {
if let widthAttr = attributes[ignoreCase: "width"],
let heightAttr = attributes[ignoreCase: "height"],
let width = SVGLengthParser.xAxis.double(string: widthAttr, context: context),
let height = SVGLengthParser.yAxis.double(string: heightAttr, context: context) {
return CGRect(x: 0.0, y: 0.0, width: width, height: height)
}
return nil
}

static func parseViewBox(_ attributes: [String: String], context: SVGContext) -> CGRect? {
// TODO: temporary solution, all attributes should be case insensitive
if let string = attributes[ignoreCase: "viewBox"] {
Expand Down
16 changes: 16 additions & 0 deletions Tests/SVGViewTests/SVGCustomTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Generated by make generate-test-cases

import XCTest
@testable import SVGView

class SVGCustomTests: BaseTestCase {

override var dir: String {
return "Custom"
}

func testViewport01() {
compareToReference("viewport-01")
}

}
15 changes: 15 additions & 0 deletions Tests/SVGViewTests/w3c/Custom/refs/viewport-01.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SVGViewport {
width: "780",
height: "950",
scaling: "none",
contents: [
SVGRect { width: 780, height: 950, fill: "#F4F4F4" },
SVGText {
text: "1-to-1 Meeting Template",
font: { size: 24, weight: "bold" },
textAnchor: "middle",
fill: "black",
transform: [1, 0, 0, 1, 390, 50]
}
]
}
14 changes: 14 additions & 0 deletions Tests/SVGViewTests/w3c/Custom/refs/viewport-02.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SVGViewport {
width: "180%",
scaling: "none",
contents: [
SVGRect { width: 180, height: 100, fill: "#F4F4F4" },
SVGText {
text: "1-to-1 Meeting Template",
font: { size: 24, weight: "bold" },
textAnchor: "middle",
fill: "black",
transform: [1, 0, 0, 1, 390, 50]
}
]
}
7 changes: 7 additions & 0 deletions Tests/SVGViewTests/w3c/Custom/svg/viewport-01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Tests/SVGViewTests/w3c/Custom/svg/viewport-02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.