Skip to content

Truly independent web browser

2.6 KB
Raw Blame
<!doctype html>
<script src="../include.js"></script>
<style>
    @font-face {
        font-family: Ahem;
        src: url("../../../Ref/data/fonts/Ahem.ttf");
    }
    #text {
        font: 20px/20px Ahem;
        width: 0;
    }
</style>
<div id="text"></div>
<script>
    function softWrapOpportunities(text) {
        let result = "";
        let previousCenter = null;
        for (const node of text.childNodes) {
            for (let i = 0; i < node.length; ++i) {
                const range = document.createRange();
                range.setStart(node, i);
                range.setEnd(node, i + 1);
                // A range that starts at a line break can also cover the end of the line before it.
                const rects = range.getClientRects();
                const rect = rects[rects.length - 1];
                const center = rect.top + rect.height / 2;
                if (previousCenter !== null && Math.abs(center - previousCenter) > 10)
                    result += "|";
                previousCenter = center;
                result += node.data[i];
            }
        }
        return result;
    }

    promiseTest(async () => {
        await document.fonts.load("20px Ahem");
        const text = document.getElementById("text");
        for (const [wordBreak, content, splitAt] of [
            ["break-all", "\u1019\u102c\u1005\u102c"],
            // A grapheme cluster split between two text nodes.
            ["break-all", "\u0915\u094d\u0937", 2],
            ["break-all", "a+a+a"],
            ["break-all", "a$a"],
            ["break-all", "a\\a"],
            ["break-all", "a%a"],
            ["break-all", "a.a"],
            ["break-all", "a,a"],
            ["break-all", "a:a"],
            ["break-all", "a/a"],
            ["break-all", "a(a)a"],
            ["break-all", "a[a]a"],
            ["break-all", "1.5"],
            ["break-all", "1%1$1"],
            ["break-all", "(-1)"],
            ["break-all", "a-a"],
            ["break-all", "a!a"],
            ["break-all", 'a"a'],
            ["break-all", "a\u00a0a"],
            ["break-all", "a\u00bba"],
            ["keep-all", "\u30d5\u30a9\u30d5"],
        ]) {
            text.style.wordBreak = wordBreak;
            if (splitAt === undefined) {
                text.textContent = content;
            } else {
                text.textContent = content.slice(0, splitAt);
                text.append(content.slice(splitAt));
            }
            const nodes = [...text.childNodes].map(node => JSON.stringify(node.data)).join(" + ");
            println(`${wordBreak} ${nodes}: ${JSON.stringify(softWrapOpportunities(text))}`);
        }
    });
</script>