| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IAttributeData, IColorRGB, IExtendedAttrs } from 'common/Types'; |
| 7 |
import { Attributes, FgFlags, BgFlags, UnderlineStyle, ExtFlags } from 'common/buffer/Constants'; |
| 8 |
|
| 9 |
export class AttributeData implements IAttributeData { |
| 10 |
public static toColorRGB(value: number): IColorRGB { |
| 11 |
return [ |
| 12 |
value >>> Attributes.RED_SHIFT & 255, |
| 13 |
value >>> Attributes.GREEN_SHIFT & 255, |
| 14 |
value & 255 |
| 15 |
]; |
| 16 |
} |
| 17 |
|
| 18 |
public static fromColorRGB(value: IColorRGB): number { |
| 19 |
return (value[0] & 255) << Attributes.RED_SHIFT | (value[1] & 255) << Attributes.GREEN_SHIFT | value[2] & 255; |
| 20 |
} |
| 21 |
|
| 22 |
public clone(): IAttributeData { |
| 23 |
const newObj = new AttributeData(); |
| 24 |
newObj.fg = this.fg; |
| 25 |
newObj.bg = this.bg; |
| 26 |
newObj.extended = this.extended.clone(); |
| 27 |
return newObj; |
| 28 |
} |
| 29 |
|
| 30 |
// data |
| 31 |
public fg = 0; |
| 32 |
public bg = 0; |
| 33 |
public extended: IExtendedAttrs = new ExtendedAttrs(); |
| 34 |
|
| 35 |
// flags |
| 36 |
public isInverse(): number { return this.fg & FgFlags.INVERSE; } |
| 37 |
public isBold(): number { return this.fg & FgFlags.BOLD; } |
| 38 |
public isUnderline(): number { |
| 39 |
if (this.hasExtendedAttrs() && this.extended.underlineStyle !== UnderlineStyle.NONE) { |
| 40 |
return 1; |
| 41 |
} |
| 42 |
return this.fg & FgFlags.UNDERLINE; |
| 43 |
} |
| 44 |
public isBlink(): number { return this.fg & FgFlags.BLINK; } |
| 45 |
public isInvisible(): number { return this.fg & FgFlags.INVISIBLE; } |
| 46 |
public isItalic(): number { return this.bg & BgFlags.ITALIC; } |
| 47 |
public isDim(): number { return this.bg & BgFlags.DIM; } |
| 48 |
public isStrikethrough(): number { return this.fg & FgFlags.STRIKETHROUGH; } |
| 49 |
public isProtected(): number { return this.bg & BgFlags.PROTECTED; } |
| 50 |
public isOverline(): number { return this.bg & BgFlags.OVERLINE; } |
| 51 |
|
| 52 |
// color modes |
| 53 |
public getFgColorMode(): number { return this.fg & Attributes.CM_MASK; } |
| 54 |
public getBgColorMode(): number { return this.bg & Attributes.CM_MASK; } |
| 55 |
public isFgRGB(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_RGB; } |
| 56 |
public isBgRGB(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_RGB; } |
| 57 |
public isFgPalette(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.fg & Attributes.CM_MASK) === Attributes.CM_P256; } |
| 58 |
public isBgPalette(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.bg & Attributes.CM_MASK) === Attributes.CM_P256; } |
| 59 |
public isFgDefault(): boolean { return (this.fg & Attributes.CM_MASK) === 0; } |
| 60 |
public isBgDefault(): boolean { return (this.bg & Attributes.CM_MASK) === 0; } |
| 61 |
public isAttributeDefault(): boolean { return this.fg === 0 && this.bg === 0; } |
| 62 |
|
| 63 |
// colors |
| 64 |
public getFgColor(): number { |
| 65 |
switch (this.fg & Attributes.CM_MASK) { |
| 66 |
case Attributes.CM_P16: |
| 67 |
case Attributes.CM_P256: return this.fg & Attributes.PCOLOR_MASK; |
| 68 |
case Attributes.CM_RGB: return this.fg & Attributes.RGB_MASK; |
| 69 |
default: return -1; // CM_DEFAULT defaults to -1 |
| 70 |
} |
| 71 |
} |
| 72 |
public getBgColor(): number { |
| 73 |
switch (this.bg & Attributes.CM_MASK) { |
| 74 |
case Attributes.CM_P16: |
| 75 |
case Attributes.CM_P256: return this.bg & Attributes.PCOLOR_MASK; |
| 76 |
case Attributes.CM_RGB: return this.bg & Attributes.RGB_MASK; |
| 77 |
default: return -1; // CM_DEFAULT defaults to -1 |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// extended attrs |
| 82 |
public hasExtendedAttrs(): number { |
| 83 |
return this.bg & BgFlags.HAS_EXTENDED; |
| 84 |
} |
| 85 |
public updateExtended(): void { |
| 86 |
if (this.extended.isEmpty()) { |
| 87 |
this.bg &= ~BgFlags.HAS_EXTENDED; |
| 88 |
} else { |
| 89 |
this.bg |= BgFlags.HAS_EXTENDED; |
| 90 |
} |
| 91 |
} |
| 92 |
public getUnderlineColor(): number { |
| 93 |
if ((this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor) { |
| 94 |
switch (this.extended.underlineColor & Attributes.CM_MASK) { |
| 95 |
case Attributes.CM_P16: |
| 96 |
case Attributes.CM_P256: return this.extended.underlineColor & Attributes.PCOLOR_MASK; |
| 97 |
case Attributes.CM_RGB: return this.extended.underlineColor & Attributes.RGB_MASK; |
| 98 |
default: return this.getFgColor(); |
| 99 |
} |
| 100 |
} |
| 101 |
return this.getFgColor(); |
| 102 |
} |
| 103 |
public getUnderlineColorMode(): number { |
| 104 |
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor |
| 105 |
? this.extended.underlineColor & Attributes.CM_MASK |
| 106 |
: this.getFgColorMode(); |
| 107 |
} |
| 108 |
public isUnderlineColorRGB(): boolean { |
| 109 |
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor |
| 110 |
? (this.extended.underlineColor & Attributes.CM_MASK) === Attributes.CM_RGB |
| 111 |
: this.isFgRGB(); |
| 112 |
} |
| 113 |
public isUnderlineColorPalette(): boolean { |
| 114 |
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor |
| 115 |
? (this.extended.underlineColor & Attributes.CM_MASK) === Attributes.CM_P16 |
| 116 |
|| (this.extended.underlineColor & Attributes.CM_MASK) === Attributes.CM_P256 |
| 117 |
: this.isFgPalette(); |
| 118 |
} |
| 119 |
public isUnderlineColorDefault(): boolean { |
| 120 |
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor |
| 121 |
? (this.extended.underlineColor & Attributes.CM_MASK) === 0 |
| 122 |
: this.isFgDefault(); |
| 123 |
} |
| 124 |
public getUnderlineStyle(): UnderlineStyle { |
| 125 |
return this.fg & FgFlags.UNDERLINE |
| 126 |
? (this.bg & BgFlags.HAS_EXTENDED ? this.extended.underlineStyle : UnderlineStyle.SINGLE) |
| 127 |
: UnderlineStyle.NONE; |
| 128 |
} |
| 129 |
public getUnderlineVariantOffset(): number { |
| 130 |
return this.extended.underlineVariantOffset; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Extended attributes for a cell. |
| 137 |
* Holds information about different underline styles and color. |
| 138 |
*/ |
| 139 |
export class ExtendedAttrs implements IExtendedAttrs { |
| 140 |
private _ext: number = 0; |
| 141 |
public get ext(): number { |
| 142 |
if (this._urlId) { |
| 143 |
return ( |
| 144 |
(this._ext & ~ExtFlags.UNDERLINE_STYLE) | |
| 145 |
(this.underlineStyle << 26) |
| 146 |
); |
| 147 |
} |
| 148 |
return this._ext; |
| 149 |
} |
| 150 |
public set ext(value: number) { this._ext = value; } |
| 151 |
|
| 152 |
public get underlineStyle(): UnderlineStyle { |
| 153 |
// Always return the URL style if it has one |
| 154 |
if (this._urlId) { |
| 155 |
return UnderlineStyle.DASHED; |
| 156 |
} |
| 157 |
return (this._ext & ExtFlags.UNDERLINE_STYLE) >> 26; |
| 158 |
} |
| 159 |
public set underlineStyle(value: UnderlineStyle) { |
| 160 |
this._ext &= ~ExtFlags.UNDERLINE_STYLE; |
| 161 |
this._ext |= (value << 26) & ExtFlags.UNDERLINE_STYLE; |
| 162 |
} |
| 163 |
|
| 164 |
public get underlineColor(): number { |
| 165 |
return this._ext & (Attributes.CM_MASK | Attributes.RGB_MASK); |
| 166 |
} |
| 167 |
public set underlineColor(value: number) { |
| 168 |
this._ext &= ~(Attributes.CM_MASK | Attributes.RGB_MASK); |
| 169 |
this._ext |= value & (Attributes.CM_MASK | Attributes.RGB_MASK); |
| 170 |
} |
| 171 |
|
| 172 |
private _urlId: number = 0; |
| 173 |
public get urlId(): number { |
| 174 |
return this._urlId; |
| 175 |
} |
| 176 |
public set urlId(value: number) { |
| 177 |
this._urlId = value; |
| 178 |
} |
| 179 |
|
| 180 |
public get underlineVariantOffset(): number { |
| 181 |
const val = (this._ext & ExtFlags.VARIANT_OFFSET) >> 29; |
| 182 |
if (val < 0) { |
| 183 |
return val ^ 0xFFFFFFF8; |
| 184 |
} |
| 185 |
return val; |
| 186 |
} |
| 187 |
public set underlineVariantOffset(value: number) { |
| 188 |
this._ext &= ~ExtFlags.VARIANT_OFFSET; |
| 189 |
this._ext |= (value << 29) & ExtFlags.VARIANT_OFFSET; |
| 190 |
} |
| 191 |
|
| 192 |
constructor( |
| 193 |
ext: number = 0, |
| 194 |
urlId: number = 0 |
| 195 |
) { |
| 196 |
this._ext = ext; |
| 197 |
this._urlId = urlId; |
| 198 |
} |
| 199 |
|
| 200 |
public clone(): IExtendedAttrs { |
| 201 |
return new ExtendedAttrs(this._ext, this._urlId); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Convenient method to indicate whether the object holds no additional information, |
| 206 |
* that needs to be persistant in the buffer. |
| 207 |
*/ |
| 208 |
public isEmpty(): boolean { |
| 209 |
return this.underlineStyle === UnderlineStyle.NONE && this._urlId === 0; |
| 210 |
} |
| 211 |
} |