Move SSD OCR bounding box selection to typed Kotlin

ober

102fed830ed9ac42bba16cef0e48f71485206914

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 055b086..4bd3072 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -3135,12 +3135,25 @@
                  (list-ref (SsdSession-groups session) i)))))))
 
     (typed-kotlin-file "com/sfb/ssdreview/OcrGeometry.kt"
+      (kotlin-imports (android graphics Rect))
       (typed-library (com sfb ssdreview)
         (export floatArrayGetOrZero
-                centerInside rectArea normalizeRect rectsOverlap distanceToRect)
+                centerInside rectArea normalizeRect rectsOverlap distanceToRect
+                ocrBoundingBoxPresent ocrBoundingBoxOrEmpty)
         (type Float32)
         (type FloatArray)
         (type Int32)
+        (type Rect)
+        (def (ocrBoundingBoxPresent (box : (Nullable Rect))) : Bool
+          (not (nullable-null? box)))
+        (def (ocrBoundingBoxOrEmpty (elementBox : (Nullable Rect))
+                                    (lineBox : (Nullable Rect))
+                                    (blockBox : (Nullable Rect))) : (Nullable Rect)
+          (if (not (nullable-null? elementBox))
+            elementBox
+            (if (not (nullable-null? lineBox))
+              lineBox
+              blockBox)))
         (def (floatArrayGetOrZero (items : FloatArray) (index : Int32)) : Float32
           (if (> (float-array-length items) index)
             (float-array-ref items index)
@@ -8169,7 +8182,9 @@
        "            result.textBlocks.forEach { block ->"
        "                block.lines.forEach { line ->"
        "                    line.elements.forEach { element ->"
-       "                        val box = element.boundingBox ?: line.boundingBox ?: block.boundingBox ?: return@forEach"
+       "                        val maybeBox = ocrBoundingBoxOrEmpty(element.boundingBox, line.boundingBox, block.boundingBox)"
+       "                        if (!ocrBoundingBoxPresent(maybeBox)) return@forEach"
+       "                        val box = checkNotNull(maybeBox)"
        "                        val text = element.text.trim()"
        "                        if (ocrTextPresent(text)) {"
        "                            words.add(ocrWordFromBounds(text, box.left, box.top, box.right, box.bottom))"