| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IDimensions, IRenderDimensions } from 'browser/renderer/shared/Types'; |
| 7 |
|
| 8 |
export function throwIfFalsy<T>(value: T | undefined | null): T { |
| 9 |
if (!value) { |
| 10 |
throw new Error('value must not be falsy'); |
| 11 |
} |
| 12 |
return value; |
| 13 |
} |
| 14 |
|
| 15 |
export function isPowerlineGlyph(codepoint: number): boolean { |
| 16 |
// Only return true for Powerline symbols which require |
| 17 |
// different padding and should be excluded from minimum contrast |
| 18 |
// ratio standards |
| 19 |
return 0xE0A4 <= codepoint && codepoint <= 0xE0D6; |
| 20 |
} |
| 21 |
|
| 22 |
export function isRestrictedPowerlineGlyph(codepoint: number): boolean { |
| 23 |
return 0xE0B0 <= codepoint && codepoint <= 0xE0B7; |
| 24 |
} |
| 25 |
|
| 26 |
function isNerdFontGlyph(codepoint: number): boolean { |
| 27 |
return 0xE000 <= codepoint && codepoint <= 0xF8FF; |
| 28 |
} |
| 29 |
|
| 30 |
function isBoxOrBlockGlyph(codepoint: number): boolean { |
| 31 |
return 0x2500 <= codepoint && codepoint <= 0x259F; |
| 32 |
} |
| 33 |
|
| 34 |
export function isEmoji(codepoint: number): boolean { |
| 35 |
return ( |
| 36 |
codepoint >= 0x1F600 && codepoint <= 0x1F64F || // Emoticons |
| 37 |
codepoint >= 0x1F300 && codepoint <= 0x1F5FF || // Misc Symbols and Pictographs |
| 38 |
codepoint >= 0x1F680 && codepoint <= 0x1F6FF || // Transport and Map |
| 39 |
codepoint >= 0x2600 && codepoint <= 0x26FF || // Misc symbols |
| 40 |
codepoint >= 0x2700 && codepoint <= 0x27BF || // Dingbats |
| 41 |
codepoint >= 0xFE00 && codepoint <= 0xFE0F || // Variation Selectors |
| 42 |
codepoint >= 0x1F900 && codepoint <= 0x1F9FF || // Supplemental Symbols and Pictographs |
| 43 |
codepoint >= 0x1F1E6 && codepoint <= 0x1F1FF |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
export function allowRescaling(codepoint: number | undefined, width: number, glyphSizeX: number, deviceCellWidth: number): boolean { |
| 48 |
return ( |
| 49 |
// Is single cell width |
| 50 |
width === 1 && |
| 51 |
// Glyph exceeds cell bounds, add 50% to avoid hurting readability by rescaling glyphs that |
| 52 |
// barely overlap |
| 53 |
glyphSizeX > Math.ceil(deviceCellWidth * 1.5) && |
| 54 |
// Never rescale ascii |
| 55 |
codepoint !== undefined && codepoint > 0xFF && |
| 56 |
// Never rescale emoji |
| 57 |
!isEmoji(codepoint) && |
| 58 |
// Never rescale powerline or nerd fonts |
| 59 |
!isPowerlineGlyph(codepoint) && !isNerdFontGlyph(codepoint) |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
export function treatGlyphAsBackgroundColor(codepoint: number): boolean { |
| 64 |
return isPowerlineGlyph(codepoint) || isBoxOrBlockGlyph(codepoint); |
| 65 |
} |
| 66 |
|
| 67 |
export function createRenderDimensions(): IRenderDimensions { |
| 68 |
return { |
| 69 |
css: { |
| 70 |
canvas: createDimension(), |
| 71 |
cell: createDimension() |
| 72 |
}, |
| 73 |
device: { |
| 74 |
canvas: createDimension(), |
| 75 |
cell: createDimension(), |
| 76 |
char: { |
| 77 |
width: 0, |
| 78 |
height: 0, |
| 79 |
left: 0, |
| 80 |
top: 0 |
| 81 |
} |
| 82 |
} |
| 83 |
}; |
| 84 |
} |
| 85 |
|
| 86 |
function createDimension(): IDimensions { |
| 87 |
return { |
| 88 |
width: 0, |
| 89 |
height: 0 |
| 90 |
}; |
| 91 |
} |
| 92 |
|
| 93 |
export function computeNextVariantOffset(cellWidth: number, lineWidth: number, currentOffset: number = 0): number { |
| 94 |
return (cellWidth - (Math.round(lineWidth) * 2 - currentOffset)) % (Math.round(lineWidth) * 2); |
| 95 |
} |