cd_canvas_coordinate_system.e 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. deferred class CD_CANVAS_COORDINATE_SYSTEM
  2. inherit
  3. CANVAS_DRAW
  4. feature {ANY}
  5. get_size_pixels: TUPLE[INTEGER, INTEGER]
  6. -- Returns the canvas size in pixels.
  7. local
  8. p: POINTER
  9. ix, iy: INTEGER
  10. tup: TUPLE[INTEGER, INTEGER]
  11. do
  12. int_canvas_get_size(cnvs, $ix, $iy, p, p)
  13. tup := [ix, iy]
  14. Result := tup
  15. end
  16. get_size_millimeters: TUPLE[REAL_64, REAL_64]
  17. -- Returns the canvas size in millimeters.
  18. local
  19. p: POINTER
  20. ix, iy: REAL_64
  21. tup: TUPLE[REAL_64, REAL_64]
  22. do
  23. int_canvas_get_size(cnvs, p, p, $ix, $iy)
  24. tup := [ix, iy]
  25. Result := tup
  26. end
  27. update_y_axis (y: INTEGER): INTEGER
  28. -- Invert the given Y coordinate if the native Y axis orientation is
  29. -- different from the CD axis orientation. The CD axis orientation is
  30. -- always bottom-top.
  31. local
  32. yc: INTEGER
  33. do
  34. yc := y
  35. Result := int_canvas_update_y_axis(cnvs, $yc)
  36. end
  37. invert_y_axis (y: INTEGER): INTEGER
  38. -- Invert the given Y coordinate independent of the driver Y axis
  39. -- orientation.
  40. do
  41. Result := int_canvas_invert_y_axis(cnvs, y)
  42. end
  43. convert_mm_to_pixels (mmx, mmy: REAL_64): TUPLE[INTEGER, INTEGER]
  44. -- Converts sizes in millimeters into pixels (canvas coordinates).
  45. -- Return integer values.
  46. local
  47. px, py: INTEGER
  48. tup: TUPLE[INTEGER, INTEGER]
  49. do
  50. int_canvas_mm_to_pixels(cnvs, mmx, mmy, $px, $py)
  51. tup := [px, py]
  52. Result := tup
  53. end
  54. convert_mm_to_pixels_real (mmx, mmy: REAL_64): TUPLE[REAL_64, REAL_64]
  55. -- Converts sizes in millimeters into pixels (canvas coordinates).
  56. -- Return real 64 values.
  57. local
  58. px, py: REAL_64
  59. tup: TUPLE[REAL_64, REAL_64]
  60. do
  61. int_canvas_c_double_mm_to_pixels(cnvs, mmx, mmy, $px, $py)
  62. tup := [px, py]
  63. Result := tup
  64. end
  65. convert_pixels_to_mm (px, py: INTEGER): TUPLE[REAL_64, REAL_64]
  66. -- Converts sizes in pixels (canvas coordinates as integer values) into
  67. -- millimeters. Use this function to obtain the horizontal and vertical
  68. -- resolution of the canvas by passing 1 as parameter in dx and dy. The
  69. -- resolution value is obtained using the formula res=1.0/mm.
  70. local
  71. mmx, mmy: REAL_64
  72. tup: TUPLE[REAL_64, REAL_64]
  73. do
  74. int_canvas_pixels_to_mm(cnvs, px, py, $mmx, $mmy)
  75. tup := [mmx, mmy]
  76. Result := tup
  77. end
  78. convert_pixels_to_mm_real (px, py: REAL_64): TUPLE[REAL_64, REAL_64]
  79. -- Converts sizes in pixels (canvas coordinates as real 64 values) into
  80. -- millimeters. Use this function to obtain the horizontal and vertical
  81. -- resolution of the canvas by passing 1 as parameter in dx and dy. The
  82. -- resolution value is obtained using the formula res=1.0/mm.
  83. local
  84. mmx, mmy: REAL_64
  85. tup: TUPLE[REAL_64, REAL_64]
  86. do
  87. int_canvas_c_double_pixels_to_mm(cnvs, px, py, $mmx, $mmy)
  88. tup := [mmx, mmy]
  89. Result := tup
  90. end
  91. set_origin (x, y: INTEGER)
  92. -- Allows translating the origin - for instance, to the center of the
  93. -- canvas. The function profits from the architecture of the library to
  94. -- simulate a translation of the origin, which in fact is never actually
  95. -- passed to the canvas in the respective driver. It is not related with
  96. -- WD nor Transformation Matrix. Default values: (0, 0)
  97. do
  98. int_canvas_origin(cnvs, x, y)
  99. end
  100. set_origin_real (x, y: REAL_64)
  101. -- As "set_origin" but the parematers are real 64 values.
  102. do
  103. int_canvas_c_double_origin(cnvs, x, y)
  104. end
  105. get_origin: TUPLE[INTEGER, INTEGER]
  106. -- Return the origin as integer values.
  107. local
  108. x, y: INTEGER
  109. do
  110. int_canvas_get_origin(cnvs, $x, $y)
  111. Result := [x, y]
  112. end
  113. get_origin_real: TUPLE[REAL_64, REAL_64]
  114. -- Return the origin as real 64 values.
  115. local
  116. x, y: REAL_64
  117. do
  118. int_canvas_c_double_get_origin(cnvs, $x, $y)
  119. Result := [x, y]
  120. end
  121. -- Transformation matrix
  122. set_transform (matrix: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64])
  123. -- Defines a transformation matrix with 6 elements. If the matrix is
  124. -- Void, the transformation is reset to the identity. Default value: Void.
  125. --
  126. -- The matrix contains scale (sx,sy), rotation (angle) and translation
  127. -- (dx,dy) elements as follows:
  128. --
  129. -- matrix[0] = sx*cos(angle) // Horizontal Scale and Rotation component
  130. -- matrix[1] = sin(angle) // Rotation component (can also contain an
  131. -- horizontal shear component)
  132. -- matrix[2] = -sin(angle) // Rotation component (can also contain a
  133. -- vertical shear component)
  134. -- matrix[3] = sy*cos(angle) // Vertical Scale and Rotation component
  135. -- matrix[4] = dx // Horizontal Translation component
  136. -- matrix[5] = dy // Vertical Translation component
  137. --
  138. -- Functions that retrieve images from the canvas are not affected by the
  139. -- transformation matrix, such as "get_image", "get_image_rgb" and
  140. -- "scroll_area".
  141. --
  142. -- Transformation matrix is independent of the World Coordinate and
  143. -- Origin functions. And those are affected if a transformation is set,
  144. -- just like other regular primitives.
  145. --
  146. -- The transformation matrix and world coordinates perform similar
  147. -- functions. World coordinates were developed before the transformation
  148. -- matrix support. The transformation matrix operates at a lower level
  149. -- than world coordinates, and, as such, might be faster, but might
  150. -- behave differently on different platforms. World coordinates behave
  151. -- consistently across platforms.
  152. local
  153. m: NATIVE_ARRAY[REAL_64]
  154. do
  155. m := m.calloc(6)
  156. m.put(matrix.item_1, 0)
  157. m.put(matrix.item_1, 1)
  158. m.put(matrix.item_1, 2)
  159. m.put(matrix.item_1, 3)
  160. m.put(matrix.item_1, 4)
  161. m.put(matrix.item_1, 5)
  162. int_canvas_transform(cnvs, m.to_external)
  163. end
  164. get_transform: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]
  165. -- Returns the transformation matrix. If the identity is set,
  166. -- returns Void.
  167. local
  168. p: POINTER
  169. m: NATIVE_ARRAY[REAL_64]
  170. do
  171. p := int_canvas_get_transform(cnvs)
  172. if p.is_not_null then
  173. m := m.calloc(6)
  174. m := m.from_pointer(p)
  175. Result := [m.item(0), m.item(1), m.item(2), m.item(3), m.item(4), m.item(5)]
  176. end
  177. end
  178. apply_transform_multiply (matrix: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64])
  179. -- Left multiply the current transformation by the given transformation.
  180. local
  181. m: NATIVE_ARRAY[REAL_64]
  182. do
  183. m := m.calloc(4)
  184. m.put(matrix.item_1, 0)
  185. m.put(matrix.item_1, 1)
  186. m.put(matrix.item_1, 2)
  187. m.put(matrix.item_1, 3)
  188. int_canvas_transform_multiply(cnvs, m.to_external)
  189. end
  190. apply_transform_translate (dx, dy: REAL_64)
  191. -- Applies a translation to the current transformation.
  192. do
  193. int_canvas_transform_translate(cnvs, dx, dy)
  194. end
  195. apply_transform_scale (sx, sy: REAL_64)
  196. -- Applies a scale to the current transformation.
  197. do
  198. int_canvas_transform_scale(cnvs, sx, sy)
  199. end
  200. apply_transform_rotate (angle: REAL_64)
  201. -- Applies a rotation to the current transformation. Angle is in degrees,
  202. -- oriented counter-clockwise from the horizontal axis.
  203. do
  204. int_canvas_transform_rotate(cnvs, angle)
  205. end
  206. transform_point (x, y: INTEGER): TUPLE[INTEGER, INTEGER]
  207. -- Applies a transformation to a given point.
  208. local
  209. tx, ty: INTEGER
  210. do
  211. int_canvas_transform_point(cnvs, x, y, $tx, $ty)
  212. Result := [tx, ty]
  213. end
  214. transform_point_real (x, y: REAL_64): TUPLE[REAL_64, REAL_64]
  215. -- Like "transform_point" but with REAL_64 values.
  216. local
  217. tx, ty: REAL_64
  218. do
  219. int_canvas_c_double_transform_point(cnvs, x, y, $tx, $ty)
  220. Result := [tx, ty]
  221. end
  222. feature {}
  223. int_canvas_get_size (wgt: POINTER; x, y, xm, ym: POINTER)
  224. external "plug_in"
  225. alias "{
  226. location: "${sys}/plugins"
  227. module_name: "iup"
  228. feature_name: "cdCanvasGetSize"
  229. }"
  230. end
  231. int_canvas_update_y_axis (wgt, y: POINTER): INTEGER
  232. external "plug_in"
  233. alias "{
  234. location: "${sys}/plugins"
  235. module_name: "iup"
  236. feature_name: "cdCanvasUpdateYAxis"
  237. }"
  238. end
  239. int_canvas_invert_y_axis (wgt: POINTER; yc: INTEGER): INTEGER
  240. external "plug_in"
  241. alias "{
  242. location: "${sys}/plugins"
  243. module_name: "iup"
  244. feature_name: "cdCanvasInvertYAxis"
  245. }"
  246. end
  247. int_canvas_mm_to_pixels (wgt: POINTER; mm_x, mm_y: REAL_64; cx, cy: POINTER)
  248. external "plug_in"
  249. alias "{
  250. location: "${sys}/plugins"
  251. module_name: "iup"
  252. feature_name: "cdCanvasMM2Pixel"
  253. }"
  254. end
  255. int_canvas_c_double_mm_to_pixels (wgt: POINTER; mm_x, mm_y: REAL_64; cx, cy: POINTER)
  256. external "plug_in"
  257. alias "{
  258. location: "${sys}/plugins"
  259. module_name: "iup"
  260. feature_name: "cdfCanvasMM2Pixel"
  261. }"
  262. end
  263. int_canvas_pixels_to_mm (wgt: POINTER; p_x, p_y: INTEGER; cx, cy: POINTER)
  264. external "plug_in"
  265. alias "{
  266. location: "${sys}/plugins"
  267. module_name: "iup"
  268. feature_name: "cdCanvasPixel2MM"
  269. }"
  270. end
  271. int_canvas_c_double_pixels_to_mm (wgt: POINTER; p_x, p_y: REAL_64; cx, cy: POINTER)
  272. external "plug_in"
  273. alias "{
  274. location: "${sys}/plugins"
  275. module_name: "iup"
  276. feature_name: "cdfCanvasPixel2MM"
  277. }"
  278. end
  279. int_canvas_origin (wgt: POINTER; x, y: INTEGER)
  280. external "plug_in"
  281. alias "{
  282. location: "${sys}/plugins"
  283. module_name: "iup"
  284. feature_name: "cdCanvasOrigin"
  285. }"
  286. end
  287. int_canvas_c_double_origin (wgt: POINTER; x, y: REAL_64)
  288. external "plug_in"
  289. alias "{
  290. location: "${sys}/plugins"
  291. module_name: "iup"
  292. feature_name: "cdfCanvasOrigin"
  293. }"
  294. end
  295. int_canvas_get_origin (wgt, x, y: POINTER)
  296. external "plug_in"
  297. alias "{
  298. location: "${sys}/plugins"
  299. module_name: "iup"
  300. feature_name: "cdCanvasGetOrigin"
  301. }"
  302. end
  303. int_canvas_c_double_get_origin (wgt, x, y: POINTER)
  304. external "plug_in"
  305. alias "{
  306. location: "${sys}/plugins"
  307. module_name: "iup"
  308. feature_name: "cdfCanvasGetOrigin"
  309. }"
  310. end
  311. int_canvas_transform (wgt, m: POINTER)
  312. external "plug_in"
  313. alias "{
  314. location: "${sys}/plugins"
  315. module_name: "iup"
  316. feature_name: "cdCanvasTransform"
  317. }"
  318. end
  319. int_canvas_get_transform (wgt: POINTER): POINTER
  320. external "plug_in"
  321. alias "{
  322. location: "${sys}/plugins"
  323. module_name: "iup"
  324. feature_name: "cdCanvasGetTransform"
  325. }"
  326. end
  327. int_canvas_transform_multiply (wgt, m: POINTER)
  328. external "plug_in"
  329. alias "{
  330. location: "${sys}/plugins"
  331. module_name: "iup"
  332. feature_name: "cdCanvasTransformMultiply"
  333. }"
  334. end
  335. int_canvas_transform_translate (wgt: POINTER; dx, dy: REAL_64)
  336. external "plug_in"
  337. alias "{
  338. location: "${sys}/plugins"
  339. module_name: "iup"
  340. feature_name: "cdCanvasTransformTranslate"
  341. }"
  342. end
  343. int_canvas_transform_scale (wgt: POINTER; sx, sy: REAL_64)
  344. external "plug_in"
  345. alias "{
  346. location: "${sys}/plugins"
  347. module_name: "iup"
  348. feature_name: "cdCanvasTransformScale"
  349. }"
  350. end
  351. int_canvas_transform_rotate (wgt: POINTER; ang: REAL_64)
  352. external "plug_in"
  353. alias "{
  354. location: "${sys}/plugins"
  355. module_name: "iup"
  356. feature_name: "cdCanvasTransformRotate"
  357. }"
  358. end
  359. int_canvas_transform_point (wgt: POINTER; x, y: INTEGER; tx, ty: POINTER)
  360. external "plug_in"
  361. alias "{
  362. location: "${sys}/plugins"
  363. module_name: "iup"
  364. feature_name: "cdCanvasTransformPoint"
  365. }"
  366. end
  367. int_canvas_c_double_transform_point (wgt: POINTER; x, y: REAL_64; tx, ty: POINTER)
  368. external "plug_in"
  369. alias "{
  370. location: "${sys}/plugins"
  371. module_name: "iup"
  372. feature_name: "cdfCanvasTransformPoint"
  373. }"
  374. end
  375. end
  376. -- The MIT License (MIT)
  377. -- Copyright (c) 2016, 2017 by German A. Arias
  378. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  379. -- of this software and associated documentation files (the "Software"), to deal
  380. -- in the Software without restriction, including without limitation the rights
  381. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  382. -- copies of the Software, and to permit persons to whom the Software is
  383. -- furnished to do so, subject to the following conditions:
  384. --
  385. -- The above copyright notice and this permission notice shall be included in
  386. -- all copies or substantial portions of the Software.
  387. --
  388. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  389. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  390. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  391. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  392. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  393. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  394. -- SOFTWARE.