Skip to content

Commit 9faeb19

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Using the new constructor FontData(fontData) to deep copy the object.
[win32] Introducing new FontData Constructor to create a deep copy This change will give us a chance to use proper channel to create a deep copy of FontData object instead of using FontData(fd.toString)
1 parent fe64f69 commit 9faeb19

File tree

7 files changed

+121
-5
lines changed

7 files changed

+121
-5
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/FontData.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ public FontData(String string) {
186186
}
187187
}
188188

189+
/**
190+
* Constructs a deep copy of the given font data object.
191+
*
192+
* @param fontData the FontData object to copy
193+
*
194+
* @exception IllegalArgumentException
195+
* <ul>
196+
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
197+
* </ul>
198+
* @since 3.131
199+
*/
200+
public FontData(FontData fontData) {
201+
if (fontData == null) {
202+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
203+
}
204+
205+
this.name = fontData.name;
206+
this.height = fontData.height;
207+
this.style = fontData.style;
208+
this.nsName = fontData.nsName;
209+
this.lang = fontData.lang;
210+
this.country = fontData.country;
211+
this.variant = fontData.variant;
212+
}
213+
189214
/**
190215
* Constructs a new font data given a font name,
191216
* the height of the desired font in points,

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/FontData.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,35 @@ public FontData(String string) {
184184
}
185185
}
186186

187+
/**
188+
* Constructs a deep copy of the given font data object.
189+
*
190+
* @param fontData the FontData object to copy
191+
*
192+
* @exception IllegalArgumentException
193+
* <ul>
194+
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
195+
* </ul>
196+
* @since 3.131
197+
*/
198+
public FontData(FontData fontData) {
199+
if (fontData == null) {
200+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
201+
}
202+
203+
this.name = fontData.name;
204+
this.height = fontData.height;
205+
this.style = fontData.style;
206+
this.lang = fontData.lang;
207+
this.country = fontData.country;
208+
this.variant = fontData.variant;
209+
210+
if (fontData.string != null) {
211+
this.string = new byte[fontData.string.length];
212+
System.arraycopy(fontData.string, 0, this.string, 0, fontData.string.length);
213+
}
214+
}
215+
187216
/**
188217
* Constructs a new font data given a font name,
189218
* the height of the desired font in points,

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public Font(Device device, FontData fd) {
103103
super(device);
104104
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
105105
this.zoom = DPIUtil.getNativeDeviceZoom();
106-
this.fontData = new FontData(fd.toString());
106+
this.fontData = new FontData(fd);
107107
this.fontHeight = fd.height;
108108
init();
109109
}
@@ -112,7 +112,7 @@ private Font(Device device, FontData fd, int zoom) {
112112
super(device);
113113
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
114114
this.zoom = zoom;
115-
this.fontData = new FontData(fd.toString());
115+
this.fontData = new FontData(fd);
116116
this.fontHeight = fd.height;
117117
init();
118118
}
@@ -151,7 +151,7 @@ public Font(Device device, FontData[] fds) {
151151
}
152152
this.zoom = DPIUtil.getNativeDeviceZoom();
153153
FontData fd = fds[0];
154-
this.fontData = new FontData(fd.toString());
154+
this.fontData = new FontData(fd);
155155
this.fontHeight = fd.height;
156156
init();
157157
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontData.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,59 @@ public FontData(String string) {
240240
}
241241
}
242242

243+
244+
/**
245+
* Constructs a deep copy of the given font data object.
246+
*
247+
* @param fontData the FontData object to copy
248+
*
249+
* @exception IllegalArgumentException
250+
* <ul>
251+
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
252+
* </ul>
253+
* @since 3.131
254+
*/
255+
public FontData(FontData fontData) {
256+
if (fontData == null) {
257+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
258+
}
259+
this.height = fontData.height;
260+
this.lang = fontData.lang;
261+
this.country = fontData.country;
262+
this.variant = fontData.variant;
263+
this.data = cloneLogFont(fontData);
264+
}
265+
266+
private static LOGFONT cloneLogFont(FontData fontData) {
267+
if (fontData.data == null) {
268+
return null;
269+
}
270+
271+
LOGFONT existingData = fontData.data;
272+
LOGFONT newData = new LOGFONT();
273+
newData.lfHeight = existingData.lfHeight;
274+
newData.lfWidth = existingData.lfWidth;
275+
newData.lfEscapement = existingData.lfEscapement;
276+
newData.lfOrientation = existingData.lfOrientation;
277+
newData.lfWeight = existingData.lfWeight;
278+
newData.lfItalic = existingData.lfItalic;
279+
newData.lfUnderline = existingData.lfUnderline;
280+
newData.lfStrikeOut = existingData.lfStrikeOut;
281+
newData.lfCharSet = existingData.lfCharSet;
282+
newData.lfOutPrecision = existingData.lfOutPrecision;
283+
newData.lfClipPrecision = existingData.lfClipPrecision;
284+
newData.lfQuality = existingData.lfQuality;
285+
newData.lfPitchAndFamily = existingData.lfPitchAndFamily;
286+
287+
// Deep copy of the char array lfFaceName
288+
if (existingData.lfFaceName != null) {
289+
newData.lfFaceName = new char[existingData.lfFaceName.length];
290+
System.arraycopy(existingData.lfFaceName, 0, newData.lfFaceName, 0, existingData.lfFaceName.length);
291+
}
292+
return newData;
293+
}
294+
295+
243296
/**
244297
* Constructs a new font data given a font name,
245298
* the height of the desired font in points,

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/DefaultSWTFontRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Font getFont(long fontHandle, int zoom) {
7777
}
7878

7979
private Font registerFont(FontData fontData, Font font) {
80-
FontData clonedFontData = new FontData(fontData.toString());
80+
FontData clonedFontData = new FontData(fontData);
8181
fontsMap.put(clonedFontData, font);
8282
return font;
8383
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public Font getFont(FontData fontData, int zoom) {
128128
if (customFontsKeyMap.containsKey(fontData)) {
129129
container = customFontsKeyMap.get(fontData);
130130
} else {
131-
FontData clonedFontData = new FontData(fontData.toString());
131+
FontData clonedFontData = new FontData(fontData);
132132
container = new ScaledCustomFontContainer(clonedFontData);
133133
customFontsKeyMap.put(clonedFontData, container);
134134
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_FontData.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ public void test_ConstructorLjava_lang_StringII() {
9090
}
9191
}
9292

93+
@Test
94+
public void test_ConstructorLjava_lang_FontData() {
95+
// Test new FontData(FontData fontData)
96+
FontData fd = new FontData(SwtTestUtil.testFontName, 30, SWT.ITALIC);
97+
FontData reconstructedFontDataFromCopyConstructor = new FontData(fd);
98+
assertEquals(fd, reconstructedFontDataFromCopyConstructor);
99+
assertEquals(fd.hashCode(), reconstructedFontDataFromCopyConstructor.hashCode());
100+
}
101+
93102
@Test
94103
public void test_equalsLjava_lang_Object() {
95104
FontData fd1 = new FontData(SwtTestUtil.testFontName, 10, SWT.NORMAL);

0 commit comments

Comments
 (0)