(text, rect, rotation, fontSize, bgColor, strokeAlpha)
| 38967 | }; |
| 38968 | } |
| 38969 | createAppearance(text, rect, rotation, fontSize, bgColor, strokeAlpha) { |
| 38970 | const ctx = this._createContext(); |
| 38971 | const lines = []; |
| 38972 | let maxWidth = -Infinity; |
| 38973 | for (const line of text.split(/\r\n?|\n/)) { |
| 38974 | lines.push(line); |
| 38975 | const lineWidth = ctx.measureText(line).width; |
| 38976 | maxWidth = Math.max(maxWidth, lineWidth); |
| 38977 | for (const code of codePointIter(line)) { |
| 38978 | const char = String.fromCodePoint(code); |
| 38979 | let width = this.widths.get(code); |
| 38980 | if (width === undefined) { |
| 38981 | const metrics = ctx.measureText(char); |
| 38982 | width = Math.ceil(metrics.width); |
| 38983 | this.widths.set(code, width); |
| 38984 | this.firstChar = Math.min(code, this.firstChar); |
| 38985 | this.lastChar = Math.max(code, this.lastChar); |
| 38986 | } |
| 38987 | } |
| 38988 | } |
| 38989 | maxWidth *= fontSize / 1000; |
| 38990 | const [x1, y1, x2, y2] = rect; |
| 38991 | let w = x2 - x1; |
| 38992 | let h = y2 - y1; |
| 38993 | if (rotation % 180 !== 0) { |
| 38994 | [w, h] = [h, w]; |
| 38995 | } |
| 38996 | let hscale = 1; |
| 38997 | if (maxWidth > w) { |
| 38998 | hscale = w / maxWidth; |
| 38999 | } |
| 39000 | let vscale = 1; |
| 39001 | const lineHeight = LINE_FACTOR * fontSize; |
| 39002 | const lineDescent = LINE_DESCENT_FACTOR * fontSize; |
| 39003 | const maxHeight = lineHeight * lines.length; |
| 39004 | if (maxHeight > h) { |
| 39005 | vscale = h / maxHeight; |
| 39006 | } |
| 39007 | const fscale = Math.min(hscale, vscale); |
| 39008 | const newFontSize = fontSize * fscale; |
| 39009 | const buffer = ["q", `0 0 ${numberToString(w)} ${numberToString(h)} re W n`, `BT`, `1 0 0 1 0 ${numberToString(h + lineDescent)} Tm 0 Tc ${getPdfColor(bgColor, true)}`, `/${this.fontName.name} ${numberToString(newFontSize)} Tf`]; |
| 39010 | const { |
| 39011 | resources |
| 39012 | } = this; |
| 39013 | strokeAlpha = typeof strokeAlpha === "number" && strokeAlpha >= 0 && strokeAlpha <= 1 ? strokeAlpha : 1; |
| 39014 | if (strokeAlpha !== 1) { |
| 39015 | buffer.push("/R0 gs"); |
| 39016 | const extGState = new Dict(this.xref); |
| 39017 | const r0 = new Dict(this.xref); |
| 39018 | r0.set("ca", strokeAlpha); |
| 39019 | r0.set("CA", strokeAlpha); |
| 39020 | r0.set("Type", Name.get("ExtGState")); |
| 39021 | extGState.set("R0", r0); |
| 39022 | resources.set("ExtGState", extGState); |
| 39023 | } |
| 39024 | const vShift = numberToString(lineHeight); |
| 39025 | for (const line of lines) { |
| 39026 | buffer.push(`0 -${vShift} Td <${stringToUTF16HexString(line)}> Tj`); |
no test coverage detected