<commit_before>//
//  SketchOmniGraffle.swift
//  Sketch
//
//  Created by <NAME> on 9/2/14.
//  Copyright (c) 2014 schwa.io. All rights reserved.
//

import Foundation

func loadTest() -> Node {
    let path = NSBundle.mainBundle().pathForResource("Test", ofType:"graffle")
    let doc = OmniGraffleDocumentModel(path: path!)
    let root = convert(doc.rootNode)
    return root
}

internal func convert(input:OmniGraffleNode) -> Node! {
    switch input {
        case let input as OmniGraffleGroup:
            return convert(input)
        case let input as OmniGraffleShape:
            return convert(input)
        case let input as OmniGraffleLine:
            return convert(input)
        default:
            return nil
    }
}

internal func convert(input:OmniGraffleGroup) -> Node! {
    let group = Group()
    group.children = input.children.map {
        (node:Node) -> Node in
        return convert(node as OmniGraffleNode)
    }
    return group
}

internal func convert(input:OmniGraffleShape) -> Node! {
    let shapeName = input.dictionary["Shape"] as String
    switch shapeName {
        case "Circle":
            return Circle(name:"Hello", center:input.bounds.mid, radius:input.bounds.size.width * 0.5)
        case "Rectangle":
            return Rectangle(name:"Hello", frame:input.bounds)
////                    case "Bezier":
////                        println(d)
//                        return nil
        default:
            println("Unknown shape: \(shapeName)")
            return nil
    }
}

internal func convert(input:OmniGraffleLine) -> Node! {
    let shape = Line(name:"Hello", start:input.start, end:input.end)
    return shape
}<commit_msg>Flips coordinate systems.<commit_after>//
//  SketchOmniGraffle.swift
//  Sketch
//
//  Created by <NAME> on 9/2/14.
//  Copyright (c) 2014 schwa.io. All rights reserved.
//

import Foundation

import SwiftGraphics

func loadTest() -> Node {
    let path = NSBundle.mainBundle().pathForResource("Test", ofType:"graffle")
    let doc = OmniGraffleDocumentModel(path: path!)
    let root = convert(doc.rootNode)
    return root
}

internal func convert(input:OmniGraffleNode) -> Node! {
    switch input {
        case let input as OmniGraffleGroup:
            return convert(input)
        case let input as OmniGraffleShape:
            return convert(input)
        case let input as OmniGraffleLine:
            return convert(input)
        default:
            return nil
    }
}

internal func convert(input:OmniGraffleGroup) -> Node! {
    let group = Group()
    group.children = input.children.map {
        (node:Node) -> Node in
        return convert(node as OmniGraffleNode)
    }
    return group
}


enum GraphicsOrigin {
    case TopLeft
    case BottomLeft
    case Native

    var resolved : GraphicsOrigin {
        get {
            switch self {
                case .TopLeft, .BottomLeft:
                    return self
                case .Native:
                    #if os(OSX)
                    return .TopLeft
                    #else
                    return .BottomLeft
                    #endif
            }
        }
    }

    var isNative : Bool {
        get {
            switch self {
                case .TopLeft:
                    #if os(OSX)
                    return false
                    #else
                    return true
                    #endif
                case .BottomLeft:
                    #if os(OSX)
                    return true
                    #else
                    return false
                    #endif
                case .Native:
                    return true
            }
        }
    }
}

extension CGPoint {
    func flipped(origin:GraphicsOrigin, insideRect:CGRect) -> CGPoint {
        if origin.isNative {
            return self
        }
        else {
            return self * CGAffineTransform(tx:0, ty:insideRect.size.height).scaled(sx:1, sy:-1)
        }
    }
}

extension CGRect {
    func flipped(origin:GraphicsOrigin, insideRect:CGRect) -> CGRect {
        if origin.isNative {
            return self
        }
        else {
            return self * CGAffineTransform(tx:0, ty:insideRect.size.height).scaled(sx:1, sy:-1)
        }
    }
}

internal func convert(input:OmniGraffleShape) -> Node! {
    let shapeName = input.dictionary["Shape"] as String
    switch shapeName {
        case "Circle":
            println(input.bounds)
            let bounds = input.bounds.flipped(.TopLeft, insideRect:CGRect(w:1024, h:768))
            println(bounds)
            return Circle(name:"Hello", center:bounds.mid, radius:bounds.size.width * 0.5)
        case "Rectangle":
            let bounds = input.bounds.flipped(.TopLeft, insideRect:CGRect(w:1024, h:768))
            return Rectangle(name:"Hello", frame:bounds)
////                    case "Bezier":
////                        println(d)
//                        return nil
        default:
            println("Unknown shape: \(shapeName)")
            return nil
    }
}

internal func convert(input:OmniGraffleLine) -> Node! {
    let start = input.start.flipped(.TopLeft, insideRect:CGRect(w:1024, h:768))
    let end = input.end.flipped(.TopLeft, insideRect:CGRect(w:1024, h:768))

    let shape = Line(name:"Hello", start:start, end:end)
    return shape
}
{'fraction_non_alphanumeric': 0.10137037732307115, 'fraction_numerical': 0.010137037732307114, 'mean_word_length': 1.7337095946639303, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2262214', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1495, 'n_words': 369}