1use skia_bindings::SkTextUtils;
2
3use crate::{prelude::*, Canvas, EncodedText, Font, Paint, Path, Point};
4
5pub use skia_bindings::SkTextUtils_Align as Align;
6variant_name!(Align::Center);
7
8pub fn draw_str(
9 canvas: &Canvas,
10 text: impl AsRef<str>,
11 p: impl Into<Point>,
12 font: &Font,
13 paint: &Paint,
14 align: Align,
15) {
16 draw_text(canvas, text.as_ref(), p, font, paint, align)
17}
18
19pub fn draw_text(
20 canvas: &Canvas,
21 text: impl EncodedText,
22 p: impl Into<Point>,
23 font: &Font,
24 paint: &Paint,
25 align: Align,
26) {
27 let (ptr: *const c_void, size: usize, encoding: TextEncoding) = text.as_raw();
28 let p = p.into();
29 unsafe {
30 SkTextUtils::Draw(
31 arg1:canvas.native_mut(),
32 text:ptr,
33 size,
34 arg2:encoding.into_native(),
35 p.x,
36 p.y,
37 arg3:font.native(),
38 arg4:paint.native(),
39 arg5:align,
40 )
41 }
42}
43
44impl Canvas {
45 pub fn draw_str_align(
46 &self,
47 text: impl AsRef<str>,
48 p: impl Into<Point>,
49 font: &Font,
50 paint: &Paint,
51 align: Align,
52 ) -> &Self {
53 self.draw_text_align(text.as_ref(), p, font, paint, align)
54 }
55
56 pub fn draw_text_align(
57 &self,
58 text: impl EncodedText,
59 p: impl Into<Point>,
60 font: &Font,
61 paint: &Paint,
62 align: Align,
63 ) -> &Self {
64 draw_text(self, text, p, font, paint, align);
65 self
66 }
67}
68
69pub fn get_path(text: impl EncodedText, p: impl Into<Point>, font: &Font) -> Path {
70 let (ptr: *const c_void, size: usize, encoding: TextEncoding) = text.as_raw();
71 let p = p.into();
72 let mut path: Handle = Path::default();
73 unsafe {
74 SkTextUtils::GetPath(
75 text:ptr,
76 length:size,
77 arg1:encoding.into_native(),
78 p.x,
79 p.y,
80 arg2:font.native(),
81 arg3:path.native_mut(),
82 )
83 }
84 path
85}
86
87impl Path {
88 pub fn from_str(text: impl AsRef<str>, p: impl Into<Point>, font: &Font) -> Self {
89 get_path(text.as_ref(), p, font)
90 }
91}
92