1
1
/*
2
- * Copyright 2020 DiffPlug
2
+ * Copyright (C) 2020-2025 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package com .diffplug .common .swt ;
17
17
18
-
19
18
import com .diffplug .common .base .Box ;
20
19
import com .diffplug .common .base .Errors ;
21
20
import com .diffplug .common .base .Preconditions ;
@@ -358,7 +357,7 @@ public static int blockForMessageBox(String title, String message, int style) {
358
357
///////////////////
359
358
/** The cached height of the system font. */
360
359
static int systemFontHeight = 0 ;
361
- static int systemFontWidth = 0 ;
360
+ static double systemFontWidth = 0 ;
362
361
363
362
/** Populates the height and width of the system font. */
364
363
private static void populateSystemFont () {
@@ -368,7 +367,7 @@ private static void populateSystemFont() {
368
367
369
368
FontMetrics metrics = gc .getFontMetrics ();
370
369
systemFontHeight = metrics .getHeight ();
371
- systemFontWidth = metrics .getAverageCharWidth ();
370
+ systemFontWidth = metrics .getAverageCharacterWidth ();
372
371
if (OS .getNative ().isMac ()) {
373
372
// add 20% width on Mac
374
373
systemFontWidth = (systemFontWidth * 12 ) / 10 ;
@@ -387,26 +386,34 @@ public static int systemFontHeight() {
387
386
}
388
387
389
388
/** Returns the width of the system font. */
390
- public static int systemFontWidth () {
389
+ public static double systemFontWidth () {
391
390
if (systemFontWidth == 0 ) {
392
391
populateSystemFont ();
393
392
}
394
393
return systemFontWidth ;
395
394
}
396
395
396
+ public static int systemFontWidthTimes (int numChars ) {
397
+ return (int ) Math .round (systemFontWidth () * numChars );
398
+ }
399
+
400
+ public static int systemFontWidthTimes (String str ) {
401
+ return systemFontWidthTimes (str .length ());
402
+ }
403
+
397
404
/** Returns a distance which is a snug fit for a line of text in the system font. */
398
405
public static int systemFontSnug () {
399
406
return systemFontHeight () + Layouts .defaultMargin ();
400
407
}
401
408
402
409
/** Returns the default width of a button, scaled for the system font. */
403
410
public static int defaultButtonWidth () {
404
- return systemFontWidth () * " Cancel " . length ( );
411
+ return systemFontWidthTimes ( " Cancel " );
405
412
}
406
413
407
414
/** Returns the default width of a dialog. */
408
415
public static int defaultDialogWidth () {
409
- return 50 * systemFontWidth ( );
416
+ return systemFontWidthTimes ( 50 );
410
417
}
411
418
412
419
/** Returns a size which is scaled by the system font's height. */
@@ -421,7 +428,7 @@ public static int scaleByFontHeight(int rows) {
421
428
422
429
/** Returns a point that represents the size of a (cols x rows) grid of characters printed in the standard system font. */
423
430
public static Point scaleByFont (int cols , int rows ) {
424
- return new Point (cols * systemFontWidth ( ), rows * systemFontHeight ());
431
+ return new Point (systemFontWidthTimes ( cols ), rows * systemFontHeight ());
425
432
}
426
433
427
434
/** Returns a dimension which is guaranteed to be comfortable for the given string. */
0 commit comments