(text, font)
| 1200 | FUNCTION getTextWidth |
| 1201 | ------------------------------------------------------------------*/ |
| 1202 | async function getTextWidth(text, font){ |
| 1203 | const buffer = 5; // This is a temporary solution until I figure out why width returned is a bit short. |
| 1204 | const html = `<canvas id="canvas"></canvas>`; |
| 1205 | const js = ` |
| 1206 | function getTextWidth() { |
| 1207 | let canvas = document.getElementById("canvas"); |
| 1208 | let ctx = canvas.getContext("2d"); |
| 1209 | ctx.font = "${font}"; |
| 1210 | let metrics = ctx.measureText("${text}"); |
| 1211 | return metrics.width; |
| 1212 | } |
| 1213 | getTextWidth(); |
| 1214 | `; |
| 1215 | let wv = new WebView(); |
| 1216 | await wv.loadHTML(html); |
| 1217 | const width = await wv.evaluateJavaScript(js); |
| 1218 | return Math.ceil(width) + buffer; |
| 1219 | } |
| 1220 | /*------------------------------------------------------------------ |
| 1221 | FUNCTION writeLOG |
| 1222 | ------------------------------------------------------------------*/ |
no outgoing calls
no test coverage detected