1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-05 22:20:24 -08:00

Respond to display configuration updates on Android

* java/org/gnu/emacs/EmacsNative.java
(sendConfigurationChanged): Declare function.

* java/org/gnu/emacs/EmacsSdk7FontDriver.java (Sdk7FontEntity)
(Sdk7FontObject): Do not access `metrics' field deleted from
`EmacsService'.

* java/org/gnu/emacs/EmacsService.java (EmacsService)
<metrics, resources>: Delete fields.
<dpiX, dpiY, dpiScaled>: New fields.
(onCreate): Adjust accordingly.  Record current display metrics
for subsequent comparison.
(onConfigurationChanged): New function.

* lisp/dynamic-setting.el (font-setting-change-default-font):
Enable on systems where font-get-system-font is not defined if
invoked with SET-FONT nil.

* src/android.c (sendConfigurationChanged): New function.

* src/androidgui.h (ANDROID_CONFIGURATION_CHANGED): New enumerator.
(struct android_configuration_changed): New structure.
(union android_event): Add `config' member.

* src/androidterm.c (handle_one_android_event): Handle
ANDROID_CONFIGURATION_CHANGED events.
(syms_of_androidterm): Define Qfont_render, and
Qdynamic_setting.  Provide the latter.
This commit is contained in:
Po Lu 2025-04-10 15:21:15 +08:00
parent cb339ad8f4
commit 884ede7c95
7 changed files with 132 additions and 17 deletions

View file

@ -196,6 +196,10 @@ public final class EmacsNative
/* Send an ANDROID_NOTIFICATION_ACTION event. */
public static native void sendNotificationAction (String tag, String action);
/* Send an ANDROID_CONFIGURATION_CHANGED event. */
public static native void sendConfigurationChanged (float dpiX, float dpiY,
float dpiScaled);
/* Return the file name associated with the specified file
descriptor, or NULL if there is none. */
public static native byte[] getProcName (int fd);

View file

@ -29,6 +29,7 @@ import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.util.Log;
@ -103,6 +104,8 @@ public class EmacsSdk7FontDriver extends EmacsFontDriver
public
Sdk7FontEntity (Sdk7Typeface typeface)
{
DisplayMetrics metrics;
foundry = "Google";
family = typeface.familyName;
adstyle = null;
@ -110,7 +113,8 @@ public class EmacsSdk7FontDriver extends EmacsFontDriver
slant = typeface.slant;
spacing = typeface.spacing;
width = typeface.width;
dpi = Math.round (EmacsService.SERVICE.metrics.scaledDensity * 160f);
metrics = EmacsService.SERVICE.getResources ().getDisplayMetrics ();
dpi = Math.round (metrics.scaledDensity * 160f);
this.typeface = typeface;
}
@ -127,6 +131,7 @@ public class EmacsSdk7FontDriver extends EmacsFontDriver
{
float totalWidth;
String testWidth, testString;
DisplayMetrics metrics;
this.typeface = typeface;
this.pixelSize = pixelSize;
@ -137,7 +142,8 @@ public class EmacsSdk7FontDriver extends EmacsFontDriver
slant = typeface.slant;
spacing = typeface.spacing;
width = typeface.width;
dpi = Math.round (EmacsService.SERVICE.metrics.scaledDensity * 160f);
metrics = EmacsService.SERVICE.getResources ().getDisplayMetrics ();
dpi = Math.round (metrics.scaledDensity * 160f);
/* Compute the ascent and descent. */
typeface.typefacePaint.setTextSize (pixelSize);

View file

@ -123,9 +123,6 @@ public final class EmacsService extends Service
public static final int IC_MODE_TEXT = 2;
public static final int IC_MODE_PASSWORD = 3;
/* Display metrics used by font backends. */
public DisplayMetrics metrics;
/* Flag that says whether or not to print verbose debugging
information when responding to an input method. */
public static final boolean DEBUG_IC = false;
@ -149,8 +146,9 @@ public final class EmacsService extends Service
thread. */
private Thread mainThread;
/* "Resources" object required by GContext bookkeeping. */
public static Resources resources;
/* The display's horizontal and vertical density and that which is
consulted for font scaling. */
private double dpiX, dpiY, dpiScaled;
static
{
@ -236,10 +234,12 @@ public final class EmacsService extends Service
final AssetManager manager;
Context app_context;
final String filesDir, libDir, cacheDir, classPath;
final double pixelDensityX;
final double pixelDensityY;
final double scaledDensity;
double tempScaledDensity;
final float pixelDensityX;
final float pixelDensityY;
final float scaledDensity;
float tempScaledDensity;
Resources resources;
DisplayMetrics metrics;
super.onCreate ();
@ -265,13 +265,18 @@ public final class EmacsService extends Service
corresponds to 1 pixel, not 72 or 96 as used elsewhere. This
difference is codified in PT_PER_INCH defined in font.h. */
if (tempScaledDensity < 160)
tempScaledDensity = 160;
if (tempScaledDensity < 160.0f)
tempScaledDensity = 160.0f;
/* scaledDensity is const as required to refer to it from within
the nested function below. */
scaledDensity = tempScaledDensity;
/* Save these fields for future reference. */
dpiX = pixelDensityX;
dpiY = pixelDensityY;
dpiScaled = scaledDensity;
/* Remove all tasks from previous Emacs sessions but the task
created by the system at startup. */
EmacsWindowManager.MANAGER.removeOldTasks (this);
@ -304,9 +309,8 @@ public final class EmacsService extends Service
run ()
{
EmacsNative.setEmacsParams (manager, filesDir, libDir,
cacheDir, (float) pixelDensityX,
(float) pixelDensityY,
(float) scaledDensity,
cacheDir, pixelDensityX,
pixelDensityY, scaledDensity,
classPath, EmacsService.this,
Build.VERSION.SDK_INT);
}
@ -344,6 +348,40 @@ public final class EmacsService extends Service
super.onLowMemory ();
}
@Override
public void
onConfigurationChanged (Configuration newConfig)
{
DisplayMetrics metrics;
float pixelDensityX, pixelDensityY, scaledDensity;
metrics = getResources ().getDisplayMetrics ();
/* The display configuration may have been altered. Retrieve the
revised display density and deliver an event if so. */
pixelDensityX = metrics.xdpi;
pixelDensityY = metrics.ydpi;
scaledDensity = ((getScaledDensity (metrics)
/ metrics.density) * pixelDensityX);
/* A density below 160 probably indicates a system bug. See
onCreate for more commentary. */
if (scaledDensity < 160.0f)
scaledDensity = 160.0f;
if (pixelDensityX != dpiX || pixelDensityY != dpiY
|| scaledDensity != dpiScaled)
{
dpiX = pixelDensityX;
dpiY = pixelDensityY;
dpiScaled = scaledDensity;
EmacsNative.sendConfigurationChanged (pixelDensityX, pixelDensityY,
scaledDensity);
}
super.onConfigurationChanged (newConfig);
}
/* Functions from here on must only be called from the Emacs