@@ -351,21 +351,78 @@ export class MeasurementEntity implements Entity {
351351 const drawPoints = this . getDrawPoints ( ) ;
352352
353353 if ( ! drawPoints ) {
354- return new Box ( this . startPoint . x , this . startPoint . y , this . startPoint . x , this . startPoint . y ) ;
354+ throw new Error ( 'Failed to get draw points from measurement entity' ) ;
355355 }
356356
357- const extremePoints = [
357+ const lineExtremePoints = [
358358 drawPoints . offsetStartPointMargin ,
359359 drawPoints . offsetStartPointExtend ,
360360 drawPoints . offsetEndPointMargin ,
361361 drawPoints . offsetEndPointExtend ,
362+ // Also include the main measurement line itself in the bounding box calculation for lines
363+ drawPoints . offsetStartPoint ,
364+ drawPoints . offsetEndPoint ,
362365 ] ;
363366
367+ // Calculate text properties
368+ const distance = String (
369+ round ( pointDistance ( this . startPoint , this . endPoint ) , MEASUREMENT_DECIMAL_PLACES )
370+ ) ;
371+ const textHeight = MEASUREMENT_FONT_SIZE ;
372+ // Estimate width: textString.length * fontSize * aspectRatioFactor
373+ const textWidth = distance . length * MEASUREMENT_FONT_SIZE * 0.6 ;
374+
375+ const { midpointMeasurementLineOffset, normalUnit} = drawPoints ;
376+
377+ // Determine text direction (similar to draw method)
378+ const originalTextDirection = normalUnit . rotate90CW ( ) ;
379+ let finalTextDirection = originalTextDirection ;
380+ if (
381+ originalTextDirection . x < - EPSILON ||
382+ ( Math . abs ( originalTextDirection . x ) < EPSILON && originalTextDirection . y > EPSILON )
383+ ) {
384+ finalTextDirection = new Vector ( - originalTextDirection . x , - originalTextDirection . y ) ;
385+ }
386+
387+ // Text center
388+ const textCenterX = midpointMeasurementLineOffset . x ;
389+ const textCenterY = midpointMeasurementLineOffset . y ;
390+
391+ // Half dimensions
392+ const halfTextWidth = textWidth / 2 ;
393+ const halfTextHeight = textHeight / 2 ;
394+
395+ // Text corner calculations
396+ // Vector along the text direction for width, and perpendicular for height
397+ const dirVec = finalTextDirection . normalize ( ) ; // Vector along the text direction
398+ const perpVec = dirVec . rotate90CW ( ) ; // Vector perpendicular to text direction (for height offset)
399+
400+ const textCorners = [
401+ new Point (
402+ textCenterX - dirVec . x * halfTextWidth - perpVec . x * halfTextHeight ,
403+ textCenterY - dirVec . y * halfTextWidth - perpVec . y * halfTextHeight
404+ ) ,
405+ new Point (
406+ textCenterX + dirVec . x * halfTextWidth - perpVec . x * halfTextHeight ,
407+ textCenterY + dirVec . y * halfTextWidth - perpVec . y * halfTextHeight
408+ ) ,
409+ new Point (
410+ textCenterX + dirVec . x * halfTextWidth + perpVec . x * halfTextHeight ,
411+ textCenterY + dirVec . y * halfTextWidth + perpVec . y * halfTextHeight
412+ ) ,
413+ new Point (
414+ textCenterX - dirVec . x * halfTextWidth + perpVec . x * halfTextHeight ,
415+ textCenterY - dirVec . y * halfTextWidth + perpVec . y * halfTextHeight
416+ ) ,
417+ ] ;
418+
419+ const allExtremePoints = [ ...lineExtremePoints , ...textCorners ] ;
420+
364421 return new Box (
365- min ( extremePoints . map ( ( point ) => point . x ) ) ,
366- min ( extremePoints . map ( ( point ) => point . y ) ) ,
367- max ( extremePoints . map ( ( point ) => point . x ) ) ,
368- max ( extremePoints . map ( ( point ) => point . y ) )
422+ min ( allExtremePoints . map ( ( point ) => point . x ) ) ,
423+ min ( allExtremePoints . map ( ( point ) => point . y ) ) ,
424+ max ( allExtremePoints . map ( ( point ) => point . x ) ) ,
425+ max ( allExtremePoints . map ( ( point ) => point . y ) )
369426 ) ;
370427 }
371428
0 commit comments