GtkDrawingArea, how to draw a line which is exactly 10 cm long?

Or maybe exactly 0.2 mm thick?

I am tuning a drawing app, currently it uses arbitrary scaling for content, but some sizes are in pixels, like grab distance for objects or measurement related arrow headers. Pixels is bad due to new very high resolution screens of course. I found functions like gdk-monitor-get-width-mm(), but then there is gdk_monitor_get_geometry() with ”application pixels”, not in ”device pixels”, and gdk_window_get_scale_factor (). I guess a combination of these will do, but it is not that straight forward, and for actual testing one needs various devices.

Seems to work:

# convert abs distance x in mm into distance value for GtkDrawingArea
# when drawing with cairo and cairo_scale == 1
proc absToScr(x: float): float =
  let d = gdk.getDefaultDisplay()
  let m = gdk.getMonitor(d, 0)
  var r: gdk.Rectangle
  gdk.getGeometry(m, r)
  echo r.width
  let s = gdk.getScaleFactor(m)
  echo s
  echo "Horizontal Pixels: ", s * r.width
  let mm: int  = gdk.getWidth_mm(m)
  echo "Display width in mm: ", mm
  return x * float(s) * float(r.width) / float(mm)

Output for my monitor is

Horizontal Pixels: 3840
Display width in mm: 600

and 100 mm is converted to a cairo distance of 640.0

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.