objc2_core_text/generated/
CTLine.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::cell::UnsafeCell;
4use core::ffi::*;
5use core::marker::{PhantomData, PhantomPinned};
6use core::ptr::NonNull;
7#[cfg(feature = "objc2")]
8use objc2::__framework_prelude::*;
9use objc2_core_foundation::*;
10#[cfg(feature = "objc2-core-graphics")]
11use objc2_core_graphics::*;
12
13use crate::*;
14
15/// [Apple's documentation](https://developer.apple.com/documentation/coretext/ctline?language=objc)
16#[repr(C)]
17pub struct CTLine {
18    inner: [u8; 0],
19    _p: UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>,
20}
21
22cf_type!(
23    #[encoding_name = "__CTLine"]
24    unsafe impl CTLine {}
25);
26
27/// Options for CTLineGetBoundsWithOptions.
28///
29///
30/// Passing 0 (no options) returns the typographic bounds,
31/// including typographic leading and shifts.
32///
33///
34/// Pass this option to exclude typographic leading.
35///
36///
37/// Pass this option to ignore cross-stream shifts due to
38/// positioning (such as kerning or baseline alignment).
39///
40///
41/// Normally line bounds include all glyphs; pass this option to
42/// treat standard punctuation hanging off either end of the line
43/// as fully hanging.
44///
45///
46/// Pass this option to use glyph path bounds rather than the
47/// default typographic bounds.
48///
49///
50/// Pass this option to use optical bounds, as determined by
51/// CTFontGetOpticalBoundsForGlyphs. This option overrides
52/// kCTLineBoundsUseGlyphPathBounds.
53///
54///
55/// Pass this option to include additional space based on common
56/// glyph sequences for various languages. The result is intended
57/// to be used when drawing to avoid clipping that may be caused
58/// by the typographic bounds. This option does not have any effect
59/// when used with kCTLineBoundsUseGlyphPathBounds.
60///
61/// See also [Apple's documentation](https://developer.apple.com/documentation/coretext/ctlineboundsoptions?language=objc)
62// NS_OPTIONS
63#[repr(transparent)]
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
65pub struct CTLineBoundsOptions(pub CFOptionFlags);
66bitflags::bitflags! {
67    impl CTLineBoundsOptions: CFOptionFlags {
68        #[doc(alias = "kCTLineBoundsExcludeTypographicLeading")]
69        const ExcludeTypographicLeading = 1<<0;
70        #[doc(alias = "kCTLineBoundsExcludeTypographicShifts")]
71        const ExcludeTypographicShifts = 1<<1;
72        #[doc(alias = "kCTLineBoundsUseHangingPunctuation")]
73        const UseHangingPunctuation = 1<<2;
74        #[doc(alias = "kCTLineBoundsUseGlyphPathBounds")]
75        const UseGlyphPathBounds = 1<<3;
76        #[doc(alias = "kCTLineBoundsUseOpticalBounds")]
77        const UseOpticalBounds = 1<<4;
78        #[doc(alias = "kCTLineBoundsIncludeLanguageExtents")]
79        const IncludeLanguageExtents = 1<<5;
80    }
81}
82
83#[cfg(feature = "objc2")]
84unsafe impl Encode for CTLineBoundsOptions {
85    const ENCODING: Encoding = CFOptionFlags::ENCODING;
86}
87
88#[cfg(feature = "objc2")]
89unsafe impl RefEncode for CTLineBoundsOptions {
90    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
91}
92
93/// Truncation types required by CTLineCreateTruncatedLine. These
94/// will tell truncation engine which type of truncation is being
95/// requested.
96///
97///
98/// Truncate at the beginning of the line, leaving the end portion
99/// visible.
100///
101///
102/// Truncate at the end of the line, leaving the start portion
103/// visible.
104///
105///
106/// Truncate in the middle of the line, leaving both the start
107/// and the end portions visible.
108///
109/// See also [Apple's documentation](https://developer.apple.com/documentation/coretext/ctlinetruncationtype?language=objc)
110// NS_ENUM
111#[repr(transparent)]
112#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
113pub struct CTLineTruncationType(pub u32);
114impl CTLineTruncationType {
115    #[doc(alias = "kCTLineTruncationStart")]
116    pub const Start: Self = Self(0);
117    #[doc(alias = "kCTLineTruncationEnd")]
118    pub const End: Self = Self(1);
119    #[doc(alias = "kCTLineTruncationMiddle")]
120    pub const Middle: Self = Self(2);
121}
122
123#[cfg(feature = "objc2")]
124unsafe impl Encode for CTLineTruncationType {
125    const ENCODING: Encoding = u32::ENCODING;
126}
127
128#[cfg(feature = "objc2")]
129unsafe impl RefEncode for CTLineTruncationType {
130    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
131}
132
133unsafe impl ConcreteType for CTLine {
134    /// Returns the CFType of the line object
135    #[doc(alias = "CTLineGetTypeID")]
136    #[inline]
137    fn type_id() -> CFTypeID {
138        extern "C-unwind" {
139            fn CTLineGetTypeID() -> CFTypeID;
140        }
141        unsafe { CTLineGetTypeID() }
142    }
143}
144
145/// Creates a single immutable line object directly from an
146/// attributed string.
147///
148///
149/// This will allow clients who need very simple line generation to
150/// create a line without needing to create a typesetter object. The
151/// typesetting will be done under the hood. Without a typesetter
152/// object, the line cannot be properly broken. However, for simple
153/// things like text labels and other things, this is not an issue.
154///
155///
156/// Parameter `attrString`: The attributed string which the line will be created for.
157///
158///
159/// Returns: This function will return a reference to a CTLine object.
160#[inline]
161pub unsafe extern "C-unwind" fn CTLineCreateWithAttributedString(
162    attr_string: &CFAttributedString,
163) -> CFRetained<CTLine> {
164    extern "C-unwind" {
165        fn CTLineCreateWithAttributedString(
166            attr_string: &CFAttributedString,
167        ) -> Option<NonNull<CTLine>>;
168    }
169    let ret = unsafe { CTLineCreateWithAttributedString(attr_string) };
170    let ret = ret.expect("function was marked as returning non-null, but actually returned NULL");
171    unsafe { CFRetained::from_raw(ret) }
172}
173
174/// Creates a truncated line from an existing line.
175///
176///
177/// Parameter `line`: The line that you want to create a truncated line for.
178///
179///
180/// Parameter `width`: The width at which truncation will begin. The line will be
181/// truncated if its width is greater than the width passed in this.
182///
183///
184/// Parameter `truncationType`: The type of truncation to perform if needed.
185///
186///
187/// Parameter `truncationToken`: This token will be added to the point where truncation took place
188/// to indicate that the line was truncated. Usually, the truncation
189/// token is the ellipsis character (U+2026). If this parameter is
190/// set to NULL, then no truncation token is used, and the line is
191/// simply cut off. The line specified in truncationToken should have
192/// a width less than the width specified by the width parameter. If
193/// the width of the line specified in truncationToken is greater,
194/// this function will return NULL if truncation is needed.
195///
196///
197/// Returns: This function will return a reference to a truncated CTLine
198/// object if the call was successful. Otherwise, it will return
199/// NULL.
200#[inline]
201pub unsafe extern "C-unwind" fn CTLineCreateTruncatedLine(
202    line: &CTLine,
203    width: c_double,
204    truncation_type: CTLineTruncationType,
205    truncation_token: Option<&CTLine>,
206) -> Option<CFRetained<CTLine>> {
207    extern "C-unwind" {
208        fn CTLineCreateTruncatedLine(
209            line: &CTLine,
210            width: c_double,
211            truncation_type: CTLineTruncationType,
212            truncation_token: Option<&CTLine>,
213        ) -> Option<NonNull<CTLine>>;
214    }
215    let ret = unsafe { CTLineCreateTruncatedLine(line, width, truncation_type, truncation_token) };
216    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
217}
218
219/// Creates a justified line from an existing line.
220///
221///
222/// Parameter `line`: The line that you want to create a justified line for.
223///
224///
225/// Parameter `justificationFactor`: Allows for full or partial justification. When set to 1.0 or
226/// greater indicates, full justification will be performed. If less
227/// than 1.0, varying degrees of partial justification will be
228/// performed. If set to 0 or less, then no justification will be
229/// performed.
230///
231///
232/// Parameter `justificationWidth`: The width to which the resultant line will be justified. If
233/// justificationWidth is less than the actual width of the line,
234/// then negative justification will be performed ("text squishing").
235///
236///
237/// Returns: This function will return a reference to a justified CTLine
238/// object if the call was successful. Otherwise, it will return
239/// NULL.
240#[inline]
241pub unsafe extern "C-unwind" fn CTLineCreateJustifiedLine(
242    line: &CTLine,
243    justification_factor: CGFloat,
244    justification_width: c_double,
245) -> Option<CFRetained<CTLine>> {
246    extern "C-unwind" {
247        fn CTLineCreateJustifiedLine(
248            line: &CTLine,
249            justification_factor: CGFloat,
250            justification_width: c_double,
251        ) -> Option<NonNull<CTLine>>;
252    }
253    let ret = unsafe { CTLineCreateJustifiedLine(line, justification_factor, justification_width) };
254    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
255}
256
257extern "C-unwind" {
258    /// Returns the total glyph count for the line object.
259    ///
260    ///
261    /// The total glyph count is equal to the sum of all of the glyphs in
262    /// the glyph runs forming the line.
263    ///
264    ///
265    /// Parameter `line`: The line that you want to obtain the glyph count for.
266    ///
267    ///
268    /// Returns: The total glyph count for the line passed in.
269    pub fn CTLineGetGlyphCount(line: &CTLine) -> CFIndex;
270}
271
272/// Returns the array of glyph runs that make up the line object.
273///
274///
275/// Parameter `line`: The line that you want to obtain the glyph run array for.
276///
277///
278/// Returns: A CFArrayRef containing the CTRun objects that make up the line.
279#[inline]
280pub unsafe extern "C-unwind" fn CTLineGetGlyphRuns(line: &CTLine) -> CFRetained<CFArray> {
281    extern "C-unwind" {
282        fn CTLineGetGlyphRuns(line: &CTLine) -> Option<NonNull<CFArray>>;
283    }
284    let ret = unsafe { CTLineGetGlyphRuns(line) };
285    let ret = ret.expect("function was marked as returning non-null, but actually returned NULL");
286    unsafe { CFRetained::retain(ret) }
287}
288
289extern "C-unwind" {
290    /// Gets the range of characters that originally spawned the glyphs
291    /// in the line.
292    ///
293    ///
294    /// Parameter `line`: The line that you want to obtain the string range from.
295    ///
296    ///
297    /// Returns: A CFRange that contains the range over the backing store string
298    /// that spawned the glyphs. If the function fails for any reason, an
299    /// empty range will be returned.
300    pub fn CTLineGetStringRange(line: &CTLine) -> CFRange;
301}
302
303extern "C-unwind" {
304    /// Gets the pen offset required to draw flush text.
305    ///
306    ///
307    /// Parameter `line`: The line that you want to obtain a flush position from.
308    ///
309    ///
310    /// Parameter `flushFactor`: Specifies what kind of flushness you want. A flushFactor of 0 or
311    /// less indicates left flush. A flushFactor of 1.0 or more indicates
312    /// right flush. Flush factors between 0 and 1.0 indicate varying
313    /// degrees of center flush, with a value of 0.5 being totally center
314    /// flush.
315    ///
316    ///
317    /// Parameter `flushWidth`: Specifies the width that the flushness operation should apply to.
318    ///
319    ///
320    /// Returns: A value which can be used to offset the current pen position for
321    /// the flush operation.
322    pub fn CTLineGetPenOffsetForFlush(
323        line: &CTLine,
324        flush_factor: CGFloat,
325        flush_width: c_double,
326    ) -> c_double;
327}
328
329extern "C-unwind" {
330    /// Draws a line.
331    ///
332    ///
333    /// This is a convenience call, since the line could be drawn
334    /// run-by-run by getting the glyph runs and accessing the glyphs out
335    /// of them. This call may leave the graphics context in any state and
336    /// does not flush the context after drawing. This call also expects
337    /// a text matrix with `y` values increasing from bottom to top; a
338    /// flipped text matrix may result in misplaced diacritics.
339    ///
340    ///
341    /// Parameter `line`: The line that you want to draw.
342    ///
343    ///
344    /// Parameter `context`: The context to which the line will be drawn.
345    #[cfg(feature = "objc2-core-graphics")]
346    pub fn CTLineDraw(line: &CTLine, context: &CGContext);
347}
348
349extern "C-unwind" {
350    /// Calculates the typographic bounds for a line.
351    ///
352    ///
353    /// A line's typographic width is the distance to the rightmost
354    /// glyph advance width edge. Note that this distance includes
355    /// trailing whitespace glyphs.
356    ///
357    ///
358    /// Parameter `line`: The line that you want to calculate the typographic bounds for.
359    ///
360    ///
361    /// Parameter `ascent`: Upon return, this parameter will contain the ascent of the line.
362    /// This may be set to NULL if not needed.
363    ///
364    ///
365    /// Parameter `descent`: Upon return, this parameter will contain the descent of the line.
366    /// This may be set to NULL if not needed.
367    ///
368    ///
369    /// Parameter `leading`: Upon return, this parameter will contain the leading of the line.
370    /// This may be set to NULL if not needed.
371    ///
372    ///
373    /// Returns: The typographic width of the line. If line is invalid, this
374    /// function will always return zero.
375    ///
376    ///
377    /// See also: CTLineGetTrailingWhitespaceWidth
378    pub fn CTLineGetTypographicBounds(
379        line: &CTLine,
380        ascent: *mut CGFloat,
381        descent: *mut CGFloat,
382        leading: *mut CGFloat,
383    ) -> c_double;
384}
385
386extern "C-unwind" {
387    /// Calculates the bounds for a line.
388    ///
389    ///
390    /// Parameter `line`: The line that you want to calculate the bounds for.
391    ///
392    ///
393    /// Parameter `options`: Desired options or 0 if none.
394    ///
395    ///
396    /// Returns: The bounds of the line as specified by the type and options,
397    /// such that the coordinate origin is coincident with the line
398    /// origin and the rect origin is at the bottom left. If the line
399    /// is invalid this function will return CGRectNull.
400    pub fn CTLineGetBoundsWithOptions(line: &CTLine, options: CTLineBoundsOptions) -> CGRect;
401}
402
403extern "C-unwind" {
404    /// Calculates the trailing whitespace width for a line.
405    ///
406    ///
407    /// Parameter `line`: The line that you want to calculate the trailing whitespace width
408    /// for. Creating a line for a width can result in a line that is
409    /// actually longer than the desired width due to trailing
410    /// whitespace. Normally this is not an issue due to whitespace being
411    /// invisible, but this function may be used to determine what amount
412    /// of a line's width is due to trailing whitespace.
413    ///
414    ///
415    /// Returns: The width of the line's trailing whitespace. If line is invalid,
416    /// this function will always return zero.
417    pub fn CTLineGetTrailingWhitespaceWidth(line: &CTLine) -> c_double;
418}
419
420extern "C-unwind" {
421    /// Calculates the image bounds for a line.
422    ///
423    ///
424    /// The image bounds for a line is the union of all non-empty glyph
425    /// bounding rects, each positioned as it would be if drawn using
426    /// CTLineDraw using the current context. Note that the result is
427    /// ideal and does not account for raster coverage due to rendering.
428    /// This function is purely a convenience for using glyphs as an
429    /// image and should not be used for typographic purposes.
430    ///
431    ///
432    /// Parameter `line`: The line that you want to calculate the image bounds for.
433    ///
434    ///
435    /// Parameter `context`: The context which the image bounds will be calculated for or NULL,
436    /// in which case the bounds are relative to CGPointZero.
437    ///
438    ///
439    /// Returns: A rectangle that tightly encloses the paths of the line's glyphs,
440    /// which will be translated by the supplied context's text position.
441    /// If the line is invalid, CGRectNull will be returned.
442    ///
443    ///
444    /// See also: CTLineGetTypographicBounds
445    ///
446    /// See also: CTLineGetBoundsWithOptions
447    ///
448    /// See also: CTLineGetPenOffsetForFlush
449    #[cfg(feature = "objc2-core-graphics")]
450    pub fn CTLineGetImageBounds(line: &CTLine, context: Option<&CGContext>) -> CGRect;
451}
452
453extern "C-unwind" {
454    /// Performs hit testing.
455    ///
456    ///
457    /// This function can be used to determine the string index for a
458    /// mouse click or other event. This string index corresponds to the
459    /// character before which the next character should be inserted.
460    /// This determination is made by analyzing the string from which a
461    /// typesetter was created and the corresponding glyphs as embodied
462    /// by a particular line.
463    ///
464    ///
465    /// Parameter `line`: The line being examined.
466    ///
467    ///
468    /// Parameter `position`: The location of the mouse click relative to the line's origin.
469    ///
470    ///
471    /// Returns: The string index for the position. Relative to the line's string
472    /// range, this value will be no less than the first string index and
473    /// no greater than one plus the last string index. In the event of
474    /// failure, this function will return kCFNotFound.
475    pub fn CTLineGetStringIndexForPosition(line: &CTLine, position: CGPoint) -> CFIndex;
476}
477
478extern "C-unwind" {
479    /// Determines the graphical offset(s) for a string index.
480    ///
481    ///
482    /// This function returns the graphical offset(s) corresponding to
483    /// a string index, suitable for movement between adjacent lines or
484    /// for drawing a custom caret. For the former, the primary offset
485    /// may be adjusted for any relative indentation of the two lines;
486    /// a CGPoint constructed with the adjusted offset for its x value
487    /// and 0.0 for its y value is suitable for passing to
488    /// CTLineGetStringIndexForPosition. In either case, the primary
489    /// offset corresponds to the portion of the caret that represents
490    /// the visual insertion location for a character whose direction
491    /// matches the line's writing direction.
492    ///
493    ///
494    /// Parameter `line`: The line from which the offset is requested.
495    ///
496    ///
497    /// Parameter `charIndex`: The string index corresponding to the desired position.
498    ///
499    ///
500    /// Parameter `secondaryOffset`: An output parameter that will be set to the secondary offset
501    /// along the baseline for charIndex. When a single caret is
502    /// sufficient for a string index, this value will be the same as
503    /// the primary offset, which is the return value of this function.
504    /// This parameter may be NULL.
505    ///
506    ///
507    /// Returns: The primary offset along the baseline for charIndex, or 0.0 in
508    /// the event of failure.
509    pub fn CTLineGetOffsetForStringIndex(
510        line: &CTLine,
511        char_index: CFIndex,
512        secondary_offset: *mut CGFloat,
513    ) -> CGFloat;
514}
515
516extern "C-unwind" {
517    /// Enumerates caret offsets for characters in a line.
518    ///
519    ///
520    /// The provided block is invoked once for each logical caret edge in the line, in left-to-right visual order.
521    ///
522    ///
523    /// Parameter `block`: The offset parameter is relative to the line origin. The leadingEdge parameter of this block refers to logical order.
524    #[cfg(feature = "block2")]
525    pub fn CTLineEnumerateCaretOffsets(
526        line: &CTLine,
527        block: &block2::Block<dyn Fn(c_double, CFIndex, bool, NonNull<bool>)>,
528    );
529}