diff options
author | Carolyn MacLeod <carolyn> | 2010-10-25 16:51:18 +0000 |
---|---|---|
committer | Carolyn MacLeod <carolyn> | 2010-10-25 16:51:18 +0000 |
commit | cc65e22d5c0c66bbe589f586cbf6f1277adb4a12 (patch) | |
tree | c1a325782e0b262151d93bfa3b0c4a6dd8297938 | |
parent | ce69b2880832d86c0b304b2378b4131bbb54c4fa (diff) | |
download | eclipse.platform.swt-cc65e22d5c0c66bbe589f586cbf6f1277adb4a12.tar.gz eclipse.platform.swt-cc65e22d5c0c66bbe589f586cbf6f1277adb4a12.tar.xz eclipse.platform.swt-cc65e22d5c0c66bbe589f586cbf6f1277adb4a12.zip |
Bug 150870 - GTK+ Printer getDPI returns 72 x 72
4 files changed, 52 insertions, 44 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/gtk/org/eclipse/swt/printing/Printer.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/gtk/org/eclipse/swt/printing/Printer.java index 0812440988..e38bfe6253 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Printing/gtk/org/eclipse/swt/printing/Printer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/gtk/org/eclipse/swt/printing/Printer.java @@ -352,6 +352,9 @@ public Font getSystemFont () { if (systemFont != null) return systemFont; int /*long*/ style = OS.gtk_widget_get_default_style(); int /*long*/ defaultFont = OS.pango_font_description_copy (OS.gtk_style_get_font_desc (style)); + int size = OS.pango_font_description_get_size(defaultFont); + Point dpi = getDPI(), screenDPI = super.getDPI(); + OS.pango_font_description_set_size(defaultFont, size * dpi.y / screenDPI.y); return systemFont = Font.gtk_new (this, defaultFont); } @@ -387,10 +390,15 @@ public int /*long*/ internal_new_GC(GCData data) { data.background = getSystemColor (SWT.COLOR_WHITE).handle; data.foreground = getSystemColor (SWT.COLOR_BLACK).handle; data.font = getSystemFont (); - //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) - data.width = (int)OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); - data.height = (int)OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); + Point dpi = getDPI(), screenDPI = getIndependentDPI(); + data.width = (int)(OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS) * dpi.x / screenDPI.x); + data.height = (int)(OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS) * dpi.y / screenDPI.y); if (cairo == 0) SWT.error(SWT.ERROR_NO_HANDLES); + Cairo.cairo_identity_matrix(cairo); + double printX = OS.gtk_page_setup_get_left_margin(pageSetup, OS.GTK_UNIT_POINTS); + double printY = OS.gtk_page_setup_get_top_margin(pageSetup, OS.GTK_UNIT_POINTS); + Cairo.cairo_translate(cairo, printX, printY); + Cairo.cairo_scale(cairo, screenDPI.x / (float)dpi.x, screenDPI.y / (float)dpi.y); data.cairo = cairo; isGCCreated = true; } @@ -591,10 +599,9 @@ public Point getDPI() { checkDevice(); int resolution = OS.gtk_print_settings_get_resolution(settings); if (DEBUG) System.out.println("print_settings.resolution=" + resolution); - //TODO: Return 72 (1/72 inch = 1 point) until gtk bug 346245 is fixed - //TODO: Fix this: gtk_print_settings_get_resolution returns 0? (see gtk bug 346252) - /*if (resolution == 0)*/ return new Point(72, 72); -// return new Point(resolution, resolution); + //TODO: use new api for get x resolution and get y resolution + if (resolution == 0) return new Point(72, 72); + return new Point(resolution, resolution); } /** @@ -614,9 +621,9 @@ public Point getDPI() { */ public Rectangle getBounds() { checkDevice(); - //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) - double width = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); - double height = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); + Point dpi = getDPI(), screenDPI = getIndependentDPI(); + double width = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS) * dpi.x / screenDPI.x; + double height = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS) * dpi.y / screenDPI.y; return new Rectangle(0, 0, (int) width, (int) height); } @@ -639,12 +646,16 @@ public Rectangle getBounds() { */ public Rectangle getClientArea() { checkDevice(); - //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) - double width = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS); - double height = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS); + Point dpi = getDPI(), screenDPI = getIndependentDPI(); + double width = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS) * dpi.x / screenDPI.x; + double height = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS) * dpi.y / screenDPI.y; return new Rectangle(0, 0, (int) width, (int) height); } +Point getIndependentDPI () { + return new Point(72, 72); +} + /** * Given a <em>client area</em> (as described by the arguments), * returns a rectangle, relative to the client area's coordinates, @@ -682,13 +693,13 @@ public Rectangle getClientArea() { */ public Rectangle computeTrim(int x, int y, int width, int height) { checkDevice(); - //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) - double printWidth = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS); - double printHeight = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS); - double paperWidth = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); - double paperHeight = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); - double printX = -OS.gtk_page_setup_get_left_margin(pageSetup, OS.GTK_UNIT_POINTS); - double printY = -OS.gtk_page_setup_get_top_margin(pageSetup, OS.GTK_UNIT_POINTS); + Point dpi = getDPI(), screenDPI = getIndependentDPI(); + double printWidth = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS) * dpi.x / screenDPI.x; + double printHeight = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS) * dpi.y / screenDPI.y; + double paperWidth = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS) * dpi.x / screenDPI.x; + double paperHeight = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS) * dpi.y / screenDPI.y; + double printX = -OS.gtk_page_setup_get_left_margin(pageSetup, OS.GTK_UNIT_POINTS) * dpi.x / screenDPI.x; + double printY = -OS.gtk_page_setup_get_top_margin(pageSetup, OS.GTK_UNIT_POINTS) * dpi.y / screenDPI.y; double hTrim = paperWidth - printWidth; double vTrim = paperHeight - printHeight; return new Rectangle(x + (int)printX, y + (int)printY, width + (int)hTrim, height + (int)vTrim); @@ -720,7 +731,6 @@ protected void create(DeviceData deviceData) { * @see #create */ protected void init() { - super.init (); settings = OS.gtk_print_settings_new(); pageSetup = OS.gtk_page_setup_new(); if (data.otherData != null) { @@ -742,6 +752,7 @@ protected void init() { int orientation = data.orientation == PrinterData.LANDSCAPE ? OS.GTK_PAGE_ORIENTATION_LANDSCAPE : OS.GTK_PAGE_ORIENTATION_PORTRAIT; OS.gtk_page_setup_set_orientation(pageSetup, orientation); OS.gtk_print_settings_set_orientation(settings, orientation); + super.init (); } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java index 32e16e8e87..8406f6cb66 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java @@ -79,6 +79,9 @@ public abstract class Device implements Drawable { /* System Font */ Font systemFont; + /* Device dpi */ + Point dpi; + int /*long*/ emptyTab; boolean useXRender; @@ -406,7 +409,7 @@ public int getDepth () { */ public Point getDPI () { checkDevice (); - return new Point (72, 72); + return getScreenDPI(); } /** @@ -473,6 +476,13 @@ public FontData[] getFontList (String faceName, boolean scalable) { return result; } +Point getScreenDPI () { + int widthMM = OS.gdk_screen_width_mm (); + int width = OS.gdk_screen_width (); + int dpi = Compatibility.round (254 * width, widthMM * 10); + return new Point (dpi, dpi); +} + /** * Returns the matching standard color for the given * constant, which should be one of the color constants @@ -568,6 +578,8 @@ public boolean getWarnings () { * @see #create */ protected void init () { + this.dpi = getDPI(); + if (xDisplay != 0) { int[] event_basep = new int[1], error_basep = new int [1]; if (OS.XRenderQueryExtension (xDisplay, event_basep, error_basep)) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Font.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Font.java index 2619dbb0d7..73d4574147 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Font.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Font.java @@ -186,6 +186,8 @@ public FontData[] getFontData() { OS.memmove(buffer, family, length); String name = new String(Converter.mbcsToWcs(null, buffer)); float height = (float)OS.pango_font_description_get_size(handle) / OS.PANGO_SCALE; + Point dpi = device.dpi, screenDPI = device.getScreenDPI(); + float size = height * screenDPI.y / dpi.y; int pangoStyle = OS.pango_font_description_get_style(handle); int pangoWeight = OS.pango_font_description_get_weight(handle); int style = SWT.NORMAL; @@ -197,7 +199,7 @@ public FontData[] getFontData() { buffer = new byte [length + 1]; OS.memmove (buffer, fontString, length); OS.g_free (fontString); - FontData data = new FontData(name, height, style); + FontData data = new FontData(name, size, style); data.string = buffer; return new FontData[]{data}; } @@ -240,6 +242,8 @@ public int hashCode() { void init(String name, float height, int style, byte[] fontString) { if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + Point dpi = device.dpi, screenDPI = device.getScreenDPI(); + float size = height * dpi.y / screenDPI.y; if (fontString != null) { handle = OS.pango_font_description_from_string (fontString); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); @@ -248,8 +252,8 @@ void init(String name, float height, int style, byte[] fontString) { if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); byte[] buffer = Converter.wcsToMbcs(null, name, true); OS.pango_font_description_set_family(handle, buffer); - if (height > 0) { - OS.pango_font_description_set_size(handle, (int)(0.5f + height * OS.PANGO_SCALE)); + if (size > 0) { + OS.pango_font_description_set_size(handle, (int)(0.5f + size * OS.PANGO_SCALE)); } OS.pango_font_description_set_stretch(handle, OS.PANGO_STRETCH_NORMAL); int pangoStyle = OS.PANGO_STYLE_NORMAL; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java index b2f03111b1..de2f2e39a6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java @@ -1646,25 +1646,6 @@ public Object getData () { return data; } -/** - * Returns a point whose x coordinate is the horizontal - * dots per inch of the display, and whose y coordinate - * is the vertical dots per inch of the display. - * - * @return the horizontal and vertical DPI - * - * @exception SWTException <ul> - * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> - * </ul> - */ -public Point getDPI () { - checkDevice (); - int widthMM = OS.gdk_screen_width_mm (); - int width = OS.gdk_screen_width (); - int dpi = Compatibility.round (254 * width, widthMM * 10); - return new Point (dpi, dpi); -} - int /*long*/ gtk_fixed_get_type () { return fixed_type; } |