1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Defines the [PdfFontGlyph] struct, exposing functionality related to a single
//! font glyph in a `PdfFontGlyphs` collection.

use crate::bindgen::{FPDF_FONT, FPDF_GLYPHPATH};
use crate::bindings::PdfiumLibraryBindings;
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::font_glyphs::PdfFontGlyphIndex;
use crate::path_segment::PdfPathSegment;
use crate::path_segments::{PdfPathSegmentIndex, PdfPathSegments, PdfPathSegmentsIterator};
use crate::points::PdfPoints;
use std::convert::TryInto;
use std::os::raw::{c_float, c_int, c_uint};

pub struct PdfFontGlyph<'a> {
    handle: FPDF_FONT,
    index: PdfFontGlyphIndex,
    bindings: &'a dyn PdfiumLibraryBindings,
}

impl<'a> PdfFontGlyph<'a> {
    #[inline]
    pub(crate) fn from_pdfium(
        handle: FPDF_FONT,
        index: PdfFontGlyphIndex,
        bindings: &'a dyn PdfiumLibraryBindings,
    ) -> Self {
        Self {
            handle,
            index,
            bindings,
        }
    }

    /// Returns the [PdfiumLibraryBindings] used by this [PdfFontGlyph].
    #[inline]
    pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
        self.bindings
    }

    /// Returns the width of this [PdfFontGlyph] when rendered at the given font size.
    pub fn width_at_font_size(&self, size: PdfPoints) -> PdfPoints {
        let mut width = 0.0;

        if self.bindings.is_true(self.bindings.FPDFFont_GetGlyphWidth(
            self.handle,
            self.index as c_uint,
            size.value as c_float,
            &mut width,
        )) {
            PdfPoints::new(width)
        } else {
            PdfPoints::ZERO
        }
    }

    /// Returns the path segments of this [PdfFontGlyph] when rendered at the given font size.
    pub fn segments_at_font_size(&self, size: PdfPoints) -> Result<PdfFontGlyphPath, PdfiumError> {
        let handle = self.bindings().FPDFFont_GetGlyphPath(
            self.handle,
            self.index as c_uint,
            size.value as c_float,
        );

        if handle.is_null() {
            Err(PdfiumError::PdfiumLibraryInternalError(
                PdfiumInternalError::Unknown,
            ))
        } else {
            Ok(PdfFontGlyphPath::from_pdfium(handle, self.bindings()))
        }
    }
}

/// The collection of [PdfPathSegment] objects inside a font glyph path.
pub struct PdfFontGlyphPath<'a> {
    handle: FPDF_GLYPHPATH,
    bindings: &'a dyn PdfiumLibraryBindings,
}

impl<'a> PdfFontGlyphPath<'a> {
    #[inline]
    pub(crate) fn from_pdfium(
        handle: FPDF_GLYPHPATH,
        bindings: &'a dyn PdfiumLibraryBindings,
    ) -> Self {
        Self { handle, bindings }
    }
}

impl<'a> PdfPathSegments<'a> for PdfFontGlyphPath<'a> {
    #[inline]
    fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
        self.bindings
    }

    #[inline]
    fn len(&self) -> PdfPathSegmentIndex {
        self.bindings()
            .FPDFGlyphPath_CountGlyphSegments(self.handle)
            .try_into()
            .unwrap_or(0)
    }

    fn get(&self, index: PdfPathSegmentIndex) -> Result<PdfPathSegment<'a>, PdfiumError> {
        let handle = self
            .bindings()
            .FPDFGlyphPath_GetGlyphPathSegment(self.handle, index as c_int);

        if handle.is_null() {
            Err(PdfiumError::PdfiumLibraryInternalError(
                PdfiumInternalError::Unknown,
            ))
        } else {
            Ok(PdfPathSegment::from_pdfium(handle, None, self.bindings()))
        }
    }

    #[inline]
    fn iter(&'a self) -> PdfPathSegmentsIterator<'a> {
        PdfPathSegmentsIterator::new(self)
    }
}