| 1 |
// Copyright 2023 Google LLC |
| 2 |
// Use of this source code is governed by a BSD-style license that can be found |
| 3 |
// in the LICENSE file. |
| 4 |
|
| 5 |
use read_fonts::{ |
| 6 |
tables::{ |
| 7 |
bitmap::{BitmapContent, BitmapData, BitmapDataFormat, BitmapMetrics, BitmapSize}, |
| 8 |
sbix::{GlyphData, Strike}, |
| 9 |
}, |
| 10 |
FontRef, TableProvider, |
| 11 |
}; |
| 12 |
|
| 13 |
use font_types::{BoundingBox, GlyphId}; |
| 14 |
use skrifa::{ |
| 15 |
instance::{LocationRef, Size}, |
| 16 |
metrics::GlyphMetrics, |
| 17 |
}; |
| 18 |
|
| 19 |
use crate::{ffi::BitmapMetrics as FfiBitmapMetrics, BridgeFontRef}; |
| 20 |
|
| 21 |
pub enum BitmapPixelData<'a> { |
| 22 |
PngData(&'a [u8]), |
| 23 |
} |
| 24 |
|
| 25 |
struct CblcGlyph<'a> { |
| 26 |
bitmap_data: BitmapData<'a>, |
| 27 |
ppem_x: u8, |
| 28 |
ppem_y: u8, |
| 29 |
} |
| 30 |
|
| 31 |
struct SbixGlyph<'a> { |
| 32 |
glyph_data: GlyphData<'a>, |
| 33 |
ppem: u16, |
| 34 |
} |
| 35 |
|
| 36 |
#[derive(Default)] |
| 37 |
pub struct BridgeBitmapGlyph<'a> { |
| 38 |
pub data: Option<BitmapPixelData<'a>>, |
| 39 |
pub metrics: FfiBitmapMetrics, |
| 40 |
} |
| 41 |
|
| 42 |
trait StrikeSizeRetrievable { |
| 43 |
fn strike_size(&self) -> f32; |
| 44 |
} |
| 45 |
|
| 46 |
impl StrikeSizeRetrievable for &BitmapSize { |
| 47 |
fn strike_size(&self) -> f32 { |
| 48 |
self.ppem_y() as f32 |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl StrikeSizeRetrievable for Strike<'_> { |
| 53 |
fn strike_size(&self) -> f32 { |
| 54 |
self.ppem() as f32 |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Find the nearest larger strike size, or if no larger one is available, the nearest smaller. |
| 59 |
fn best_strike_size<T>(strikes: impl Iterator<Item = T>, font_size: f32) -> Option<T> |
| 60 |
where |
| 61 |
T: StrikeSizeRetrievable, |
| 62 |
{ |
| 63 |
// After a bigger strike size is found, the order of strike sizes smaller |
| 64 |
// than the requested font size does not matter anymore. A new strike size |
| 65 |
// is only an improvement if it gets closer to the requested font size (and |
| 66 |
// is smaller than the current best, but bigger than font size). And vice |
| 67 |
// versa: As long as we have found only smaller ones so far, only any strike |
| 68 |
// size matters that is bigger than the current best. |
| 69 |
strikes.reduce(|best, entry| { |
| 70 |
let entry_size = entry.strike_size(); |
| 71 |
if (entry_size >= font_size && entry_size < best.strike_size()) |
| 72 |
|| (best.strike_size() < font_size && entry_size > best.strike_size()) |
| 73 |
{ |
| 74 |
entry |
| 75 |
} else { |
| 76 |
best |
| 77 |
} |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
fn sbix_glyph<'a>( |
| 82 |
font_ref: &'a FontRef, |
| 83 |
glyph_id: GlyphId, |
| 84 |
font_size: Option<f32>, |
| 85 |
) -> Option<SbixGlyph<'a>> { |
| 86 |
let sbix = font_ref.sbix().ok()?; |
| 87 |
let mut strikes = sbix.strikes().iter().filter_map(|strike| strike.ok()); |
| 88 |
|
| 89 |
let best_strike = match font_size { |
| 90 |
Some(size) => best_strike_size(strikes, size), |
| 91 |
_ => strikes.next(), |
| 92 |
}?; |
| 93 |
|
| 94 |
Some(SbixGlyph { |
| 95 |
ppem: best_strike.ppem(), |
| 96 |
glyph_data: best_strike.glyph_data(glyph_id).ok()??, |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
fn cblc_glyph<'a>( |
| 101 |
font_ref: &'a FontRef, |
| 102 |
glyph_id: GlyphId, |
| 103 |
font_size: Option<f32>, |
| 104 |
) -> Option<CblcGlyph<'a>> { |
| 105 |
let cblc = font_ref.cblc().ok()?; |
| 106 |
let cbdt = font_ref.cbdt().ok()?; |
| 107 |
|
| 108 |
let strikes = &cblc.bitmap_sizes(); |
| 109 |
let best_strike = font_size |
| 110 |
.and_then(|size| best_strike_size(strikes.iter(), size)) |
| 111 |
.or(strikes.get(0))?; |
| 112 |
|
| 113 |
let location = best_strike.location(cblc.offset_data(), glyph_id).ok()?; |
| 114 |
|
| 115 |
Some(CblcGlyph { |
| 116 |
bitmap_data: cbdt.data(&location).ok()?, |
| 117 |
ppem_x: best_strike.ppem_x, |
| 118 |
ppem_y: best_strike.ppem_y, |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
pub fn has_bitmap_glyph(font_ref: &BridgeFontRef, glyph_id: u16) -> bool { |
| 123 |
font_ref |
| 124 |
.with_font(|font| { |
| 125 |
let glyph_id = GlyphId::from(glyph_id); |
| 126 |
let has_sbix = sbix_glyph(font, glyph_id, None).is_some(); |
| 127 |
let has_cblc = cblc_glyph(font, glyph_id, None).is_some(); |
| 128 |
Some(has_sbix || has_cblc) |
| 129 |
}) |
| 130 |
.unwrap_or_default() |
| 131 |
} |
| 132 |
|
| 133 |
fn glyf_bounds(font_ref: &FontRef, glyph_id: GlyphId) -> Option<BoundingBox<i16>> { |
| 134 |
let glyf_table = font_ref.glyf().ok()?; |
| 135 |
let glyph = font_ref |
| 136 |
.loca(None) |
| 137 |
.ok()? |
| 138 |
.get_glyf(glyph_id, &glyf_table) |
| 139 |
.ok()??; |
| 140 |
Some(BoundingBox { |
| 141 |
x_min: glyph.x_min(), |
| 142 |
y_min: glyph.y_min(), |
| 143 |
x_max: glyph.x_max(), |
| 144 |
y_max: glyph.y_max(), |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
pub unsafe fn bitmap_glyph<'a>( |
| 149 |
font_ref: &'a BridgeFontRef, |
| 150 |
glyph_id: u16, |
| 151 |
font_size: f32, |
| 152 |
) -> Box<BridgeBitmapGlyph<'a>> { |
| 153 |
let glyph_id = GlyphId::from(glyph_id); |
| 154 |
font_ref |
| 155 |
.with_font(|font| { |
| 156 |
if let Some(sbix_glyph) = sbix_glyph(font, glyph_id, Some(font_size)) { |
| 157 |
// https://learn.microsoft.com/en-us/typography/opentype/spec/sbix |
| 158 |
// "If there is a glyph contour, the glyph design space |
| 159 |
// origin for the graphic is placed at the lower left corner |
| 160 |
// of the glyph bounding box (xMin, yMin)." |
| 161 |
let glyf_bb = glyf_bounds(font, glyph_id).unwrap_or_default(); |
| 162 |
let glyf_left_side_bearing = |
| 163 |
GlyphMetrics::new(font, Size::unscaled(), LocationRef::default()) |
| 164 |
.left_side_bearing(glyph_id) |
| 165 |
.unwrap_or_default(); |
| 166 |
|
| 167 |
return Some(Box::new(BridgeBitmapGlyph { |
| 168 |
data: Some(BitmapPixelData::PngData(sbix_glyph.glyph_data.data())), |
| 169 |
metrics: FfiBitmapMetrics { |
| 170 |
bearing_x: glyf_left_side_bearing, |
| 171 |
bearing_y: glyf_bb.y_min as f32, |
| 172 |
inner_bearing_x: sbix_glyph.glyph_data.origin_offset_x() as f32, |
| 173 |
inner_bearing_y: sbix_glyph.glyph_data.origin_offset_y() as f32, |
| 174 |
ppem_x: sbix_glyph.ppem as f32, |
| 175 |
ppem_y: sbix_glyph.ppem as f32, |
| 176 |
placement_origin_bottom_left: true, |
| 177 |
advance: f32::NAN, |
| 178 |
}, |
| 179 |
})); |
| 180 |
} else if let Some(cblc_glyph) = cblc_glyph(font, glyph_id, Some(font_size)) { |
| 181 |
let (bearing_x, bearing_y, advance) = match cblc_glyph.bitmap_data.metrics { |
| 182 |
BitmapMetrics::Small(small_metrics) => ( |
| 183 |
small_metrics.bearing_x() as f32, |
| 184 |
small_metrics.bearing_y() as f32, |
| 185 |
small_metrics.advance as f32, |
| 186 |
), |
| 187 |
BitmapMetrics::Big(big_metrics) => ( |
| 188 |
big_metrics.hori_bearing_x() as f32, |
| 189 |
big_metrics.hori_bearing_y() as f32, |
| 190 |
big_metrics.hori_advance as f32, |
| 191 |
), |
| 192 |
}; |
| 193 |
if let BitmapContent::Data(BitmapDataFormat::Png, png_buffer) = |
| 194 |
cblc_glyph.bitmap_data.content |
| 195 |
{ |
| 196 |
return Some(Box::new(BridgeBitmapGlyph { |
| 197 |
data: Some(BitmapPixelData::PngData(png_buffer)), |
| 198 |
metrics: FfiBitmapMetrics { |
| 199 |
bearing_x: 0.0, |
| 200 |
bearing_y: 0.0, |
| 201 |
inner_bearing_x: bearing_x, |
| 202 |
inner_bearing_y: bearing_y, |
| 203 |
ppem_x: cblc_glyph.ppem_x as f32, |
| 204 |
ppem_y: cblc_glyph.ppem_y as f32, |
| 205 |
placement_origin_bottom_left: false, |
| 206 |
advance: advance, |
| 207 |
}, |
| 208 |
})); |
| 209 |
} |
| 210 |
} |
| 211 |
None |
| 212 |
}) |
| 213 |
.unwrap_or_default() |
| 214 |
} |
| 215 |
|
| 216 |
pub unsafe fn png_data<'a>(bitmap_glyph: &'a BridgeBitmapGlyph) -> &'a [u8] { |
| 217 |
match bitmap_glyph.data { |
| 218 |
Some(BitmapPixelData::PngData(glyph_data)) => glyph_data, |
| 219 |
_ => &[], |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
pub unsafe fn bitmap_metrics<'a>(bitmap_glyph: &'a BridgeBitmapGlyph) -> &'a FfiBitmapMetrics { |
| 224 |
&bitmap_glyph.metrics |
| 225 |
} |