Skip to content

Commit

Permalink
[Editor] (WIP) Add a new tool in order to add an handwritten signatur…
Browse files Browse the repository at this point in the history
…e to a pdf (bug 1942343)

This patch is adding some code in order to extract a drawing as curves from an image.
The algorithm is basically the following:
 - reduce the dimensions
 - make it gray
 - apply a bilateral filter in order to add some blurryness while keeping the edges
 - compute the histogram
 - guess what's the background color which should contain a large majority of the pixels
 - make a binary image
 - extract the contours in using the Suzuki algorithm
 - apply the Douglas-Peucker algorithm in order to reduce the number of points

The algorithm is improvable but it should work pretty well if there's a clear difference between
the background and the drawing.
In a v2 we could use a ML model in order to improve the extraction.

There's few changes related to the UI in order to make the tool usable, but they're very basic
for the moment.
  • Loading branch information
calixteman committed Jan 17, 2025
1 parent 711bf2b commit d4acae6
Show file tree
Hide file tree
Showing 18 changed files with 884 additions and 9 deletions.
4 changes: 4 additions & 0 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"type": "string",
"default": ""
},
"enableSignatureEditor": {
"type": "boolean",
"default": false
},
"enableUpdatedAddImage": {
"type": "boolean",
"default": false
Expand Down
18 changes: 13 additions & 5 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { FreeTextEditor } from "./freetext.js";
import { HighlightEditor } from "./highlight.js";
import { InkEditor } from "./ink.js";
import { setLayerDimensions } from "../display_utils.js";
import { SignatureEditor } from "./signature.js";
import { StampEditor } from "./stamp.js";

/**
Expand Down Expand Up @@ -89,10 +90,13 @@ class AnnotationEditorLayer {
static _initialized = false;

static #editorTypes = new Map(
[FreeTextEditor, InkEditor, StampEditor, HighlightEditor].map(type => [
type._editorType,
type,
])
[
FreeTextEditor,
InkEditor,
StampEditor,
HighlightEditor,
SignatureEditor,
].map(type => [type._editorType, type])
);

/**
Expand Down Expand Up @@ -758,7 +762,11 @@ class AnnotationEditorLayer {
return;
}

if (this.#uiManager.getMode() === AnnotationEditorType.STAMP) {
const currentMode = this.#uiManager.getMode();
if (
currentMode === AnnotationEditorType.STAMP ||
currentMode === AnnotationEditorType.SIGNATURE
) {
this.#uiManager.unselectAll();
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/display/editor/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class DrawingEditor extends AnnotationEditor {
super(params);
this.#mustBeCommitted = params.mustBeCommitted || false;

this._addOutlines(params);
}

_addOutlines(params) {
if (params.drawOutlines) {
this.#createDrawOutlines(params);
this.#addToDrawLayer();
Expand Down
28 changes: 28 additions & 0 deletions src/display/editor/drawers/contour.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2024 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { InkDrawOutline } from "./inkdraw.js";

class ContourDrawOutline extends InkDrawOutline {
toSVGPath() {
let path = super.toSVGPath();
if (!path.endsWith("Z")) {
path += "Z";
}
return path;
}
}

export { ContourDrawOutline };
Loading

0 comments on commit d4acae6

Please sign in to comment.