// The torn strip: the passage inside curly quotes, as many lines as it // takes up to a sensible limit and cut past it, each line underlined beneath // its words in a pencil of the highlight's colour that runs past its ends and // is never even; the title and author on one line under it; a note curls the // lower right corner with the note on the curl and no word under it, no note // no curl; the strip as tall as its words, in the upper middle of the room; // the parts tagged; the print does not move with the text size; the strip // lies on the navy felt. @testable import CottonFaces import CottonPaper import CottonSpecimens import CottonValues import CoreText import SwiftUI import XCTest /// The parts a drawn face tagged. private final class HighlightPartsTagged { var parts: Set = [] } @MainActor final class HighlightPassageTests: XCTestCase { private typealias Catalogue = HighlightPassageFaceCatalogue /// The deck's card on a phone. private let card = CGSize(width: 340, height: 451) private func strip(_ card: DeckCard) -> HighlightStrip { HighlightPassageSetting.strip(card, room: self.card) } /// RULE: The passage is set inside curly quotes, whole, in as many lines as it takes up to nine, every line within the writing's room; past nine (or fewer where the room is shorter) it stops at the last, which ends in an ellipsis after a whole word. func testThePassageIsQuotedAndSetWholeUpToALimit() { let knobs = HighlightPassageFaceKnobs.self let font = HighlightPassageSetting.caslon(knobs.passageSize * card.width) let room = knobs.textWidth * card.width / knobs.width let whole = strip(Catalogue.yellowBare).passage XCTAssertEqual(whole.joined(separator: " "), "\u{201C}" + Catalogue.yellowBare.text("quote")! + "\u{201D}", "quoted, nothing cut") let largest = strip(Catalogue.largest).passage XCTAssertGreaterThan(largest.count, 6, "a long passage makes a taller strip, not a cut one") XCTAssertFalse(largest.last?.hasSuffix("\u{2026}") ?? true) let cut = strip(Catalogue.tealCut).passage XCTAssertLessThanOrEqual(cut.count, knobs.passageMost) XCTAssertGreaterThanOrEqual(cut.count, knobs.passageLeast) XCTAssertEqual(cut.last?.last, "\u{2026}") for line in cut + largest + whole { XCTAssertLessThanOrEqual(HighlightPassageSetting.wide(line, font), room + 0.5, line) } let kept = cut.joined(separator: " ").dropLast() let passage = Catalogue.tealCut.text("quote")!.split(whereSeparator: \.isWhitespace).joined(separator: " ") XCTAssertTrue(("\u{201C}" + passage).hasPrefix(kept), "the cut keeps the passage's own words in order: \(kept)") XCTAssertEqual(HighlightPassageSetting.quoted("\u{201C}Said.\u{201D}"), "\u{201C}Said.\u{201D}", "a quote the book has is not quoted twice") XCTAssertEqual(HighlightPassageSetting.lines("one two three four", font: font, width: 10_000, limit: 6), ["one two three four"]) XCTAssertEqual(HighlightPassageSetting.lines("", font: font, width: 100, limit: 6), []) } /// RULE: The strip is as wide as the room and as tall as its words need, lying in the upper middle of the room and never past it: a longer passage makes a taller strip, a bare short one a short strip, well short of the room. func testTheStripIsAsTallAsItsWords() { let short = strip(Catalogue.yellowBare) let long = strip(Catalogue.largest) XCTAssertEqual(short.size.width, card.width) XCTAssertLessThan(short.size.height, card.height * 0.5, "it sits in the deck, not filling it") XCTAssertGreaterThan(long.size.height, short.size.height) for one in [short, long, strip(Catalogue.tealCut), strip(Catalogue.redNote)] { XCTAssertGreaterThanOrEqual(one.frame.minY, 0) XCTAssertLessThanOrEqual(one.frame.maxY, card.height + 0.5) let above = one.frame.minY, below = card.height - one.frame.maxY XCTAssertLessThanOrEqual(above, below + 0.5, "the upper middle") } XCTAssertGreaterThan(short.frame.minY, 0, "a short strip lies down from the top") } /// RULE: The line under the passage is the book's title, then the author after a middle dot; a line too long cuts the author first, then the title, with an ellipsis; a card that says neither has none. func testTheTitleAndAuthorShareALine() { let set = strip(Catalogue.redNote) XCTAssertEqual(set.title, "The Harbour Lamp") XCTAssertEqual(set.author, "Asha Mercer") let long = strip(Catalogue.largest) XCTAssertNotNil(long.title) XCTAssertTrue(long.author?.hasSuffix("\u{2026}") ?? true || long.title?.hasSuffix("\u{2026}") == true, "cut, not run past the curl") let bare = strip(Catalogue.highlight(["quote": .text("A line.")])) XCTAssertNil(bare.title) XCTAssertNil(bare.author) } /// RULE: A note lays the painted curl over the strip's lower right corner, the note pencilled on its face in at most two lines, and no word of the passage or the line under it lies under the curl's paper; no note, no curl. func testANoteCurlsTheCornerAndNoWordLiesUnderIt() throws { XCTAssertNil(strip(Catalogue.yellowBare).curl) XCTAssertEqual(strip(Catalogue.yellowBare).note, []) let knobs = HighlightPassageFaceKnobs.self for card in [Catalogue.redNote, Catalogue.tealCut, Catalogue.largest] { let set = strip(card) let curl = try XCTUnwrap(set.curl) XCTAssertFalse(set.note.isEmpty) XCTAssertLessThanOrEqual(set.note.count, knobs.noteLines) XCTAssertEqual(curl.maxX, set.size.width, accuracy: 0.01, "the curl is the strip's lower right corner") XCTAssertEqual(set.size.height - curl.maxY, knobs.curlFoot * set.scale, accuracy: 0.01) let font = HighlightPassageSetting.caslon(set.passageSize) let ascent = CTFontGetAscent(font) for (index, line) in set.passage.enumerated() { let base = set.passageTop + ascent + CGFloat(index) * set.passageSize * knobs.passageStep let rect = CGRect(x: set.passageLeft, y: base - ascent, width: HighlightPassageSetting.wide(line, font), height: ascent + set.passageSize * (knobs.strokeDrop + knobs.strokeWidth)) XCTAssertFalse(set.underCurl(rect), "\(line) lies under the curl") } if let title = set.title { let by = HighlightPassageSetting.wide(title, HighlightPassageSetting.italic(knobs.titleSize * self.card.width)) + (set.author.map { HighlightPassageSetting.wide(" \u{00B7} " + $0, HighlightPassageSetting.caslon(knobs.authorSize * self.card.width)) } ?? 0) XCTAssertFalse(set.underCurl(CGRect(x: set.passageLeft, y: set.byTop, width: by, height: set.byHeight)), "the line under it clears the curl") } } XCTAssertEqual(HighlightPassageSetting.noteLines(Catalogue.largest, width: card.width).count, knobs.noteLines) XCTAssertEqual(HighlightPassageSetting.noteLines(Catalogue.largest, width: card.width).last?.last, "\u{2026}") XCTAssertEqual(HighlightPassageSetting.noteLines(Catalogue.redNote, width: card.width).joined(separator: " "), Catalogue.redNote.text("note")) } /// RULE: The stroke's pencil is the highlight's colour by the choice's stored value: red, yellow, teal; a highlight that says none, or a value with no pencil, is set bare. func testTheStrokeIsTheChoicesColour() { XCTAssertEqual(HighlightInk(Catalogue.redNote), .red) XCTAssertEqual(HighlightInk(Catalogue.yellowBare), .yellow) XCTAssertEqual(HighlightInk(Catalogue.tealCut), .teal) XCTAssertEqual(HighlightInk.red.colour, BooksMaterial.pencilRed.colour) XCTAssertEqual(HighlightInk.yellow.colour, BooksMaterial.pencilYellow.colour) XCTAssertEqual(HighlightInk.teal.colour, BooksMaterial.pencilTeal.colour) XCTAssertNil(HighlightInk(Catalogue.highlight(["quote": .text("A line.")]))) XCTAssertNil(HighlightInk(Catalogue.highlight(["quote": .text("A line."), "ink": .text("Red")])), "the label is copy; the pencil goes by the value") let drawn = { (ink: String) in SpecimenShot.image(Catalogue.face, Specimen("ink", Catalogue.highlight(["quote": .text("A line kept."), "ink": .text(ink)])), size: self.card) } XCTAssertNotEqual(bytes(drawn("red")), bytes(drawn("teal")), "the colour reaches the stroke") } /// RULE: The pencil stroke runs a hair past the line's first and last glyph under its baseline, is uneven in width, grainy past its edges, and is the same stroke every time for the same line. func testTheStrokeIsDrawnInPencil() { let size: CGFloat = 22 let stroke = PencilStroke(length: 200, base: 30, size: size, seed: "0 A line kept.") XCTAssertLessThan(stroke.upper.first!.x, 0) XCTAssertGreaterThan(stroke.upper.last!.x, 200) XCTAssertLessThan(stroke.upper.last!.x - 200, size * 0.5, "a hair past, not a rule across the card") XCTAssertTrue(stroke.upper.allSatisfy { $0.y > 30 }, "under the baseline") let widths = zip(stroke.upper, stroke.lower).map { $1.y - $0.y } XCTAssertGreaterThan(widths.max()! - widths.min()!, size * 0.03, "uneven in width") let outside = stroke.grain.filter { speck in let index = stroke.upper.indices.min { abs(stroke.upper[$0].x - speck.at.x) < abs(stroke.upper[$1].x - speck.at.x) }! return speck.at.y < stroke.upper[index].y || speck.at.y > stroke.lower[index].y } XCTAssertFalse(outside.isEmpty, "grain strays past the edges") let again = PencilStroke(length: 200, base: 30, size: size, seed: "0 A line kept.") XCTAssertEqual(stroke.upper, again.upper) XCTAssertEqual(stroke.grain.map(\.at), again.grain.map(\.at)) XCTAssertNotEqual(stroke.upper, PencilStroke(length: 200, base: 30, size: size, seed: "1 Another line.").upper) } /// RULE: The strip tags its passage, title and author, and its note on the curl; with no note it tags none and draws no curl, and the note is drawn. func testTheStripTagsItsParts() { func parts(_ face: Drawn) -> Set { let tagged = HighlightPartsTagged() let drawn = face .frame(width: card.width, height: card.height) .backgroundPreferenceValue(FacePartsKey.self) { regions in let _ = tagged.parts.formUnion(regions.map(\.part)) Color.clear } XCTAssertNotNil(ImageRenderer(content: drawn).cgImage) return tagged.parts } XCTAssertEqual(parts(HighlightPassageFace(card: Catalogue.redNote)), [.quote, .note, .title, .subtitle]) XCTAssertEqual(parts(HighlightPassageFace(card: Catalogue.yellowBare)), [.quote, .title, .subtitle]) let noted = Catalogue.highlight(["quote": .text("A line kept."), "note": .text("Why."), "source": .text("A Book"), "ink": .text("red")]) var bare = noted bare.fields["note"] = nil XCTAssertNotEqual(bytes(shot(noted)), bytes(shot(bare)), "the note is drawn") XCTAssertEqual(bytes(shot(bare)), bytes(shot(Catalogue.highlight(["quote": .text("A line kept."), "source": .text("A Book"), "ink": .text("red")])))) } /// RULE: Print on the card is the card's, never Dynamic Type's: the fullest card draws the same at the smallest and the largest text size. func testTheFullestCardDrawsTheSameAtEveryTextSize() throws { let largest = try XCTUnwrap(Catalogue.face.specimens.first { $0.name == "largest" }) let small = SpecimenShot.image(Catalogue.face, largest, size: card, textSize: .xSmall) let large = SpecimenShot.image(Catalogue.face, largest, size: card, textSize: .accessibility5) XCTAssertNotNil(large) XCTAssertEqual(bytes(small), bytes(large)) } /// RULE: The torn strip draws its own paper (its painted bands, shadow and curl, in the catalogue), lies on the books' dark navy felt, laid with its painted tile, and is stepped by a cut. func testTheCardStandsOnTheNavyFeltAndCuts() { XCTAssertEqual(FaceRegistry.ground("highlight.passage"), .feltNavy) XCTAssertEqual(FaceRegistry.stock("highlight.passage"), .own) for name in ["highlight-strip-top", "highlight-strip-middle", "highlight-strip-bottom", "highlight-strip-shadow", "highlight-curl", "highlight-curl-shadow"] { XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue") } XCTAssertTrue(FaceGround.feltNavy.dark) XCTAssertEqual(FaceGround.feltNavy.tile, "books-felt") XCTAssertEqual(FaceGround.feltNavy.colour, BooksMaterial.felt.colour) XCTAssertEqual(Catalogue.face.steps, .cut) XCTAssertEqual(Catalogue.face.specimens.map(\.name), ["red-note", "yellow-bare", "teal-cut", "largest"]) XCTAssertTrue(Catalogue.face.specimens.allSatisfy { $0.picture == nil }, "the strip shows no cover") } private func shot(_ card: DeckCard) -> CGImage? { SpecimenShot.image(Catalogue.face, Specimen("shot", card), size: self.card) } private func bytes(_ image: CGImage?) -> Data? { image?.dataProvider?.data as Data? } }