mirror of
https://github.com/touchHLE/touchHLE.git
synced 2026-01-31 01:25:24 +01:00
Implement font loading and size calculation (word wrapping TODO)
This commit is contained in:
32
Cargo.lock
generated
32
Cargo.lock
generated
@@ -2,6 +2,12 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ab_glyph_rasterizer"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
@@ -147,6 +153,15 @@ dependencies = [
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "owned_ttf_parser"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05e6affeb1632d6ff6a23d2cd40ffed138e82f1532571a26f527c8a284bb2fbb"
|
||||
dependencies = [
|
||||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plist"
|
||||
version = "1.3.1"
|
||||
@@ -179,6 +194,16 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rusttype"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ff8374aa04134254b7995b63ad3dc41c7f7236f69528b28553da7d72efaa967"
|
||||
dependencies = [
|
||||
"ab_glyph_rasterizer",
|
||||
"owned_ttf_parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "safemem"
|
||||
version = "0.3.3"
|
||||
@@ -284,6 +309,7 @@ dependencies = [
|
||||
"hound",
|
||||
"mach_object",
|
||||
"plist",
|
||||
"rusttype",
|
||||
"sdl2",
|
||||
"sdl2-sys",
|
||||
"touchHLE_dynarmic_wrapper",
|
||||
@@ -321,6 +347,12 @@ dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.5"
|
||||
|
||||
@@ -21,6 +21,7 @@ caf = "0.1.0"
|
||||
hound = "3.5.0"
|
||||
mach_object = "0.1.17"
|
||||
plist = "1.3.1"
|
||||
rusttype = "0.9.3"
|
||||
# sdl2 crates pinned at 0.35.1 because static linking seems to be broken for
|
||||
# 0.35.2 on macOS (build errors about undefined symbols for
|
||||
# _CHHapticDynamicParameterIDHapticIntensityControl etc)
|
||||
|
||||
@@ -39,6 +39,8 @@ There is one special external dependency, Boost:
|
||||
|
||||
Then you just need to run `cargo run --release` (for a release build) or `cargo run` (for a debug build) to build and run touchHLE.
|
||||
|
||||
The `touchHLE_dylibs` and `touchHLE_fonts` directories contain files that the resulting binary will need at runtime, so you'll need to copy them if you want to distribute the result. You also should include the license files.
|
||||
|
||||
# Contributing
|
||||
|
||||
Please run `cargo fmt` and `cargo clippy` on your changes before committing. For the handful of C and C++ files, please use `clang-format -i` to format them.
|
||||
@@ -51,7 +53,7 @@ TBD
|
||||
|
||||
We stand on the shoulders of giants. Thank you to:
|
||||
|
||||
* The authors of and contributors to the many libraries used by this project: [dynarmic](https://github.com/merryhime/dynarmic), [rust-macho](https://github.com/flier/rust-macho), [SDL](https://libsdl.org/), [rust-sdl2](https://github.com/Rust-SDL2/rust-sdl2), [stb\_image](https://github.com/nothings/stb), [openal-soft](https://github.com/kcat/openal-soft), [hound](https://github.com/ruuda/hound), [caf](https://github.com/rustaudio/caf), [rust-plist](https://github.com/ebarnard/rust-plist), [gl-rs](https://github.com/brendanzab/gl-rs), [cc-rs](https://github.com/rust-lang/cc-rs), [cmake-rs](https://github.com/rust-lang/cmake-rs), and the Rust standard library.
|
||||
* The authors of and contributors to the many libraries used by this project: [dynarmic](https://github.com/merryhime/dynarmic), [rust-macho](https://github.com/flier/rust-macho), [SDL](https://libsdl.org/), [rust-sdl2](https://github.com/Rust-SDL2/rust-sdl2), [stb\_image](https://github.com/nothings/stb), [openal-soft](https://github.com/kcat/openal-soft), [hound](https://github.com/ruuda/hound), [caf](https://github.com/rustaudio/caf), [RustType](https://gitlab.redox-os.org/redox-os/rusttype), [the Liberation fonts](https://github.com/liberationfonts/liberation-fonts), [rust-plist](https://github.com/ebarnard/rust-plist), [gl-rs](https://github.com/brendanzab/gl-rs), [cc-rs](https://github.com/rust-lang/cc-rs), [cmake-rs](https://github.com/rust-lang/cmake-rs), and the Rust standard library.
|
||||
* The [Rust project](https://www.rust-lang.org/) generally.
|
||||
* The various people out there who've documented the iPhone OS platform, officially or otherwise. Much of this documentation is linked to within this codebase!
|
||||
* The Free Software Foundation, for making libgcc and libstdc++ copyleft and therefore saving this project from ABI hell.
|
||||
|
||||
53
src/font.rs
Normal file
53
src/font.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
//! Text layout and font rasterization abstraction.
|
||||
//!
|
||||
//! This is implemented using the [rusttype] library. All usage of that library
|
||||
//! should be confined to this module.
|
||||
|
||||
use rusttype::{Rect, Scale};
|
||||
use std::cmp;
|
||||
|
||||
pub struct Font {
|
||||
font: rusttype::Font<'static>,
|
||||
}
|
||||
|
||||
impl Font {
|
||||
fn from_file(path: &str) -> Font {
|
||||
let Ok(bytes) = std::fs::read(path) else {
|
||||
panic!("Couldn't read bundled font file {:?}. Perhaps the directory is missing?", path);
|
||||
};
|
||||
|
||||
let Some(font) = rusttype::Font::try_from_vec(bytes) else {
|
||||
panic!("Couldn't parse bundled font file {:?}. This probably means the file is corrupt. Try re-downloading it.", path);
|
||||
};
|
||||
|
||||
Font { font }
|
||||
}
|
||||
|
||||
pub fn sans_regular() -> Font {
|
||||
Self::from_file("touchHLE_fonts/LiberationSans-Regular.ttf")
|
||||
}
|
||||
pub fn sans_bold() -> Font {
|
||||
Self::from_file("touchHLE_fonts/LiberationSans-Bold.ttf")
|
||||
}
|
||||
pub fn sans_italic() -> Font {
|
||||
Self::from_file("touchHLE_fonts/LiberationSans-Italic.ttf")
|
||||
}
|
||||
|
||||
/// Calculate the on-screen width and height of text with a given font size.
|
||||
pub fn calculate_text_size(&self, font_size: f32, text: &str) -> (f32, f32) {
|
||||
let mut text_bounds: Rect<i32> = Default::default();
|
||||
|
||||
for glyph in self
|
||||
.font
|
||||
.layout(text, Scale::uniform(font_size), Default::default())
|
||||
{
|
||||
let glyph_bounds = glyph.pixel_bounding_box().unwrap();
|
||||
text_bounds.min.x = cmp::min(text_bounds.min.x, glyph_bounds.min.x);
|
||||
text_bounds.min.y = cmp::min(text_bounds.min.y, glyph_bounds.min.y);
|
||||
text_bounds.max.x = cmp::max(text_bounds.max.x, glyph_bounds.max.x);
|
||||
text_bounds.max.y = cmp::max(text_bounds.max.y, glyph_bounds.max.y);
|
||||
}
|
||||
|
||||
(text_bounds.width() as f32, text_bounds.height() as f32)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
//! `CGGeometry.h` (`CGPoint`, `CGSize`, `CGRect`, etc)
|
||||
|
||||
use super::CGFloat;
|
||||
use crate::abi::impl_GuestRet_for_large_struct;
|
||||
use crate::abi::{impl_GuestRet_for_large_struct, GuestArg};
|
||||
use crate::mem::SafeRead;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
@@ -21,6 +21,20 @@ pub struct CGSize {
|
||||
}
|
||||
unsafe impl SafeRead for CGSize {}
|
||||
impl_GuestRet_for_large_struct!(CGSize);
|
||||
impl GuestArg for CGSize {
|
||||
const REG_COUNT: usize = 2;
|
||||
|
||||
fn from_regs(regs: &[u32]) -> Self {
|
||||
CGSize {
|
||||
width: GuestArg::from_regs(®s[0..1]),
|
||||
height: GuestArg::from_regs(®s[1..2]),
|
||||
}
|
||||
}
|
||||
fn to_regs(self, regs: &mut [u32]) {
|
||||
self.width.to_regs(&mut regs[0..1]);
|
||||
self.height.to_regs(&mut regs[1..2]);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[repr(C, packed)]
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
use super::ns_array;
|
||||
use super::NSUInteger;
|
||||
use crate::frameworks::core_graphics::CGSize;
|
||||
use crate::frameworks::uikit::ui_font::{self, NSLineBreakByWordWrapping, NSLineBreakMode};
|
||||
use crate::fs::GuestPath;
|
||||
use crate::mem::{ConstPtr, Mem, MutPtr, MutVoidPtr, SafeRead};
|
||||
use crate::objc::{
|
||||
@@ -423,6 +425,28 @@ pub const CLASSES: ClassExports = objc_classes! {
|
||||
c_string
|
||||
}
|
||||
|
||||
// These come from a category in UIKit (UIStringDrawing).
|
||||
// TODO: Implement categories so we can completely move the code to UIFont.
|
||||
// TODO: More `sizeWithFont:` variants
|
||||
- (CGSize)sizeWithFont:(id)font { // UIFont*
|
||||
// TODO: avoid copy
|
||||
let text = to_rust_string(env, this);
|
||||
ui_font::size_with_font(env, font, &text, None)
|
||||
}
|
||||
- (CGSize)sizeWithFont:(id)font // UIFont*
|
||||
constrainedToSize:(CGSize)size {
|
||||
msg![env; this sizeWithFont:font
|
||||
constrainedToSize:size
|
||||
lineBreakMode:NSLineBreakByWordWrapping]
|
||||
}
|
||||
- (CGSize)sizeWithFont:(id)font // UIFont*
|
||||
constrainedToSize:(CGSize)size
|
||||
lineBreakMode:(NSLineBreakMode)line_break_mode {
|
||||
// TODO: avoid copy
|
||||
let text = to_rust_string(env, this);
|
||||
ui_font::size_with_font(env, font, &text, Some((size, line_break_mode)))
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// Our private subclass that is the single implementation of NSString for the
|
||||
|
||||
@@ -1,33 +1,83 @@
|
||||
//! `UIFont`.
|
||||
|
||||
use crate::frameworks::core_graphics::CGFloat;
|
||||
use crate::font::Font;
|
||||
use crate::frameworks::core_graphics::{CGFloat, CGSize};
|
||||
use crate::frameworks::foundation::NSInteger;
|
||||
use crate::objc::{autorelease, id, objc_classes, ClassExports, HostObject};
|
||||
use crate::Environment;
|
||||
|
||||
struct UIFontHostObject {
|
||||
_size: CGFloat,
|
||||
size: CGFloat,
|
||||
font: Font,
|
||||
}
|
||||
impl HostObject for UIFontHostObject {}
|
||||
|
||||
/// Line break mode.
|
||||
///
|
||||
/// This is put here for convenience since it's font-related.
|
||||
/// Apple puts it in its own header, also in UIKit.
|
||||
pub type NSLineBreakMode = NSInteger;
|
||||
#[allow(dead_code)]
|
||||
pub const NSLineBreakByWordWrapping: NSLineBreakMode = 0;
|
||||
#[allow(dead_code)]
|
||||
pub const NSLineBreakByCharWrapping: NSLineBreakMode = 1;
|
||||
#[allow(dead_code)]
|
||||
pub const NSLineBreakByClipping: NSLineBreakMode = 3;
|
||||
#[allow(dead_code)]
|
||||
pub const NSLineBreakByTruncatingHead: NSLineBreakMode = 4;
|
||||
#[allow(dead_code)]
|
||||
pub const NSLineBreakByTruncatingTail: NSLineBreakMode = 5;
|
||||
#[allow(dead_code)]
|
||||
pub const NSLineBreakByTruncatingMiddle: NSLineBreakMode = 6;
|
||||
|
||||
pub const CLASSES: ClassExports = objc_classes! {
|
||||
|
||||
(env, this, _cmd);
|
||||
|
||||
// For now this is a singleton (the only instance is returned by mainScreen),
|
||||
// so there are hardcoded assumptions related to that.
|
||||
@implementation UIFont: NSObject
|
||||
|
||||
+ (id)systemFontOfSize:(CGFloat)font_size {
|
||||
// TODO: actually load and render fonts
|
||||
let new = env.objc.alloc_object(
|
||||
this,
|
||||
Box::new(UIFontHostObject {
|
||||
_size: font_size,
|
||||
}),
|
||||
&mut env.mem
|
||||
);
|
||||
// TODO: cache these fonts in case an app uses them in multiple sizes
|
||||
+ (id)systemFontOfSize:(CGFloat)size {
|
||||
let host_object = UIFontHostObject {
|
||||
size,
|
||||
font: Font::sans_regular(),
|
||||
};
|
||||
let new = env.objc.alloc_object(this, Box::new(host_object), &mut env.mem);
|
||||
autorelease(env, new)
|
||||
}
|
||||
+ (id)boldSystemFontOfSize:(CGFloat)size {
|
||||
let host_object = UIFontHostObject {
|
||||
size,
|
||||
font: Font::sans_bold(),
|
||||
};
|
||||
let new = env.objc.alloc_object(this, Box::new(host_object), &mut env.mem);
|
||||
autorelease(env, new)
|
||||
}
|
||||
+ (id)italicSystemFontOfSize:(CGFloat)size {
|
||||
let host_object = UIFontHostObject {
|
||||
size,
|
||||
font: Font::sans_italic(),
|
||||
};
|
||||
let new = env.objc.alloc_object(this, Box::new(host_object), &mut env.mem);
|
||||
autorelease(env, new)
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
};
|
||||
|
||||
/// Called by the `sizeWithFont:` method family on `NSString`.
|
||||
pub fn size_with_font(
|
||||
env: &mut Environment,
|
||||
font: id,
|
||||
text: &str,
|
||||
_constrained: Option<(CGSize, NSLineBreakMode)>,
|
||||
) -> CGSize {
|
||||
let host_object = env.objc.borrow::<UIFontHostObject>(font);
|
||||
|
||||
// FIXME: line break support
|
||||
|
||||
let (width, height) = host_object.font.calculate_text_size(host_object.size, text);
|
||||
|
||||
CGSize { width, height }
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ mod audio;
|
||||
mod bundle;
|
||||
mod cpu;
|
||||
mod dyld;
|
||||
mod font;
|
||||
mod frameworks;
|
||||
mod fs;
|
||||
mod image;
|
||||
|
||||
102
touchHLE_fonts/LICENSE
Normal file
102
touchHLE_fonts/LICENSE
Normal file
@@ -0,0 +1,102 @@
|
||||
Digitized data copyright (c) 2010 Google Corporation
|
||||
with Reserved Font Arimo, Tinos and Cousine.
|
||||
Copyright (c) 2012 Red Hat, Inc.
|
||||
with Reserved Font Name Liberation.
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License,
|
||||
Version 1.1.
|
||||
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
http://scripts.sil.org/OFL
|
||||
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
|
||||
PREAMBLE The goals of the Open Font License (OFL) are to stimulate
|
||||
worldwide development of collaborative font projects, to support the font
|
||||
creation efforts of academic and linguistic communities, and to provide
|
||||
a free and open framework in which fonts may be shared and improved in
|
||||
partnership with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves.
|
||||
The fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply to
|
||||
any document created using the fonts or their derivatives.
|
||||
|
||||
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such.
|
||||
This may include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components
|
||||
as distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting ? in part or in whole ?
|
||||
any of the components of the Original Version, by changing formats or
|
||||
by porting the Font Software to a new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical writer
|
||||
or other person who contributed to the Font Software.
|
||||
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,in
|
||||
Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the
|
||||
corresponding Copyright Holder. This restriction only applies to the
|
||||
primary font name as presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole, must
|
||||
be distributed entirely under this license, and must not be distributed
|
||||
under any other license. The requirement for fonts to remain under
|
||||
this license does not apply to any document created using the Font
|
||||
Software.
|
||||
|
||||
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are not met.
|
||||
|
||||
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
|
||||
DEALINGS IN THE FONT SOFTWARE.
|
||||
|
||||
BIN
touchHLE_fonts/LiberationSans-Bold.ttf
Normal file
BIN
touchHLE_fonts/LiberationSans-Bold.ttf
Normal file
Binary file not shown.
BIN
touchHLE_fonts/LiberationSans-Italic.ttf
Normal file
BIN
touchHLE_fonts/LiberationSans-Italic.ttf
Normal file
Binary file not shown.
BIN
touchHLE_fonts/LiberationSans-Regular.ttf
Normal file
BIN
touchHLE_fonts/LiberationSans-Regular.ttf
Normal file
Binary file not shown.
5
touchHLE_fonts/README.md
Normal file
5
touchHLE_fonts/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Fonts
|
||||
|
||||
These are taken from the Liberation fonts release 2.1.5: https://github.com/liberationfonts/liberation-fonts/releases/tag/2.1.5
|
||||
|
||||
See `LICENSE` for the license details.
|
||||
Reference in New Issue
Block a user