263 lines
9 KiB
Rust
263 lines
9 KiB
Rust
//! GDI ile metni görüntüye çizer: harici motor olmadan
|
||
//! metin tabanlı PDF sayfalarını PNG'ye çevirmek ve belgelerden görüntü PDF üretmek için kullanılır.
|
||
//! Yalnız Windows'ta derlenir; diğer platformlarda derleyici bu modülü atlar.
|
||
|
||
#[cfg(windows)]
|
||
mod windows_impl {
|
||
use std::ffi::c_void;
|
||
|
||
use windows_sys::Win32::Foundation::{RECT, SIZE};
|
||
use windows_sys::Win32::Graphics::Gdi::{
|
||
CreateCompatibleDC, CreateDIBSection, CreateFontW, CreateSolidBrush, DeleteDC, DeleteObject,
|
||
FillRect, GetTextExtentPoint32W, SelectObject, SetBkMode, SetTextColor, TextOutW,
|
||
BITMAPINFOHEADER, BI_RGB, CLIP_DEFAULT_PRECIS, DEFAULT_CHARSET, DEFAULT_PITCH,
|
||
FW_NORMAL, OUT_DEFAULT_PRECIS, PROOF_QUALITY, TRANSPARENT,
|
||
};
|
||
|
||
use crate::error::{AppError, AppResult};
|
||
|
||
const FONT: &str = "Arial";
|
||
|
||
fn wide(text: &str) -> Vec<u16> {
|
||
text.encode_utf16().chain(std::iter::once(0)).collect()
|
||
}
|
||
|
||
/// Bir A4 (veya verilen ölçüde) sayfaya metni sığdırır; satırları kendi ölçerek böler.
|
||
pub struct TextRenderer {
|
||
hdc: *mut c_void,
|
||
bitmap: *mut c_void,
|
||
old_bitmap: *mut c_void,
|
||
font: *mut c_void,
|
||
old_font: *mut c_void,
|
||
width: i32,
|
||
height: i32,
|
||
bits: *mut u8,
|
||
brush: *mut c_void,
|
||
}
|
||
|
||
impl TextRenderer {
|
||
pub fn new(width: u32, height: u32, font_px: i32) -> AppResult<Self> {
|
||
unsafe {
|
||
let hdc = CreateCompatibleDC(std::ptr::null_mut());
|
||
if hdc.is_null() {
|
||
return Err(AppError::Engine("GDI bağlamı oluşturulamadı".to_string()));
|
||
}
|
||
|
||
let mut info: BITMAPINFOHEADER = std::mem::zeroed();
|
||
info.biSize = std::mem::size_of::<BITMAPINFOHEADER>() as u32;
|
||
info.biWidth = width as i32;
|
||
info.biHeight = -(height as i32); // top-down
|
||
info.biPlanes = 1;
|
||
info.biBitCount = 32;
|
||
info.biCompression = BI_RGB;
|
||
|
||
let mut bits: *mut c_void = std::ptr::null_mut();
|
||
let bitmap = CreateDIBSection(
|
||
hdc,
|
||
&info as *const BITMAPINFOHEADER as *const _,
|
||
0, // DIB_RGB_COLORS
|
||
&mut bits,
|
||
std::ptr::null_mut(),
|
||
0,
|
||
);
|
||
if bitmap.is_null() || bits.is_null() {
|
||
DeleteDC(hdc);
|
||
return Err(AppError::Engine("GDI bitmap oluşturulamadı".to_string()));
|
||
}
|
||
let old_bitmap = SelectObject(hdc, bitmap);
|
||
|
||
let face = wide(FONT);
|
||
let font = CreateFontW(
|
||
-font_px,
|
||
0,
|
||
0,
|
||
0,
|
||
FW_NORMAL as i32,
|
||
0,
|
||
0,
|
||
0,
|
||
DEFAULT_CHARSET as u32,
|
||
OUT_DEFAULT_PRECIS as u32,
|
||
CLIP_DEFAULT_PRECIS as u32,
|
||
PROOF_QUALITY as u32,
|
||
DEFAULT_PITCH as u32,
|
||
face.as_ptr(),
|
||
);
|
||
let old_font = if font.is_null() {
|
||
std::ptr::null_mut()
|
||
} else {
|
||
SelectObject(hdc, font)
|
||
};
|
||
|
||
SetBkMode(hdc, TRANSPARENT as i32);
|
||
SetTextColor(hdc, 0x000000); // siyah
|
||
|
||
let brush = CreateSolidBrush(0x00FF_FFFF); // beyaz
|
||
|
||
Ok(Self {
|
||
hdc,
|
||
bitmap,
|
||
old_bitmap,
|
||
font,
|
||
old_font,
|
||
width: width as i32,
|
||
height: height as i32,
|
||
bits: bits as *mut u8,
|
||
brush,
|
||
})
|
||
}
|
||
}
|
||
|
||
pub fn width(&self) -> i32 {
|
||
self.width
|
||
}
|
||
|
||
pub fn height(&self) -> i32 {
|
||
self.height
|
||
}
|
||
|
||
fn clear(&self) {
|
||
unsafe {
|
||
let rect = RECT {
|
||
left: 0,
|
||
top: 0,
|
||
right: self.width,
|
||
bottom: self.height,
|
||
};
|
||
FillRect(self.hdc, &rect, self.brush);
|
||
}
|
||
}
|
||
|
||
pub fn measure(&self, text: &str) -> i32 {
|
||
let utf16 = wide(text);
|
||
let mut size = SIZE { cx: 0, cy: 0 };
|
||
unsafe {
|
||
GetTextExtentPoint32W(
|
||
self.hdc,
|
||
utf16.as_ptr(),
|
||
utf16.len() as i32 - 1,
|
||
&mut size,
|
||
);
|
||
}
|
||
size.cx
|
||
}
|
||
|
||
pub fn draw_line(&self, text: &str, x: i32, y: i32) {
|
||
let utf16 = wide(text);
|
||
unsafe {
|
||
TextOutW(self.hdc, x, y, utf16.as_ptr(), utf16.len() as i32 - 1);
|
||
}
|
||
}
|
||
|
||
/// Bitmap'i RGB görüntüye dönüştürür (BGRA -> RGB).
|
||
pub fn to_image(&self) -> image::RgbImage {
|
||
let mut buffer = image::RgbImage::new(self.width as u32, self.height as u32);
|
||
unsafe {
|
||
let stride = self.width as usize * 4;
|
||
let raw = std::slice::from_raw_parts(self.bits, stride * self.height as usize);
|
||
for y in 0..self.height as usize {
|
||
for x in 0..self.width as usize {
|
||
let offset = y * stride + x * 4;
|
||
buffer.put_pixel(
|
||
x as u32,
|
||
y as u32,
|
||
image::Rgb([raw[offset + 2], raw[offset + 1], raw[offset]]),
|
||
);
|
||
}
|
||
}
|
||
}
|
||
buffer
|
||
}
|
||
}
|
||
|
||
impl Drop for TextRenderer {
|
||
fn drop(&mut self) {
|
||
unsafe {
|
||
if !self.old_font.is_null() {
|
||
SelectObject(self.hdc, self.old_font);
|
||
}
|
||
if !self.font.is_null() {
|
||
DeleteObject(self.font);
|
||
}
|
||
if !self.old_bitmap.is_null() {
|
||
SelectObject(self.hdc, self.old_bitmap);
|
||
}
|
||
if !self.bitmap.is_null() {
|
||
DeleteObject(self.bitmap);
|
||
}
|
||
if !self.brush.is_null() {
|
||
DeleteObject(self.brush);
|
||
}
|
||
DeleteDC(self.hdc);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Metni A4 sayfalara bölüp her sayfayı görüntü olarak döner (harici araç gerektirmez).
|
||
pub fn render_text_pages(
|
||
text: &str,
|
||
font_px: i32,
|
||
width: u32,
|
||
height: u32,
|
||
margin: i32,
|
||
) -> AppResult<Vec<image::RgbImage>> {
|
||
let renderer = TextRenderer::new(width, height, font_px)?;
|
||
let line_height = (font_px as f64 * 1.42).round() as i32;
|
||
let usable_width = width as i32 - margin * 2;
|
||
let lines_per_page = ((height as i32 - margin * 2) / line_height).max(1) as usize;
|
||
|
||
let mut lines: Vec<String> = Vec::new();
|
||
for raw_line in text.replace("\r\n", "\n").split('\n') {
|
||
let mut current = String::new();
|
||
for word in raw_line.split(' ') {
|
||
let candidate = if current.is_empty() {
|
||
word.to_string()
|
||
} else {
|
||
format!("{} {}", current, word)
|
||
};
|
||
if renderer.measure(&candidate) <= usable_width {
|
||
current = candidate;
|
||
} else {
|
||
if !current.is_empty() {
|
||
lines.push(std::mem::take(&mut current));
|
||
}
|
||
// Tek kelime satırdan uzunsa karakter bazında böl
|
||
if renderer.measure(word) > usable_width {
|
||
let mut chunk = String::new();
|
||
for ch in word.chars() {
|
||
let candidate = format!("{}{}", chunk, ch);
|
||
if renderer.measure(&candidate) > usable_width && !chunk.is_empty() {
|
||
lines.push(std::mem::take(&mut chunk));
|
||
}
|
||
chunk.push(ch);
|
||
}
|
||
current = chunk;
|
||
} else {
|
||
current = word.to_string();
|
||
}
|
||
}
|
||
}
|
||
lines.push(current);
|
||
}
|
||
|
||
if lines.is_empty() {
|
||
lines.push(String::new());
|
||
}
|
||
|
||
let mut pages = Vec::new();
|
||
for chunk in lines.chunks(lines_per_page) {
|
||
renderer.clear();
|
||
let mut y = margin;
|
||
for line in chunk {
|
||
renderer.draw_line(line, margin, y);
|
||
y += line_height;
|
||
}
|
||
pages.push(renderer.to_image());
|
||
}
|
||
|
||
Ok(pages)
|
||
}
|
||
}
|
||
|
||
#[cfg(windows)]
|
||
pub use windows_impl::{render_text_pages, TextRenderer};
|