f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
use super::*;

fn points(value: i64) -> Length {
    Length::from_points(value).unwrap()
}

/// The share in billionths.
fn billionths(share: i128) -> i128 {
    div_round_i128(share * 1_000_000_000, ONE)
}

#[test]
fn takes_the_whole_length_at_the_reference_size_and_less_below_it() {
    assert_eq!(share(TEXT_SHADOW_SIZE), ONE);
    // (36 / 72) ^ 0.7, (18 / 72) ^ 0.7, (144 / 72) ^ 0.7 and (11 / 72) ^ 0.7.
    for (size, expected) in [
        (36, 615_572_207),
        (18, 378_929_142),
        (144, 1_624_504_793),
        (11, 268_436_849),
    ] {
        assert_eq!(billionths(share(points(size))), expected, "{size} pt");
    }
    assert_eq!(share(Length::ZERO), 0);
    assert_eq!(share(Length::from_units(-1)), 0);
}

#[test]
fn gives_the_room_of_the_blur_less_and_plus_the_offset_down() {
    let shadow = |blur, distance, direction| TextShadow {
        blur: points(blur),
        distance: points(distance),
        direction,
        color: None,
        transparency: 0,
    };
    // At 72 pt the share is 1: a blur of 4 pt and an offset of 3 pt down.
    let room = room(shadow(4, 3, 5_400_000), TEXT_SHADOW_SIZE);
    assert_eq!(room.above, points(1));
    assert_eq!(room.below, points(7));
    // Up, the rooms swap; to the right, both take the blur.
    let up = super::room(shadow(4, 3, 16_200_000), TEXT_SHADOW_SIZE);
    assert_eq!((up.above, up.below), (points(7), points(1)));
    let right = super::room(shadow(4, 3, 0), TEXT_SHADOW_SIZE);
    assert_eq!((right.above, right.below), (points(4), points(4)));
    // A sharp shadow down takes room below only.
    let sharp = super::room(shadow(0, 3, 5_400_000), TEXT_SHADOW_SIZE);
    assert_eq!((sharp.above, sharp.below), (Length::ZERO, points(3)));
    // At 18 pt, 3 pt down with a blur of 4 pt: 0.3789 of 1 pt above, of 7 pt below.
    let small = super::room(shadow(4, 3, 5_400_000), points(18));
    assert_eq!(small.above, Length::from_units(9_625));
    assert_eq!(small.below, Length::from_units(67_373));
}

#[test]
fn draws_a_sharp_opaque_shadow_as_paths_or_picture_for_an_outline() {
    let sharp = TextShadow {
        distance: points(3),
        direction: 0,
        color: Some([0x44, 0x72, 0xC4]),
        ..TextShadow::default()
    };
    // At 72 pt the share is 1, and the whole distance is the offset.
    let shadow = drawn(sharp, None, false, TEXT_SHADOW_SIZE).unwrap();
    assert_eq!((shadow.across, shadow.down), (points(3), Length::ZERO));
    assert_eq!(
        shadow.color,
        Rgb {
            r: 0x44,
            g: 0x72,
            b: 0xC4
        }
    );
    assert_eq!(shadow.form, ShadowForm::Paths);
    // With a solid outline, it becomes an effect picture of that outline width.
    let outline = Outline {
        width: points(1),
        color: Rgb::BLACK,
        join: f_office_paint::Join::Round,
    };
    let outlined = drawn(sharp, Some(outline), false, TEXT_SHADOW_SIZE).unwrap();
    assert_eq!(
        outlined.form,
        ShadowForm::Picture {
            blur: Length::ZERO,
            opacity: Opacity::FULL,
            outline_width: points(1),
        }
    );
}

#[test]
fn draws_blurred_and_translucent_text_shadows_as_pictures() {
    let sharp = TextShadow {
        distance: points(3),
        direction: 0,
        color: Some([0, 0, 0]),
        ..TextShadow::default()
    };
    let blurred = drawn(
        TextShadow {
            blur: points(4),
            transparency: 60_000,
            ..sharp
        },
        None,
        false,
        TEXT_SHADOW_SIZE,
    )
    .unwrap();
    assert_eq!(
        blurred.form,
        ShadowForm::Picture {
            blur: points(4),
            opacity: Opacity::clamped(40_000),
            outline_width: Length::ZERO,
        }
    );
}

#[test]
fn leaves_unknown_clear_and_scaled_text_shadows_undrawn() {
    let sharp = TextShadow {
        distance: points(3),
        direction: 0,
        color: Some([0x44, 0x72, 0xC4]),
        ..TextShadow::default()
    };
    let outline = Outline {
        width: points(1),
        color: Rgb::BLACK,
        join: f_office_paint::Join::Round,
    };
    assert_eq!(
        drawn(
            TextShadow {
                transparency: DRAWING_WHOLE,
                ..sharp
            },
            None,
            false,
            TEXT_SHADOW_SIZE
        ),
        None
    );
    assert_eq!(
        drawn(
            TextShadow {
                color: None,
                ..sharp
            },
            None,
            false,
            TEXT_SHADOW_SIZE
        ),
        None
    );
    assert_eq!(drawn(sharp, None, true, TEXT_SHADOW_SIZE), None);
    assert_eq!(drawn(sharp, Some(outline), true, TEXT_SHADOW_SIZE), None);
}

#[test]
fn takes_a_non_default_scale_as_scaled_run() {
    let mut props = RunProps::default();
    assert!(!scaled_run(&props));
    props.scale = Some(PERCENT);
    assert!(!scaled_run(&props));
    props.scale = Some(150);
    assert!(scaled_run(&props));
}

#[test]
fn scales_text_glow_room_and_opacity() {
    let glow = TextGlow {
        radius: points(8),
        color: Some([0xFF, 0x00, 0x00]),
        transparency: 40_000,
    };
    // At the reference size (72 pt), scaled radius is 8 pt and room is 3/4 * 8 = 6 pt symmetrically.
    assert_eq!(
        glow_room(glow, TEXT_SHADOW_SIZE),
        ShadowRoom {
            above: points(6),
            below: points(6),
        }
    );
    assert_eq!(
        drawn_glow(glow, false, TEXT_SHADOW_SIZE),
        Some(DrawnGlow {
            radius: points(8),
            color: Rgb { r: 255, g: 0, b: 0 },
            opacity: Opacity::clamped(60_000),
        })
    );
    assert_eq!(drawn_glow(glow, true, TEXT_SHADOW_SIZE), None);
    assert_eq!(
        drawn_glow(
            TextGlow {
                transparency: DRAWING_WHOLE,
                ..glow
            },
            false,
            TEXT_SHADOW_SIZE
        ),
        None
    );
}