| 1 |
/* |
| 2 |
* fontconfig/fc-fontations/pattern_bindings/fc_wrapper.rs |
| 3 |
* |
| 4 |
* Copyright 2025 Google LLC. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 7 |
* documentation for any purpose is hereby granted without fee, provided that |
| 8 |
* the above copyright notice appear in all copies and that both that |
| 9 |
* copyright notice and this permission notice appear in supporting |
| 10 |
* documentation, and that the name of the author(s) not be used in |
| 11 |
* advertising or publicity pertaining to distribution of the software without |
| 12 |
* specific, written prior permission. The authors make no |
| 13 |
* representations about the suitability of this software for any purpose. It |
| 14 |
* is provided "as is" without express or implied warranty. |
| 15 |
* |
| 16 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 18 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 22 |
* PERFORMANCE OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
use fontconfig_bindings::{FcLangSet, FcLangSetCopy, FcLangSetCreate, FcLangSetDestroy}; |
| 26 |
|
| 27 |
use fcint_bindings::{ |
| 28 |
FcCharSet, FcCharSetAddChar, FcCharSetCopy, FcCharSetCreate, FcCharSetDestroy, FcPattern, |
| 29 |
FcPatternCreate, FcPatternDestroy, FcRange, FcRangeCopy, FcRangeCreateDouble, FcRangeDestroy, |
| 30 |
}; |
| 31 |
|
| 32 |
macro_rules! wrap_fc_object { |
| 33 |
( |
| 34 |
$wrapper_name:ident, |
| 35 |
$wrapped_type:ident, |
| 36 |
$destroy_fn:ident |
| 37 |
$(, $copy_fn:ident)? |
| 38 |
) => { |
| 39 |
#[allow(unused)] |
| 40 |
#[derive(Debug)] |
| 41 |
pub struct $wrapper_name { |
| 42 |
inner: *mut $wrapped_type, |
| 43 |
} |
| 44 |
|
| 45 |
impl $wrapper_name { |
| 46 |
#[allow(unused)] |
| 47 |
pub fn from_raw(ptr: *mut $wrapped_type) -> Self { |
| 48 |
Self { inner: ptr } |
| 49 |
} |
| 50 |
|
| 51 |
#[allow(unused)] |
| 52 |
pub fn into_raw(self) -> *mut $wrapped_type { |
| 53 |
let ptr = self.inner; |
| 54 |
std::mem::forget(self); |
| 55 |
ptr |
| 56 |
} |
| 57 |
|
| 58 |
#[allow(unused)] |
| 59 |
pub fn as_ptr(&self) -> *mut $wrapped_type { |
| 60 |
assert!(!self.inner.is_null()); |
| 61 |
self.inner |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl Drop for $wrapper_name { |
| 66 |
fn drop(&mut self) { |
| 67 |
unsafe { |
| 68 |
$destroy_fn(self.inner); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$( |
| 74 |
impl Clone for $wrapper_name { |
| 75 |
fn clone(&self) -> Self { |
| 76 |
Self { |
| 77 |
inner: unsafe { $copy_fn(self.inner) }, |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
)? |
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
wrap_fc_object! { |
| 86 |
FcRangeWrapper, |
| 87 |
FcRange, |
| 88 |
FcRangeDestroy, |
| 89 |
FcRangeCopy |
| 90 |
} |
| 91 |
|
| 92 |
impl FcRangeWrapper { |
| 93 |
#[allow(unused)] |
| 94 |
pub fn new(min: f64, max: f64) -> Option<Self> { |
| 95 |
unsafe { |
| 96 |
let ptr = FcRangeCreateDouble(min, max); |
| 97 |
if ptr.is_null() { |
| 98 |
None |
| 99 |
} else { |
| 100 |
Some(Self { inner: ptr }) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
wrap_fc_object! { |
| 107 |
FcPatternWrapper, |
| 108 |
FcPattern, |
| 109 |
FcPatternDestroy |
| 110 |
} |
| 111 |
|
| 112 |
impl FcPatternWrapper { |
| 113 |
#[allow(unused)] |
| 114 |
pub fn new() -> Option<Self> { |
| 115 |
// Corrected new function for FcPattern |
| 116 |
unsafe { |
| 117 |
let ptr = FcPatternCreate(); |
| 118 |
if ptr.is_null() { |
| 119 |
None |
| 120 |
} else { |
| 121 |
Some(Self { inner: ptr }) |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
wrap_fc_object! { |
| 128 |
FcCharSetWrapper, |
| 129 |
FcCharSet, |
| 130 |
FcCharSetDestroy, |
| 131 |
FcCharSetCopy |
| 132 |
} |
| 133 |
|
| 134 |
impl FcCharSetWrapper { |
| 135 |
pub fn new() -> Option<Self> { |
| 136 |
let created_charset: *mut FcCharSet; |
| 137 |
unsafe { |
| 138 |
created_charset = FcCharSetCreate(); |
| 139 |
} |
| 140 |
if created_charset.is_null() { |
| 141 |
None |
| 142 |
} else { |
| 143 |
Some(Self { |
| 144 |
inner: created_charset, |
| 145 |
}) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
pub fn add_char(&mut self, char: u32) -> Result<(), ()> { |
| 150 |
unsafe { |
| 151 |
if FcCharSetAddChar(self.as_ptr(), char) == 1 { |
| 152 |
Ok(()) |
| 153 |
} else { |
| 154 |
Err(()) |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
wrap_fc_object! { |
| 161 |
FcLangSetWrapper, |
| 162 |
FcLangSet, |
| 163 |
FcLangSetDestroy, |
| 164 |
FcLangSetCopy |
| 165 |
} |
| 166 |
|
| 167 |
impl FcLangSetWrapper { |
| 168 |
#[allow(unused)] |
| 169 |
pub fn new() -> Option<Self> { |
| 170 |
let created_langset: *mut fontconfig_bindings::FcLangSet; |
| 171 |
unsafe { |
| 172 |
created_langset = FcLangSetCreate(); |
| 173 |
} |
| 174 |
if created_langset.is_null() { |
| 175 |
None |
| 176 |
} else { |
| 177 |
Some(Self { |
| 178 |
inner: created_langset, |
| 179 |
}) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
pub fn is_null(&self) -> bool { |
| 184 |
self.inner.is_null() |
| 185 |
} |
| 186 |
} |