1
|
import { Injectable } from '@angular/core';
|
2
|
import {LoggerService} from '../logger/logger.service';
|
3
|
import {TextRegion} from '../../model/parts/TextRegion';
|
4
|
import {Point} from '../../model/parts/Point';
|
5
|
import Utils from '../../../Utils';
|
6
|
|
7
|
@Injectable({
|
8
|
providedIn: 'root'
|
9
|
})
|
10
|
export class DrawerService {
|
11
|
|
12
|
constructor(private LOGGER: LoggerService) { }
|
13
|
|
14
|
public drawResult(textRegion: TextRegion, imageId: string, canvasId: string) {
|
15
|
const canvasContext = this.initCanvas(imageId, canvasId);
|
16
|
|
17
|
if (textRegion !== null) {
|
18
|
if (Utils.isIterable(textRegion.textLines) && textRegion.textLines.length !== 0) {
|
19
|
for (const textLine of textRegion.textLines) {
|
20
|
if (Utils.isIterable(textLine.textWords) && textLine.textWords.length !== 0) {
|
21
|
for (const textWord of textLine.textWords) {
|
22
|
if (textWord !== null) {
|
23
|
// print words
|
24
|
this.draw(textWord.wordCoords.points, canvasContext);
|
25
|
this.LOGGER.info('printing words');
|
26
|
}
|
27
|
}
|
28
|
return;
|
29
|
} else {
|
30
|
// print lines
|
31
|
this.draw(textLine.lineCoords.points, canvasContext);
|
32
|
this.LOGGER.info('Print lines');
|
33
|
}
|
34
|
}
|
35
|
return;
|
36
|
} else {
|
37
|
// all region
|
38
|
this.draw(textRegion.regionCoords.points, canvasContext);
|
39
|
this.LOGGER.info('Print region');
|
40
|
}
|
41
|
} else {
|
42
|
// all document
|
43
|
// TODO : obarvit celý výřez
|
44
|
this.LOGGER.warn('Print all document');
|
45
|
}
|
46
|
}
|
47
|
|
48
|
private initCanvas(imageId: string, canvasId: string) {
|
49
|
const img = document.getElementById(imageId);
|
50
|
const canvasElement = document.getElementById(canvasId);
|
51
|
const canvas = <HTMLCanvasElement> document.getElementById(canvasId);
|
52
|
|
53
|
const absolutePosition = Utils.getAbsolutePositionOfElement(document.getElementById(imageId));
|
54
|
|
55
|
canvasElement.style.display = 'block';
|
56
|
canvasElement.style.left = absolutePosition.left + 'px';
|
57
|
canvasElement.style.top = absolutePosition.top + 'px';
|
58
|
|
59
|
const ctx = canvas.getContext('2d');
|
60
|
ctx.canvas.width = img.offsetWidth;
|
61
|
ctx.canvas.height = img.offsetHeight;
|
62
|
|
63
|
return ctx;
|
64
|
}
|
65
|
|
66
|
private draw(points: Point[], context: CanvasRenderingContext2D) {
|
67
|
let firstPoint = true;
|
68
|
context.beginPath();
|
69
|
|
70
|
for (const point of points) {
|
71
|
if (firstPoint) {
|
72
|
context.moveTo(point.x / 1.676, point.y / 1.676);
|
73
|
firstPoint = false;
|
74
|
} else {
|
75
|
context.lineTo(point.x / 1.676, point.y / 1.676);
|
76
|
}
|
77
|
}
|
78
|
context.closePath();
|
79
|
|
80
|
context.lineWidth = 2;
|
81
|
context.strokeStyle = '#ff253b';
|
82
|
context.fillStyle = 'hsla(14, 100%, 53%, 0.3)';
|
83
|
context.fill();
|
84
|
context.stroke();
|
85
|
}
|
86
|
}
|