1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
export const createCustomWatermark: CreateCustomWatermark = ({
text,
fontSize = 14,
color = "rgba(128, 128, 128, 0.3)", // 더 연한 회색, 더 투명하게
opacity = 30, // 더 낮은 opacity
rotation = -45,
fontFamily = "Arial",
}) => {
return (ctx, pageNumber, pageWidth, pageHeight) => {
if (!text) return;
const lines = text.split("\n");
ctx.save();
// 전체 opacity 설정
ctx.globalAlpha = opacity / 100;
// 텍스트 스타일 설정
ctx.fillStyle = color;
ctx.font = `${fontSize}px ${fontFamily}`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
// 텍스트 크기 계산
const maxLineWidth = Math.max(...lines.map(line =>
ctx.measureText(line).width
));
const lineHeight = fontSize * 1.2;
const textBlockHeight = lines.length * lineHeight;
// 격자 간격 계산 (텍스트 블록 크기 기반)
const horizontalSpacing = maxLineWidth * 0.8; // 가로 간격
const verticalSpacing = textBlockHeight * 1.5; // 세로 간격
// 회전을 고려한 대각선 길이 계산
const diagonal = Math.sqrt(pageWidth * pageWidth + pageHeight * pageHeight);
const rotationRad = (rotation * Math.PI) / 180;
// 시작 위치 계산 (페이지 밖에서 시작하여 전체 커버)
const startX = -diagonal / 2;
const startY = -diagonal / 2;
const endX = diagonal / 2;
const endY = diagonal / 2;
// 격자 패턴으로 워터마크 그리기
for (let x = startX; x <= endX; x += horizontalSpacing) {
for (let y = startY; y <= endY; y += verticalSpacing) {
ctx.save();
// 중심점으로 이동
ctx.translate(pageWidth / 2, pageHeight / 2);
// 회전 적용
ctx.rotate(rotationRad);
// 현재 위치로 이동
ctx.translate(x, y);
// 각 줄 그리기
let yOffset = -(lines.length - 1) * lineHeight / 2;
lines.forEach((line) => {
ctx.fillText(line, 0, yOffset);
yOffset += lineHeight;
});
ctx.restore();
}
}
ctx.restore();
};
};
// 대안 1: 더 촘촘한 격자 패턴 (보안 강화)
export const createDenseGridWatermark: CreateCustomWatermark = ({
text,
fontSize = 12, // 더 작은 폰트
color = "rgba(150, 150, 150, 0.08)", // 매우 연한 색상
opacity = 8,
rotation = -45,
fontFamily = "Arial",
}) => {
return (ctx, pageNumber, pageWidth, pageHeight) => {
if (!text) return;
const lines = text.split("\n");
ctx.save();
ctx.globalAlpha = opacity / 100;
ctx.fillStyle = color;
ctx.font = `${fontSize}px ${fontFamily}`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
// 더 촘촘한 간격
const horizontalSpacing = 120; // 고정 간격
const verticalSpacing = 80;
const diagonal = Math.sqrt(pageWidth * pageWidth + pageHeight * pageHeight);
const rotationRad = (rotation * Math.PI) / 180;
for (let x = -diagonal; x <= diagonal; x += horizontalSpacing) {
for (let y = -diagonal; y <= diagonal; y += verticalSpacing) {
ctx.save();
ctx.translate(pageWidth / 2 + x, pageHeight / 2 + y);
ctx.rotate(rotationRad);
let yOffset = 0;
lines.forEach((line) => {
ctx.fillText(line, 0, yOffset);
yOffset += fontSize * 1.2;
});
ctx.restore();
}
}
ctx.restore();
};
};
// 대안 2: 랜덤 위치 워터마크 (패턴 예측 방지)
export const createRandomizedWatermark: CreateCustomWatermark = ({
text,
fontSize = 14,
color = "rgba(140, 140, 140, 0.1)",
opacity = 10,
rotation = -45,
fontFamily = "Arial",
}) => {
return (ctx, pageNumber, pageWidth, pageHeight) => {
if (!text) return;
const lines = text.split("\n");
ctx.save();
ctx.globalAlpha = opacity / 100;
ctx.fillStyle = color;
ctx.font = `${fontSize}px ${fontFamily}`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
// 페이지 번호를 시드로 사용하여 일관된 랜덤 생성
const seed = pageNumber * 1000;
const random = (min: number, max: number, index: number) => {
const x = Math.sin(seed + index) * 10000;
return min + (x - Math.floor(x)) * (max - min);
};
// 워터마크 개수 (페이지 크기에 비례)
const count = Math.floor((pageWidth * pageHeight) / 40000); // 조정 가능
for (let i = 0; i < count; i++) {
const x = random(0, pageWidth, i * 2);
const y = random(0, pageHeight, i * 2 + 1);
const rotationVariation = random(-5, 5, i * 3); // ±5도 변화
ctx.save();
ctx.translate(x, y);
ctx.rotate((rotation + rotationVariation) * Math.PI / 180);
let yOffset = 0;
lines.forEach((line) => {
ctx.fillText(line, 0, yOffset);
yOffset += fontSize * 1.2;
});
ctx.restore();
}
ctx.restore();
};
};
|