Skip to content

Commit

Permalink
SDL_ttf wip
Browse files Browse the repository at this point in the history
  • Loading branch information
maia-s committed Jan 27, 2025
1 parent 74608ea commit e3bf93c
Show file tree
Hide file tree
Showing 22 changed files with 4,337 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "SDL_image"]
path = sdl3-image-src/SDL_image
url = https://github.com/libsdl-org/SDL_image
[submodule "SDL_ttf"]
path = sdl3-ttf-src/SDL_ttf
url = https://github.com/libsdl-org/SDL_ttf
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ resolver = "2"
members = [
"sdl3-image-src",
"sdl3-image-sys",
"sdl3-main",
"sdl3-main-macros",
"sdl3-main",
"sdl3-src",
"sdl3-sys",
"sdl3-sys-gen",
"sdl3-sys",
"sdl3-ttf-src",
"sdl3-ttf-sys",
]
default-members = [
"sdl3-sys",
"sdl3-main",
"sdl3-image-sys",
"sdl3-ttf-sys",
"sdl3-main",
]
33 changes: 21 additions & 12 deletions sdl3-sys-gen/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ mod expr;
pub use expr::Value;
mod item;
mod patch;
use patch::{patch_emit_define, patch_emit_function, patch_emit_macro_call, patch_emit_type_def};
use patch::{
patch_emit_define, patch_emit_function, patch_emit_macro_call, patch_emit_struct_or_union,
patch_emit_type_def,
};
mod state;
pub use state::{Cfg, DefineState, EmitContext, InnerEmitContext, Sym, SymKind};
use state::{EmitStatus, PreProcState, StructSym};
Expand Down Expand Up @@ -205,16 +208,18 @@ impl Emit for Item {
Item::FileDoc(dc) => dc.emit(ctx),
Item::StructOrUnion(s) => {
if let Some(ident) = &s.ident {
ctx.scope_mut().register_struct_or_union_sym(StructSym {
kind: s.kind,
doc: s.doc.clone(),
ident: ident.clone(),
fields: s.fields.clone(),
emit_status: EmitStatus::NotEmitted,
hidden: s.hidden,
can_copy: s.can_copy,
can_construct: s.can_construct,
})?;
if !patch_emit_struct_or_union(ctx, ident.as_str(), s)? {
ctx.scope_mut().register_struct_or_union_sym(StructSym {
kind: s.kind,
doc: s.doc.clone(),
ident: ident.clone(),
fields: s.fields.clone(),
emit_status: EmitStatus::NotEmitted,
hidden: s.hidden,
can_copy: s.can_copy,
can_construct: s.can_construct,
})?;
}
Ok(())
} else {
todo!()
Expand Down Expand Up @@ -775,7 +780,11 @@ impl Emit for Include {
}
writeln!(ctx, "use sdl3_sys::everything::*;")?;
writeln!(ctx)?;
} else if let Some(module) = self.path.as_str().strip_prefix("SDL3/SDL_") {
} else if let Some(module) = self
.path
.as_str()
.strip_prefix(&format!("{}/SDL_", ctx.gen.lib_name))
{
let module = module.strip_suffix(".h").unwrap();
if !ctx.r#gen.emitted.borrow().contains_key(module) {
ctx.r#gen.emit(module)?;
Expand Down
3 changes: 2 additions & 1 deletion sdl3-sys-gen/src/emit/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ impl Eval for SizeOf {
false,
))))
} else {
todo!()
ctx.add_unresolved_sym_dependency(ident.clone())?;
Err(ParseErr::new(ident.span(), "unresolved type").into())
}
}

Expand Down
47 changes: 44 additions & 3 deletions sdl3-sys-gen/src/emit/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use super::{
};
use crate::parse::{
Block, CanDefault, Define, DefineValue, Expr, FnCall, Function, GetSpan, Ident, IdentOrKw,
Item, Items, Kw_static, ParseErr, RustCode, Span, StringLiteral, Type, TypeDef,
Item, Items, Kw_static, ParseErr, RustCode, Span, StringLiteral, StructOrUnion, Type, TypeDef,
TypeEnum,
};
use core::fmt::Write;
use std::ffi::CString;
use core::{cell::RefCell, fmt::Write};
use std::{ffi::CString, rc::Rc};
use str_block::str_block;

const VULKAN_CRATE_VERSIONS: &[(&str, &[&str])] = &[("ash", &["0.38"])];
Expand Down Expand Up @@ -928,6 +929,37 @@ const EVAL_MACRO_CALL_PATCHES: &[EvalMacroCallPatch] = &[
},
];

type EmitStructOrUnionPatch = EmitPatch<StructOrUnion>;

const EMIT_STRUCT_OR_UNION_PATCHES: &[EmitStructOrUnionPatch] = &[EmitStructOrUnionPatch {
module: Some("textengine"),
match_ident: |i| i == "TTF_TextEngine",
patch: |ctx, s| {
TypeDef {
span: Span::none(),
doc: None,
ident: s.ident.as_ref().unwrap().clone(),
ty: Type {
span: Span::none(),
is_const: false,
ty: TypeEnum::Struct(Box::new(s.clone())),
},
use_for_defines: None,
associated_defines: Rc::new(RefCell::new(Vec::new())),
}
.emit(ctx)?;
Ok(true)
},
}];

pub fn patch_emit_struct_or_union(
ctx: &mut EmitContext,
ident: &str,
s: &StructOrUnion,
) -> Result<bool, EmitErr> {
patch_emit(ctx, s, ident, EMIT_STRUCT_OR_UNION_PATCHES)
}

type EmitOpaqueStructPatch = EmitPatch<StructSym>;

const EMIT_OPAQUE_STRUCT_PATCHES: &[EmitOpaqueStructPatch] = &[EmitOpaqueStructPatch {
Expand Down Expand Up @@ -1071,6 +1103,15 @@ const EMIT_TYPEDEF_PATCHES: &[EmitTypeDefPatch] = &[
Ok(true)
},
},
EmitTypeDefPatch {
module: Some("ttf"),
match_ident: |i| i == "TTF_TextEngine",
patch: |ctx, _| {
writeln!(ctx, "pub use super::textengine::TTF_TextEngine;")?;
writeln!(ctx)?;
Ok(true)
},
},
];

pub fn patch_emit_type_def(
Expand Down
1 change: 1 addition & 0 deletions sdl3-sys-gen/src/emit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ impl<'a, 'b> EmitContext<'a, 'b> {
"NO_SDL_VULKAN_TYPEDEFS",
format!("SDL_{module}_h_"), "SDL_locale_h", "SDL_main_impl_h_",
format!("SDL_{module}_h_").to_ascii_uppercase(),
"SDL_TTF_TEXTENGINE_H_",
"SDL_ASSERT_LEVEL",
"SDL_AssertBreakpoint",
"SDL_AtomicDecRef",
Expand Down
5 changes: 5 additions & 0 deletions sdl3-sys-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,11 @@ pub fn generate(root: &Path, libs: &[&str]) -> Result<(), Error> {
let headers_path = lib.headers_path();

let mut gen = Gen::new(
lib.lib_name.clone(),
match lib.lib_name.as_str() {
"SDL3" => "SDL_",
"SDL3_image" => "IMG_",
"SDL3_ttf" => "TTF_",
_ => return Err(format!("unknown library `{}`", lib.lib_name).into()),
},
emitted_sdl3,
Expand Down Expand Up @@ -398,6 +400,7 @@ pub type EmittedItems = BTreeMap<String, InnerEmitContext>;

#[derive(Default)]
pub struct Gen {
lib_name: String,
sym_prefix: &'static str,
revision: String,
parsed: ParsedItems,
Expand All @@ -410,6 +413,7 @@ pub struct Gen {

impl Gen {
pub fn new(
lib_name: String,
sym_prefix: &'static str,
emitted_sdl3: EmittedItems,
headers_path: PathBuf,
Expand All @@ -418,6 +422,7 @@ impl Gen {
) -> Result<Self, Error> {
DirBuilder::new().recursive(true).create(&output_path)?;
Ok(Self {
lib_name,
sym_prefix,
revision,
parsed: ParsedItems::new(),
Expand Down
2 changes: 1 addition & 1 deletion sdl3-sys-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

fn main() {
let root = PathBuf::from_iter([env!("CARGO_MANIFEST_DIR"), ".."]);
let crates = ["sdl3", "sdl3-image"];
let crates = ["sdl3", "sdl3-image", "sdl3-ttf"];
match sdl3_sys_gen::generate(&root, &crates) {
Ok(()) => (),
Err(e) => {
Expand Down
9 changes: 9 additions & 0 deletions sdl3-sys-gen/src/parse/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ const STRUCT_PATCHES: &[StructPatch] = &[
Ok(true)
},
},
StructPatch {
module: Some("textengine"),
match_ident: |i| i == "TTF_CopyOperation",
patch: |_, s| {
// part of union
s.can_copy = CanCopy::Always;
Ok(true)
},
},
];

pub fn patch_parsed_typedef(ctx: &ParseContext, e: &mut TypeDef) -> Result<bool, ParseErr> {
Expand Down
14 changes: 14 additions & 0 deletions sdl3-ttf-src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "sdl3-ttf-src"
version = "2.20.0-release-401-gec2271b"
edition = "2021"
authors = ["SDL developers"]
license = "Zlib"
description = "Source code of the SDL3_ttf library"
repository = "https://github.com/maia-s/sdl3-sys-rs"
# documentation = "https://docs.rs/sdl3-ttf-src"
keywords = ["sdl"]
categories = ["no-std"]
exclude = [
"/SDL_ttf/external/harfbuzz/test",
]
2 changes: 2 additions & 0 deletions sdl3-ttf-src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This crate contains the source code of the SDL3_ttf library. It's used by the
`sdl3-ttf-sys` crate when building from source.
1 change: 1 addition & 0 deletions sdl3-ttf-src/SDL_ttf
Submodule SDL_ttf added at ec2271
26 changes: 26 additions & 0 deletions sdl3-ttf-src/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![no_std]
#![doc = include_str!("../README.md")]

/// Location of the SDL3_ttf source code
#[cfg(not(windows))]
pub const SOURCE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/SDL_ttf");
#[cfg(windows)]
pub const SOURCE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "\\SDL_ttf");

/// Revision
pub const REVISION: &str = "SDL3_ttf-release-2.20.0-401-gec2271b";

/// Version part of the revision
pub const VERSION: &str = "2.20.0";

/// Tag part of the revision
pub const REVISION_TAG: &str = "release-2.20.0";

/// Tag part of the revision without version
pub const REVISION_TAG_BASE: &str = "release";

/// Offset from tag part of the revision
pub const REVISION_OFFSET: &str = "401";

/// Hash part of the revision
pub const REVISION_HASH: &str = "gec2271b";
60 changes: 60 additions & 0 deletions sdl3-ttf-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[package]
name = "sdl3-ttf-sys"
version = "0.0.0"
edition = "2021"
authors = ["Maia S. R."]
license = "Zlib"
description = "Low level Rust bindings for SDL3_ttf"
repository = "https://github.com/maia-s/sdl3-sys-rs"
documentation = "https://docs.rs/sdl3-ttf-sys"
keywords = ["sdl", "font"]
categories = ["external-ffi-bindings", "no-std"]
links = "SDL3_ttf"

[features]
default = []

# Build and link SDL3_ttf from source instead of linking a pre-existing library
build-from-source = ["dep:cmake", "dep:rpkg-config", "dep:sdl3-ttf-src"]

# Build and link a static SDL3_ttf library from source
build-from-source-static = ["build-from-source", "link-static"]

# Link SDL3_ttf as a static library. The default is to link a shared/dynamic library.
link-static = []

# Use pkg-config to get link flags for SDL3_ttf. Only used when not building from source.
# This has no effect if the link-framework feature is enabled.
use-pkg-config = ["dep:pkg-config"]

# Use vcpkg to get link flags for SDL3_ttf. Only used when not building from source.
# The link-static feature has no effect when using vcpkg.
# This has no effect if the link-framework feature is enabled.
use-vcpkg = ["dep:vcpkg"]

# Implement the Debug trait for applicable types
debug-impls = ["sdl3-sys/debug-impls"]

[dependencies]
sdl3-sys = { version = "0.4", path = "../sdl3-sys" }

[build-dependencies.cmake]
version = "0.1"
optional = true

[build-dependencies.pkg-config]
version = "0.3"
optional = true

[build-dependencies.rpkg-config]
version = "0.1.2"
optional = true

[build-dependencies.sdl3-ttf-src]
version = "2.20.0-release-401-gec2271b"
path = "../sdl3-ttf-src"
optional = true

[build-dependencies.vcpkg]
version = "0.2"
optional = true
3 changes: 3 additions & 0 deletions sdl3-ttf-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sdl3-ttf-sys

This version of `sdl3-ttf-sys` has bindings for SDL_ttf version `2.20.0-release-401-gec2271b` and earlier.
Loading

0 comments on commit e3bf93c

Please sign in to comment.