diff options
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | TODO.norg | 1 | ||||
| -rw-r--r-- | cpp/libchelleport.cpp | 28 | ||||
| -rw-r--r-- | include/recognizer.h | 6 | ||||
| -rw-r--r-- | src/Chelleport.hs | 2 | ||||
| -rw-r--r-- | src/Chelleport/View.hs | 15 |
6 files changed, 31 insertions, 23 deletions
@@ -55,7 +55,7 @@ Use [sxhkd](https://github.com/baskerville/sxhkd), [shotkey](https://github.com/ - Once a match is found, you can now use `hjkl` keys to make smaller movements. Hold `shift` + `hjkl` to move in bigger increments. - Press `space` to click -### Search mode (`ctrl+s` to switch to search mode) +### Search mode (`ctrl+s` to switch to search mode or run with `chelleport -m search`) - Words that are recognized by OCR will be highlighted - Type the characters in one of the words to move the cursor to it - Press `ctrl+n` & `ctrl+p` to go to next/previous match respectively @@ -1,5 +1,6 @@ * Current - (-) Optimize speed of ocr + - ( ) Ctrl+Click * Later - ( ) Middle click diff --git a/cpp/libchelleport.cpp b/cpp/libchelleport.cpp index 010fdd4..ff3f7d6 100644 --- a/cpp/libchelleport.cpp +++ b/cpp/libchelleport.cpp @@ -24,6 +24,7 @@ extern "C" OCRMatch *findWordCoordinates(const char *image_path, int *size) { OCRMatchSet extractTextMatches(const char *imagePath) { Pix *image = image::loadImage(imagePath); + if (image == nullptr) { return OCRMatchSet(); } @@ -35,17 +36,17 @@ OCRMatchSet extractTextMatches(const char *imagePath) { int height = pixGetHeight(image); std::vector<std::unique_ptr<Recognizer>> recognizers; - recognizers.push_back( - std::make_unique<Recognizer>(0, 0, width / 2, height / 2)); + // clang-format off recognizers.push_back( - std::make_unique<Recognizer>(width / 2, 0, width / 2, height / 2)); - + std::make_unique<Recognizer>("top-left", 0, 0, width / 2, height / 2)); recognizers.push_back( - std::make_unique<Recognizer>(0, height / 2, width / 2, height / 2)); - - recognizers.push_back(std::make_unique<Recognizer>(width / 2, height / 2, - width / 2, height / 2)); + std::make_unique<Recognizer>("top-right", width / 2, 0, width / 2, height / 2)); + recognizers.push_back( + std::make_unique<Recognizer>("bottom-left", 0, height / 2, width / 2, height / 2)); + recognizers.push_back( + std::make_unique<Recognizer>("bottom-right", width / 2, height / 2, width / 2, height / 2)); + // clang-format on return runRecognizers(recognizers, image); } @@ -59,9 +60,10 @@ runRecognizers(std::vector<std::unique_ptr<Recognizer>> &recognizers, std::vector<std::thread> workers; workers.reserve(recognizers.size()); - for (auto &ext : recognizers) { - workers.push_back(std::thread( - [&ext, &sharedImage]() { ext->recognize(sharedImage.get()); })); + for (auto &recognizer : recognizers) { + workers.push_back(std::thread([&recognizer, &sharedImage]() { + MEASURE(recognizer->id, { recognizer->recognize(sharedImage.get()); }) + })); } for (std::thread &t : workers) { @@ -69,8 +71,8 @@ runRecognizers(std::vector<std::unique_ptr<Recognizer>> &recognizers, t.join(); } - for (auto &ext : recognizers) { - for (auto &match : ext->getResults()) + for (auto &recognizer : recognizers) { + for (auto &match : recognizer->getResults()) results.insert(match); } diff --git a/include/recognizer.h b/include/recognizer.h index 3b13fdc..260a139 100644 --- a/include/recognizer.h +++ b/include/recognizer.h @@ -23,8 +23,10 @@ class Recognizer { bool failed = false; public: - Recognizer(int x, int y, int width, int height) - : x(x), y(y), width(width), height(height) { + const char *id; + + Recognizer(const char *id, int x, int y, int width, int height) + : id(id), x(x), y(y), width(width), height(height) { initializeTesseract(); } diff --git a/src/Chelleport.hs b/src/Chelleport.hs index 8cc02c7..6486b4a 100644 --- a/src/Chelleport.hs +++ b/src/Chelleport.hs @@ -24,7 +24,7 @@ run config = do runAppWithCtx :: (MonadIO m) => DrawContext -> AppM m x -> m x runAppWithCtx ctx = (`runReaderT` ctx) . runAppM --- TODO: Make event handling independent of state +-- TODO: Make event handling independent of state? eventHandler :: State -> SDL.Event -> Maybe AppAction eventHandler state event = case SDL.eventPayload event of diff --git a/src/Chelleport/View.hs b/src/Chelleport/View.hs index 314aa39..369b93d 100644 --- a/src/Chelleport/View.hs +++ b/src/Chelleport/View.hs @@ -27,13 +27,16 @@ getSearchText state (ModeSearchData {searchInputText, searchFilteredWords, searc renderSearchView :: (MonadDraw m) => State -> ModeSearchData -> m () renderSearchView state searchData@(ModeSearchData {searchFilteredWords, searchHighlightedIndex}) = do renderGridLines state + mapM_ highlightWord $ zip [0 ..] searchFilteredWords + drawSearchText + where + highlightWord (index, OCRMatch {matchStartX, matchStartY, matchEndX, matchEndY}) = do + setDrawColor $ if searchHighlightedIndex == index then colorAccent else colorLightGray + fillRectVertices (matchStartX, matchStartY) (matchEndX, matchEndY) - forM_ (zip [0 ..] searchFilteredWords) $ \(index, OCRMatch {matchStartX, matchStartY, matchEndX, matchEndY}) -> do - setDrawColor $ if searchHighlightedIndex == index then colorAccent else colorLightGray - fillRectVertices (matchStartX, matchStartY) (matchEndX, matchEndY) - - (w, h) <- windowSize - void $ drawText (w `div` 2, 5 + h `div` 2) searchingTextStyle (Text.pack $ getSearchText state searchData) + drawSearchText = do + (w, h) <- windowSize + void $ drawText (w `div` 2, 5 + h `div` 2) searchingTextStyle (Text.pack $ getSearchText state searchData) renderHintsView :: (MonadDraw m) => State -> ModeHintsData -> m () renderHintsView state (ModeHintsData {stateGrid, stateKeySequence, stateIsMatched}) = do |
