src/java.desktop/share/classes/javax/swing/text/html/ImageView.java
changeset 54236 a5af6175d62b
parent 52236 2105d8064ca2
equal deleted inserted replaced
54235:63946f20c24f 54236:a5af6175d62b
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 package javax.swing.text.html;
    25 package javax.swing.text.html;
    26 
    26 
    27 import java.awt.Rectangle;
    27 import java.awt.Rectangle;
    28 import java.awt.Image;
    28 import java.awt.Image;
       
    29 import java.awt.Dimension;
    29 import java.awt.Container;
    30 import java.awt.Container;
    30 import java.awt.Color;
    31 import java.awt.Color;
    31 import java.awt.Shape;
    32 import java.awt.Shape;
    32 import java.awt.Graphics;
    33 import java.awt.Graphics;
    33 import java.awt.Toolkit;
    34 import java.awt.Toolkit;
   778                 newState |= WIDTH_FLAG;
   779                 newState |= WIDTH_FLAG;
   779             }
   780             }
   780 
   781 
   781             if (newHeight > 0) {
   782             if (newHeight > 0) {
   782                 newState |= HEIGHT_FLAG;
   783                 newState |= HEIGHT_FLAG;
       
   784             }
       
   785 
       
   786             /*
       
   787             If synchronous loading flag is set, then make sure that the image is
       
   788             scaled appropriately.
       
   789             Otherwise, the ImageHandler::imageUpdate takes care of scaling the image
       
   790             appropriately.
       
   791             */
       
   792             if (getLoadsSynchronously()) {
       
   793                 Dimension d = adjustWidthHeight(image.getWidth(imageObserver),
       
   794                                                 image.getHeight(imageObserver));
       
   795                 newWidth = d.width;
       
   796                 newHeight = d.height;
       
   797                 newState |= (WIDTH_FLAG | HEIGHT_FLAG);
   783             }
   798             }
   784 
   799 
   785             // Make sure the image starts loading:
   800             // Make sure the image starts loading:
   786             if ((newState & (WIDTH_FLAG | HEIGHT_FLAG)) != 0) {
   801             if ((newState & (WIDTH_FLAG | HEIGHT_FLAG)) != 0) {
   787                 Toolkit.getDefaultToolkit().prepareImage(newImage, newWidth,
   802                 Toolkit.getDefaultToolkit().prepareImage(newImage, newWidth,
   881                     public void run() {
   896                     public void run() {
   882                         safePreferenceChanged();
   897                         safePreferenceChanged();
   883                     }
   898                     }
   884                 });
   899                 });
   885         }
   900         }
       
   901     }
       
   902 
       
   903     private Dimension adjustWidthHeight(int newWidth, int newHeight) {
       
   904         Dimension d = new Dimension();
       
   905         double proportion = 0.0;
       
   906         final int specifiedWidth = getIntAttr(HTML.Attribute.WIDTH, -1);
       
   907         final int specifiedHeight = getIntAttr(HTML.Attribute.HEIGHT, -1);
       
   908         /**
       
   909          * If either of the attributes are not specified, then calculate the
       
   910          * proportion for the specified dimension wrt actual value, and then
       
   911          * apply the same proportion to the unspecified dimension as well,
       
   912          * so that the aspect ratio of the image is maintained.
       
   913          */
       
   914         if (specifiedWidth != -1 && specifiedHeight != -1) {
       
   915             newWidth = specifiedWidth;
       
   916             newHeight = specifiedHeight;
       
   917         } else if (specifiedWidth != -1 ^ specifiedHeight != -1) {
       
   918             if (specifiedWidth <= 0) {
       
   919                 proportion = specifiedHeight / ((double)newHeight);
       
   920                 newWidth = (int)(proportion * newWidth);
       
   921                 newHeight = specifiedHeight;
       
   922             }
       
   923 
       
   924             if (specifiedHeight <= 0) {
       
   925                 proportion = specifiedWidth / ((double)newWidth);
       
   926                 newHeight = (int)(proportion * newHeight);
       
   927                 newWidth = specifiedWidth;
       
   928             }
       
   929         }
       
   930 
       
   931         d.width = newWidth;
       
   932         d.height = newHeight;
       
   933 
       
   934         return d;
   886     }
   935     }
   887 
   936 
   888     /**
   937     /**
   889      * ImageHandler implements the ImageObserver to correctly update the
   938      * ImageHandler implements the ImageObserver to correctly update the
   890      * display as new parts of the image become available.
   939      * display as new parts of the image become available.
   948                  * then figure out if scaling is necessary based on the
   997                  * then figure out if scaling is necessary based on the
   949                  * specified HTML attributes.
   998                  * specified HTML attributes.
   950                  */
   999                  */
   951                 if (((flags & ImageObserver.HEIGHT) != 0) &&
  1000                 if (((flags & ImageObserver.HEIGHT) != 0) &&
   952                     ((flags & ImageObserver.WIDTH) != 0)) {
  1001                     ((flags & ImageObserver.WIDTH) != 0)) {
   953                     double proportion = 0.0;
  1002                         Dimension d = adjustWidthHeight(newWidth, newHeight);
   954                     final int specifiedWidth = getIntAttr(HTML.Attribute.WIDTH, -1);
  1003                         newWidth = d.width;
   955                     final int specifiedHeight = getIntAttr(HTML.Attribute.HEIGHT, -1);
  1004                         newHeight = d.height;
   956                     /**
       
   957                      * If either of the attributes are not specified, then calculate the
       
   958                      * proportion for the specified dimension wrt actual value, and then
       
   959                      * apply the same proportion to the unspecified dimension as well,
       
   960                      * so that the aspect ratio of the image is maintained.
       
   961                      */
       
   962                     if (specifiedWidth != -1 ^ specifiedHeight != -1) {
       
   963                         if (specifiedWidth <= 0) {
       
   964                             proportion = specifiedHeight / ((double)newHeight);
       
   965                             newWidth = (int)(proportion * newWidth);
       
   966                         }
       
   967 
       
   968                         if (specifiedHeight <= 0) {
       
   969                             proportion = specifiedWidth / ((double)newWidth);
       
   970                             newHeight = (int)(proportion * newHeight);
       
   971                         }
       
   972                         changed |= 3;
  1005                         changed |= 3;
   973                     }
       
   974                 }
  1006                 }
   975                 synchronized(ImageView.this) {
  1007                 synchronized(ImageView.this) {
   976                     if ((changed & 1) == 1 && (state & HEIGHT_FLAG) == 0) {
  1008                     if ((changed & 1) == 1 && (state & HEIGHT_FLAG) == 0) {
   977                         height = newHeight;
  1009                         height = newHeight;
   978                     }
  1010                     }