11/*
2- * Copyright 2020 DiffPlug
2+ * Copyright (C) 2020-2025 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package com .diffplug .common .swt ;
1717
18-
1918import com .diffplug .common .base .Box ;
2019import com .diffplug .common .base .Errors ;
2120import com .diffplug .common .base .Preconditions ;
@@ -358,7 +357,7 @@ public static int blockForMessageBox(String title, String message, int style) {
358357 ///////////////////
359358 /** The cached height of the system font. */
360359 static int systemFontHeight = 0 ;
361- static int systemFontWidth = 0 ;
360+ static double systemFontWidth = 0 ;
362361
363362 /** Populates the height and width of the system font. */
364363 private static void populateSystemFont () {
@@ -368,7 +367,7 @@ private static void populateSystemFont() {
368367
369368 FontMetrics metrics = gc .getFontMetrics ();
370369 systemFontHeight = metrics .getHeight ();
371- systemFontWidth = metrics .getAverageCharWidth ();
370+ systemFontWidth = metrics .getAverageCharacterWidth ();
372371 if (OS .getNative ().isMac ()) {
373372 // add 20% width on Mac
374373 systemFontWidth = (systemFontWidth * 12 ) / 10 ;
@@ -387,26 +386,34 @@ public static int systemFontHeight() {
387386 }
388387
389388 /** Returns the width of the system font. */
390- public static int systemFontWidth () {
389+ public static double systemFontWidth () {
391390 if (systemFontWidth == 0 ) {
392391 populateSystemFont ();
393392 }
394393 return systemFontWidth ;
395394 }
396395
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+
397404 /** Returns a distance which is a snug fit for a line of text in the system font. */
398405 public static int systemFontSnug () {
399406 return systemFontHeight () + Layouts .defaultMargin ();
400407 }
401408
402409 /** Returns the default width of a button, scaled for the system font. */
403410 public static int defaultButtonWidth () {
404- return systemFontWidth () * " Cancel " . length ( );
411+ return systemFontWidthTimes ( " Cancel " );
405412 }
406413
407414 /** Returns the default width of a dialog. */
408415 public static int defaultDialogWidth () {
409- return 50 * systemFontWidth ( );
416+ return systemFontWidthTimes ( 50 );
410417 }
411418
412419 /** Returns a size which is scaled by the system font's height. */
@@ -421,7 +428,7 @@ public static int scaleByFontHeight(int rows) {
421428
422429 /** Returns a point that represents the size of a (cols x rows) grid of characters printed in the standard system font. */
423430 public static Point scaleByFont (int cols , int rows ) {
424- return new Point (cols * systemFontWidth ( ), rows * systemFontHeight ());
431+ return new Point (systemFontWidthTimes ( cols ), rows * systemFontHeight ());
425432 }
426433
427434 /** Returns a dimension which is guaranteed to be comfortable for the given string. */
0 commit comments