1 /******************************************************************************* 2 * Copyright (c) 2004, 2016 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jface.resource; 15 16 import java.util.Arrays; 17 18 import org.eclipse.swt.graphics.Device; 19 import org.eclipse.swt.graphics.Font; 20 import org.eclipse.swt.graphics.FontData; 21 22 /** 23 * Describes a Font using an array of FontData 24 * 25 * @since 3.1 26 */ 27 final class ArrayFontDescriptor extends FontDescriptor { 28 29 private FontData[] data; 30 private Font originalFont = null; 31 32 /** 33 * Creates a font descriptor for a font with the given name, height, 34 * and style. These arguments are passed directly to the constructor 35 * of Font. 36 * 37 * @param data FontData describing the font to create 38 * 39 * @see org.eclipse.swt.graphics.Font#Font(org.eclipse.swt.graphics.Device, org.eclipse.swt.graphics.FontData) 40 * @since 3.1 41 */ ArrayFontDescriptor(FontData[] data)42 public ArrayFontDescriptor(FontData[] data) { 43 this.data = data; 44 } 45 46 /** 47 * Creates a font descriptor that describes the given font. 48 * 49 * @param originalFont font to be described 50 * 51 * @see FontDescriptor#createFrom(org.eclipse.swt.graphics.Font) 52 * @since 3.1 53 */ ArrayFontDescriptor(Font originalFont)54 public ArrayFontDescriptor(Font originalFont) { 55 this(originalFont.getFontData()); 56 this.originalFont = originalFont; 57 } 58 59 @Override getFontData()60 public FontData[] getFontData() { 61 // Copy the original array to ensure that callers will not modify it 62 return copy(data); 63 } 64 65 66 @Override createFont(Device device)67 public Font createFont(Device device) { 68 69 // If this descriptor is an existing font, then we can return the original font 70 // if this is the same device. 71 if (originalFont != null) { 72 // If we're allocating on the same device as the original font, return the original. 73 if (originalFont.getDevice() == device) { 74 return originalFont; 75 } 76 } 77 78 return new Font(device, data); 79 } 80 81 @Override equals(Object obj)82 public boolean equals(Object obj) { 83 if (obj instanceof ArrayFontDescriptor) { 84 ArrayFontDescriptor descr = (ArrayFontDescriptor)obj; 85 86 if (descr.originalFont != originalFont) { 87 return false; 88 } 89 90 if (originalFont != null) { 91 return true; 92 } 93 94 if (!Arrays.equals(data, descr.data)) { 95 return false; 96 } 97 98 return true; 99 } 100 101 return false; 102 } 103 104 @Override hashCode()105 public int hashCode() { 106 if (originalFont != null) { 107 return originalFont.hashCode(); 108 } 109 return Arrays.hashCode(data); 110 } 111 112 @Override destroyFont(Font previouslyCreatedFont)113 public void destroyFont(Font previouslyCreatedFont) { 114 if (previouslyCreatedFont == originalFont) { 115 return; 116 } 117 previouslyCreatedFont.dispose(); 118 } 119 120 } 121