Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

feat(import): a Photoshop preset's size comes from its tip's pixels

Josh8086cacde14e
diff --git a/apps/procraft-android/src/brush_files.rs b/apps/procraft-android/src/brush_files.rs
index 72f6d7f..2c15df3 100644
--- a/apps/procraft-android/src/brush_files.rs
+++ b/apps/procraft-android/src/brush_files.rs
@@ -19,6 +19,11 @@ const THUMBNAIL: (u32, u32) = (327, 100);
/// The most a brush file's picture may decode to (8192 × 8192 in colour), bytes.
const MOST_PNG_BYTES: usize = 256 << 20;
+/// A Photoshop preset's most size (as brush files keep it) for each pixel of its tip: 3.28% of
+/// the scale a pixel, as 35 presets of a real file come in elsewhere (8 px reading 26%, 442 px
+/// 1448%).
+const ABR_SIZE_PER_PIXEL: f32 = 0.03277;
+
/// A brush file's pictures, made: the shape, the grain if any and the preview (PNGs).
struct Made {
shape: Vec<u8>,
@@ -312,10 +317,17 @@ fn abr_brush(a: &procraft_engine::abr::AbrBrush) -> Brush {
("streamline", 0.0),
("rendering_mode", 1.0),
("flow", 1.0),
- ("min_size", 0.01),
] {
b.set_setting(key, value);
}
+ // its most size from its tip's pixels (a sampled tip's no taller than its picture), the least 1%
+ let pixels = match (&a.tip, a.diameter) {
+ (None, d) => d,
+ (Some(t), d) => Some(d.map_or(t.height as f32, |d| d.min(t.height as f32))),
+ };
+ if let Some(px) = pixels {
+ procraft_engine::brushfile::set_sizes(&mut b, px * ABR_SIZE_PER_PIXEL, Some(0.01));
+ }
let d = a.dynamics;
b.set_setting("pressure_size", if d.tip && d.size_by_pressure { (1.0 - d.min_diameter) / 2.0 } else { 0.0 });
// the stylus page's opacity is kept as "pressure_flow", its flow as "pressure_opacity"
@@ -890,6 +902,53 @@ mod tests {
assert!(plain.grain.source.is_none());
}
+ #[test]
+ fn a_photoshop_preset_comes_in_at_its_size_with_its_dynamics() {
+ use procraft_engine::abr::{AbrBrush, AbrDynamics, AbrTip};
+ let shown = |b: &Brush, key: &str| (b.setting(key) * 100.0).round();
+ // a computed 10 px round, pressure on its size (down to 87%) and opacity
+ let round = AbrBrush {
+ name: "Hard".into(),
+ tip: None,
+ spacing: Some(0.09),
+ angle: 129f32.to_radians(),
+ roundness: Some(1.0),
+ hardness: Some(1.0),
+ diameter: Some(10.0),
+ texture: None,
+ dynamics: AbrDynamics {
+ tip: true,
+ min_diameter: 0.87,
+ size_by_pressure: true,
+ paint: true,
+ opacity_by_pressure: true,
+ flow_by_pressure: false,
+ },
+ };
+ let b = abr_brush(&round);
+ // most size 33% (as the file keeps it), the least 1%
+ assert_eq!(
+ ((b.max_size / b.size_unit() * 100.0).round(), (b.min_size / b.size_unit() * 100.0).round()),
+ (33.0, 1.0)
+ );
+ // pressure: size 6.5% (half of what the dynamics let go), the stylus page's opacity full
+ assert!((b.setting("pressure_size") - 0.065).abs() < 1e-6);
+ assert_eq!((shown(&b, "pressure_flow"), shown(&b, "pressure_opacity")), (100.0, 0.0));
+ // untapered, no StreamLine, a uniformed glaze; turned the other way
+ assert_eq!(
+ (b.setting("pen_taper_size"), b.setting("touch_taper_size"), b.setting("streamline")),
+ (0.0, 0.0, 0.0)
+ );
+ assert_eq!(b.setting("rendering_mode"), 1.0);
+ assert!((b.shape.rotation + 129f32.to_radians()).abs() < 1e-6);
+ // a sampled 165 x 91 tip at 165 px: no taller than its picture, 298%
+ let tip = AbrTip { name: "Tip".into(), spacing: None, width: 165, height: 91, pixels: vec![255; 165 * 91] };
+ let sampled =
+ abr_brush(&AbrBrush { tip: Some(tip), diameter: Some(165.0), dynamics: AbrDynamics::default(), ..round });
+ assert_eq!((sampled.max_size / sampled.size_unit() * 100.0).round(), 298.0);
+ assert_eq!(sampled.setting("pressure_size"), 0.0);
+ }
+
#[test]
fn an_image_becomes_a_texture() {
// a 4 × 4 grey PNG: white on the left half
diff --git a/crates/engine/src/abr.rs b/crates/engine/src/abr.rs
index 581e6e5..e0f529c 100644
--- a/crates/engine/src/abr.rs
+++ b/crates/engine/src/abr.rs
@@ -29,6 +29,8 @@ pub struct AbrBrush {
pub roundness: Option<f32>,
/// A computed tip's edge, 0 soft to 1 hard.
pub hardness: Option<f32>,
+ /// Its tip's diameter, pixels, if the preset gives it.
+ pub diameter: Option<f32>,
/// The pattern the preset paints through, if it uses one that is in the file.
pub texture: Option<AbrTexture>,
/// What the pen's pressure changes.
@@ -91,6 +93,7 @@ pub fn read_brushes(bytes: &[u8]) -> Option<Vec<AbrBrush>> {
angle: 0.0,
roundness: None,
hardness: None,
+ diameter: None,
texture: None,
dynamics: AbrDynamics::default(),
};
@@ -207,6 +210,7 @@ fn read_sections(r: &mut Reader<'_>) -> Option<Vec<AbrBrush>> {
angle: p.angle,
roundness: p.roundness,
hardness: p.hardness,
+ diameter: p.diameter,
texture,
dynamics: p.dynamics,
});
@@ -223,6 +227,7 @@ fn read_sections(r: &mut Reader<'_>) -> Option<Vec<AbrBrush>> {
angle: 0.0,
roundness: None,
hardness: None,
+ diameter: None,
texture: None,
dynamics: AbrDynamics::default(),
});
@@ -421,6 +426,7 @@ struct Preset {
angle: f32,
roundness: Option<f32>,
hardness: Option<f32>,
+ diameter: Option<f32>,
/// Whether its texture is on, and the pattern's id, scale, depth and inversion.
use_texture: bool,
texture: Option<String>,
@@ -436,7 +442,7 @@ struct Preset {
/// aren't the preset's: the samples they name come after the presets. Its texture comes after its
/// brushes, so it goes to what was kept of the preset, and to the preset as it is still read.
fn read_presets(data: &[u8]) -> (Vec<Preset>, Vec<String>) {
- const KEYS: [&[u8]; 22] = [
+ const KEYS: [&[u8]; 23] = [
b"brushPreset",
b"Nm TEXT",
b"SpcnUntF#Prc",
@@ -459,6 +465,7 @@ fn read_presets(data: &[u8]) -> (Vec<Preset>, Vec<String>) {
b"opVrObjc",
b"prVrObjc",
b"bVTylong",
+ b"DmtrUntF#Pxl",
];
let (mut out, mut others) = (Vec::new(), Vec::new());
// the preset being read, whether its brush is computed, and whether its brush is done
@@ -533,6 +540,7 @@ fn read_presets(data: &[u8]) -> (Vec<Preset>, Vec<String>) {
4 => p.angle = r.f64().filter(|v| v.is_finite()).map_or(0.0, |v| v.to_radians() as f32),
5 => p.roundness = percent(&mut r).filter(|v| *v > 0.0 && *v <= 1.0),
6 => p.hardness = percent(&mut r).map(|v| v.clamp(0.0, 1.0)),
+ 22 => p.diameter = r.f64().filter(|v| v.is_finite() && *v > 0.0).map(|v| v as f32),
7 => computed = true,
8 => end(&p, computed, &mut done, &mut out),
_ => {}