@@ -92,6 +92,9 @@ export const drawingDividingLine = (
9292 return false ;
9393} ;
9494
95+ // 代码块水平内边距
96+ export const HORIZONTAL_PADDING = 1 ;
97+
9598export const drawingBackground = (
9699 ctx : CanvasRenderingContext2D ,
97100 matrix : TextMatrix ,
@@ -105,22 +108,72 @@ export const drawingBackground = (
105108 ctx . beginPath ( ) ;
106109 const background = item . config [ TEXT_ATTRS . BACKGROUND ] ;
107110 let backgroundWidth = item . width + halfGap ;
111+ let isStart = true ;
112+ let isEnd = true ;
113+
114+ // 检查是否是代码块的开始和结束
115+ if ( i > 0 ) {
116+ const prev = matrix . items [ i - 1 ] ;
117+ isStart = ! prev . config [ TEXT_ATTRS . BACKGROUND ] || prev . config [ TEXT_ATTRS . BACKGROUND ] !== background ;
118+ }
119+
120+ // 合并连续的相同背景色
108121 for ( let k = i + 1 ; k < matrix . items . length ; ++ k ) {
109122 const next = matrix . items [ k ] ;
110123 if ( next . config [ TEXT_ATTRS . BACKGROUND ] === background ) {
111124 backgroundWidth = backgroundWidth + next . width + halfGap ;
112125 next . config [ TEXT_ATTRS . BACKGROUND ] = "" ;
113126 } else {
127+ isEnd = true ;
114128 break ;
115129 }
116130 }
131+
117132 ctx . fillStyle = background ;
118- ctx . fillRect (
119- offsetX - halfGap ,
120- offsetYBaseLine - matrix . originHeight - BACKGROUND_OFFSET - 1 ,
121- backgroundWidth ,
122- matrix . originHeight + BACKGROUND_OFFSET * 2
123- ) ;
133+
134+ // 计算代码块的位置和尺寸,添加水平内边距
135+ const x = offsetX - halfGap - ( isStart ? HORIZONTAL_PADDING : 0 ) ;
136+ const y = offsetYBaseLine - matrix . originHeight - BACKGROUND_OFFSET - 1 ;
137+ const width = backgroundWidth + ( isStart ? HORIZONTAL_PADDING : 0 ) + ( isEnd ? HORIZONTAL_PADDING : 0 ) ;
138+ const height = matrix . originHeight + BACKGROUND_OFFSET * 2 ;
139+ const borderRadius = 4 ;
140+
141+ // 根据是否是代码块的开始和结束来调整圆角
142+ if ( isStart && isEnd ) {
143+ // 单个字符的代码块,四个角都是圆角
144+ ctx . moveTo ( x + borderRadius , y ) ;
145+ ctx . lineTo ( x + width - borderRadius , y ) ;
146+ ctx . arcTo ( x + width , y , x + width , y + borderRadius , borderRadius ) ;
147+ ctx . lineTo ( x + width , y + height - borderRadius ) ;
148+ ctx . arcTo ( x + width , y + height , x + width - borderRadius , y + height , borderRadius ) ;
149+ ctx . lineTo ( x + borderRadius , y + height ) ;
150+ ctx . arcTo ( x , y + height , x , y + height - borderRadius , borderRadius ) ;
151+ ctx . lineTo ( x , y + borderRadius ) ;
152+ ctx . arcTo ( x , y , x + borderRadius , y , borderRadius ) ;
153+ } else if ( isStart ) {
154+ // 代码块开始,只有左边是圆角
155+ ctx . moveTo ( x + borderRadius , y ) ;
156+ ctx . lineTo ( x + width , y ) ;
157+ ctx . lineTo ( x + width , y + height ) ;
158+ ctx . lineTo ( x + borderRadius , y + height ) ;
159+ ctx . arcTo ( x , y + height , x , y + height - borderRadius , borderRadius ) ;
160+ ctx . lineTo ( x , y + borderRadius ) ;
161+ ctx . arcTo ( x , y , x + borderRadius , y , borderRadius ) ;
162+ } else if ( isEnd ) {
163+ // 代码块结束,只有右边是圆角
164+ ctx . moveTo ( x , y ) ;
165+ ctx . lineTo ( x + width - borderRadius , y ) ;
166+ ctx . arcTo ( x + width , y , x + width , y + borderRadius , borderRadius ) ;
167+ ctx . lineTo ( x + width , y + height - borderRadius ) ;
168+ ctx . arcTo ( x + width , y + height , x + width - borderRadius , y + height , borderRadius ) ;
169+ ctx . lineTo ( x , y + height ) ;
170+ ctx . lineTo ( x , y ) ;
171+ } else {
172+ // 代码块中间,没有圆角
173+ ctx . rect ( x , y , width , height ) ;
174+ }
175+
176+ ctx . fill ( ) ;
124177 ctx . closePath ( ) ;
125178 }
126179} ;
0 commit comments