Skip to content

font metrics should consider text node area instead of character count #10

@fbuchinger

Description

@fbuchinger

currently, the most popular fontSize/fontColor/fontStyle/font is determined by the amount of text characters that were styled with the measured font property. This favours long texts in tiny fonts (e.g. footer texts) that a reader wouldn't consider the most "dominant" texts on the page.

if we measure the pixel area consumed by a certain text node (width x height of its bounding box), we would get a more accurate measurement. furthermore we can calculate the total text area on a page and compare that to the area covered by images.

The bounding box of a text node can be calculated using range selections:

function getTextNodeBBox(textNode) {
    var dims = {};

    var range = document.createRange();
    range.selectNode(textNode);
    var rect = range.getBoundingClientRect();
    if (rect.left && rect.right && rect.top && rect.bottom && rect.width && rect.height){
        dims.bottom = rect.bottom;
        dims.left = rect.left;
        dims.right = rect.right;
        dims.top = rect.top;
        dims.width = rect.width;
        dims.height = rect.height;
    }
    range.detach();
    return rect;
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions