Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[Win32] TextLayout.getBounds() calculates incorrect width on monitors with non 100% scaling

オープン
#3,461 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
java
領域
desktop

調査の方向性

Start by tracing TextLayout.getBounds() and TextLayout.setWidth() in SWT's Windows implementation, using the supplied standalone snippet to reproduce the issue at different monitor scaling levels. Compare the returned bounds and wrapping behavior under disabled and enabled autoscaling, including after moving the window between monitors. Done means the width from getBounds() keeps the text unwrapped in the reported Windows scenarios without breaking autoscaling behavior.

索引モデルが issue の本文から書いたものです。

説明

Describe the bug
TextLayout.getBounds() method returns bad width on monitors with non 100% scaling. The code that I expect to work correctly is the following:

textLayout.setWidth(textLayout.getBounds().width)

When doing this I expect the text of the TextLayout to not wrap. But with certain font sizes and on monitors with non 100% scaling the text wraps. I've initially suspected fractional scaling values like 125%, but testing with 200% shows that the problem is present there too. I've tried to adjust the width by 1px and even by 2px, but it looks like this is not just an off by 1 error.

Note that I am testing this with 2 autoscale modes for the control:

  • AutoscaleMode.DISABLED - the text is wrapped incorrectly
  • AutoscaleMode.ENABLED - the text is correct initially, but broken when window is moved from to another monitor, for example from one with 125% to one with 100% scaling

To Reproduce
A standalone SWT snippet to reproduce the behavior:

package org.eclipse.swt.snippets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.AutoscalingMode;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TextLayoutBoundsBug {
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
		shell.setText("TextLayoutBoundsBug");
		shell.setAutoscalingMode(AutoscalingMode.DISABLED);
		shell.setSize(1000, 1000);

		Font font = shell.getFont();
		FontData fontData = font.getFontData()[0];

		createTextLayoutAt(shell, 10, 10, fontData, 42, "Reset", 0);
		createTextLayoutAt(shell, 10, 80, fontData, 41, "Reset", 0);

		createTextLayoutAt(shell, 210, 10, fontData, 42, "Download", 1);
		createTextLayoutAt(shell, 210, 80, fontData, 41, "Download", 1);

		createTextLayoutAt(shell, 510, 10, fontData, 42, "Download & Reset", 2);
		createTextLayoutAt(shell, 510, 80, fontData, 41, "Download & Reset", 2);

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private static TextLayout createTextLayoutAt(Control parent, int x, int y, FontData fontData, int fontHeight,
			String text,
			int widthCorrection) {
		Font derivedFont = new Font(parent.getDisplay(),
				new FontData(fontData.getName(), fontHeight, fontData.getStyle()));

		TextLayout textLayout = new TextLayout(parent.getDisplay());
		textLayout.setFont(derivedFont);
		textLayout.setText(text);
		textLayout.setWidth(textLayout.getBounds().width + widthCorrection);
		parent.addListener(SWT.Paint, event -> {
			textLayout.draw(event.gc, x, y);
		});

		return textLayout;
	}
}

Screenshots

  1. AutoscalingMode.DISABLED, 125% scaling. Text is wrapping:
Image
  1. AutoscalingMode.ENABLED, 125% scaling. Text looks correct:
Image

Then moved to monitor with 100% scaling. Text is wrapping again:

Image

Exactly the same problem can be seen with 200% scaling so I am not including additional screenshots.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS

I've tested this also on Linux with 200% scaling and it worked correctly. Didn't test macOS.

主要言語
Java
スター
202
フォーク
206
平均マージ
16時間 56分
マージ済み PR(30日)
110

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

eclipse-platform/eclipse.platform.swt のほかの issue

eclipse-platform/eclipse.platform.swt の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。