diff --git a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.2.json b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.2.json index e43de9f0..6e01929a 100644 --- a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.2.json +++ b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.2.json @@ -6,7 +6,7 @@ 244 ], [ - 113, + 268, 95 ] ], @@ -78,11 +78,11 @@ { "frame" : [ [ - 386, + 230, 244 ], [ - 85, + 241, 95 ] ], @@ -101,7 +101,7 @@ 339 ], [ - 145, + 209, 109 ] ], @@ -173,11 +173,11 @@ { "frame" : [ [ - 294, + 230, 339 ], [ - 85, + 149, 109 ] ], @@ -557,7 +557,7 @@ 781 ], [ - 109, + 219, 107 ] ], @@ -610,11 +610,11 @@ { "frame" : [ [ - 339, + 230, 781 ], [ - 127, + 236, 107 ] ], @@ -633,7 +633,7 @@ 888 ], [ - 60, + 281, 87 ] ], @@ -667,11 +667,11 @@ { "frame" : [ [ - 448, + 230, 888 ], [ - 80, + 298, 87 ] ], diff --git a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.3.png b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.3.png index b16faf8b..05ca9894 100644 Binary files a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.3.png and b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage1.3.png differ diff --git a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage3.3.png b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage3.3.png index d4a52d15..76cbcec2 100644 Binary files a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage3.3.png and b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage3.3.png differ diff --git a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.2.json b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.2.json index 2a491000..5ac5b74b 100644 --- a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.2.json +++ b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.2.json @@ -747,7 +747,7 @@ 1071 ], [ - 163, + 257, 117 ] ], @@ -876,11 +876,11 @@ { "frame" : [ [ - 138, + 50, 1071 ], [ - 103, + 191, 117 ] ], @@ -1222,7 +1222,7 @@ 1734 ], [ - 194, + 357, 133 ] ], @@ -1294,11 +1294,11 @@ { "frame" : [ [ - 205, + 50, 1734 ], [ - 108, + 263, 133 ] ], @@ -1317,7 +1317,7 @@ 1867 ], [ - 68, + 331, 113 ] ], @@ -1370,11 +1370,11 @@ { "frame" : [ [ - 304, + 50, 1867 ], [ - 109, + 363, 113 ] ], diff --git a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.3.png b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.3.png index d0c96b90..d6adfd73 100644 Binary files a/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.3.png and b/Domain/ImageService/Tests/__Snapshots__/ImageDataServiceTests/testGettingImageAtPage604.3.png differ diff --git a/Domain/WordFrameService/WordFrameScale+Extension.swift b/Domain/WordFrameService/WordFrame+Extension.swift similarity index 65% rename from Domain/WordFrameService/WordFrameScale+Extension.swift rename to Domain/WordFrameService/WordFrame+Extension.swift index d6fc1d79..eb4cd550 100644 --- a/Domain/WordFrameService/WordFrameScale+Extension.swift +++ b/Domain/WordFrameService/WordFrame+Extension.swift @@ -1,5 +1,5 @@ // -// WordFrameScale+Extension.swift +// WordFrame+Extension.swift // // // Created by Mohamed Afifi on 2021-12-26. @@ -7,21 +7,6 @@ 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() { @@ -87,41 +72,19 @@ extension WordFrame { private static func unionEdge( _ list: inout [WordFrame], keyPath: WritableKeyPath, - 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() }) } } diff --git a/Domain/WordFrameService/WordFrameCollection+Extension.swift b/Domain/WordFrameService/WordFrameCollection+Extension.swift new file mode 100644 index 00000000..25569a0b --- /dev/null +++ b/Domain/WordFrameService/WordFrameCollection+Extension.swift @@ -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 + } +} diff --git a/Domain/WordFrameService/WordFrameProcessor.swift b/Domain/WordFrameService/WordFrameProcessor.swift index bb77c8c9..603bce36 100644 --- a/Domain/WordFrameService/WordFrameProcessor.swift +++ b/Domain/WordFrameService/WordFrameProcessor.swift @@ -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 @@ -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] } } }