cd_vector_text.e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. deferred class CD_VECTOR_TEXT
  2. -- It is a text that uses a font created only with line segments. It is very
  3. -- useful to be scaled and very fast. You must set the text size before drawing
  4. -- any text. The default direction is horizontal from left to right.
  5. --
  6. -- All vector text drawing in all drivers are simulated with other CD
  7. -- primitives using polygons only.
  8. inherit
  9. CANVAS_DRAW
  10. feature {ANY}
  11. draws_vector_text (x, y: INTEGER; text: STRING)
  12. -- Draws a vector text in position (x,y), respecting the alignment
  13. -- defined by "set_text_alignment". It ignores the configuration
  14. -- "set_back_opacity", being always transparent. It accepts strings with
  15. -- multiple lines using '%n'. It is ESSENTIAL to call
  16. -- "set_vector_text_size" or "set_vector_char_size" before using this
  17. -- function.
  18. do
  19. int_canvas_vector_text(cnvs, x, y, get_pointer(text.to_c))
  20. end
  21. wd_draws_vector_text (x, y: REAL_64; text: STRING)
  22. -- Like "draws_vector_text" but using World Coordinates.
  23. do
  24. int_wd_canvas_vector_text(cnvs, x, y, get_pointer(text.to_c))
  25. end
  26. -- Attributes
  27. set_vector_text_direction (x1, x2, y1, y2: INTEGER)
  28. -- Defines the text direction by means of two points, (x1,y1) and
  29. -- (x2,y2). The default direction is horizontal from left to right. It is
  30. -- independent from the transformation matrix.
  31. do
  32. int_canvas_vector_text_direction(cnvs, x1, x2, y1, y2)
  33. end
  34. set_vector_text_direction_real (x1, x2, y1, y2: REAL_64)
  35. -- Like "set_vector_text_direction" but using World Coordinates.
  36. do
  37. int_canvas_c_double_vector_text_direction(cnvs, x1, x2, y1, y2)
  38. end
  39. set_vector_text_transform (matrix: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]
  40. -- Defines a transformation matrix with 6 elements. If the matrix is
  41. -- Void, no transformation is set. The default is no transformation. The
  42. -- origin is the left bottom corner of matrix. It returns the previous
  43. -- matrix, and the returned vector is only valid until the following call
  44. -- to the function.
  45. --
  46. -- The matrix contains scale, rotation and translation elements. It is
  47. -- applied after computing the position and orientation normal to the
  48. -- vector text. We can describe the elements as follows:
  49. --
  50. -- matrix[0] = sin(angle) // Rotation component (can also contain an
  51. -- horizontal shear component)
  52. -- matrix[1] = scl_y*cos(angle) // Horizontal Scale and Rotation component
  53. -- matrix[2] = trans_y // Vertical Translation component
  54. -- matrix[3] = scl_x*cos(angle) // Vertical Scale and Rotation component
  55. -- matrix[4] = -sin(angle) // Rotation component (can also contain a
  56. -- vertical shear component)
  57. -- matrix[5] = trans_x // Horizontal Translation component
  58. --
  59. -- It has the same effect of the "set_transform", but notice that the
  60. -- indices are different.
  61. local
  62. p: POINTER
  63. m1: ARRAY[REAL_64]
  64. m2: MANAGED_POINTER
  65. do
  66. create m1.make_filled((0).to_double, 1, 6 + 1)
  67. m1.put(matrix.real_64_item(1), 1)
  68. m1.put(matrix.real_64_item(2), 2)
  69. m1.put(matrix.real_64_item(3), 3)
  70. m1.put(matrix.real_64_item(4), 4)
  71. m1.put(matrix.real_64_item(5), 5)
  72. m1.put(matrix.real_64_item(6), 6)
  73. p := int_canvas_vector_text_transform(cnvs, get_pointer(m1.to_c))
  74. if p /= default_pointer then
  75. create m2.make_from_pointer(p, 6)
  76. Result := [m2.read_real_64(0), m2.read_real_64(1), m2.read_real_64(2), m2.read_real_64(3), m2.read_real_64(4), m2.read_real_64(5)]
  77. else
  78. io.put_string("Somenthing is wrong %N")
  79. Result := [(0).to_double, (0).to_double, (0).to_double, (0).to_double, (0).to_double, (0).to_double]
  80. end
  81. end
  82. set_vector_text_size (width, height: INTEGER; text: STRING)
  83. -- Modifies the font size of the vector text so that it fits the string
  84. -- in the box defined by width and height.
  85. do
  86. int_canvas_vector_text_size(cnvs, width, height, get_pointer(text.to_c))
  87. end
  88. set_vector_text_size_real (width, height: REAL_64; text: STRING)
  89. -- Like "set_vector_text_size" but using REAL_64 values.
  90. do
  91. int_canvas_c_double_vector_text_size(cnvs, width, height, get_pointer(text.to_c))
  92. end
  93. set_vector_char_size (size: INTEGER)
  94. -- Modifies the font size by specifying the height of the characters.
  95. local
  96. i: INTEGER
  97. do
  98. i := int_canvas_vector_char_size(cnvs, size)
  99. end
  100. get_vector_char_size: INTEGER
  101. do
  102. Result := int_canvas_vector_char_size(cnvs, -1)
  103. end
  104. set_vector_char_size_real (size: REAL_64)
  105. -- Like "set_vector_char_size" but with a REAL_64 value.
  106. local
  107. i: REAL_64
  108. do
  109. i := int_canvas_c_double_vector_char_size(cnvs, size)
  110. end
  111. get_vector_char_size_real: REAL_64
  112. do
  113. Result := int_canvas_c_double_vector_char_size(cnvs, -1)
  114. end
  115. set_vector_font_size (size_x, size_y: REAL_64)
  116. -- Directly modifies the font size. Set size_x=size_y to maintain the
  117. -- original aspect ratio of the font.
  118. do
  119. int_canvas_vector_font_size(cnvs, size_x, size_y)
  120. end
  121. get_vector_font_size: TUPLE[REAL_64, REAL_64]
  122. -- Returns the font size.
  123. local
  124. x, y: REAL_64
  125. do
  126. int_canvas_get_vector_font_size(cnvs, $x, $y)
  127. Result := [x, y]
  128. end
  129. set_vector_font (filename: STRING): STRING
  130. -- Replaces the current vector font with a font stored in a file with a
  131. -- given name. Returns the name of the font loaded or Void, if it fails.
  132. -- If filename is Void, it activates the default font "Simplex II" (There
  133. -- is no file associated to this font, it is an embedded font). The
  134. -- library will attempt to load a font from the current directory, if it
  135. -- fails then it will try the directory defined by the environment
  136. -- variable "CDDIR", if it fails, it will attempt to load it using the
  137. -- filename as a string containing the font as if the file was loaded
  138. -- into that string, if it fails again the font is reset to the default
  139. -- font and returns Void. The file format is compatible with the GKS file
  140. -- format (text mode).
  141. local
  142. p: POINTER
  143. s: STRING
  144. do
  145. p := int_canvas_vector_font(cnvs, get_pointer(filename.to_c))
  146. if p /= default_pointer then
  147. create s.make_from_c(p)
  148. Result := s
  149. else
  150. create Result.make_empty
  151. end
  152. end
  153. -- Properties
  154. get_vector_text_size (text: STRING): TUPLE[INTEGER, INTEGER]
  155. -- Returns the text size independent from orientation.
  156. local
  157. width, height: INTEGER
  158. do
  159. int_canvas_get_vector_text_size(cnvs, get_pointer(text.to_c), $width, $height)
  160. Result := [width, height]
  161. end
  162. get_vector_text_size_real (text: STRING): TUPLE[REAL_64, REAL_64]
  163. -- Like "get_vector_text_size" but with REAL_64 values.
  164. local
  165. width, height: REAL_64
  166. do
  167. int_canvas_c_double_get_vector_text_size(cnvs, get_pointer(text.to_c), $width, $height)
  168. Result := [width, height]
  169. end
  170. get_vector_text_bounds (x, y: INTEGER; text: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER]
  171. -- Returns the oriented bounding rectangle occupied by a text at a given
  172. -- position. The rectangle has the same dimentions returned by
  173. -- "get_vector_text_size". The rectangle corners are returned in
  174. -- counter-clock wise order starting with the bottom left corner, arranged
  175. -- (x0,y0,x1,y1,x2,y2,x3,y3).
  176. local
  177. p: POINTER
  178. points: MANAGED_POINTER
  179. do
  180. int_canvas_get_vector_text_bounds(cnvs, get_pointer(text.to_c), x, y, p)
  181. create points.make_from_pointer(p, 8)
  182. Result := [points.read_integer_32(0), points.read_integer_32(1),
  183. points.read_integer_32(2), points.read_integer_32(3),
  184. points.read_integer_32(4), points.read_integer_32(5),
  185. points.read_integer_32(6), points.read_integer_32(7)]
  186. end
  187. get_vector_text_bounds_real (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]
  188. -- Like 'get_vector_text_bounds' but with REAL_64 values.
  189. local
  190. p: POINTER
  191. points: MANAGED_POINTER
  192. do
  193. int_canvas_c_double_get_vector_text_bounds(cnvs, get_pointer(text.to_c), x, y, p)
  194. create points.make_from_pointer(p, 8)
  195. Result := [points.read_real_64(0), points.read_real_64(1),
  196. points.read_real_64(2), points.read_real_64(3),
  197. points.read_real_64(4), points.read_real_64(5),
  198. points.read_real_64(6), points.read_real_64(7)]
  199. end
  200. get_vector_text_box (x, y: INTEGER; text: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  201. -- Returns the horizontal bounding rectangle occupied by a text at a
  202. -- given position. If orientation is not 0 then its area is always larger
  203. -- than the area of the rectangle returned by "get_vector_text_bounds".
  204. local
  205. xmin, xmax, ymin, ymax: INTEGER
  206. do
  207. int_canvas_get_vector_text_box(cnvs, x, y, get_pointer(text.to_c), $xmin, $xmax, $ymin, $ymax)
  208. Result := [xmin, xmax, ymin, ymax]
  209. end
  210. get_vector_text_box_real (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
  211. -- Like 'get_vector_text_box' but using REAL_64 values.
  212. local
  213. xmin, xmax, ymin, ymax: REAL_64
  214. do
  215. int_canvas_c_double_get_vector_text_box(cnvs, x, y, get_pointer(text.to_c), $xmin, $xmax, $ymin, $ymax)
  216. Result := [xmin, xmax, ymin, ymax]
  217. end
  218. feature {NONE}
  219. int_canvas_vector_text(wgt: POINTER; x, y: INTEGER; text: POINTER)
  220. external
  221. "C inline use %"eiffel-iup.h%""
  222. alias
  223. "cdCanvasVectorText ($wgt, $x, $y, $text);"
  224. end
  225. int_wd_canvas_vector_text(wgt: POINTER; x, y: REAL_64; text: POINTER)
  226. external
  227. "C inline use %"eiffel-iup.h%""
  228. alias
  229. "wdCanvasVectorText ($wgt, $x, $y, $text);"
  230. end
  231. int_canvas_vector_text_direction(wgt: POINTER; x1, x2, y1, y2: INTEGER)
  232. external
  233. "C inline use %"eiffel-iup.h%""
  234. alias
  235. "cdCanvasVectorTextDirection ($wgt, $x1, $x2, $y1, $y2);"
  236. end
  237. int_canvas_c_double_vector_text_direction(wgt: POINTER; x1, x2, y1, y2: REAL_64)
  238. external
  239. "C inline use %"eiffel-iup.h%""
  240. alias
  241. "wdCanvasVectorTextDirection ($wgt, $x1, $x2, $y1, $y2);"
  242. end
  243. int_canvas_vector_text_transform(wgt, m: POINTER): POINTER
  244. external
  245. "C inline use %"eiffel-iup.h%""
  246. alias
  247. "return cdCanvasVectorTextTransform ($wgt, $m);"
  248. end
  249. int_canvas_vector_text_size(wgt: POINTER; w, h: INTEGER; t: POINTER)
  250. external
  251. "C inline use %"eiffel-iup.h%""
  252. alias
  253. "cdCanvasVectorTextSize ($wgt, $w, $h, $t);"
  254. end
  255. int_canvas_c_double_vector_text_size(wgt: POINTER; w, h: REAL_64; t: POINTER)
  256. external
  257. "C inline use %"eiffel-iup.h%""
  258. alias
  259. "wdCanvasVectorTextSize ($wgt, $w, $h, $t);"
  260. end
  261. int_canvas_vector_char_size(wgt: POINTER; s: INTEGER): INTEGER
  262. external
  263. "C inline use %"eiffel-iup.h%""
  264. alias
  265. "return cdCanvasVectorCharSize ($wgt, $s);"
  266. end
  267. int_canvas_c_double_vector_char_size(wgt: POINTER; s: REAL_64): REAL_64
  268. external
  269. "C inline use %"eiffel-iup.h%""
  270. alias
  271. "return wdCanvasVectorCharSize ($wgt, $s);"
  272. end
  273. int_canvas_vector_font_size(wgt: POINTER; x, y: REAL_64)
  274. external
  275. "C inline use %"eiffel-iup.h%""
  276. alias
  277. "cdCanvasVectorFontSize ($wgt, $x, $y);"
  278. end
  279. int_canvas_get_vector_font_size(wgt, x, y: POINTER)
  280. external
  281. "C inline use %"eiffel-iup.h%""
  282. alias
  283. "cdCanvasGetVectorFontSize ($wgt, $x, $y);"
  284. end
  285. int_canvas_vector_font(wgt, fn: POINTER): POINTER
  286. external
  287. "C inline use %"eiffel-iup.h%""
  288. alias
  289. "return cdCanvasVectorFont ($wgt, $fn);"
  290. end
  291. int_canvas_get_vector_text_size(wgt, t, w, h: POINTER)
  292. external
  293. "C inline use %"eiffel-iup.h%""
  294. alias
  295. "cdCanvasGetVectorTextSize ($wgt, $t, $w, $h);"
  296. end
  297. int_canvas_c_double_get_vector_text_size(wgt, t, w, h: POINTER)
  298. external
  299. "C inline use %"eiffel-iup.h%""
  300. alias
  301. "wdCanvasGetVectorTextSize ($wgt, $t, $w, $h);"
  302. end
  303. int_canvas_get_vector_text_bounds (wgt, text: POINTER; x, y: INTEGER; r: POINTER)
  304. external
  305. "C inline use %"eiffel-iup.h%""
  306. alias
  307. "cdCanvasGetVectorTextBounds ($wgt, $text, $x, $y, $r);"
  308. end
  309. int_canvas_c_double_get_vector_text_bounds (wgt, text: POINTER; x, y: REAL_64; r: POINTER)
  310. external
  311. "C inline use %"eiffel-iup.h%""
  312. alias
  313. "wdCanvasGetVectorTextBounds ($wgt, $text, $x, $y, $r);"
  314. end
  315. int_canvas_get_vector_text_box (wgt: POINTER; x, y: INTEGER; text, xmin, xmax, ymin, ymax: POINTER)
  316. external
  317. "C inline use %"eiffel-iup.h%""
  318. alias
  319. "cdCanvasGetVectorTextBox ($wgt, $x, $y, $text, $xmin, $xmax, $ymin, $ymax);"
  320. end
  321. int_canvas_c_double_get_vector_text_box (wgt: POINTER; x, y: REAL_64; text, xmin, xmax, ymin, ymax: POINTER)
  322. external
  323. "C inline use %"eiffel-iup.h%""
  324. alias
  325. "wdCanvasGetVectorTextBox ($wgt, $x, $y, $text, $xmin, $xmax, $ymin, $ymax);"
  326. end
  327. end
  328. -- The MIT License (MIT)
  329. -- Copyright (c) 2017, 2019, 2020 by German A. Arias
  330. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  331. -- of this software and associated documentation files (the "Software"), to deal
  332. -- in the Software without restriction, including without limitation the rights
  333. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  334. -- copies of the Software, and to permit persons to whom the Software is
  335. -- furnished to do so, subject to the following conditions:
  336. --
  337. -- The above copyright notice and this permission notice shall be included in
  338. -- all copies or substantial portions of the Software.
  339. --
  340. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  341. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  342. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  343. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  344. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  345. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  346. -- SOFTWARE.