@@ -54,6 +54,8 @@ public final class TextLayout extends Resource {
54
54
int wrapWidth ;
55
55
int orientation ;
56
56
private double defaultTabWidth ;
57
+ private FontMetrics fixedLineMetrics ;
58
+ private double fixedLineMetricsDy ;
57
59
58
60
int [] lineOffsets ;
59
61
NSRect [] lineBounds ;
@@ -349,6 +351,16 @@ void computeRuns() {
349
351
OS .memmove (lineRange , rangePtr , NSRange .sizeof );
350
352
offsets [numberOfLines ] = (int )lineRange .location ;
351
353
index = lineRange .location + lineRange .length ;
354
+ if (fixedLineMetrics != null ) {
355
+ // Preserve baseline location for best visual results
356
+ final int lineOffset = untranslateOffset (offsets [numberOfLines ]);
357
+ final double realHeight = bounds [numberOfLines ].height ;
358
+ final double realDescent = layoutManager .typesetter ().baselineOffsetInLayoutManager (layoutManager , lineOffset );
359
+ final double realAscent = realHeight - realDescent ;
360
+ final double wantAscent = fixedLineMetrics .ascent ;
361
+ fixedLineMetricsDy = wantAscent - realAscent ;
362
+ bounds [numberOfLines ].height = fixedLineMetrics .height ;
363
+ }
352
364
}
353
365
if (numberOfLines == 0 ) {
354
366
Font font = this .font != null ? this .font : device .systemFont ;
@@ -484,6 +496,7 @@ public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Colo
484
496
fixRect (rect );
485
497
rect .x += pt .x ;
486
498
rect .y += pt .y ;
499
+ if (fixedLineMetrics != null ) rect .height = fixedLineMetrics .height ;
487
500
rect .height = Math .max (rect .height , ascent + descent );
488
501
path .appendBezierPathWithRect (rect );
489
502
}
@@ -515,9 +528,13 @@ public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Colo
515
528
layoutManager .addTemporaryAttribute (OS .NSForegroundColorAttributeName , gc .data .fg , range );
516
529
}
517
530
}
531
+ NSPoint ptGlyphs = new NSPoint ();
532
+ ptGlyphs .x = pt .x ;
533
+ ptGlyphs .y = pt .y ;
534
+ if (fixedLineMetrics != null ) ptGlyphs .y += fixedLineMetricsDy ;
518
535
range .location = 0 ;
519
536
range .length = numberOfGlyphs ;
520
- layoutManager .drawGlyphsForGlyphRange (range , pt );
537
+ layoutManager .drawGlyphsForGlyphRange (range , ptGlyphs );
521
538
if (!defaultFg ) {
522
539
range .location = 0 ;
523
540
range .length = length ;
@@ -754,6 +771,7 @@ public Rectangle getBounds() {
754
771
NSFont nsFont = font .handle ;
755
772
rect .height = layoutManager .defaultLineHeightForFont (nsFont );
756
773
}
774
+ if (fixedLineMetrics != null ) rect .height = fixedLineMetrics .height ;
757
775
rect .height = Math .max (rect .height , ascent + descent ) + spacing ;
758
776
return new Rectangle (0 , 0 , (int )Math .ceil (rect .width ), (int )Math .ceil (rect .height ) + getVerticalIndent ());
759
777
} finally {
@@ -804,6 +822,7 @@ public Rectangle getBounds(int start, int end) {
804
822
top = Math .min (top , (int )rect .y );
805
823
bottom = Math .max (bottom , (int )Math .ceil (rect .y + rect .height ));
806
824
}
825
+ if (fixedLineMetrics != null ) bottom = top + fixedLineMetrics .height ;
807
826
return new Rectangle (left , top , right - left , bottom - top + getVerticalIndent ());
808
827
} finally {
809
828
if (pool != null ) pool .release ();
@@ -1043,6 +1062,7 @@ public FontMetrics getLineMetrics (int lineIndex) {
1043
1062
computeRuns ();
1044
1063
int lineCount = getLineCount ();
1045
1064
if (!(0 <= lineIndex && lineIndex < lineCount )) SWT .error (SWT .ERROR_INVALID_RANGE );
1065
+ if (fixedLineMetrics != null ) return fixedLineMetrics .makeCopy ();
1046
1066
int length = text .length ();
1047
1067
if (length == 0 ) {
1048
1068
Font font = this .font != null ? this .font : device .systemFont ;
@@ -1798,8 +1818,12 @@ public void setDescent (int descent) {
1798
1818
* @since 3.125
1799
1819
*/
1800
1820
public void setFixedLineMetrics (FontMetrics metrics ) {
1801
- if (metrics == null ) return ;
1802
- SWT .error (SWT .ERROR_NOT_IMPLEMENTED );
1821
+ if (metrics == null ) {
1822
+ fixedLineMetrics = null ;
1823
+ return ;
1824
+ }
1825
+
1826
+ fixedLineMetrics = metrics .makeCopy ();
1803
1827
}
1804
1828
1805
1829
/**
0 commit comments