| 1 |
/** |
| 2 |
* Copyright (c) 2021 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils'; |
| 7 |
|
| 8 |
interface IBlockVector { |
| 9 |
x: number; |
| 10 |
y: number; |
| 11 |
w: number; |
| 12 |
h: number; |
| 13 |
} |
| 14 |
|
| 15 |
export const blockElementDefinitions: { [index: string]: IBlockVector[] | undefined } = { |
| 16 |
// Block elements (0x2580-0x2590) |
| 17 |
'▀': [{ x: 0, y: 0, w: 8, h: 4 }], // UPPER HALF BLOCK |
| 18 |
'▁': [{ x: 0, y: 7, w: 8, h: 1 }], // LOWER ONE EIGHTH BLOCK |
| 19 |
'▂': [{ x: 0, y: 6, w: 8, h: 2 }], // LOWER ONE QUARTER BLOCK |
| 20 |
'▃': [{ x: 0, y: 5, w: 8, h: 3 }], // LOWER THREE EIGHTHS BLOCK |
| 21 |
'▄': [{ x: 0, y: 4, w: 8, h: 4 }], // LOWER HALF BLOCK |
| 22 |
'▅': [{ x: 0, y: 3, w: 8, h: 5 }], // LOWER FIVE EIGHTHS BLOCK |
| 23 |
'▆': [{ x: 0, y: 2, w: 8, h: 6 }], // LOWER THREE QUARTERS BLOCK |
| 24 |
'▇': [{ x: 0, y: 1, w: 8, h: 7 }], // LOWER SEVEN EIGHTHS BLOCK |
| 25 |
'█': [{ x: 0, y: 0, w: 8, h: 8 }], // FULL BLOCK |
| 26 |
'▉': [{ x: 0, y: 0, w: 7, h: 8 }], // LEFT SEVEN EIGHTHS BLOCK |
| 27 |
'▊': [{ x: 0, y: 0, w: 6, h: 8 }], // LEFT THREE QUARTERS BLOCK |
| 28 |
'▋': [{ x: 0, y: 0, w: 5, h: 8 }], // LEFT FIVE EIGHTHS BLOCK |
| 29 |
'▌': [{ x: 0, y: 0, w: 4, h: 8 }], // LEFT HALF BLOCK |
| 30 |
'▍': [{ x: 0, y: 0, w: 3, h: 8 }], // LEFT THREE EIGHTHS BLOCK |
| 31 |
'▎': [{ x: 0, y: 0, w: 2, h: 8 }], // LEFT ONE QUARTER BLOCK |
| 32 |
'▏': [{ x: 0, y: 0, w: 1, h: 8 }], // LEFT ONE EIGHTH BLOCK |
| 33 |
'▐': [{ x: 4, y: 0, w: 4, h: 8 }], // RIGHT HALF BLOCK |
| 34 |
|
| 35 |
// Block elements (0x2594-0x2595) |
| 36 |
'▔': [{ x: 0, y: 0, w: 8, h: 1 }], // UPPER ONE EIGHTH BLOCK |
| 37 |
'▕': [{ x: 7, y: 0, w: 1, h: 8 }], // RIGHT ONE EIGHTH BLOCK |
| 38 |
|
| 39 |
// Terminal graphic characters (0x2596-0x259F) |
| 40 |
'▖': [{ x: 0, y: 4, w: 4, h: 4 }], // QUADRANT LOWER LEFT |
| 41 |
'▗': [{ x: 4, y: 4, w: 4, h: 4 }], // QUADRANT LOWER RIGHT |
| 42 |
'▘': [{ x: 0, y: 0, w: 4, h: 4 }], // QUADRANT UPPER LEFT |
| 43 |
'▙': [{ x: 0, y: 0, w: 4, h: 8 }, { x: 0, y: 4, w: 8, h: 4 }], // QUADRANT UPPER LEFT AND LOWER LEFT AND LOWER RIGHT |
| 44 |
'▚': [{ x: 0, y: 0, w: 4, h: 4 }, { x: 4, y: 4, w: 4, h: 4 }], // QUADRANT UPPER LEFT AND LOWER RIGHT |
| 45 |
'▛': [{ x: 0, y: 0, w: 4, h: 8 }, { x: 4, y: 0, w: 4, h: 4 }], // QUADRANT UPPER LEFT AND UPPER RIGHT AND LOWER LEFT |
| 46 |
'▜': [{ x: 0, y: 0, w: 8, h: 4 }, { x: 4, y: 0, w: 4, h: 8 }], // QUADRANT UPPER LEFT AND UPPER RIGHT AND LOWER RIGHT |
| 47 |
'▝': [{ x: 4, y: 0, w: 4, h: 4 }], // QUADRANT UPPER RIGHT |
| 48 |
'▞': [{ x: 4, y: 0, w: 4, h: 4 }, { x: 0, y: 4, w: 4, h: 4 }], // QUADRANT UPPER RIGHT AND LOWER LEFT |
| 49 |
'▟': [{ x: 4, y: 0, w: 4, h: 8 }, { x: 0, y: 4, w: 8, h: 4 }], // QUADRANT UPPER RIGHT AND LOWER LEFT AND LOWER RIGHT |
| 50 |
|
| 51 |
// VERTICAL ONE EIGHTH BLOCK-2 through VERTICAL ONE EIGHTH BLOCK-7 |
| 52 |
'\u{1FB70}': [{ x: 1, y: 0, w: 1, h: 8 }], |
| 53 |
'\u{1FB71}': [{ x: 2, y: 0, w: 1, h: 8 }], |
| 54 |
'\u{1FB72}': [{ x: 3, y: 0, w: 1, h: 8 }], |
| 55 |
'\u{1FB73}': [{ x: 4, y: 0, w: 1, h: 8 }], |
| 56 |
'\u{1FB74}': [{ x: 5, y: 0, w: 1, h: 8 }], |
| 57 |
'\u{1FB75}': [{ x: 6, y: 0, w: 1, h: 8 }], |
| 58 |
|
| 59 |
// HORIZONTAL ONE EIGHTH BLOCK-2 through HORIZONTAL ONE EIGHTH BLOCK-7 |
| 60 |
'\u{1FB76}': [{ x: 0, y: 1, w: 8, h: 1 }], |
| 61 |
'\u{1FB77}': [{ x: 0, y: 2, w: 8, h: 1 }], |
| 62 |
'\u{1FB78}': [{ x: 0, y: 3, w: 8, h: 1 }], |
| 63 |
'\u{1FB79}': [{ x: 0, y: 4, w: 8, h: 1 }], |
| 64 |
'\u{1FB7A}': [{ x: 0, y: 5, w: 8, h: 1 }], |
| 65 |
'\u{1FB7B}': [{ x: 0, y: 6, w: 8, h: 1 }], |
| 66 |
|
| 67 |
// LEFT AND LOWER ONE EIGHTH BLOCK |
| 68 |
'\u{1FB7C}': [{ x: 0, y: 0, w: 1, h: 8 }, { x: 0, y: 7, w: 8, h: 1 }], |
| 69 |
// LEFT AND UPPER ONE EIGHTH BLOCK |
| 70 |
'\u{1FB7D}': [{ x: 0, y: 0, w: 1, h: 8 }, { x: 0, y: 0, w: 8, h: 1 }], |
| 71 |
// RIGHT AND UPPER ONE EIGHTH BLOCK |
| 72 |
'\u{1FB7E}': [{ x: 7, y: 0, w: 1, h: 8 }, { x: 0, y: 0, w: 8, h: 1 }], |
| 73 |
// RIGHT AND LOWER ONE EIGHTH BLOCK |
| 74 |
'\u{1FB7F}': [{ x: 7, y: 0, w: 1, h: 8 }, { x: 0, y: 7, w: 8, h: 1 }], |
| 75 |
// UPPER AND LOWER ONE EIGHTH BLOCK |
| 76 |
'\u{1FB80}': [{ x: 0, y: 0, w: 8, h: 1 }, { x: 0, y: 7, w: 8, h: 1 }], |
| 77 |
// HORIZONTAL ONE EIGHTH BLOCK-1358 |
| 78 |
'\u{1FB81}': [{ x: 0, y: 0, w: 8, h: 1 }, { x: 0, y: 2, w: 8, h: 1 }, { x: 0, y: 4, w: 8, h: 1 }, { x: 0, y: 7, w: 8, h: 1 }], |
| 79 |
|
| 80 |
// UPPER ONE QUARTER BLOCK |
| 81 |
'\u{1FB82}': [{ x: 0, y: 0, w: 8, h: 2 }], |
| 82 |
// UPPER THREE EIGHTHS BLOCK |
| 83 |
'\u{1FB83}': [{ x: 0, y: 0, w: 8, h: 3 }], |
| 84 |
// UPPER FIVE EIGHTHS BLOCK |
| 85 |
'\u{1FB84}': [{ x: 0, y: 0, w: 8, h: 5 }], |
| 86 |
// UPPER THREE QUARTERS BLOCK |
| 87 |
'\u{1FB85}': [{ x: 0, y: 0, w: 8, h: 6 }], |
| 88 |
// UPPER SEVEN EIGHTHS BLOCK |
| 89 |
'\u{1FB86}': [{ x: 0, y: 0, w: 8, h: 7 }], |
| 90 |
|
| 91 |
// RIGHT ONE QUARTER BLOCK |
| 92 |
'\u{1FB87}': [{ x: 6, y: 0, w: 2, h: 8 }], |
| 93 |
// RIGHT THREE EIGHTHS B0OCK |
| 94 |
'\u{1FB88}': [{ x: 5, y: 0, w: 3, h: 8 }], |
| 95 |
// RIGHT FIVE EIGHTHS BL0CK |
| 96 |
'\u{1FB89}': [{ x: 3, y: 0, w: 5, h: 8 }], |
| 97 |
// RIGHT THREE QUARTERS 0LOCK |
| 98 |
'\u{1FB8A}': [{ x: 2, y: 0, w: 6, h: 8 }], |
| 99 |
// RIGHT SEVEN EIGHTHS B0OCK |
| 100 |
'\u{1FB8B}': [{ x: 1, y: 0, w: 7, h: 8 }], |
| 101 |
|
| 102 |
// CHECKER BOARD FILL |
| 103 |
'\u{1FB95}': [ |
| 104 |
{ x: 0, y: 0, w: 2, h: 2 }, { x: 4, y: 0, w: 2, h: 2 }, |
| 105 |
{ x: 2, y: 2, w: 2, h: 2 }, { x: 6, y: 2, w: 2, h: 2 }, |
| 106 |
{ x: 0, y: 4, w: 2, h: 2 }, { x: 4, y: 4, w: 2, h: 2 }, |
| 107 |
{ x: 2, y: 6, w: 2, h: 2 }, { x: 6, y: 6, w: 2, h: 2 } |
| 108 |
], |
| 109 |
// INVERSE CHECKER BOARD FILL |
| 110 |
'\u{1FB96}': [ |
| 111 |
{ x: 2, y: 0, w: 2, h: 2 }, { x: 6, y: 0, w: 2, h: 2 }, |
| 112 |
{ x: 0, y: 2, w: 2, h: 2 }, { x: 4, y: 2, w: 2, h: 2 }, |
| 113 |
{ x: 2, y: 4, w: 2, h: 2 }, { x: 6, y: 4, w: 2, h: 2 }, |
| 114 |
{ x: 0, y: 6, w: 2, h: 2 }, { x: 4, y: 6, w: 2, h: 2 } |
| 115 |
], |
| 116 |
// HEAVY HORIZONTAL FILL (upper middle and lower one quarter block) |
| 117 |
'\u{1FB97}': [{ x: 0, y: 2, w: 8, h: 2 }, { x: 0, y: 6, w: 8, h: 2 }] |
| 118 |
}; |
| 119 |
|
| 120 |
type PatternDefinition = number[][]; |
| 121 |
|
| 122 |
/** |
| 123 |
* Defines the repeating pattern used by special characters, the pattern is made up of a 2d array of |
| 124 |
* pixel values to be filled (1) or not filled (0). |
| 125 |
*/ |
| 126 |
const patternCharacterDefinitions: { [key: string]: PatternDefinition | undefined } = { |
| 127 |
// Shade characters (0x2591-0x2593) |
| 128 |
'░': [ // LIGHT SHADE (25%) |
| 129 |
[1, 0, 0, 0], |
| 130 |
[0, 0, 0, 0], |
| 131 |
[0, 0, 1, 0], |
| 132 |
[0, 0, 0, 0] |
| 133 |
], |
| 134 |
'▒': [ // MEDIUM SHADE (50%) |
| 135 |
[1, 0], |
| 136 |
[0, 0], |
| 137 |
[0, 1], |
| 138 |
[0, 0] |
| 139 |
], |
| 140 |
'▓': [ // DARK SHADE (75%) |
| 141 |
[0, 1], |
| 142 |
[1, 1], |
| 143 |
[1, 0], |
| 144 |
[1, 1] |
| 145 |
] |
| 146 |
}; |
| 147 |
|
| 148 |
const enum Shapes { |
| 149 |
/** │ */ TOP_TO_BOTTOM = 'M.5,0 L.5,1', |
| 150 |
/** ─ */ LEFT_TO_RIGHT = 'M0,.5 L1,.5', |
| 151 |
|
| 152 |
/** └ */ TOP_TO_RIGHT = 'M.5,0 L.5,.5 L1,.5', |
| 153 |
/** ┘ */ TOP_TO_LEFT = 'M.5,0 L.5,.5 L0,.5', |
| 154 |
/** ┐ */ LEFT_TO_BOTTOM = 'M0,.5 L.5,.5 L.5,1', |
| 155 |
/** ┌ */ RIGHT_TO_BOTTOM = 'M0.5,1 L.5,.5 L1,.5', |
| 156 |
|
| 157 |
/** ╵ */ MIDDLE_TO_TOP = 'M.5,.5 L.5,0', |
| 158 |
/** ╴ */ MIDDLE_TO_LEFT = 'M.5,.5 L0,.5', |
| 159 |
/** ╶ */ MIDDLE_TO_RIGHT = 'M.5,.5 L1,.5', |
| 160 |
/** ╷ */ MIDDLE_TO_BOTTOM = 'M.5,.5 L.5,1', |
| 161 |
|
| 162 |
/** ┴ */ T_TOP = 'M0,.5 L1,.5 M.5,.5 L.5,0', |
| 163 |
/** ┤ */ T_LEFT = 'M.5,0 L.5,1 M.5,.5 L0,.5', |
| 164 |
/** ├ */ T_RIGHT = 'M.5,0 L.5,1 M.5,.5 L1,.5', |
| 165 |
/** ┬ */ T_BOTTOM = 'M0,.5 L1,.5 M.5,.5 L.5,1', |
| 166 |
|
| 167 |
/** ┼ */ CROSS = 'M0,.5 L1,.5 M.5,0 L.5,1', |
| 168 |
|
| 169 |
/** ╌ */ TWO_DASHES_HORIZONTAL = 'M.1,.5 L.4,.5 M.6,.5 L.9,.5', // .2 empty, .3 filled |
| 170 |
/** ┄ */ THREE_DASHES_HORIZONTAL = 'M.0667,.5 L.2667,.5 M.4,.5 L.6,.5 M.7333,.5 L.9333,.5', // .1333 empty, .2 filled |
| 171 |
/** ┉ */ FOUR_DASHES_HORIZONTAL = 'M.05,.5 L.2,.5 M.3,.5 L.45,.5 M.55,.5 L.7,.5 M.8,.5 L.95,.5', // .1 empty, .15 filled |
| 172 |
/** ╎ */ TWO_DASHES_VERTICAL = 'M.5,.1 L.5,.4 M.5,.6 L.5,.9', |
| 173 |
/** ┆ */ THREE_DASHES_VERTICAL = 'M.5,.0667 L.5,.2667 M.5,.4 L.5,.6 M.5,.7333 L.5,.9333', |
| 174 |
/** ┊ */ FOUR_DASHES_VERTICAL = 'M.5,.05 L.5,.2 M.5,.3 L.5,.45 L.5,.55 M.5,.7 L.5,.95', |
| 175 |
} |
| 176 |
|
| 177 |
const enum Style { |
| 178 |
NORMAL = 1, |
| 179 |
BOLD = 3 |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @param xp The percentage of 15% of the x axis. |
| 184 |
* @param yp The percentage of 15% of the x axis on the y axis. |
| 185 |
*/ |
| 186 |
type DrawFunctionDefinition = (xp: number, yp: number) => string; |
| 187 |
|
| 188 |
/** |
| 189 |
* This contains the definitions of all box drawing characters in the format of SVG paths (ie. the |
| 190 |
* svg d attribute). |
| 191 |
*/ |
| 192 |
export const boxDrawingDefinitions: { [character: string]: { [fontWeight: number]: string | DrawFunctionDefinition } | undefined } = { |
| 193 |
// Uniform normal and bold |
| 194 |
'─': { [Style.NORMAL]: Shapes.LEFT_TO_RIGHT }, |
| 195 |
'━': { [Style.BOLD]: Shapes.LEFT_TO_RIGHT }, |
| 196 |
'│': { [Style.NORMAL]: Shapes.TOP_TO_BOTTOM }, |
| 197 |
'┃': { [Style.BOLD]: Shapes.TOP_TO_BOTTOM }, |
| 198 |
'┌': { [Style.NORMAL]: Shapes.RIGHT_TO_BOTTOM }, |
| 199 |
'┏': { [Style.BOLD]: Shapes.RIGHT_TO_BOTTOM }, |
| 200 |
'┐': { [Style.NORMAL]: Shapes.LEFT_TO_BOTTOM }, |
| 201 |
'┓': { [Style.BOLD]: Shapes.LEFT_TO_BOTTOM }, |
| 202 |
'└': { [Style.NORMAL]: Shapes.TOP_TO_RIGHT }, |
| 203 |
'┗': { [Style.BOLD]: Shapes.TOP_TO_RIGHT }, |
| 204 |
'┘': { [Style.NORMAL]: Shapes.TOP_TO_LEFT }, |
| 205 |
'┛': { [Style.BOLD]: Shapes.TOP_TO_LEFT }, |
| 206 |
'├': { [Style.NORMAL]: Shapes.T_RIGHT }, |
| 207 |
'┣': { [Style.BOLD]: Shapes.T_RIGHT }, |
| 208 |
'┤': { [Style.NORMAL]: Shapes.T_LEFT }, |
| 209 |
'┫': { [Style.BOLD]: Shapes.T_LEFT }, |
| 210 |
'┬': { [Style.NORMAL]: Shapes.T_BOTTOM }, |
| 211 |
'┳': { [Style.BOLD]: Shapes.T_BOTTOM }, |
| 212 |
'┴': { [Style.NORMAL]: Shapes.T_TOP }, |
| 213 |
'┻': { [Style.BOLD]: Shapes.T_TOP }, |
| 214 |
'┼': { [Style.NORMAL]: Shapes.CROSS }, |
| 215 |
'╋': { [Style.BOLD]: Shapes.CROSS }, |
| 216 |
'╴': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT }, |
| 217 |
'╸': { [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 218 |
'╵': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP }, |
| 219 |
'╹': { [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 220 |
'╶': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT }, |
| 221 |
'╺': { [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 222 |
'╷': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM }, |
| 223 |
'╻': { [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 224 |
|
| 225 |
// Double border |
| 226 |
'═': { [Style.NORMAL]: (xp, yp) => `M0,${.5 - yp} L1,${.5 - yp} M0,${.5 + yp} L1,${.5 + yp}` }, |
| 227 |
'║': { [Style.NORMAL]: (xp, yp) => `M${.5 - xp},0 L${.5 - xp},1 M${.5 + xp},0 L${.5 + xp},1` }, |
| 228 |
'╒': { [Style.NORMAL]: (xp, yp) => `M.5,1 L.5,${.5 - yp} L1,${.5 - yp} M.5,${.5 + yp} L1,${.5 + yp}` }, |
| 229 |
'╓': { [Style.NORMAL]: (xp, yp) => `M${.5 - xp},1 L${.5 - xp},.5 L1,.5 M${.5 + xp},.5 L${.5 + xp},1` }, |
| 230 |
'╔': { [Style.NORMAL]: (xp, yp) => `M1,${.5 - yp} L${.5 - xp},${.5 - yp} L${.5 - xp},1 M1,${.5 + yp} L${.5 + xp},${.5 + yp} L${.5 + xp},1` }, |
| 231 |
'╕': { [Style.NORMAL]: (xp, yp) => `M0,${.5 - yp} L.5,${.5 - yp} L.5,1 M0,${.5 + yp} L.5,${.5 + yp}` }, |
| 232 |
'╖': { [Style.NORMAL]: (xp, yp) => `M${.5 + xp},1 L${.5 + xp},.5 L0,.5 M${.5 - xp},.5 L${.5 - xp},1` }, |
| 233 |
'╗': { [Style.NORMAL]: (xp, yp) => `M0,${.5 + yp} L${.5 - xp},${.5 + yp} L${.5 - xp},1 M0,${.5 - yp} L${.5 + xp},${.5 - yp} L${.5 + xp},1` }, |
| 234 |
'╘': { [Style.NORMAL]: (xp, yp) => `M.5,0 L.5,${.5 + yp} L1,${.5 + yp} M.5,${.5 - yp} L1,${.5 - yp}` }, |
| 235 |
'╙': { [Style.NORMAL]: (xp, yp) => `M1,.5 L${.5 - xp},.5 L${.5 - xp},0 M${.5 + xp},.5 L${.5 + xp},0` }, |
| 236 |
'╚': { [Style.NORMAL]: (xp, yp) => `M1,${.5 - yp} L${.5 + xp},${.5 - yp} L${.5 + xp},0 M1,${.5 + yp} L${.5 - xp},${.5 + yp} L${.5 - xp},0` }, |
| 237 |
'╛': { [Style.NORMAL]: (xp, yp) => `M0,${.5 + yp} L.5,${.5 + yp} L.5,0 M0,${.5 - yp} L.5,${.5 - yp}` }, |
| 238 |
'╜': { [Style.NORMAL]: (xp, yp) => `M0,.5 L${.5 + xp},.5 L${.5 + xp},0 M${.5 - xp},.5 L${.5 - xp},0` }, |
| 239 |
'╝': { [Style.NORMAL]: (xp, yp) => `M0,${.5 - yp} L${.5 - xp},${.5 - yp} L${.5 - xp},0 M0,${.5 + yp} L${.5 + xp},${.5 + yp} L${.5 + xp},0` }, |
| 240 |
'╞': { [Style.NORMAL]: (xp, yp) => `${Shapes.TOP_TO_BOTTOM} M.5,${.5 - yp} L1,${.5 - yp} M.5,${.5 + yp} L1,${.5 + yp}` }, |
| 241 |
'╟': { [Style.NORMAL]: (xp, yp) => `M${.5 - xp},0 L${.5 - xp},1 M${.5 + xp},0 L${.5 + xp},1 M${.5 + xp},.5 L1,.5` }, |
| 242 |
'╠': { [Style.NORMAL]: (xp, yp) => `M${.5 - xp},0 L${.5 - xp},1 M1,${.5 + yp} L${.5 + xp},${.5 + yp} L${.5 + xp},1 M1,${.5 - yp} L${.5 + xp},${.5 - yp} L${.5 + xp},0` }, |
| 243 |
'╡': { [Style.NORMAL]: (xp, yp) => `${Shapes.TOP_TO_BOTTOM} M0,${.5 - yp} L.5,${.5 - yp} M0,${.5 + yp} L.5,${.5 + yp}` }, |
| 244 |
'╢': { [Style.NORMAL]: (xp, yp) => `M0,.5 L${.5 - xp},.5 M${.5 - xp},0 L${.5 - xp},1 M${.5 + xp},0 L${.5 + xp},1` }, |
| 245 |
'╣': { [Style.NORMAL]: (xp, yp) => `M${.5 + xp},0 L${.5 + xp},1 M0,${.5 + yp} L${.5 - xp},${.5 + yp} L${.5 - xp},1 M0,${.5 - yp} L${.5 - xp},${.5 - yp} L${.5 - xp},0` }, |
| 246 |
'╤': { [Style.NORMAL]: (xp, yp) => `M0,${.5 - yp} L1,${.5 - yp} M0,${.5 + yp} L1,${.5 + yp} M.5,${.5 + yp} L.5,1` }, |
| 247 |
'╥': { [Style.NORMAL]: (xp, yp) => `${Shapes.LEFT_TO_RIGHT} M${.5 - xp},.5 L${.5 - xp},1 M${.5 + xp},.5 L${.5 + xp},1` }, |
| 248 |
'╦': { [Style.NORMAL]: (xp, yp) => `M0,${.5 - yp} L1,${.5 - yp} M0,${.5 + yp} L${.5 - xp},${.5 + yp} L${.5 - xp},1 M1,${.5 + yp} L${.5 + xp},${.5 + yp} L${.5 + xp},1` }, |
| 249 |
'╧': { [Style.NORMAL]: (xp, yp) => `M.5,0 L.5,${.5 - yp} M0,${.5 - yp} L1,${.5 - yp} M0,${.5 + yp} L1,${.5 + yp}` }, |
| 250 |
'╨': { [Style.NORMAL]: (xp, yp) => `${Shapes.LEFT_TO_RIGHT} M${.5 - xp},.5 L${.5 - xp},0 M${.5 + xp},.5 L${.5 + xp},0` }, |
| 251 |
'╩': { [Style.NORMAL]: (xp, yp) => `M0,${.5 + yp} L1,${.5 + yp} M0,${.5 - yp} L${.5 - xp},${.5 - yp} L${.5 - xp},0 M1,${.5 - yp} L${.5 + xp},${.5 - yp} L${.5 + xp},0` }, |
| 252 |
'╪': { [Style.NORMAL]: (xp, yp) => `${Shapes.TOP_TO_BOTTOM} M0,${.5 - yp} L1,${.5 - yp} M0,${.5 + yp} L1,${.5 + yp}` }, |
| 253 |
'╫': { [Style.NORMAL]: (xp, yp) => `${Shapes.LEFT_TO_RIGHT} M${.5 - xp},0 L${.5 - xp},1 M${.5 + xp},0 L${.5 + xp},1` }, |
| 254 |
'╬': { [Style.NORMAL]: (xp, yp) => `M0,${.5 + yp} L${.5 - xp},${.5 + yp} L${.5 - xp},1 M1,${.5 + yp} L${.5 + xp},${.5 + yp} L${.5 + xp},1 M0,${.5 - yp} L${.5 - xp},${.5 - yp} L${.5 - xp},0 M1,${.5 - yp} L${.5 + xp},${.5 - yp} L${.5 + xp},0` }, |
| 255 |
|
| 256 |
// Diagonal |
| 257 |
'╱': { [Style.NORMAL]: 'M1,0 L0,1' }, |
| 258 |
'╲': { [Style.NORMAL]: 'M0,0 L1,1' }, |
| 259 |
'╳': { [Style.NORMAL]: 'M1,0 L0,1 M0,0 L1,1' }, |
| 260 |
|
| 261 |
// Mixed weight |
| 262 |
'╼': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 263 |
'╽': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 264 |
'╾': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 265 |
'╿': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 266 |
'┍': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 267 |
'┎': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 268 |
'┑': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 269 |
'┒': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 270 |
'┕': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 271 |
'┖': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 272 |
'┙': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 273 |
'┚': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 274 |
'┝': { [Style.NORMAL]: Shapes.TOP_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 275 |
'┞': { [Style.NORMAL]: Shapes.RIGHT_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 276 |
'┟': { [Style.NORMAL]: Shapes.TOP_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 277 |
'┠': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: Shapes.TOP_TO_BOTTOM }, |
| 278 |
'┡': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: Shapes.TOP_TO_RIGHT }, |
| 279 |
'┢': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: Shapes.RIGHT_TO_BOTTOM }, |
| 280 |
'┥': { [Style.NORMAL]: Shapes.TOP_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 281 |
'┦': { [Style.NORMAL]: Shapes.LEFT_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 282 |
'┧': { [Style.NORMAL]: Shapes.TOP_TO_LEFT, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 283 |
'┨': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: Shapes.TOP_TO_BOTTOM }, |
| 284 |
'┩': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: Shapes.TOP_TO_LEFT }, |
| 285 |
'┪': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: Shapes.LEFT_TO_BOTTOM }, |
| 286 |
'┭': { [Style.NORMAL]: Shapes.RIGHT_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 287 |
'┮': { [Style.NORMAL]: Shapes.LEFT_TO_BOTTOM, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 288 |
'┯': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: Shapes.LEFT_TO_RIGHT }, |
| 289 |
'┰': { [Style.NORMAL]: Shapes.LEFT_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 290 |
'┱': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: Shapes.LEFT_TO_BOTTOM }, |
| 291 |
'┲': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: Shapes.RIGHT_TO_BOTTOM }, |
| 292 |
'┵': { [Style.NORMAL]: Shapes.TOP_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 293 |
'┶': { [Style.NORMAL]: Shapes.TOP_TO_LEFT, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 294 |
'┷': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: Shapes.LEFT_TO_RIGHT }, |
| 295 |
'┸': { [Style.NORMAL]: Shapes.LEFT_TO_RIGHT, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 296 |
'┹': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: Shapes.TOP_TO_LEFT }, |
| 297 |
'┺': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: Shapes.TOP_TO_RIGHT }, |
| 298 |
'┽': { [Style.NORMAL]: `${Shapes.TOP_TO_BOTTOM} ${Shapes.MIDDLE_TO_RIGHT}`, [Style.BOLD]: Shapes.MIDDLE_TO_LEFT }, |
| 299 |
'┾': { [Style.NORMAL]: `${Shapes.TOP_TO_BOTTOM} ${Shapes.MIDDLE_TO_LEFT}`, [Style.BOLD]: Shapes.MIDDLE_TO_RIGHT }, |
| 300 |
'┿': { [Style.NORMAL]: Shapes.TOP_TO_BOTTOM, [Style.BOLD]: Shapes.LEFT_TO_RIGHT }, |
| 301 |
'╀': { [Style.NORMAL]: `${Shapes.LEFT_TO_RIGHT} ${Shapes.MIDDLE_TO_BOTTOM}`, [Style.BOLD]: Shapes.MIDDLE_TO_TOP }, |
| 302 |
'╁': { [Style.NORMAL]: `${Shapes.MIDDLE_TO_TOP} ${Shapes.LEFT_TO_RIGHT}`, [Style.BOLD]: Shapes.MIDDLE_TO_BOTTOM }, |
| 303 |
'╂': { [Style.NORMAL]: Shapes.LEFT_TO_RIGHT, [Style.BOLD]: Shapes.TOP_TO_BOTTOM }, |
| 304 |
'╃': { [Style.NORMAL]: Shapes.RIGHT_TO_BOTTOM, [Style.BOLD]: Shapes.TOP_TO_LEFT }, |
| 305 |
'╄': { [Style.NORMAL]: Shapes.LEFT_TO_BOTTOM, [Style.BOLD]: Shapes.TOP_TO_RIGHT }, |
| 306 |
'╅': { [Style.NORMAL]: Shapes.TOP_TO_RIGHT, [Style.BOLD]: Shapes.LEFT_TO_BOTTOM }, |
| 307 |
'╆': { [Style.NORMAL]: Shapes.TOP_TO_LEFT, [Style.BOLD]: Shapes.RIGHT_TO_BOTTOM }, |
| 308 |
'╇': { [Style.NORMAL]: Shapes.MIDDLE_TO_BOTTOM, [Style.BOLD]: `${Shapes.MIDDLE_TO_TOP} ${Shapes.LEFT_TO_RIGHT}` }, |
| 309 |
'╈': { [Style.NORMAL]: Shapes.MIDDLE_TO_TOP, [Style.BOLD]: `${Shapes.LEFT_TO_RIGHT} ${Shapes.MIDDLE_TO_BOTTOM}` }, |
| 310 |
'╉': { [Style.NORMAL]: Shapes.MIDDLE_TO_RIGHT, [Style.BOLD]: `${Shapes.TOP_TO_BOTTOM} ${Shapes.MIDDLE_TO_LEFT}` }, |
| 311 |
'╊': { [Style.NORMAL]: Shapes.MIDDLE_TO_LEFT, [Style.BOLD]: `${Shapes.TOP_TO_BOTTOM} ${Shapes.MIDDLE_TO_RIGHT}` }, |
| 312 |
|
| 313 |
// Dashed |
| 314 |
'╌': { [Style.NORMAL]: Shapes.TWO_DASHES_HORIZONTAL }, |
| 315 |
'╍': { [Style.BOLD]: Shapes.TWO_DASHES_HORIZONTAL }, |
| 316 |
'┄': { [Style.NORMAL]: Shapes.THREE_DASHES_HORIZONTAL }, |
| 317 |
'┅': { [Style.BOLD]: Shapes.THREE_DASHES_HORIZONTAL }, |
| 318 |
'┈': { [Style.NORMAL]: Shapes.FOUR_DASHES_HORIZONTAL }, |
| 319 |
'┉': { [Style.BOLD]: Shapes.FOUR_DASHES_HORIZONTAL }, |
| 320 |
'╎': { [Style.NORMAL]: Shapes.TWO_DASHES_VERTICAL }, |
| 321 |
'╏': { [Style.BOLD]: Shapes.TWO_DASHES_VERTICAL }, |
| 322 |
'┆': { [Style.NORMAL]: Shapes.THREE_DASHES_VERTICAL }, |
| 323 |
'┇': { [Style.BOLD]: Shapes.THREE_DASHES_VERTICAL }, |
| 324 |
'┊': { [Style.NORMAL]: Shapes.FOUR_DASHES_VERTICAL }, |
| 325 |
'┋': { [Style.BOLD]: Shapes.FOUR_DASHES_VERTICAL }, |
| 326 |
|
| 327 |
// Curved |
| 328 |
'╭': { [Style.NORMAL]: (xp, yp) => `M.5,1 L.5,${.5 + (yp / .15 * .5)} C.5,${.5 + (yp / .15 * .5)},.5,.5,1,.5` }, |
| 329 |
'╮': { [Style.NORMAL]: (xp, yp) => `M.5,1 L.5,${.5 + (yp / .15 * .5)} C.5,${.5 + (yp / .15 * .5)},.5,.5,0,.5` }, |
| 330 |
'╯': { [Style.NORMAL]: (xp, yp) => `M.5,0 L.5,${.5 - (yp / .15 * .5)} C.5,${.5 - (yp / .15 * .5)},.5,.5,0,.5` }, |
| 331 |
'╰': { [Style.NORMAL]: (xp, yp) => `M.5,0 L.5,${.5 - (yp / .15 * .5)} C.5,${.5 - (yp / .15 * .5)},.5,.5,1,.5` } |
| 332 |
}; |
| 333 |
|
| 334 |
interface IVectorShape { |
| 335 |
d: string; |
| 336 |
type: VectorType; |
| 337 |
leftPadding?: number; |
| 338 |
rightPadding?: number; |
| 339 |
} |
| 340 |
|
| 341 |
const enum VectorType { |
| 342 |
FILL, |
| 343 |
STROKE |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* This contains the definitions of the primarily used box drawing characters as vector shapes. The |
| 348 |
* reason these characters are defined specially is to avoid common problems if a user's font has |
| 349 |
* not been patched with powerline characters and also to get pixel perfect rendering as rendering |
| 350 |
* issues can occur around AA/SPAA. |
| 351 |
* |
| 352 |
* The line variants draw beyond the cell and get clipped to ensure the end of the line is not |
| 353 |
* visible. |
| 354 |
* |
| 355 |
* Original symbols defined in https://github.com/powerline/fontpatcher |
| 356 |
*/ |
| 357 |
export const powerlineDefinitions: { [index: string]: IVectorShape } = { |
| 358 |
// Git branch |
| 359 |
'\u{E0A0}': { d: 'M.3,1 L.03,1 L.03,.88 C.03,.82,.06,.78,.11,.73 C.15,.7,.2,.68,.28,.65 L.43,.6 C.49,.58,.53,.56,.56,.53 C.59,.5,.6,.47,.6,.43 L.6,.27 L.4,.27 L.69,.1 L.98,.27 L.78,.27 L.78,.46 C.78,.52,.76,.56,.72,.61 C.68,.66,.63,.67,.56,.7 L.48,.72 C.42,.74,.38,.76,.35,.78 C.32,.8,.31,.84,.31,.88 L.31,1 M.3,.5 L.03,.59 L.03,.09 L.3,.09 L.3,.655', type: VectorType.FILL }, |
| 360 |
// L N |
| 361 |
'\u{E0A1}': { d: 'M.7,.4 L.7,.47 L.2,.47 L.2,.03 L.355,.03 L.355,.4 L.705,.4 M.7,.5 L.86,.5 L.86,.95 L.69,.95 L.44,.66 L.46,.86 L.46,.95 L.3,.95 L.3,.49 L.46,.49 L.71,.78 L.69,.565 L.69,.5', type: VectorType.FILL }, |
| 362 |
// Lock |
| 363 |
'\u{E0A2}': { d: 'M.25,.94 C.16,.94,.11,.92,.11,.87 L.11,.53 C.11,.48,.15,.455,.23,.45 L.23,.3 C.23,.25,.26,.22,.31,.19 C.36,.16,.43,.15,.51,.15 C.59,.15,.66,.16,.71,.19 C.77,.22,.79,.26,.79,.3 L.79,.45 C.87,.45,.91,.48,.91,.53 L.91,.87 C.91,.92,.86,.94,.77,.94 L.24,.94 M.53,.2 C.49,.2,.45,.21,.42,.23 C.39,.25,.38,.27,.38,.3 L.38,.45 L.68,.45 L.68,.3 C.68,.27,.67,.25,.64,.23 C.61,.21,.58,.2,.53,.2 M.58,.82 L.58,.66 C.63,.65,.65,.63,.65,.6 C.65,.58,.64,.57,.61,.56 C.58,.55,.56,.54,.52,.54 C.48,.54,.46,.55,.43,.56 C.4,.57,.39,.59,.39,.6 C.39,.63,.41,.64,.46,.66 L.46,.82 L.57,.82', type: VectorType.FILL }, |
| 364 |
// Right triangle solid |
| 365 |
'\u{E0B0}': { d: 'M0,0 L1,.5 L0,1', type: VectorType.FILL, rightPadding: 2 }, |
| 366 |
// Right triangle line |
| 367 |
'\u{E0B1}': { d: 'M-1,-.5 L1,.5 L-1,1.5', type: VectorType.STROKE, leftPadding: 1, rightPadding: 1 }, |
| 368 |
// Left triangle solid |
| 369 |
'\u{E0B2}': { d: 'M1,0 L0,.5 L1,1', type: VectorType.FILL, leftPadding: 2 }, |
| 370 |
// Left triangle line |
| 371 |
'\u{E0B3}': { d: 'M2,-.5 L0,.5 L2,1.5', type: VectorType.STROKE, leftPadding: 1, rightPadding: 1 }, |
| 372 |
// Right semi-circle solid |
| 373 |
'\u{E0B4}': { d: 'M0,0 L0,1 C0.552,1,1,0.776,1,.5 C1,0.224,0.552,0,0,0', type: VectorType.FILL, rightPadding: 1 }, |
| 374 |
// Right semi-circle line |
| 375 |
'\u{E0B5}': { d: 'M.2,1 C.422,1,.8,.826,.78,.5 C.8,.174,0.422,0,.2,0', type: VectorType.STROKE, rightPadding: 1 }, |
| 376 |
// Left semi-circle solid |
| 377 |
'\u{E0B6}': { d: 'M1,0 L1,1 C0.448,1,0,0.776,0,.5 C0,0.224,0.448,0,1,0', type: VectorType.FILL, leftPadding: 1 }, |
| 378 |
// Left semi-circle line |
| 379 |
'\u{E0B7}': { d: 'M.8,1 C0.578,1,0.2,.826,.22,.5 C0.2,0.174,0.578,0,0.8,0', type: VectorType.STROKE, leftPadding: 1 }, |
| 380 |
// Lower left triangle |
| 381 |
'\u{E0B8}': { d: 'M-.5,-.5 L1.5,1.5 L-.5,1.5', type: VectorType.FILL }, |
| 382 |
// Backslash separator |
| 383 |
'\u{E0B9}': { d: 'M-.5,-.5 L1.5,1.5', type: VectorType.STROKE, leftPadding: 1, rightPadding: 1 }, |
| 384 |
// Lower right triangle |
| 385 |
'\u{E0BA}': { d: 'M1.5,-.5 L-.5,1.5 L1.5,1.5', type: VectorType.FILL }, |
| 386 |
// Upper left triangle |
| 387 |
'\u{E0BC}': { d: 'M1.5,-.5 L-.5,1.5 L-.5,-.5', type: VectorType.FILL }, |
| 388 |
// Forward slash separator |
| 389 |
'\u{E0BD}': { d: 'M1.5,-.5 L-.5,1.5', type: VectorType.STROKE, leftPadding: 1, rightPadding: 1 }, |
| 390 |
// Upper right triangle |
| 391 |
'\u{E0BE}': { d: 'M-.5,-.5 L1.5,1.5 L1.5,-.5', type: VectorType.FILL } |
| 392 |
}; |
| 393 |
// Forward slash separator redundant |
| 394 |
powerlineDefinitions['\u{E0BB}'] = powerlineDefinitions['\u{E0BD}']; |
| 395 |
// Backslash separator redundant |
| 396 |
powerlineDefinitions['\u{E0BF}'] = powerlineDefinitions['\u{E0B9}']; |
| 397 |
|
| 398 |
/** |
| 399 |
* Try drawing a custom block element or box drawing character, returning whether it was |
| 400 |
* successfully drawn. |
| 401 |
*/ |
| 402 |
export function tryDrawCustomChar( |
| 403 |
ctx: CanvasRenderingContext2D, |
| 404 |
c: string, |
| 405 |
xOffset: number, |
| 406 |
yOffset: number, |
| 407 |
deviceCellWidth: number, |
| 408 |
deviceCellHeight: number, |
| 409 |
fontSize: number, |
| 410 |
devicePixelRatio: number |
| 411 |
): boolean { |
| 412 |
const blockElementDefinition = blockElementDefinitions[c]; |
| 413 |
if (blockElementDefinition) { |
| 414 |
drawBlockElementChar(ctx, blockElementDefinition, xOffset, yOffset, deviceCellWidth, deviceCellHeight); |
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
const patternDefinition = patternCharacterDefinitions[c]; |
| 419 |
if (patternDefinition) { |
| 420 |
drawPatternChar(ctx, patternDefinition, xOffset, yOffset, deviceCellWidth, deviceCellHeight); |
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
const boxDrawingDefinition = boxDrawingDefinitions[c]; |
| 425 |
if (boxDrawingDefinition) { |
| 426 |
drawBoxDrawingChar(ctx, boxDrawingDefinition, xOffset, yOffset, deviceCellWidth, deviceCellHeight, devicePixelRatio); |
| 427 |
return true; |
| 428 |
} |
| 429 |
|
| 430 |
const powerlineDefinition = powerlineDefinitions[c]; |
| 431 |
if (powerlineDefinition) { |
| 432 |
drawPowerlineChar(ctx, powerlineDefinition, xOffset, yOffset, deviceCellWidth, deviceCellHeight, fontSize, devicePixelRatio); |
| 433 |
return true; |
| 434 |
} |
| 435 |
|
| 436 |
return false; |
| 437 |
} |
| 438 |
|
| 439 |
function drawBlockElementChar( |
| 440 |
ctx: CanvasRenderingContext2D, |
| 441 |
charDefinition: IBlockVector[], |
| 442 |
xOffset: number, |
| 443 |
yOffset: number, |
| 444 |
deviceCellWidth: number, |
| 445 |
deviceCellHeight: number |
| 446 |
): void { |
| 447 |
for (let i = 0; i < charDefinition.length; i++) { |
| 448 |
const box = charDefinition[i]; |
| 449 |
const xEighth = deviceCellWidth / 8; |
| 450 |
const yEighth = deviceCellHeight / 8; |
| 451 |
ctx.fillRect( |
| 452 |
xOffset + box.x * xEighth, |
| 453 |
yOffset + box.y * yEighth, |
| 454 |
box.w * xEighth, |
| 455 |
box.h * yEighth |
| 456 |
); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
const cachedPatterns: Map<PatternDefinition, Map</* fillStyle */string, CanvasPattern>> = new Map(); |
| 461 |
|
| 462 |
function drawPatternChar( |
| 463 |
ctx: CanvasRenderingContext2D, |
| 464 |
charDefinition: number[][], |
| 465 |
xOffset: number, |
| 466 |
yOffset: number, |
| 467 |
deviceCellWidth: number, |
| 468 |
deviceCellHeight: number |
| 469 |
): void { |
| 470 |
let patternSet = cachedPatterns.get(charDefinition); |
| 471 |
if (!patternSet) { |
| 472 |
patternSet = new Map(); |
| 473 |
cachedPatterns.set(charDefinition, patternSet); |
| 474 |
} |
| 475 |
const fillStyle = ctx.fillStyle; |
| 476 |
if (typeof fillStyle !== 'string') { |
| 477 |
throw new Error(`Unexpected fillStyle type "${fillStyle}"`); |
| 478 |
} |
| 479 |
let pattern = patternSet.get(fillStyle); |
| 480 |
if (!pattern) { |
| 481 |
const width = charDefinition[0].length; |
| 482 |
const height = charDefinition.length; |
| 483 |
const tmpCanvas = ctx.canvas.ownerDocument.createElement('canvas'); |
| 484 |
tmpCanvas.width = width; |
| 485 |
tmpCanvas.height = height; |
| 486 |
const tmpCtx = throwIfFalsy(tmpCanvas.getContext('2d')); |
| 487 |
const imageData = new ImageData(width, height); |
| 488 |
|
| 489 |
// Extract rgba from fillStyle |
| 490 |
let r: number; |
| 491 |
let g: number; |
| 492 |
let b: number; |
| 493 |
let a: number; |
| 494 |
if (fillStyle.startsWith('#')) { |
| 495 |
r = parseInt(fillStyle.slice(1, 3), 16); |
| 496 |
g = parseInt(fillStyle.slice(3, 5), 16); |
| 497 |
b = parseInt(fillStyle.slice(5, 7), 16); |
| 498 |
a = fillStyle.length > 7 && parseInt(fillStyle.slice(7, 9), 16) || 1; |
| 499 |
} else if (fillStyle.startsWith('rgba')) { |
| 500 |
([r, g, b, a] = fillStyle.substring(5, fillStyle.length - 1).split(',').map(e => parseFloat(e))); |
| 501 |
} else { |
| 502 |
throw new Error(`Unexpected fillStyle color format "${fillStyle}" when drawing pattern glyph`); |
| 503 |
} |
| 504 |
|
| 505 |
for (let y = 0; y < height; y++) { |
| 506 |
for (let x = 0; x < width; x++) { |
| 507 |
imageData.data[(y * width + x) * 4 ] = r; |
| 508 |
imageData.data[(y * width + x) * 4 + 1] = g; |
| 509 |
imageData.data[(y * width + x) * 4 + 2] = b; |
| 510 |
imageData.data[(y * width + x) * 4 + 3] = charDefinition[y][x] * (a * 255); |
| 511 |
} |
| 512 |
} |
| 513 |
tmpCtx.putImageData(imageData, 0, 0); |
| 514 |
pattern = throwIfFalsy(ctx.createPattern(tmpCanvas, null)); |
| 515 |
patternSet.set(fillStyle, pattern); |
| 516 |
} |
| 517 |
ctx.fillStyle = pattern; |
| 518 |
ctx.fillRect(xOffset, yOffset, deviceCellWidth, deviceCellHeight); |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Draws the following box drawing characters by mapping a subset of SVG d attribute instructions to |
| 523 |
* canvas draw calls. |
| 524 |
* |
| 525 |
* Box styles: ┎┰┒┍┯┑╓╥╖╒╤╕ ┏┳┓┌┲┓┌┬┐┏┱┐ |
| 526 |
* ┌─┬─┐ ┏━┳━┓ ╔═╦═╗ ┠╂┨┝┿┥╟╫╢╞╪╡ ┡╇┩├╊┫┢╈┪┣╉┤ |
| 527 |
* │ │ │ ┃ ┃ ┃ ║ ║ ║ ┖┸┚┕┷┙╙╨╜╘╧╛ └┴┘└┺┛┗┻┛┗┹┘ |
| 528 |
* ├─┼─┤ ┣━╋━┫ ╠═╬═╣ ┏┱┐┌┲┓┌┬┐┌┬┐ ┏┳┓┌┮┓┌┬┐┏┭┐ |
| 529 |
* │ │ │ ┃ ┃ ┃ ║ ║ ║ ┡╃┤├╄┩├╆┪┢╅┤ ┞╀┦├┾┫┟╁┧┣┽┤ |
| 530 |
* └─┴─┘ ┗━┻━┛ ╚═╩═╝ └┴┘└┴┘└┺┛┗┹┘ └┴┘└┶┛┗┻┛┗┵┘ |
| 531 |
* |
| 532 |
* Other: |
| 533 |
* ╭─╮ ╲ ╱ ╷╻╎╏┆┇┊┋ ╺╾╴ ╌╌╌ ┄┄┄ ┈┈┈ |
| 534 |
* │ │ ╳ ╽╿╎╏┆┇┊┋ ╶╼╸ ╍╍╍ ┅┅┅ ┉┉┉ |
| 535 |
* ╰─╯ ╱ ╲ ╹╵╎╏┆┇┊┋ |
| 536 |
* |
| 537 |
* All box drawing characters: |
| 538 |
* ─ ━ │ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ┌ ┍ ┎ ┏ |
| 539 |
* ┐ ┑ ┒ ┓ └ ┕ ┖ ┗ ┘ ┙ ┚ ┛ ├ ┝ ┞ ┟ |
| 540 |
* ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫ ┬ ┭ ┮ ┯ |
| 541 |
* ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻ ┼ ┽ ┾ ┿ |
| 542 |
* ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╌ ╍ ╎ ╏ |
| 543 |
* ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ |
| 544 |
* ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ ╭ ╮ ╯ |
| 545 |
* ╰ ╱ ╲ ╳ ╴ ╵ ╶ ╷ ╸ ╹ ╺ ╻ ╼ ╽ ╾ ╿ |
| 546 |
* |
| 547 |
* --- |
| 548 |
* |
| 549 |
* Box drawing alignment tests: █ |
| 550 |
* ▉ |
| 551 |
* ╔══╦══╗ ┌──┬──┐ ╭──┬──╮ ╭──┬──╮ ┏━━┳━━┓ ┎┒┏┑ ╷ ╻ ┏┯┓ ┌┰┐ ▊ ╱╲╱╲╳╳╳ |
| 552 |
* ║┌─╨─┐║ │╔═╧═╗│ │╒═╪═╕│ │╓─╁─╖│ ┃┌─╂─┐┃ ┗╃╄┙ ╶┼╴╺╋╸┠┼┨ ┝╋┥ ▋ ╲╱╲╱╳╳╳ |
| 553 |
* ║│╲ ╱│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╿ │┃ ┍╅╆┓ ╵ ╹ ┗┷┛ └┸┘ ▌ ╱╲╱╲╳╳╳ |
| 554 |
* ╠╡ ╳ ╞╣ ├╢ ╟┤ ├┼─┼─┼┤ ├╫─╂─╫┤ ┣┿╾┼╼┿┫ ┕┛┖┚ ┌┄┄┐ ╎ ┏┅┅┓ ┋ ▍ ╲╱╲╱╳╳╳ |
| 555 |
* ║│╱ ╲│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╽ │┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▎ |
| 556 |
* ║└─╥─┘║ │╚═╤═╝│ │╘═╪═╛│ │╙─╀─╜│ ┃└─╂─┘┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▏ |
| 557 |
* ╚══╩══╝ └──┴──┘ ╰──┴──╯ ╰──┴──╯ ┗━━┻━━┛ └╌╌┘ ╎ ┗╍╍┛ ┋ ▁▂▃▄▅▆▇█ |
| 558 |
* |
| 559 |
* Source: https://www.w3.org/2001/06/utf-8-test/UTF-8-demo.html |
| 560 |
*/ |
| 561 |
function drawBoxDrawingChar( |
| 562 |
ctx: CanvasRenderingContext2D, |
| 563 |
charDefinition: { [fontWeight: number]: string | ((xp: number, yp: number) => string) }, |
| 564 |
xOffset: number, |
| 565 |
yOffset: number, |
| 566 |
deviceCellWidth: number, |
| 567 |
deviceCellHeight: number, |
| 568 |
devicePixelRatio: number |
| 569 |
): void { |
| 570 |
ctx.strokeStyle = ctx.fillStyle; |
| 571 |
for (const [fontWeight, instructions] of Object.entries(charDefinition)) { |
| 572 |
ctx.beginPath(); |
| 573 |
ctx.lineWidth = devicePixelRatio * Number.parseInt(fontWeight); |
| 574 |
let actualInstructions: string; |
| 575 |
if (typeof instructions === 'function') { |
| 576 |
const xp = .15; |
| 577 |
const yp = .15 / deviceCellHeight * deviceCellWidth; |
| 578 |
actualInstructions = instructions(xp, yp); |
| 579 |
} else { |
| 580 |
actualInstructions = instructions; |
| 581 |
} |
| 582 |
for (const instruction of actualInstructions.split(' ')) { |
| 583 |
const type = instruction[0]; |
| 584 |
const f = svgToCanvasInstructionMap[type]; |
| 585 |
if (!f) { |
| 586 |
console.error(`Could not find drawing instructions for "${type}"`); |
| 587 |
continue; |
| 588 |
} |
| 589 |
const args: string[] = instruction.substring(1).split(','); |
| 590 |
if (!args[0] || !args[1]) { |
| 591 |
continue; |
| 592 |
} |
| 593 |
f(ctx, translateArgs(args, deviceCellWidth, deviceCellHeight, xOffset, yOffset, true, devicePixelRatio)); |
| 594 |
} |
| 595 |
ctx.stroke(); |
| 596 |
ctx.closePath(); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
function drawPowerlineChar( |
| 601 |
ctx: CanvasRenderingContext2D, |
| 602 |
charDefinition: IVectorShape, |
| 603 |
xOffset: number, |
| 604 |
yOffset: number, |
| 605 |
deviceCellWidth: number, |
| 606 |
deviceCellHeight: number, |
| 607 |
fontSize: number, |
| 608 |
devicePixelRatio: number |
| 609 |
): void { |
| 610 |
// Clip the cell to make sure drawing doesn't occur beyond bounds |
| 611 |
const clipRegion = new Path2D(); |
| 612 |
clipRegion.rect(xOffset, yOffset, deviceCellWidth, deviceCellHeight); |
| 613 |
ctx.clip(clipRegion); |
| 614 |
|
| 615 |
ctx.beginPath(); |
| 616 |
// Scale the stroke with DPR and font size |
| 617 |
const cssLineWidth = fontSize / 12; |
| 618 |
ctx.lineWidth = devicePixelRatio * cssLineWidth; |
| 619 |
for (const instruction of charDefinition.d.split(' ')) { |
| 620 |
const type = instruction[0]; |
| 621 |
const f = svgToCanvasInstructionMap[type]; |
| 622 |
if (!f) { |
| 623 |
console.error(`Could not find drawing instructions for "${type}"`); |
| 624 |
continue; |
| 625 |
} |
| 626 |
const args: string[] = instruction.substring(1).split(','); |
| 627 |
if (!args[0] || !args[1]) { |
| 628 |
continue; |
| 629 |
} |
| 630 |
f(ctx, translateArgs( |
| 631 |
args, |
| 632 |
deviceCellWidth, |
| 633 |
deviceCellHeight, |
| 634 |
xOffset, |
| 635 |
yOffset, |
| 636 |
false, |
| 637 |
devicePixelRatio, |
| 638 |
(charDefinition.leftPadding ?? 0) * (cssLineWidth / 2), |
| 639 |
(charDefinition.rightPadding ?? 0) * (cssLineWidth / 2) |
| 640 |
)); |
| 641 |
} |
| 642 |
if (charDefinition.type === VectorType.STROKE) { |
| 643 |
ctx.strokeStyle = ctx.fillStyle; |
| 644 |
ctx.stroke(); |
| 645 |
} else { |
| 646 |
ctx.fill(); |
| 647 |
} |
| 648 |
ctx.closePath(); |
| 649 |
} |
| 650 |
|
| 651 |
function clamp(value: number, max: number, min: number = 0): number { |
| 652 |
return Math.max(Math.min(value, max), min); |
| 653 |
} |
| 654 |
|
| 655 |
const svgToCanvasInstructionMap: { [index: string]: any } = { |
| 656 |
'C': (ctx: CanvasRenderingContext2D, args: number[]) => ctx.bezierCurveTo(args[0], args[1], args[2], args[3], args[4], args[5]), |
| 657 |
'L': (ctx: CanvasRenderingContext2D, args: number[]) => ctx.lineTo(args[0], args[1]), |
| 658 |
'M': (ctx: CanvasRenderingContext2D, args: number[]) => ctx.moveTo(args[0], args[1]) |
| 659 |
}; |
| 660 |
|
| 661 |
function translateArgs(args: string[], cellWidth: number, cellHeight: number, xOffset: number, yOffset: number, doClamp: boolean, devicePixelRatio: number, leftPadding: number = 0, rightPadding: number = 0): number[] { |
| 662 |
const result = args.map(e => parseFloat(e) || parseInt(e)); |
| 663 |
|
| 664 |
if (result.length < 2) { |
| 665 |
throw new Error('Too few arguments for instruction'); |
| 666 |
} |
| 667 |
|
| 668 |
for (let x = 0; x < result.length; x += 2) { |
| 669 |
// Translate from 0-1 to 0-cellWidth |
| 670 |
result[x] *= cellWidth - (leftPadding * devicePixelRatio) - (rightPadding * devicePixelRatio); |
| 671 |
// Ensure coordinate doesn't escape cell bounds and round to the nearest 0.5 to ensure a crisp |
| 672 |
// line at 100% devicePixelRatio |
| 673 |
if (doClamp && result[x] !== 0) { |
| 674 |
result[x] = clamp(Math.round(result[x] + 0.5) - 0.5, cellWidth, 0); |
| 675 |
} |
| 676 |
// Apply the cell's offset (ie. x*cellWidth) |
| 677 |
result[x] += xOffset + (leftPadding * devicePixelRatio); |
| 678 |
} |
| 679 |
|
| 680 |
for (let y = 1; y < result.length; y += 2) { |
| 681 |
// Translate from 0-1 to 0-cellHeight |
| 682 |
result[y] *= cellHeight; |
| 683 |
// Ensure coordinate doesn't escape cell bounds and round to the nearest 0.5 to ensure a crisp |
| 684 |
// line at 100% devicePixelRatio |
| 685 |
if (doClamp && result[y] !== 0) { |
| 686 |
result[y] = clamp(Math.round(result[y] + 0.5) - 0.5, cellHeight, 0); |
| 687 |
} |
| 688 |
// Apply the cell's offset (ie. x*cellHeight) |
| 689 |
result[y] += yOffset; |
| 690 |
} |
| 691 |
|
| 692 |
return result; |
| 693 |
} |