1#![no_std]
24#![forbid(unsafe_code)]
25
26#[cfg(jflib_module)]
28extern crate modrt as rt;
29
30extern crate alloc;
31
32pub mod blend;
33pub mod blendmode;
34pub mod dash;
35pub mod flood;
36pub mod gradient;
37pub mod path;
38mod panel;
39pub mod pathdata;
40pub mod raster;
41pub mod shapes;
42pub mod straight;
43pub mod stroke;
44pub mod tmask;
45pub mod xform;
46
47use alloc::vec;
48use alloc::vec::Vec;
49
50pub use rt::fx::div255;
51
52pub const FMT_XRGB: u8 = 0; pub const FMT_PARGB: u8 = 1; pub const FMT_A8: u8 = 2; pub const FMT_ARGB: u8 = 3; pub const SFF_OWNED: u8 = 1;
57pub const SFF_OPAQUE: u8 = 2; pub const FX_ONE: i32 = 256; pub const RULE_NONZERO: u32 = 0;
60pub const RULE_EVENODD: u32 = 1;
61pub const JOIN_MITER: u32 = 0; pub const JOIN_ROUND: u32 = 1;
63pub const JOIN_BEVEL: u32 = 2;
64pub const CAP_BUTT: u32 = 0;
65pub const CAP_ROUND: u32 = 1;
66pub const CAP_SQUARE: u32 = 2;
67pub const ERR_FAIL: i32 = -1;
68pub const ERR_NOMEM: i32 = -8;
69pub const ERR_LIMIT: i32 = -15;
70const SURF_MAX: i32 = 65536; pub const COPY_PAR_MIN: usize = 1 << 20;
74pub const COPY_STREAM_MIN: usize = 12 << 20;
79const COPY_BAND: usize = 256 * 1024;
81
82pub type ParallelFor = fn(usize, &(dyn Fn(usize) + Sync));
84
85static PAR: rt::sync::SpinLock<Option<ParallelFor>> = rt::sync::SpinLock::new(None);
86
87pub fn set_parallel(f: ParallelFor) {
91 *PAR.lock() = Some(f);
92}
93
94pub fn parallel() -> Option<ParallelFor> {
96 *PAR.lock()
97}
98
99#[allow(clippy::too_many_arguments)]
103fn copy_rows(dst: &mut [u8], ds: usize, dy: usize, dx: usize, src: &[u8], ss: usize, sy: usize, sx: usize, n: usize, rows: usize) {
104 let bytes = rows.saturating_mul(n);
105 let stream = bytes >= COPY_STREAM_MIN;
106 let copy = |d: &mut [u8], s: &[u8]| {
107 if stream {
108 blend::copy_stream(d, s);
109 } else {
110 d.copy_from_slice(s);
111 }
112 };
113 let par = if bytes >= COPY_PAR_MIN && rows >= 2 { parallel() } else { None };
114 let Some(pf) = par else {
115 for r in 0..rows {
116 let (d, s) = ((dy + r) * ds + dx, (sy + r) * ss + sx);
117 copy(&mut dst[d..d + n], &src[s..s + n]);
118 }
119 if stream {
120 blend::stream_fence();
121 }
122 return;
123 };
124 let bh = rows.div_ceil((bytes / COPY_BAND).clamp(2, rows));
126 let chunks: Vec<rt::sync::SpinLock<&mut [u8]>> = dst[dy * ds..].chunks_mut(bh * ds).take(rows.div_ceil(bh)).map(rt::sync::SpinLock::new).collect();
127 pf(chunks.len(), &|i| {
128 let Some(cell) = chunks.get(i) else { return };
129 let mut c = cell.lock();
130 for r in i * bh..((i + 1) * bh).min(rows) {
131 let (d, s) = ((r - i * bh) * ds + dx, (sy + r) * ss + sx);
132 copy(&mut c[d..d + n], &src[s..s + n]);
133 }
134 if stream {
135 blend::stream_fence(); }
137 });
138}
139
140#[derive(Default)]
142pub struct Surface {
143 pub px: Vec<u8>,
144 pub w: i32,
145 pub h: i32,
146 pub stride: i32,
147 pub fmt: u8,
148 pub flags: u8,
149 pub opac: u8, pub blend: u8, }
152
153pub fn try_zeroed(n: usize) -> Option<Vec<u8>> {
157 let mut probe: Vec<u8> = Vec::new();
158 probe.try_reserve_exact(n).ok()?;
159 drop(probe);
160 Some(vec![0u8; n])
161}
162
163impl Surface {
164 pub fn new(w: i32, h: i32, fmt: u8) -> Surface {
168 let Some((stride, bytes)) = Surface::layout(w, h, fmt) else {
169 return Surface { px: Vec::new(), w: 0, h: 0, stride: 0, fmt, flags: 0, opac: 255, blend: 0 };
170 };
171 Surface { px: vec![0u8; bytes], w, h, stride, fmt, flags: SFF_OWNED, opac: 255, blend: 0 }
172 }
173
174 pub fn try_new(w: i32, h: i32, fmt: u8) -> Option<Surface> {
178 let (stride, bytes) = Surface::layout(w, h, fmt)?;
179 Some(Surface { px: try_zeroed(bytes)?, w, h, stride, fmt, flags: SFF_OWNED, opac: 255, blend: 0 })
180 }
181
182 fn layout(w: i32, h: i32, fmt: u8) -> Option<(i32, usize)> {
184 if !(0..=SURF_MAX).contains(&w) || !(0..=SURF_MAX).contains(&h) {
185 return None;
186 }
187 let row = if fmt == FMT_A8 { w } else { w * 4 };
188 let stride = (row + 63) & !63;
189 let bytes = (stride as usize).checked_mul(h as usize).filter(|&n| n <= isize::MAX as usize)?;
190 Some((stride, bytes.max(64)))
191 }
192
193 pub fn new_a8_packed(w: i32, h: i32) -> Surface {
195 Surface { px: vec![0u8; (w * h) as usize], w, h, stride: w, fmt: FMT_A8, flags: SFF_OWNED, opac: 255, blend: 0 }
196 }
197
198 pub fn resize(&mut self, w: i32, h: i32) {
200 *self = Surface::new(w, h, self.fmt);
201 }
202
203 pub fn bpp(&self) -> usize {
205 if self.fmt == FMT_A8 { 1 } else { 4 }
206 }
207}
208
209#[derive(Clone, Copy)]
211struct Saved {
212 w: i32,
213 h: i32,
214 clip: [i32; 4],
215 csp: usize,
216 ox: i32,
217 oy: i32,
218 opac: u32,
219 row0: i32,
220}
221
222pub struct Dc {
225 pub clip: [i32; 4],
226 clip_stack: Vec<[i32; 4]>,
227 targets: Vec<Saved>,
228 pub ox: i32,
229 pub oy: i32,
230 pub w: i32,
231 pub h: i32,
232 pub color: u32,
233 pub opac: u32,
234 pub pcol: [u32; 4], pub pcol32: u32,
236 pub ia: u32, pub row0: i32,
241}
242
243impl Dc {
244 pub fn new(w: i32, h: i32) -> Dc {
246 let mut dc = Dc {
247 clip: [0, 0, w, h],
248 clip_stack: Vec::new(),
249 targets: Vec::new(),
250 ox: 0,
251 oy: 0,
252 w,
253 h,
254 color: 0,
255 opac: 255,
256 pcol: [0; 4],
257 pcol32: 0,
258 ia: 255,
259 row0: 0,
260 };
261 dc.set_color(0xFF00_0000);
262 dc
263 }
264
265 pub fn begin(&mut self, sf: &Surface) {
268 self.begin_rows(sf, 0, sf.h);
269 }
270
271 pub fn begin_rows(&mut self, sf: &Surface, row0: i32, h: i32) {
275 self.targets.push(Saved {
276 w: self.w,
277 h: self.h,
278 clip: self.clip,
279 csp: self.clip_stack.len(),
280 ox: self.ox,
281 oy: self.oy,
282 opac: self.opac,
283 row0: self.row0,
284 });
285 self.w = sf.w;
286 self.h = h;
287 self.ox = 0;
288 self.oy = 0;
289 self.row0 = row0;
290 let y1 = row0.saturating_add(sf.h).min(h);
291 self.clip = [0, row0.clamp(0, y1.max(0)), sf.w, y1.max(0)];
292 }
293
294 pub fn end(&mut self) {
296 let Some(s) = self.targets.pop() else { return };
297 self.w = s.w;
298 self.h = s.h;
299 self.clip = s.clip;
300 self.clip_stack.truncate(s.csp);
301 self.ox = s.ox;
302 self.oy = s.oy;
303 self.row0 = s.row0;
304 self.clamp_clip();
305 self.set_opacity(s.opac);
306 }
307
308 pub fn set_color(&mut self, argb: u32) {
310 self.color = argb;
311 let a = div255((argb >> 24) * self.opac);
312 self.ia = 255 - a;
313 self.pcol[3] = a;
314 let mut packed = a << 24;
315 for ch in 0..3 {
316 let c = (argb >> (ch * 8)) & 255;
317 let p = div255(c * a);
318 self.pcol[ch] = p;
319 packed |= p << (ch * 8);
320 }
321 self.pcol32 = packed;
322 }
323
324 pub fn get_color(&self) -> u32 {
325 self.color
326 }
327
328 pub fn set_opacity(&mut self, op: u32) {
330 self.opac = op & 255;
331 let c = self.color;
332 self.set_color(c);
333 }
334
335 pub fn origin(&mut self, x: i32, y: i32) {
336 self.ox = x;
337 self.oy = y;
338 }
339
340 pub fn translate(&mut self, dx: i32, dy: i32) {
341 self.ox = self.ox.wrapping_add(dx);
342 self.oy = self.oy.wrapping_add(dy);
343 }
344
345 pub fn clip_push(&mut self, x: i32, y: i32, w: i32, h: i32) {
347 self.clip_stack.push(self.clip);
348 let x = x.wrapping_add(self.ox);
349 let y = y.wrapping_add(self.oy);
350 let x0 = x.max(self.clip[0]);
351 let y0 = y.max(self.clip[1]);
352 let x1 = x.wrapping_add(w).min(self.clip[2]).max(x0); let y1 = y.wrapping_add(h).min(self.clip[3]).max(y0);
354 self.clip = [x0, y0, x1, y1];
355 }
356
357 pub fn clip_pop(&mut self) {
358 if let Some(c) = self.clip_stack.pop() {
359 self.clip = c;
360 self.clamp_clip();
361 }
362 }
363
364 fn clamp_clip(&mut self) {
366 let x0 = self.clip[0].max(0).min(self.w);
367 let x1 = self.clip[2].min(self.w).max(x0);
368 let y0 = self.clip[1].max(0).min(self.h);
369 let y1 = self.clip[3].min(self.h).max(y0);
370 self.clip = [x0, y0, x1, y1];
371 }
372
373 pub fn clip_rect(&self, x: i32, y: i32, w: i32, h: i32) -> Option<[i32; 4]> {
375 let x = x.wrapping_add(self.ox);
376 let y = y.wrapping_add(self.oy);
377 let x0 = x.max(self.clip[0]);
378 let y0 = y.max(self.clip[1]);
379 let x1 = x.wrapping_add(w).min(self.clip[2]);
380 let y1 = y.wrapping_add(h).min(self.clip[3]);
381 if x1 <= x0 || y1 <= y0 { None } else { Some([x0, y0, x1, y1]) }
382 }
383
384 pub fn fill(&self, sf: &mut Surface, x: i32, y: i32, w: i32, h: i32) {
386 let Some([x0, y0, x1, y1]) = self.clip_rect(x, y, w, h) else { return };
387 let stride = sf.stride as usize;
388 if sf.fmt == FMT_A8 {
389 let a = (self.pcol32 >> 24) as u8;
390 for yy in y0..y1 {
391 let o = (yy - self.row0) as usize * stride;
392 sf.px[o + x0 as usize..o + x1 as usize].fill(a);
393 }
394 return;
395 }
396 if self.ia != 0 && self.pcol32 == 0 {
397 return; }
399 for yy in y0..y1 {
400 let o = (yy - self.row0) as usize * stride + x0 as usize * 4;
401 let row = &mut sf.px[o..o + (x1 - x0) as usize * 4];
402 if self.ia == 0 {
403 blend::fill_solid(row, self.pcol32);
404 } else {
405 blend::fill_over(row, &self.pcol, self.ia);
406 }
407 }
408 }
409
410 pub fn clear(&self, sf: &mut Surface, argb: u32) {
412 let [x0, y0, x1, y1] = self.clip;
413 if x1 <= x0 || y1 <= y0 {
414 return;
415 }
416 let a = argb >> 24;
417 let mut c = argb;
418 if a != 255 {
419 c = argb & 0xFF00_0000;
420 for ch in 0..3 {
421 c |= div255(((argb >> (ch * 8)) & 255) * a) << (ch * 8);
422 }
423 }
424 let stride = sf.stride as usize;
425 for yy in y0..y1 {
426 let o = (yy - self.row0) as usize * stride;
427 if sf.fmt == FMT_A8 {
428 sf.px[o + x0 as usize..o + x1 as usize].fill((c >> 24) as u8);
429 } else {
430 blend::fill_solid(&mut sf.px[o + x0 as usize * 4..o + x1 as usize * 4], c);
431 }
432 }
433 }
434
435 pub fn hline(&self, sf: &mut Surface, x: i32, y: i32, w: i32) {
436 self.fill(sf, x, y, w, 1);
437 }
438
439 pub fn vline(&self, sf: &mut Surface, x: i32, y: i32, h: i32) {
440 self.fill(sf, x, y, 1, h);
441 }
442
443 pub fn frame(&self, sf: &mut Surface, x: i32, y: i32, w: i32, h: i32) {
445 if w <= 0 || h <= 0 {
446 return;
447 }
448 self.hline(sf, x, y, w);
449 if h == 1 {
450 return;
451 }
452 self.hline(sf, x, y + h - 1, w);
453 if h == 2 {
454 return;
455 }
456 self.vline(sf, x, y + 1, h - 2);
457 if w == 1 {
458 return;
459 }
460 self.vline(sf, x + w - 1, y + 1, h - 2);
461 }
462
463 pub fn blit(&self, sf: &mut Surface, src: &Surface, x: i32, y: i32) {
466 if sf.fmt == FMT_A8 || src.fmt == FMT_A8 {
467 return;
468 }
469 let Some([x0, y0, x1, y1]) = self.clip_rect(x, y, src.w, src.h) else { return };
470 let sx = (x0 - x.wrapping_add(self.ox)) as usize;
471 let sy = (y0 - y.wrapping_add(self.oy)) as usize;
472 let n = (x1 - x0) as usize;
473 let (ds, ss) = (sf.stride as usize, src.stride as usize);
474 if src.fmt == FMT_XRGB {
475 copy_rows(&mut sf.px, ds, (y0 - self.row0) as usize, x0 as usize * 4, &src.px, ss, sy, sx * 4, n * 4, (y1 - y0) as usize);
476 return;
477 }
478 for r in 0..(y1 - y0) as usize {
479 let d = ((y0 - self.row0) as usize + r) * ds + x0 as usize * 4;
480 let s = (sy + r) * ss + sx * 4;
481 blend::blit_over(&mut sf.px[d..d + n * 4], &src.px[s..s + n * 4], self.opac);
482 }
483 }
484
485 pub fn blit_scaled(&self, sf: &mut Surface, src: &Surface, x: i32, y: i32, w: i32, h: i32) {
487 if w == src.w && h == src.h {
488 self.blit(sf, src, x, y);
489 return;
490 }
491 if w <= 0 || h <= 0 || src.w <= 0 || src.h <= 0 || sf.fmt == FMT_A8 || src.fmt == FMT_A8 {
492 return;
493 }
494 let xa = x.wrapping_add(self.ox);
495 let ya = y.wrapping_add(self.oy);
496 let Some([x0, y0, x1, y1]) = self.clip_rect(x, y, w, h) else { return };
497 let vw = (x1 - x0) as usize;
498 let xstep = ((src.w as u64) << 16) / w as u64;
499 let ystep = ((src.h as u64) << 16) / h as u64;
500 let mut row = vec![0u8; vw * 4];
501 for r in 0..(y1 - y0) {
502 let sy = (((y0 + r - ya) as u32 as u64 * ystep + (ystep >> 1)) >> 16) as usize;
503 let srow = sy * src.stride as usize;
504 let mut fx = (x0 - xa) as u32 as u64 * xstep + (xstep >> 1);
505 for i in 0..vw {
506 let s = srow + (fx >> 16) as usize * 4;
507 row[i * 4..i * 4 + 4].copy_from_slice(&src.px[s..s + 4]);
508 fx += xstep;
509 }
510 let d = (y0 + r - self.row0) as usize * sf.stride as usize + x0 as usize * 4;
511 let dst = &mut sf.px[d..d + vw * 4];
512 if src.fmt == FMT_XRGB {
513 dst.copy_from_slice(&row);
514 } else {
515 blend::blit_over(dst, &row, self.opac);
516 }
517 }
518 }
519}