@@ -174,6 +174,7 @@ function sameColor(left: GhosttyColor, right: GhosttyColor): boolean {
174174 * every codepoint into String.fromCodePoint at once.
175175 */
176176export function ghosttyCellText ( codepointView : DataView , graphemeLength : number ) : string {
177+ if ( graphemeLength === 1 ) return String . fromCodePoint ( codepointView . getUint32 ( 0 , true ) ) ;
177178 const CHUNK_SIZE = 4_096 ;
178179 let text = "" ;
179180 for ( let start = 0 ; start < graphemeLength ; start += CHUNK_SIZE ) {
@@ -206,6 +207,8 @@ export class GhosttyTerminalCore {
206207 private ptyWriterId = 0 ;
207208 private ptyWriter : ( ( data : string ) => void ) | null = null ;
208209 private scratch = 0 ;
210+ private graphemes = 0 ;
211+ private graphemeCapacity = 0 ;
209212 private style = 0 ;
210213 private scrollbar = 0 ;
211214 private rows : GhosttyRow [ ] = [ ] ;
@@ -878,6 +881,7 @@ export class GhosttyTerminalCore {
878881 this . runtime . free ( this . scrollbar , this . runtime . layout ( "GhosttyTerminalScrollbar" ) . size ) ;
879882 }
880883 if ( this . scratch ) this . runtime . free ( this . scratch , 16 ) ;
884+ if ( this . graphemes ) this . runtime . free ( this . graphemes , this . graphemeCapacity ) ;
881885 for ( const slot of [
882886 this . mouseEventSlot ,
883887 this . mouseEncoderSlot ,
@@ -944,14 +948,14 @@ export class GhosttyTerminalCore {
944948 ) ,
945949 ) ;
946950 const cellsIterator = this . runtime . readPointer ( this . rowCellsSlot ) ;
951+ const { size : styleSize , fields : styleFields } = this . runtime . layout ( "GhosttyStyle" ) ;
947952 const cells : GhosttyCell [ ] = [ ] ;
948953 while (
949954 cells . length < cols &&
950955 this . runtime . call ( "ghostty_render_state_row_cells_next" , cellsIterator ) !== 0
951956 ) {
952957 let foreground = this . getCellColor ( cellsIterator , CELL_DATA . foreground , defaultForeground ) ;
953958 let background = this . getCellColor ( cellsIterator , CELL_DATA . background , defaultBackground ) ;
954- const styleSize = this . runtime . layout ( "GhosttyStyle" ) . size ;
955959 this . runtime . bytes ( this . style , styleSize ) . fill ( 0 ) ;
956960 this . runtime . setField ( this . style , "GhosttyStyle" , "size" , styleSize ) ;
957961 this . runtime . call (
@@ -960,30 +964,30 @@ export class GhosttyTerminalCore {
960964 CELL_DATA . style ,
961965 this . style ,
962966 ) ;
963- const inverse = this . runtime . readField ( this . style , "GhosttyStyle" , "inverse" ) !== 0 ;
964- if ( inverse ) [ foreground , background ] = [ background , foreground ] ;
965- if ( this . runtime . readField ( this . style , "GhosttyStyle" , "faint" ) !== 0 ) {
966- foreground = blend ( foreground , background ) ;
967- }
968967 const graphemeLength = this . getCellU32 ( cellsIterator , CELL_DATA . graphemesLength ) ;
969968 let text = "" ;
970969 if ( graphemeLength > 0 ) {
971970 const bufferSize = graphemeLength * 4 ;
972- const codepoints = this . runtime . alloc ( bufferSize ) ;
971+ if ( bufferSize > this . graphemeCapacity ) {
972+ const capacity = Math . max ( bufferSize , this . graphemeCapacity * 2 ) ;
973+ const buffer = this . runtime . alloc ( capacity ) ;
974+ this . runtime . free ( this . graphemes , this . graphemeCapacity ) ;
975+ this . graphemes = buffer ;
976+ this . graphemeCapacity = capacity ;
977+ }
973978 if (
974979 this . runtime . call (
975980 "ghostty_render_state_row_cells_get" ,
976981 cellsIterator ,
977982 CELL_DATA . graphemes ,
978- codepoints ,
983+ this . graphemes ,
979984 ) === GHOSTTY_SUCCESS
980985 ) {
981986 // Read through a DataView: the byte-array allocator guarantees no
982987 // 4-byte alignment, which a Uint32Array view would require.
983- const codepointView = this . runtime . view ( codepoints , bufferSize ) ;
988+ const codepointView = this . runtime . view ( this . graphemes , bufferSize ) ;
984989 text = ghosttyCellText ( codepointView , graphemeLength ) ;
985990 }
986- this . runtime . free ( codepoints , bufferSize ) ;
987991 }
988992 let wide = 0 ;
989993 if ( text . length === 0 && cells . at ( - 1 ) ?. text . length ) {
@@ -1004,18 +1008,27 @@ export class GhosttyTerminalCore {
10041008 ) ;
10051009 wide = this . runtime . view ( this . scratch + 8 , 4 ) . getUint32 ( 0 , true ) ;
10061010 }
1011+ const selected = this . getCellBool ( cellsIterator , CELL_DATA . selected ) ;
1012+ // Read the style after allocation and ABI calls, which can grow WASM memory.
1013+ const styleView = this . runtime . view ( this . style , styleSize ) ;
1014+ if ( styleView . getUint8 ( styleFields . inverse ! . offset ) !== 0 ) {
1015+ [ foreground , background ] = [ background , foreground ] ;
1016+ }
1017+ if ( styleView . getUint8 ( styleFields . faint ! . offset ) !== 0 ) {
1018+ foreground = blend ( foreground , background ) ;
1019+ }
10071020 cells . push ( {
10081021 text,
10091022 wide,
10101023 foreground,
10111024 background,
1012- bold : this . runtime . readField ( this . style , "GhosttyStyle" , " bold" ) !== 0 ,
1013- italic : this . runtime . readField ( this . style , "GhosttyStyle" , " italic" ) !== 0 ,
1014- invisible : this . runtime . readField ( this . style , "GhosttyStyle" , " invisible" ) !== 0 ,
1015- strikethrough : this . runtime . readField ( this . style , "GhosttyStyle" , " strikethrough" ) !== 0 ,
1016- overline : this . runtime . readField ( this . style , "GhosttyStyle" , " overline" ) !== 0 ,
1017- underline : this . runtime . readField ( this . style , "GhosttyStyle" , "underline" ) !== 0 ,
1018- selected : this . getCellBool ( cellsIterator , CELL_DATA . selected ) ,
1025+ bold : styleView . getUint8 ( styleFields . bold ! . offset ) !== 0 ,
1026+ italic : styleView . getUint8 ( styleFields . italic ! . offset ) !== 0 ,
1027+ invisible : styleView . getUint8 ( styleFields . invisible ! . offset ) !== 0 ,
1028+ strikethrough : styleView . getUint8 ( styleFields . strikethrough ! . offset ) !== 0 ,
1029+ overline : styleView . getUint8 ( styleFields . overline ! . offset ) !== 0 ,
1030+ underline : styleView . getInt32 ( styleFields . underline ! . offset , true ) !== 0 ,
1031+ selected,
10191032 } ) ;
10201033 }
10211034 while ( cells . length < cols ) cells . push ( this . emptyCell ( defaultForeground , defaultBackground ) ) ;
0 commit comments