Skip to content

New FontData Constructor to create a deep copy #2128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ public FontData(String string) {
}
}

/**
* Constructs a new FontData copy
*
* @param fontData the FondData object for which copy is needed to be made
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
* </ul>
* @since 3.130
*/
public FontData(FontData fontData) {
if (fontData == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

setName(fontData.name);
setHeight(fontData.height);
setStyle(fontData.style);
this.nsName = fontData.nsName;
}

/**
* Constructs a new font data given a font name,
* the height of the desired font in points,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ public FontData(String string) {
}
}

/**
* Constructs a new FontData copy
*
* @param fontData the FondData object for which copy is needed to be made
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
* </ul>
* @since 3.130
*/
public FontData(FontData fontData) {
if (fontData == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

setName(fontData.name);
setHeight(fontData.height);
setStyle(fontData.style);
}

/**
* Constructs a new font data given a font name,
* the height of the desired font in points,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Font(Device device, FontData fd) {
super(device);
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.zoom = DPIUtil.getNativeDeviceZoom();
this.fontData = new FontData(fd.toString());
this.fontData = new FontData(fd);
this.fontHeight = fd.height;
init();
}
Expand All @@ -112,7 +112,7 @@ private Font(Device device, FontData fd, int zoom) {
super(device);
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.zoom = zoom;
this.fontData = new FontData(fd.toString());
this.fontData = new FontData(fd);
this.fontHeight = fd.height;
init();
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public Font(Device device, FontData[] fds) {
}
this.zoom = DPIUtil.getNativeDeviceZoom();
FontData fd = fds[0];
this.fontData = new FontData(fd.toString());
this.fontData = new FontData(fd);
this.fontHeight = fd.height;
init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,52 @@ public FontData(String string) {
}
}


/**
* Constructs a new FontData copy
*
* @param fontData the FondData object for which copy is needed to be made
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
* </ul>
* @since 3.130
*/
public FontData(FontData fontData) {
if (fontData == null)
SWT.error(SWT.ERROR_NULL_ARGUMENT);

if (fontData.data != null) {
LOGFONT newData = new LOGFONT();
setHeight(fontData.getHeightF());
newData.lfHeight = fontData.data.lfHeight;
newData.lfWidth = fontData.data.lfWidth;
newData.lfEscapement = fontData.data.lfEscapement;
newData.lfOrientation = fontData.data.lfOrientation;
newData.lfWeight = fontData.data.lfWeight;
newData.lfItalic = fontData.data.lfItalic;
newData.lfUnderline = fontData.data.lfUnderline;
newData.lfStrikeOut = fontData.data.lfStrikeOut;
newData.lfCharSet = fontData.data.lfCharSet;
newData.lfOutPrecision = fontData.data.lfOutPrecision;
newData.lfClipPrecision = fontData.data.lfClipPrecision;
newData.lfQuality = fontData.data.lfQuality;
newData.lfPitchAndFamily = fontData.data.lfPitchAndFamily;

// Deep copy of the char array lfFaceName
if (fontData.data.lfFaceName != null) {
newData.lfFaceName = new char[fontData.data.lfFaceName.length];
System.arraycopy(fontData.data.lfFaceName, 0, newData.lfFaceName, 0, fontData.data.lfFaceName.length);
}

this.data = newData;
} else {
this.data = null;
}
}


/**
* Constructs a new font data given a font name,
* the height of the desired font in points,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Font getFont(long fontHandle, int zoom) {
}

private Font registerFont(FontData fontData, Font font) {
FontData clonedFontData = new FontData(fontData.toString());
FontData clonedFontData = new FontData(fontData);
fontsMap.put(clonedFontData, font);
return font;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Font getFont(FontData fontData, int zoom) {
if (customFontsKeyMap.containsKey(fontData)) {
container = customFontsKeyMap.get(fontData);
} else {
FontData clonedFontData = new FontData(fontData.toString());
FontData clonedFontData = new FontData(fontData);
container = new ScaledCustomFontContainer(clonedFontData);
customFontsKeyMap.put(clonedFontData, container);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public void test_ConstructorLjava_lang_StringII() {
}
}

@Test
public void test_ConstructorLjava_lang_FontData() {
// Test new FontData(FontData fontData)
FontData fd = new FontData(SwtTestUtil.testFontName, 30, SWT.ITALIC);
FontData reconstructedFontDataFromCopyConstructor = new FontData(fd);
assertEquals(fd, reconstructedFontDataFromCopyConstructor);
assertEquals(fd.hashCode(), reconstructedFontDataFromCopyConstructor.hashCode());
}

@Test
public void test_equalsLjava_lang_Object() {
FontData fd1 = new FontData(SwtTestUtil.testFontName, 10, SWT.NORMAL);
Expand Down
Loading