rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! 축소 비트맵은 목표 장치 격자에서 한 번만 저역 통과시킨다.
//!
//! source/layout 좌표는 유지한다. 인쇄 paint의 장치 경계에서 준비한
//! 픽셀을 fractional destination에서 다시 보간하지 않는다.

use std::hash::{Hash, Hasher};
use std::io::Cursor;
use std::sync::Arc;

use crate::paint::RenderProfile;

use super::render_tree::BoundingBox;

mod cropped_window;
use cropped_window::{CacheKey, CACHE, CACHE_BYTES, CACHE_ENTRIES, MAX_PIXELS};

pub(crate) struct PreparedBitmap {
    pub bytes: Arc<Vec<u8>>,
    pub bbox: BoundingBox,
}

/// 논리 종이를 실제 정수 raster viewport에 투영하는 공통 장치 격자.
/// 배치 bbox는 바꾸지 않으며 쪽 바깥의 정수 이동도 같은 원점에서 보존한다.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PrintPaperGrid {
    factors: [u64; 2],
    origin: [u64; 2],
}

impl PrintPaperGrid {
    pub(crate) fn new(paper: [f64; 2], matrix: [f64; 6]) -> Option<Self> {
        let [a, b, c, d, e, f] = matrix;
        if b != 0.0
            || c != 0.0
            || a <= 0.0
            || d <= 0.0
            || e.fract() != 0.0
            || f.fract() != 0.0
            || !paper.into_iter().chain(matrix).all(f64::is_finite)
            || paper.into_iter().any(|size| size <= 0.0)
        {
            return None;
        }
        let extents = [paper[0] * a, paper[1] * d];
        if extents.iter().any(|size| !size.is_finite() || *size <= 0.0) {
            return None;
        }
        let factors = extents.map(|size| size.ceil() / size);
        if factors.iter().any(|factor| !factor.is_finite()) {
            return None;
        }
        Some(Self {
            factors: factors.map(f64::to_bits),
            origin: [e.to_bits(), f.to_bits()],
        })
    }

    fn project(self, [x, y, w, h]: [f64; 4]) -> [f64; 4] {
        let [sx, sy] = self.factors.map(f64::from_bits);
        let [ox, oy] = self.origin.map(f64::from_bits);
        [(x - ox) * sx + ox, (y - oy) * sy + oy, w * sx, h * sy]
    }
}

/// `bbox`는 crop/채움/안여백을 해석한 최종 paint 사각형이다. 실제로 잘린
/// source 사각형은 이 함수에 넘기지 않는다. 실패/비대상은 기존 paint로 돌아간다.
pub(crate) fn prepare_bitmap(
    bytes: &[u8],
    bbox: BoundingBox,
    matrix: [f64; 6],
    profile: RenderProfile,
    paper_grid: Option<PrintPaperGrid>,
) -> Option<PreparedBitmap> {
    // 이미 최종 격자에 합성된 WMF는 재표본화하지 않는다. 중간 장치 표면은
    // 일반 출력 장치 축소 경로에서 처리한다.
    if super::image_resolver::png_sampling::metafile_surface(bytes) {
        return None;
    }
    let [a, b, c, d, e, f] = matrix;
    if !matches!(profile, RenderProfile::Print | RenderProfile::HighQuality)
        || b != 0.0
        || c != 0.0
        || a <= 0.0
        || d <= 0.0
        || bbox.width <= 0.0
        || bbox.height <= 0.0
        || !matrix
            .into_iter()
            .chain([bbox.x, bbox.y, bbox.width, bbox.height])
            .all(f64::is_finite)
    {
        return None;
    }
    let print = profile == RenderProfile::Print;
    // Print 그림은 장치 픽셀의 덮이는 구간 전체를 소유한다. 최근접 경계로
    // 먼저 줄이면 소수 원점에 따라 가는 선의 source 구간까지 달라진다.
    let lower = |x: f64| if print { x.floor() } else { x.round() };
    let upper = |x: f64| if print { x.floor() + 1.0 } else { x.round() };
    let rectangle = [
        a * bbox.x + e,
        d * bbox.y + f,
        a * bbox.width,
        d * bbox.height,
    ];
    let rectangle = if print {
        paper_grid.map_or(rectangle, |grid| grid.project(rectangle))
    } else {
        rectangle
    };
    let [x, y, w, h] = rectangle;
    let left = lower(x);
    let right = upper(x + w);
    let top = lower(y);
    let bottom = upper(y + h);
    let (width, height) = (right - left, bottom - top);
    if ![left, right, top, bottom, width, height]
        .into_iter()
        .all(f64::is_finite)
        || width < 1.0
        || height < 1.0
        || width > u32::MAX as f64
        || height > u32::MAX as f64
        || width * height > MAX_PIXELS as f64
    {
        return None;
    }
    let (width, height) = (width as u32, height as u32);
    let dimensions = image::ImageReader::new(Cursor::new(bytes))
        .with_guessed_format()
        .ok()?
        .into_dimensions()
        .ok()?;
    // 얕은 축소는 선언된 Print 재구성 경로가 표본을 소유한다.
    // 더 큰 축소는 기존 장치 구간 평균을 유지한다.
    if super::image_resolver::png_sampling::print_bitmap_reconstruction(bytes)
        && super::cubic_sampling::modest_reduction(
            (f64::from(dimensions.0), f64::from(dimensions.1)),
            (w, h),
        )
    {
        return None;
    }
    if width >= dimensions.0
        || height >= dimensions.1
        || u64::from(dimensions.0) * u64::from(dimensions.1) > MAX_PIXELS
    {
        return None;
    }
    let mut hash = std::collections::hash_map::DefaultHasher::new();
    bytes.hash(&mut hash);
    // 투명 그림의 coverage는 source의 소수 원점과 실제 장치 배율에도
    // 의존한다. 같은 정수 출력 크기라는 이유로 다른 원점의 표본을 재사용하지 않는다.
    let device_rect = [x - left, y - top, w, h];
    let key = CacheKey {
        source_hash: hash.finish(),
        source_len: bytes.len(),
        width,
        height,
        print,
        device_rect: device_rect.map(f64::to_bits),
        source_window: None,
    };
    let png = CACHE.with(|cache| cache.borrow().entries.get(&key).cloned());
    let png = match png {
        Some(png) => png,
        None => {
            let mut reader = image::ImageReader::new(Cursor::new(bytes))
                .with_guessed_format()
                .ok()?;
            let mut limits = image::Limits::default();
            limits.max_alloc = Some(MAX_PIXELS * 4);
            reader.limits(limits);
            let source = reader.decode().ok()?;
            let transparent = source.color().has_alpha()
                && source.to_rgba8().pixels().any(|pixel| pixel.0[3] != 255);
            // 장치 픽셀이 덮는 원본 구간의 면적을 적분한다. 원본 픽셀을
            // 정수 경계 한쪽에만 배정하면 비정수 축소에서 가는 선의 밝기가
            // 구간 위상에 따라 달라진다. 두 축의 겹치는 비율을 함께 소비한다.
            let resized = if transparent {
                // Print의 투명 표본은 실제 소수 장치 사각형에서 coverage를
                // 적분한다. 숨은 RGB를 먼저 섞거나 투명 가장자리를 정수 box에
                // 늘리지 않는다. 다른 프로필의 기존 합성은 그대로 둔다.
                if !print {
                    return None;
                }
                image::DynamicImage::ImageRgba8(transparent_area_downsample(
                    &source.to_rgba8(),
                    width,
                    height,
                    device_rect,
                )?)
            } else if print {
                image::DynamicImage::ImageRgb8(opaque_area_downsample(
                    &source.to_rgb8(),
                    width,
                    height,
                ))
            } else {
                source.resize_exact(width, height, image::imageops::FilterType::Lanczos3)
            };
            let mut output = Cursor::new(Vec::new());
            resized
                .write_to(&mut output, image::ImageFormat::Png)
                .ok()?;
            let png = Arc::new(output.into_inner());
            if png.len() <= CACHE_BYTES {
                CACHE.with(|cache| {
                    let mut cache = cache.borrow_mut();
                    if cache.entries.len() >= CACHE_ENTRIES || cache.bytes + png.len() > CACHE_BYTES
                    {
                        cache.entries.clear();
                        cache.bytes = 0;
                    }
                    cache.bytes += png.len();
                    cache.entries.insert(key, png.clone());
                });
            }
            png
        }
    };
    Some(PreparedBitmap {
        bytes: png,
        bbox: BoundingBox::new(
            (left - e) / a,
            (top - f) / d,
            f64::from(width) / a,
            f64::from(height) / d,
        ),
    })
}

/// 공통 정수 격자로 구간의 겹침을 표현해 부동소수점 경계 오차를 피한다.
fn area_span(index: u32, source: u32, target: u32) -> impl Iterator<Item = (u32, u64)> + Clone {
    let source = u64::from(source);
    let target = u64::from(target);
    let start = u64::from(index) * source;
    let end = start + source;
    (start / target..end.div_ceil(target)).map(move |pixel| {
        let overlap = end.min((pixel + 1) * target) - start.max(pixel * target);
        (pixel as u32, overlap)
    })
}

fn opaque_area_downsample(source: &image::RgbImage, width: u32, height: u32) -> image::RgbImage {
    let mut horizontal = image::RgbImage::new(width, source.height());
    for y in 0..source.height() {
        for x in 0..width {
            let mut sum = [0u64; 3];
            for (source_x, weight) in area_span(x, source.width(), width) {
                let pixel = source.get_pixel(source_x, y);
                for channel in 0..3 {
                    sum[channel] += u64::from(pixel[channel]) * weight;
                }
            }
            horizontal.put_pixel(
                x,
                y,
                image::Rgb(sum.map(|value| (value / u64::from(source.width())) as u8)),
            );
        }
    }
    let mut result = image::RgbImage::new(width, height);
    for y in 0..height {
        let span = area_span(y, source.height(), height);
        for x in 0..width {
            let mut sum = [0u64; 3];
            for (source_y, weight) in span.clone() {
                let pixel = horizontal.get_pixel(x, source_y);
                for channel in 0..3 {
                    sum[channel] += u64::from(pixel[channel]) * weight;
                }
            }
            result.put_pixel(
                x,
                y,
                image::Rgb(sum.map(|value| (value / u64::from(source.height())) as u8)),
            );
        }
    }
    result
}

fn transparent_area_downsample(
    source: &image::RgbaImage,
    width: u32,
    height: u32,
    device_rect: [f64; 4],
) -> Option<image::RgbaImage> {
    let [x, y, w, h] = device_rect;
    let sx = f64::from(source.width()) / w;
    let sy = f64::from(source.height()) / h;
    let area = sx * sy;
    if !device_rect
        .into_iter()
        .chain([sx, sy, area])
        .all(f64::is_finite)
        || w <= 0.0
        || h <= 0.0
        || area <= 0.0
    {
        return None;
    }
    let spans = |count: u32, source: u32, origin: f64, scale: f64| {
        (0..count)
            .map(|i| {
                let first = (f64::from(i) - origin) * scale;
                let end = first + scale;
                (
                    first.floor().clamp(0.0, f64::from(source)) as u32,
                    end.ceil().clamp(0.0, f64::from(source)) as u32,
                    first,
                    end,
                )
            })
            .collect::<Vec<_>>()
    };
    let xs = spans(width, source.width(), x, sx);
    let ys = spans(height, source.height(), y, sy);
    let mut output = image::RgbaImage::new(width, height);
    for (y, &(top, bottom, y0, y1)) in ys.iter().enumerate() {
        for (x, &(left, right, x0, x1)) in xs.iter().enumerate() {
            let mut alpha = 0.0;
            let mut color = [0.0; 3];
            for iy in top..bottom {
                let wy = (f64::from(iy + 1).min(y1) - f64::from(iy).max(y0)).max(0.0);
                for ix in left..right {
                    let wx = (f64::from(ix + 1).min(x1) - f64::from(ix).max(x0)).max(0.0);
                    let pixel = source.get_pixel(ix, iy);
                    let weight = wx * wy * f64::from(pixel[3]);
                    alpha += weight;
                    for channel in 0..3 {
                        color[channel] += f64::from(pixel[channel]) * weight;
                    }
                }
            }
            let rgb = color.map(|value| {
                if alpha > 0.0 {
                    (value / alpha).round().clamp(0.0, 255.0) as u8
                } else {
                    0
                }
            });
            output.put_pixel(
                x as u32,
                y as u32,
                image::Rgba([
                    rgb[0],
                    rgb[1],
                    rgb[2],
                    (alpha / area).round().clamp(0.0, 255.0) as u8,
                ]),
            );
        }
    }
    Some(output)
}

/// 잘린 원본 창도 최종 Print 장치 격자에서 한 번만 축소한다.
/// 큰 축소의 불투명 bitmap만 받으며 crop 밖 표본·명시적 최근접·벡터는 재해석하지 않는다.
pub(crate) fn prepare_cropped_bitmap(
    bytes: &[u8],
    bbox: BoundingBox,
    matrix: [f64; 6],
    profile: RenderProfile,
    paper_grid: Option<PrintPaperGrid>,
    source_window: [f64; 4],
) -> Option<PreparedBitmap> {
    let [a, b, c, d, e, f] = matrix;
    let [sx, sy, sw, sh] = source_window;
    if profile != RenderProfile::Print
        || b != 0.0
        || c != 0.0
        || a <= 0.0
        || d <= 0.0
        || bbox.width <= 0.0
        || bbox.height <= 0.0
        || sx < 0.0
        || sy < 0.0
        || sw <= 0.0
        || sh <= 0.0
        || !matrix
            .into_iter()
            .chain(source_window)
            .chain([bbox.x, bbox.y, bbox.width, bbox.height])
            .all(f64::is_finite)
        || super::image_resolver::png_sampling::metafile_surface(bytes)
        || super::image_resolver::png_sampling::print_jpeg_transfer(bytes)
        || super::image_resolver::png_sampling::print_bitmap_reconstruction(bytes)
    {
        return None;
    }
    let rectangle = [
        a * bbox.x + e,
        d * bbox.y + f,
        a * bbox.width,
        d * bbox.height,
    ];
    let [x, y, w, h] = paper_grid.map_or(rectangle, |grid| grid.project(rectangle));
    let left = x.floor();
    let top = y.floor();
    let width = (x + w).floor() + 1.0 - left;
    let height = (y + h).floor() + 1.0 - top;
    // 선형 필터가 충분한 작은 축소와 확대는 기존 backend 경로가 소유한다.
    if ![left, top, width, height].into_iter().all(f64::is_finite)
        || width < 1.0
        || height < 1.0
        || width * height > MAX_PIXELS as f64
        || width >= sw
        || height >= sh
        || (sw <= w * 2.0 && sh <= h * 2.0)
    {
        return None;
    }
    let (width, height) = (width as u32, height as u32);
    let png = cropped_window::prepare(
        bytes,
        width,
        height,
        [x - left, y - top, w, h],
        source_window,
    )?;
    Some(PreparedBitmap {
        bytes: png,
        bbox: BoundingBox::new(
            (left - e) / a,
            (top - f) / d,
            f64::from(width) / a,
            f64::from(height) / d,
        ),
    })
}