cd_vector_text.e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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, text.to_external)
  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, text.to_external)
  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, m2: NATIVE_ARRAY[REAL_64]
  64. do
  65. m1 := m1.calloc(6)
  66. m1.put(matrix.item_1, 0)
  67. m1.put(matrix.item_1, 1)
  68. m1.put(matrix.item_1, 2)
  69. m1.put(matrix.item_1, 3)
  70. m1.put(matrix.item_1, 4)
  71. m1.put(matrix.item_1, 5)
  72. p := int_canvas_vector_text_transform(cnvs, m1.to_external)
  73. if p.is_not_null then
  74. m2 := m2.calloc(6)
  75. m2 := m2.from_pointer(p)
  76. Result := [m2.item(0), m2.item(1), m2.item(2), m2.item(3), m2.item(4), m2.item(5)]
  77. end
  78. end
  79. set_vector_text_size (width, height: INTEGER; text: STRING)
  80. -- Modifies the font size of the vector text so that it fits the string
  81. -- in the box defined by width and height.
  82. do
  83. int_canvas_vector_text_size(cnvs, width, height, text.to_external)
  84. end
  85. set_vector_text_size_real (width, height: REAL_64; text: STRING)
  86. -- Like "set_vector_text_size" but using REAL_64 values.
  87. do
  88. int_canvas_c_double_vector_text_size(cnvs, width, height, text.to_external)
  89. end
  90. set_vector_char_size (size: INTEGER)
  91. -- Modifies the font size by specifying the height of the characters.
  92. local
  93. i: INTEGER
  94. do
  95. i := int_canvas_vector_char_size(cnvs, size)
  96. end
  97. get_vector_char_size: INTEGER
  98. do
  99. Result := int_canvas_vector_char_size(cnvs, -1)
  100. end
  101. set_vector_char_size_real (size: REAL_64)
  102. -- Like "set_vector_char_size" but with a REAL_64 value.
  103. local
  104. i: REAL_64
  105. do
  106. i := int_canvas_c_double_vector_char_size(cnvs, size)
  107. end
  108. get_vector_char_size_real: REAL_64
  109. do
  110. Result := int_canvas_c_double_vector_char_size(cnvs, -1)
  111. end
  112. set_vector_font_size (size_x, size_y: REAL_64)
  113. -- Directly modifies the font size. Set size_x=size_y to maintain the
  114. -- original aspect ratio of the font.
  115. do
  116. int_canvas_vector_font_size(cnvs, size_x, size_y)
  117. end
  118. get_vector_font_size: TUPLE[REAL_64, REAL_64]
  119. -- Returns the font size.
  120. local
  121. x, y: REAL_64
  122. do
  123. int_canvas_get_vector_font_size(cnvs, $x, $y)
  124. Result := [x, y]
  125. end
  126. set_vector_font (filename: STRING): STRING
  127. -- Replaces the current vector font with a font stored in a file with a
  128. -- given name. Returns the name of the font loaded or Void, if it fails.
  129. -- If filename is Void, it activates the default font "Simplex II" (There
  130. -- is no file associated to this font, it is an embedded font). The
  131. -- library will attempt to load a font from the current directory, if it
  132. -- fails then it will try the directory defined by the environment
  133. -- variable "CDDIR", if it fails, it will attempt to load it using the
  134. -- filename as a string containing the font as if the file was loaded
  135. -- into that string, if it fails again the font is reset to the default
  136. -- font and returns Void. The file format is compatible with the GKS file
  137. -- format (text mode).
  138. local
  139. p: POINTER
  140. s: STRING
  141. do
  142. p := int_canvas_vector_font(cnvs, filename.to_external)
  143. if p.is_not_null then
  144. create s.from_external_copy(p)
  145. Result := s
  146. end
  147. end
  148. -- Properties
  149. get_vector_text_size (text: STRING): TUPLE[INTEGER, INTEGER]
  150. -- Returns the text size independent from orientation.
  151. local
  152. width, height: INTEGER
  153. do
  154. int_canvas_get_vector_text_size(cnvs, text.to_external, $width, $height)
  155. Result := [width, height]
  156. end
  157. get_vector_text_size_real (text: STRING): TUPLE[REAL_64, REAL_64]
  158. -- Like "get_vector_text_size" but with REAL_64 values.
  159. local
  160. width, height: REAL_64
  161. do
  162. int_canvas_c_double_get_vector_text_size(cnvs, text.to_external, $width, $height)
  163. Result := [width, height]
  164. end
  165. get_vector_text_bounds (x, y: INTEGER; text: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER]
  166. -- Returns the oriented bounding rectangle occupied by a text at a given
  167. -- position. The rectangle has the same dimentions returned by
  168. -- "get_vector_text_size". The rectangle corners are returned in
  169. -- counter-clock wise order starting with the bottom left corner, arranged
  170. -- (x0,y0,x1,y1,x2,y2,x3,y3).
  171. local
  172. points: NATIVE_ARRAY[INTEGER]
  173. do
  174. points := points.calloc(8)
  175. points.set_all_with(0, 7)
  176. int_canvas_get_vector_text_bounds(cnvs, text.to_external, x, y, points.to_external)
  177. Result := [points.item(0), points.item(1),
  178. points.item(2), points.item(3),
  179. points.item(4), points.item(5),
  180. points.item(6), points.item(7)]
  181. end
  182. 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]
  183. -- Like 'get_vector_text_bounds' but with REAL_64 values.
  184. local
  185. points: NATIVE_ARRAY[REAL_64]
  186. do
  187. points := points.calloc(8)
  188. points.set_all_with(0, 7)
  189. int_canvas_c_double_get_vector_text_bounds(cnvs, text.to_external, x, y, points.to_external)
  190. Result := [points.item(0), points.item(1),
  191. points.item(2), points.item(3),
  192. points.item(4), points.item(5),
  193. points.item(6), points.item(7)]
  194. end
  195. get_vector_text_box (x, y: INTEGER; text: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  196. -- Returns the horizontal bounding rectangle occupied by a text at a
  197. -- given position. If orientation is not 0 then its area is always larger
  198. -- than the area of the rectangle returned by "get_vector_text_bounds".
  199. local
  200. xmin, xmax, ymin, ymax: INTEGER
  201. do
  202. int_canvas_get_vector_text_box(cnvs, x, y, text.to_external, $xmin, $xmax, $ymin, $ymax)
  203. Result := [xmin, xmax, ymin, ymax]
  204. end
  205. get_vector_text_box_real (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
  206. -- Like 'get_vector_text_box' but using REAL_64 values.
  207. local
  208. xmin, xmax, ymin, ymax: REAL_64
  209. do
  210. int_canvas_c_double_get_vector_text_box(cnvs, x, y, text.to_external, $xmin, $xmax, $ymin, $ymax)
  211. Result := [xmin, xmax, ymin, ymax]
  212. end
  213. feature {}
  214. int_canvas_vector_text(wgt: POINTER; x, y: INTEGER; text: POINTER)
  215. external "plug_in"
  216. alias "{
  217. location: "${sys}/plugins"
  218. module_name: "iup"
  219. feature_name: "cdCanvasVectorText"
  220. }"
  221. end
  222. int_wd_canvas_vector_text(wgt: POINTER; x, y: REAL_64; text: POINTER)
  223. external "plug_in"
  224. alias "{
  225. location: "${sys}/plugins"
  226. module_name: "iup"
  227. feature_name: "wdCanvasVectorText"
  228. }"
  229. end
  230. int_canvas_vector_text_direction(wgt: POINTER; x1, x2, y1, y2: INTEGER)
  231. external "plug_in"
  232. alias "{
  233. location: "${sys}/plugins"
  234. module_name: "iup"
  235. feature_name: "cdCanvasVectorTextDirection"
  236. }"
  237. end
  238. int_canvas_c_double_vector_text_direction(wgt: POINTER; x1, x2, y1, y2: REAL_64)
  239. external "plug_in"
  240. alias "{
  241. location: "${sys}/plugins"
  242. module_name: "iup"
  243. feature_name: "wdCanvasVectorTextDirection"
  244. }"
  245. end
  246. int_canvas_vector_text_transform(wgt, m: POINTER): POINTER
  247. external "plug_in"
  248. alias "{
  249. location: "${sys}/plugins"
  250. module_name: "iup"
  251. feature_name: "cdCanvasVectorTextTransform"
  252. }"
  253. end
  254. int_canvas_vector_text_size(wgt: POINTER; w, h: INTEGER; t: POINTER)
  255. external "plug_in"
  256. alias "{
  257. location: "${sys}/plugins"
  258. module_name: "iup"
  259. feature_name: "cdCanvasVectorTextSize"
  260. }"
  261. end
  262. int_canvas_c_double_vector_text_size(wgt: POINTER; w, h: REAL_64; t: POINTER)
  263. external "plug_in"
  264. alias "{
  265. location: "${sys}/plugins"
  266. module_name: "iup"
  267. feature_name: "wdCanvasVectorTextSize"
  268. }"
  269. end
  270. int_canvas_vector_char_size(wgt: POINTER; s: INTEGER): INTEGER
  271. external "plug_in"
  272. alias "{
  273. location: "${sys}/plugins"
  274. module_name: "iup"
  275. feature_name: "cdCanvasVectorCharSize"
  276. }"
  277. end
  278. int_canvas_c_double_vector_char_size(wgt: POINTER; s: REAL_64): REAL_64
  279. external "plug_in"
  280. alias "{
  281. location: "${sys}/plugins"
  282. module_name: "iup"
  283. feature_name: "wdCanvasVectorCharSize"
  284. }"
  285. end
  286. int_canvas_vector_font_size(wgt: POINTER; x, y: REAL_64)
  287. external "plug_in"
  288. alias "{
  289. location: "${sys}/plugins"
  290. module_name: "iup"
  291. feature_name: "cdCanvasVectorFontSize"
  292. }"
  293. end
  294. int_canvas_get_vector_font_size(wgt, x, y: POINTER)
  295. external "plug_in"
  296. alias "{
  297. location: "${sys}/plugins"
  298. module_name: "iup"
  299. feature_name: "cdCanvasGetVectorFontSize"
  300. }"
  301. end
  302. int_canvas_vector_font(wgt, fn: POINTER): POINTER
  303. external "plug_in"
  304. alias "{
  305. location: "${sys}/plugins"
  306. module_name: "iup"
  307. feature_name: "cdCanvasVectorFont"
  308. }"
  309. end
  310. int_canvas_get_vector_text_size(wgt, t, w, h: POINTER)
  311. external "plug_in"
  312. alias "{
  313. location: "${sys}/plugins"
  314. module_name: "iup"
  315. feature_name: "cdCanvasGetVectorTextSize"
  316. }"
  317. end
  318. int_canvas_c_double_get_vector_text_size(wgt, t, w, h: POINTER)
  319. external "plug_in"
  320. alias "{
  321. location: "${sys}/plugins"
  322. module_name: "iup"
  323. feature_name: "wdCanvasGetVectorTextSize"
  324. }"
  325. end
  326. int_canvas_get_vector_text_bounds (wgt, text: POINTER; x, y: INTEGER; r: POINTER)
  327. external "plug_in"
  328. alias "{
  329. location: "${sys}/plugins"
  330. module_name: "iup"
  331. feature_name: "cdCanvasGetVectorTextBounds"
  332. }"
  333. end
  334. int_canvas_c_double_get_vector_text_bounds (wgt, text: POINTER; x, y: REAL_64; r: POINTER)
  335. external "plug_in"
  336. alias "{
  337. location: "${sys}/plugins"
  338. module_name: "iup"
  339. feature_name: "wdCanvasGetVectorTextBounds"
  340. }"
  341. end
  342. int_canvas_get_vector_text_box (wgt: POINTER; x, y: INTEGER; text, xmin, xmax, ymin, ymax: POINTER)
  343. external "plug_in"
  344. alias "{
  345. location: "${sys}/plugins"
  346. module_name: "iup"
  347. feature_name: "cdCanvasGetVectorTextBox"
  348. }"
  349. end
  350. int_canvas_c_double_get_vector_text_box (wgt: POINTER; x, y: REAL_64; text, xmin, xmax, ymin, ymax: POINTER)
  351. external "plug_in"
  352. alias "{
  353. location: "${sys}/plugins"
  354. module_name: "iup"
  355. feature_name: "wdCanvasGetVectorTextBox"
  356. }"
  357. end
  358. end
  359. -- The MIT License (MIT)
  360. -- Copyright (c) 2017 by German A. Arias
  361. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  362. -- of this software and associated documentation files (the "Software"), to deal
  363. -- in the Software without restriction, including without limitation the rights
  364. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  365. -- copies of the Software, and to permit persons to whom the Software is
  366. -- furnished to do so, subject to the following conditions:
  367. --
  368. -- The above copyright notice and this permission notice shall be included in
  369. -- all copies or substantial portions of the Software.
  370. --
  371. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  372. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  373. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  374. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  375. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  376. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  377. -- SOFTWARE.