Skip to content

Commit

Permalink
Align all frames on the left/right edges without constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamede1945 committed Apr 7, 2024
1 parent d9fc366 commit 5249057
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
244
],
[
113,
268,
95
]
],
Expand Down Expand Up @@ -78,11 +78,11 @@
{
"frame" : [
[
386,
230,
244
],
[
85,
241,
95
]
],
Expand All @@ -101,7 +101,7 @@
339
],
[
145,
209,
109
]
],
Expand Down Expand Up @@ -173,11 +173,11 @@
{
"frame" : [
[
294,
230,
339
],
[
85,
149,
109
]
],
Expand Down Expand Up @@ -557,7 +557,7 @@
781
],
[
109,
219,
107
]
],
Expand Down Expand Up @@ -610,11 +610,11 @@
{
"frame" : [
[
339,
230,
781
],
[
127,
236,
107
]
],
Expand All @@ -633,7 +633,7 @@
888
],
[
60,
281,
87
]
],
Expand Down Expand Up @@ -667,11 +667,11 @@
{
"frame" : [
[
448,
230,
888
],
[
80,
298,
87
]
],
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@
1071
],
[
163,
257,
117
]
],
Expand Down Expand Up @@ -876,11 +876,11 @@
{
"frame" : [
[
138,
50,
1071
],
[
103,
191,
117
]
],
Expand Down Expand Up @@ -1222,7 +1222,7 @@
1734
],
[
194,
357,
133
]
],
Expand Down Expand Up @@ -1294,11 +1294,11 @@
{
"frame" : [
[
205,
50,
1734
],
[
108,
263,
133
]
],
Expand All @@ -1317,7 +1317,7 @@
1867
],
[
68,
331,
113
]
],
Expand Down Expand Up @@ -1370,11 +1370,11 @@
{
"frame" : [
[
304,
50,
1867
],
[
109,
363,
113
]
],
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
//
// WordFrameScale+Extension.swift
// WordFrame+Extension.swift
//
//
// Created by Mohamed Afifi on 2021-12-26.
//

import QuranGeometry
import QuranKit
import UIKit
import VLogging

extension WordFrameCollection {
public func wordAtLocation(_ location: CGPoint, imageScale: WordFrameScale) -> Word? {
let flattenFrames = lines.flatMap { $0 }
for frame in flattenFrames {
let rectangle = frame.rect.scaled(by: imageScale)
if rectangle.contains(location) {
return frame.word
}
}
return nil
}
}

extension WordFrame {
mutating func normalize() {
Expand Down Expand Up @@ -87,41 +72,19 @@ extension WordFrame {
private static func unionEdge(
_ list: inout [WordFrame],
keyPath: WritableKeyPath<WordFrame, Int>,
isMin: Bool
reduce: ([Int]) -> Int?
) {
var longest: [Int] = []
for i in 0 ..< list.count - 1 {
let pivot = list[i]
var running = [i]
for j in i + 1 ..< list.count {
let other = list[j]
if abs(pivot[keyPath: keyPath] - other[keyPath: keyPath]) < 50 {
running.append(j)
}
}
if running.count > longest.count {
longest = running
}
}

guard !longest.isEmpty else {
return
}

let values = longest.map { list[$0][keyPath: keyPath] }
let value = isMin ? values.min()! : values.max()!
for i in longest {
var frame = list[i]
frame[keyPath: keyPath] = value
list[i] = frame
let value = reduce(list.map { $0[keyPath: keyPath] })!
for i in 0 ..< list.count {
list[i][keyPath: keyPath] = value
}
}

static func unionLeftEdge(_ list: inout [WordFrame]) {
unionEdge(&list, keyPath: \.minX, isMin: true)
unionEdge(&list, keyPath: \.minX, reduce: { $0.min() })
}

static func unionRightEdge(_ list: inout [WordFrame]) {
unionEdge(&list, keyPath: \.maxX, isMin: false)
unionEdge(&list, keyPath: \.maxX, reduce: { $0.max() })
}
}
23 changes: 23 additions & 0 deletions Domain/WordFrameService/WordFrameCollection+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// WordFrameCollection+Extension.swift
//
//
// Created by Mohamed Afifi on 2024-04-07.
//

import Foundation
import QuranGeometry
import QuranKit

extension WordFrameCollection {
public func wordAtLocation(_ location: CGPoint, imageScale: WordFrameScale) -> Word? {
let flattenFrames = lines.flatMap { $0 }
for frame in flattenFrames {
let rectangle = frame.rect.scaled(by: imageScale)
if rectangle.contains(location) {
return frame.word
}
}
return nil
}
}
26 changes: 13 additions & 13 deletions Domain/WordFrameService/WordFrameProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public struct WordFrameProcessor {

// group by line
let framesByLines = Dictionary(grouping: frames, by: { $0.line })
var sortedLines = framesByLines
var lines = framesByLines
.sorted { $0.key < $1.key }
.map { _, wordFrames in wordFrames }

normalize(&sortedLines)
alignFramesVerticallyInEachLine(&sortedLines)
unionLinesVertically(&sortedLines)
unionFramesHorizontallyInEachLine(&sortedLines)
alignLineEdges(&sortedLines)
normalize(&lines)
alignFramesVerticallyInEachLine(&lines)
unionLinesVertically(&lines)
unionFramesHorizontallyInEachLine(&lines)
alignLineEdges(&lines)

return WordFrameCollection(lines: sortedLines)
return WordFrameCollection(lines: lines)
}

// MARK: Private
Expand Down Expand Up @@ -94,14 +94,14 @@ public struct WordFrameProcessor {

private func alignLineEdges(_ lines: inout [[WordFrame]]) {
// align the edges
var firstEdge = lines.map { $0.first! }
var lastEdge = lines.map { $0.last! }
WordFrame.unionLeftEdge(&lastEdge)
WordFrame.unionRightEdge(&firstEdge)
var rightEdge = lines.map { $0[0] }
var leftEdge = lines.map { $0[$0.count - 1] }
WordFrame.unionLeftEdge(&leftEdge)
WordFrame.unionRightEdge(&rightEdge)

for i in 0 ..< lines.count {
lines[i][0] = firstEdge[i]
lines[i][lines[i].count - 1] = lastEdge[i]
lines[i][0] = rightEdge[i]
lines[i][lines[i].count - 1] = leftEdge[i]
}
}
}

0 comments on commit 5249057

Please sign in to comment.