@@ -226,82 +226,85 @@ public FontData open () {
226226
227227 display .externalEventLoop = true ;
228228 display .sendPreExternalEventDispatchEvent ();
229-
230- /* Temporarily setting the thread dpi awareness to gdi scaling because window dialog has weird resize handling */
231229 long currentDpiAwarenessContext = OS .GetThreadDpiAwarenessContext ();
232- if (display .isRescalingAtRuntime ()) {
233- currentDpiAwarenessContext = OS .SetThreadDpiAwarenessContext (OS .DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED );
234- }
230+ boolean success = false ;
231+ try {
232+ /*
233+ * Temporarily setting the thread dpi awareness to gdi scaling because window
234+ * dialog has weird resize handling
235+ */
236+ if (display .isRescalingAtRuntime ()) {
237+ currentDpiAwarenessContext = OS .SetThreadDpiAwarenessContext (OS .DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED );
238+ }
235239
236- /* Open the dialog */
237- boolean success = OS .ChooseFont (lpcf );
238- display .externalEventLoop = false ;
239- display .sendPostExternalEventDispatchEvent ();
240+ /* Open the dialog */
241+ success = OS .ChooseFont (lpcf );
242+ display .externalEventLoop = false ;
243+ display .sendPostExternalEventDispatchEvent ();
240244
241- /* Clear the temporary dialog modal parent */
242- if ((style & (SWT .APPLICATION_MODAL | SWT .SYSTEM_MODAL )) != 0 ) {
243- display .setModalDialog (oldModal );
244- }
245+ /* Clear the temporary dialog modal parent */
246+ if ((style & (SWT .APPLICATION_MODAL | SWT .SYSTEM_MODAL )) != 0 ) {
247+ display .setModalDialog (oldModal );
248+ }
245249
246- /* Compute the result */
247- if (success ) {
248- LOGFONT logFont = new LOGFONT ();
249- OS .MoveMemory (logFont , lpLogFont , LOGFONT .sizeof );
250+ /* Compute the result */
251+ if (success ) {
252+ LOGFONT logFont = new LOGFONT ();
253+ OS .MoveMemory (logFont , lpLogFont , LOGFONT .sizeof );
250254
251- /*
252- * This will not work on multiple screens or
253- * for printing. Should use DC for the proper device.
254- */
255- long hDC = OS .GetDC (0 );
256- int logPixelsY = OS .GetDeviceCaps (hDC , OS .LOGPIXELSY );
257- int pixels = 0 ;
258- if (logFont .lfHeight > 0 ) {
259255 /*
260- * Feature in Windows. If the lfHeight of the LOGFONT structure
261- * is positive, the lfHeight measures the height of the entire
262- * cell, including internal leading, in logical units. Since the
263- * height of a font in points does not include the internal leading,
264- * we must subtract the internal leading, which requires a TEXTMETRIC,
265- * which in turn requires font creation.
256+ * This will not work on multiple screens or for printing. Should use DC for the
257+ * proper device.
266258 */
267- long hFont = OS .CreateFontIndirect (logFont );
268- long oldFont = OS .SelectObject (hDC , hFont );
269- TEXTMETRIC lptm = new TEXTMETRIC ();
270- OS .GetTextMetrics (hDC , lptm );
271- OS .SelectObject (hDC , oldFont );
272- OS .DeleteObject (hFont );
273- pixels = logFont .lfHeight - lptm .tmInternalLeading ;
274- } else {
275- pixels = -logFont .lfHeight ;
276- }
277- OS .ReleaseDC (0 , hDC );
259+ long hDC = OS .GetDC (0 );
260+ int logPixelsY = OS .GetDeviceCaps (hDC , OS .LOGPIXELSY );
261+ int pixels = 0 ;
262+ if (logFont .lfHeight > 0 ) {
263+ /*
264+ * Feature in Windows. If the lfHeight of the LOGFONT structure is positive, the
265+ * lfHeight measures the height of the entire cell, including internal leading,
266+ * in logical units. Since the height of a font in points does not include the
267+ * internal leading, we must subtract the internal leading, which requires a
268+ * TEXTMETRIC, which in turn requires font creation.
269+ */
270+ long hFont = OS .CreateFontIndirect (logFont );
271+ long oldFont = OS .SelectObject (hDC , hFont );
272+ TEXTMETRIC lptm = new TEXTMETRIC ();
273+ OS .GetTextMetrics (hDC , lptm );
274+ OS .SelectObject (hDC , oldFont );
275+ OS .DeleteObject (hFont );
276+ pixels = logFont .lfHeight - lptm .tmInternalLeading ;
277+ } else {
278+ pixels = -logFont .lfHeight ;
279+ }
280+ OS .ReleaseDC (0 , hDC );
278281
279- float points = pixels * 72f /logPixelsY ;
280- fontData = FontData .win32_new (logFont , points );
281- if (effectsVisible ) {
282- int red = lpcf .rgbColors & 0xFF ;
283- int green = (lpcf .rgbColors >> 8 ) & 0xFF ;
284- int blue = (lpcf .rgbColors >> 16 ) & 0xFF ;
285- rgb = new RGB (red , green , blue );
282+ float points = pixels * 72f / logPixelsY ;
283+ fontData = FontData .win32_new (logFont , points );
284+ if (effectsVisible ) {
285+ int red = lpcf .rgbColors & 0xFF ;
286+ int green = (lpcf .rgbColors >> 8 ) & 0xFF ;
287+ int blue = (lpcf .rgbColors >> 16 ) & 0xFF ;
288+ rgb = new RGB (red , green , blue );
289+ }
286290 }
287- }
288-
289- /* Reset the dpi awareness context */
290- if (display .isRescalingAtRuntime ()) {
291- OS .SetThreadDpiAwarenessContext (currentDpiAwarenessContext );
292- }
293-
294- /* Free the OS memory */
295- if (lpLogFont != 0 ) OS .HeapFree (hHeap , 0 , lpLogFont );
291+ } finally {
292+ /* Reset the dpi awareness context */
293+ if (display .isRescalingAtRuntime ()) {
294+ OS .SetThreadDpiAwarenessContext (currentDpiAwarenessContext );
295+ }
296+ /* Free the OS memory */
297+ if (lpLogFont != 0 ) OS .HeapFree (hHeap , 0 , lpLogFont );
296298
297- /* Destroy the BIDI orientation window */
298- if (hwndParent != hwndOwner ) {
299- if (enabled ) OS .EnableWindow (hwndParent , true );
300- OS .SetActiveWindow (hwndParent );
301- OS .DestroyWindow (hwndOwner );
299+ /* Destroy the BIDI orientation window */
300+ if (hwndParent != hwndOwner ) {
301+ if (enabled ) OS .EnableWindow (hwndParent , true );
302+ OS .SetActiveWindow (hwndParent );
303+ OS .DestroyWindow (hwndOwner );
304+ }
305+ if (!success ) return null ;
302306 }
303307
304- if (!success ) return null ;
305308 return fontData ;
306309}
307310
0 commit comments