Initial
Rust validation / Windows x64 build and tests (push) Waiting to run
Rust validation / Formatting (push) Failing after 26s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s

This commit is contained in:
EonaCat committed 2026-12-04 21:27:30 +01:00
1 parent 2cff35f215
commit 53e6e612c9
360 files changed
+253990 -74

No files matched your search

+227
View File
@@ -0,0 +1,227 @@
use std::ptr;
use windows_sys::Win32::Graphics::Gdi::{
CreateCompatibleDC, CreateFontW, DEFAULT_CHARSET, DeleteDC, DeleteObject, FIXED, FW_BOLD,
GGI_MARK_NONEXISTING_GLYPHS, GGO_NATIVE, GGO_UNHINTED, GLYPHMETRICS, GetGlyphIndicesW,
GetGlyphOutlineW, HDC, HFONT, MAT2, SelectObject, TT_PRIM_LINE, TT_PRIM_QSPLINE,
};
use eonacat_xsbox_gpu::formats::gfx::{Contour, Edge};
use super::{EmPoint, Glyph};
#[derive(Clone, Copy, Debug)]
enum Face {
Letters,
Signs,
}
impl Face {
fn name(self) -> &'static str {
match self {
Self::Letters => "Segoe UI",
Self::Signs => "Segoe UI Symbol",
}
}
}
pub(super) struct Fonts {
letters: Option<Font>,
signs: Option<Font>,
}
impl Fonts {
pub(super) fn open() -> Self {
Self {
letters: Font::open(Face::Letters),
signs: Font::open(Face::Signs),
}
}
pub(super) fn glyph(&self, character: char) -> Option<Glyph> {
let from = |font: &Option<Font>| font.as_ref()?.glyph(character);
from(&self.letters).or_else(|| from(&self.signs))
}
}
const EM_PIXELS: i32 = 1024;
const LARGEST_OUTLINE_BYTES: u32 = 1 << 20;
const MISSING_GLYPH: u16 = 0xFFFF;
const OUTLINE_FAILED: u32 = u32::MAX;
struct Font {
dc: HDC,
font: HFONT,
}
impl Font {
fn open(face: Face) -> Option<Self> {
let face: Vec<u16> = face.name().encode_utf16().chain([0]).collect();
unsafe {
let dc = CreateCompatibleDC(ptr::null_mut());
if dc.is_null() {
return None;
}
let font = CreateFontW(
-EM_PIXELS,
0,
0,
0,
FW_BOLD.cast_signed(),
0,
0,
0,
u32::from(DEFAULT_CHARSET),
0,
0,
0,
0,
face.as_ptr(),
);
if font.is_null() {
DeleteDC(dc);
return None;
}
SelectObject(dc, font);
Some(Self { dc, font })
}
}
fn glyph(&self, character: char) -> Option<Glyph> {
let mut units = [0u16; 2];
let [unit] = *character.encode_utf16(&mut units) else {
return None;
};
let one = FIXED { fract: 0, value: 1 };
let zero = FIXED { fract: 0, value: 0 };
let identity = MAT2 {
eM11: one,
eM12: zero,
eM21: zero,
eM22: one,
};
let format = GGO_NATIVE | GGO_UNHINTED;
let mut index = MISSING_GLYPH;
let mut metrics = GLYPHMETRICS::default();
let size = unsafe {
GetGlyphIndicesW(
self.dc,
&raw const unit,
1,
&raw mut index,
GGI_MARK_NONEXISTING_GLYPHS,
);
if index == MISSING_GLYPH {
return None;
}
GetGlyphOutlineW(
self.dc,
u32::from(unit),
format,
&raw mut metrics,
0,
ptr::null_mut(),
&raw const identity,
)
};
if size == OUTLINE_FAILED || size > LARGEST_OUTLINE_BYTES {
return None;
}
let mut bytes = vec![0u8; usize::try_from(size).ok()?];
let written = unsafe {
GetGlyphOutlineW(
self.dc,
u32::from(unit),
format,
&raw mut metrics,
size,
bytes.as_mut_ptr().cast(),
&raw const identity,
)
};
if written != size {
return None;
}
Some(Glyph {
contours: contours(&bytes)?,
advance_ems: f64::from(metrics.gmCellIncX) / f64::from(EM_PIXELS),
})
}
}
impl Drop for Font {
fn drop(&mut self) {
unsafe {
DeleteDC(self.dc);
DeleteObject(self.font);
}
}
}
fn contours(bytes: &[u8]) -> Option<Vec<Contour<EmPoint>>> {
let word = |at: usize| Some(u16::from_le_bytes(*bytes.get(at..)?.first_chunk()?));
let long = |at: usize| Some(u32::from_le_bytes(*bytes.get(at..)?.first_chunk()?));
let point = |at: usize| -> Option<EmPoint> {
let fixed =
|at: usize| Some(f64::from(long(at)?.cast_signed()) / 65536.0 / f64::from(EM_PIXELS));
Some([fixed(at)?, fixed(at.checked_add(4)?)?])
};
let mut found = Vec::new();
let mut at = 0usize;
while at < bytes.len() {
let size = usize::try_from(long(at)?).ok()?;
let end = at
.checked_add(size)
.filter(|&end| size >= 16 && end <= bytes.len())?;
let start = point(at + 8)?;
let mut edges = Vec::new();
let mut from = start;
let mut record = at + 16;
while record < end {
let (kind, count) = (u32::from(word(record)?), usize::from(word(record + 2)?));
let next = record + 4 + count * 8;
if next > end {
return None;
}
let points = (0..count)
.map(|i| point(record + 4 + i * 8))
.collect::<Option<Vec<_>>>()?;
match kind {
TT_PRIM_LINE => edges.extend(points.iter().map(|&to| Edge::Line(to))),
TT_PRIM_QSPLINE => edges.extend(points.windows(2).enumerate().map(|(i, pair)| {
let (control, next) = (pair[0], pair[1]);
let to = if i + 2 == points.len() {
next
} else {
[(control[0] + next[0]) / 2.0, (control[1] + next[1]) / 2.0]
};
Edge::Curve { control, to }
})),
_ => return None,
}
from = edges
.last()
.map_or(from, |edge: &Edge<EmPoint>| edge.points()[1]);
record = next;
}
if from != start {
edges.push(Edge::Line(start));
}
found.push(Contour { start, edges });
at = end;
}
Some(found)
}