-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[text-autospace] Extract
getCharAdvances
This patch extracts a JavaScript function from `text-autospace-001.html`and share it with `text-spacing-trim-combinations-001.html`. Bug: 1463890, 1463891 Change-Id: I509fc6948d5242e970674f9e65ada48d27ab408c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4835287 Auto-Submit: Koji Ishii <[email protected]> Reviewed-by: Lingqi Chi <[email protected]> Commit-Queue: Lingqi Chi <[email protected]> Cr-Commit-Position: refs/heads/main@{#1191928}
- Loading branch information
1 parent
dc5ea80
commit 92313a3
Showing
3 changed files
with
32 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Returns an array of advances (widths) for all characters in the descendants | ||
* of the specified element. | ||
*/ | ||
function getCharAdvances(element) { | ||
const range = document.createRange(); | ||
let advances = []; | ||
for (const node of element.childNodes) { | ||
const nodeType = node.nodeType; | ||
if (nodeType === Node.TEXT_NODE) { | ||
const text = node.nodeValue; | ||
for (let i = 0; i < text.length; ++i) { | ||
range.setStart(node, i); | ||
range.setEnd(node, i + 1); | ||
const bounds = range.getBoundingClientRect(); | ||
advances.push(bounds.width); | ||
} | ||
} else if (nodeType === Node.ELEMENT_NODE) { | ||
advances = advances.concat(getCharAdvances(node)); | ||
} | ||
} | ||
return advances; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters