Skip to content

Commit

Permalink
Replace const_cstr crate with cstr
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb authored and wezm committed Nov 20, 2023
1 parent fb633b8 commit 0d99eeb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
ci:
strategy:
matrix:
rust: [stable, 1.56.0]
rust: [stable, 1.64.0]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion fontconfig-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ links = "fontconfig"
name = "fontconfig_sys"

[dependencies]
const-cstr = "0.3"
cstr = "0.2.11"
dlib = "0.5.0"
# This can't be optional because build.rs can't conditionally enable an
# optional dependency:
Expand Down
125 changes: 62 additions & 63 deletions fontconfig-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

#[macro_use]
extern crate const_cstr;

use std::os::raw::{c_char, c_double, c_int, c_uchar, c_uint, c_ushort, c_void};

pub use dlib::ffi_dispatch;
Expand Down Expand Up @@ -53,6 +50,10 @@ pub const FcTypeLangSet: u32 = 8_u32;
pub type FcType = enum__FcType;

pub mod constants {
use std::ffi::CStr;

use cstr::cstr;

use super::c_int;

pub const FC_WEIGHT_THIN: c_int = 0;
Expand Down Expand Up @@ -113,66 +114,64 @@ pub mod constants {
pub const FC_CHARSET_DONE: u32 = u32::MAX;
pub const FC_UTF8_MAX_LEN: c_int = 6;

const_cstr! {
pub FC_FAMILY = "family";
pub FC_STYLE = "style";
pub FC_SLANT = "slant";
pub FC_WEIGHT = "weight";
pub FC_SIZE = "size";
pub FC_ASPECT = "aspect";
pub FC_PIXEL_SIZE = "pixelsize";
pub FC_SPACING = "spacing";
pub FC_FOUNDRY = "foundry";
pub FC_ANTIALIAS = "antialias";
pub FC_HINTING = "hinting";
pub FC_HINT_STYLE = "hintstyle";
pub FC_VERTICAL_LAYOUT = "verticallayout";
pub FC_AUTOHINT = "autohint";
pub FC_GLOBAL_ADVANCE = "globaladvance";
pub FC_WIDTH = "width";
pub FC_FILE = "file";
pub FC_INDEX = "index";
pub FC_FT_FACE = "ftface";
pub FC_RASTERIZER = "rasterizer";
pub FC_OUTLINE = "outline";
pub FC_SCALABLE = "scalable";
pub FC_COLOR = "color";
pub FC_VARIABLE = "variable";
pub FC_SCALE = "scale";
pub FC_SYMBOL = "symbol";
pub FC_DPI = "dpi";
pub FC_RGBA = "rgba";
pub FC_MINSPACE = "minspace";
pub FC_SOURCE = "source";
pub FC_CHARSET = "charset";
pub FC_LANG = "lang";
pub FC_FONTVERSION = "fontversion";
pub FC_FULLNAME = "fullname";
pub FC_FAMILYLANG = "familylang";
pub FC_STYLELANG = "stylelang";
pub FC_FULLNAMELANG = "fullnamelang";
pub FC_CAPABILITY = "capability";
pub FC_FONTFORMAT = "fontformat";
pub FC_EMBOLDEN = "embolden";
pub FC_EMBEDDED_BITMAP = "embeddedbitmap";
pub FC_DECORATIVE = "decorative";
pub FC_LCD_FILTER = "lcdfilter";
pub FC_FONT_FEATURES = "fontfeatures";
pub FC_FONT_VARIATIONS = "fontvariations";
pub FC_NAMELANG = "namelang";
pub FC_PRGNAME = "prgname";
pub FC_HASH = "hash";
pub FC_POSTSCRIPT_NAME = "postscriptname";
pub FC_FONT_HAS_HINT = "fonthashint";
pub FC_CACHE_SUFFIX = ".cache-";
pub FC_DIR_CACHE_FILE = "fonts.cache-";
pub FC_USER_CACHE_FILE = ".fonts.cache-";
pub FC_CHARWIDTH = "charwidth";
pub FC_CHAR_WIDTH = "charwidth";
pub FC_CHAR_HEIGHT = "charheight";
pub FC_MATRIX = "matrix";
pub FC_ORDER = "order";
}
pub const FC_FAMILY: &CStr = cstr!("family");
pub const FC_STYLE: &CStr = cstr!("style");
pub const FC_SLANT: &CStr = cstr!("slant");
pub const FC_WEIGHT: &CStr = cstr!("weight");
pub const FC_SIZE: &CStr = cstr!("size");
pub const FC_ASPECT: &CStr = cstr!("aspect");
pub const FC_PIXEL_SIZE: &CStr = cstr!("pixelsize");
pub const FC_SPACING: &CStr = cstr!("spacing");
pub const FC_FOUNDRY: &CStr = cstr!("foundry");
pub const FC_ANTIALIAS: &CStr = cstr!("antialias");
pub const FC_HINTING: &CStr = cstr!("hinting");
pub const FC_HINT_STYLE: &CStr = cstr!("hintstyle");
pub const FC_VERTICAL_LAYOUT: &CStr = cstr!("verticallayout");
pub const FC_AUTOHINT: &CStr = cstr!("autohint");
pub const FC_GLOBAL_ADVANCE: &CStr = cstr!("globaladvance");
pub const FC_WIDTH: &CStr = cstr!("width");
pub const FC_FILE: &CStr = cstr!("file");
pub const FC_INDEX: &CStr = cstr!("index");
pub const FC_FT_FACE: &CStr = cstr!("ftface");
pub const FC_RASTERIZER: &CStr = cstr!("rasterizer");
pub const FC_OUTLINE: &CStr = cstr!("outline");
pub const FC_SCALABLE: &CStr = cstr!("scalable");
pub const FC_COLOR: &CStr = cstr!("color");
pub const FC_VARIABLE: &CStr = cstr!("variable");
pub const FC_SCALE: &CStr = cstr!("scale");
pub const FC_SYMBOL: &CStr = cstr!("symbol");
pub const FC_DPI: &CStr = cstr!("dpi");
pub const FC_RGBA: &CStr = cstr!("rgba");
pub const FC_MINSPACE: &CStr = cstr!("minspace");
pub const FC_SOURCE: &CStr = cstr!("source");
pub const FC_CHARSET: &CStr = cstr!("charset");
pub const FC_LANG: &CStr = cstr!("lang");
pub const FC_FONTVERSION: &CStr = cstr!("fontversion");
pub const FC_FULLNAME: &CStr = cstr!("fullname");
pub const FC_FAMILYLANG: &CStr = cstr!("familylang");
pub const FC_STYLELANG: &CStr = cstr!("stylelang");
pub const FC_FULLNAMELANG: &CStr = cstr!("fullnamelang");
pub const FC_CAPABILITY: &CStr = cstr!("capability");
pub const FC_FONTFORMAT: &CStr = cstr!("fontformat");
pub const FC_EMBOLDEN: &CStr = cstr!("embolden");
pub const FC_EMBEDDED_BITMAP: &CStr = cstr!("embeddedbitmap");
pub const FC_DECORATIVE: &CStr = cstr!("decorative");
pub const FC_LCD_FILTER: &CStr = cstr!("lcdfilter");
pub const FC_FONT_FEATURES: &CStr = cstr!("fontfeatures");
pub const FC_FONT_VARIATIONS: &CStr = cstr!("fontvariations");
pub const FC_NAMELANG: &CStr = cstr!("namelang");
pub const FC_PRGNAME: &CStr = cstr!("prgname");
pub const FC_HASH: &CStr = cstr!("hash");
pub const FC_POSTSCRIPT_NAME: &CStr = cstr!("postscriptname");
pub const FC_FONT_HAS_HINT: &CStr = cstr!("fonthashint");
pub const FC_CACHE_SUFFIX: &CStr = cstr!(".cache-");
pub const FC_DIR_CACHE_FILE: &CStr = cstr!("fonts.cache-");
pub const FC_USER_CACHE_FILE: &CStr = cstr!(".fonts.cache-");
pub const FC_CHARWIDTH: &CStr = cstr!("charwidth");
pub const FC_CHAR_WIDTH: &CStr = cstr!("charwidth");
pub const FC_CHAR_HEIGHT: &CStr = cstr!("charheight");
pub const FC_MATRIX: &CStr = cstr!("matrix");
pub const FC_ORDER: &CStr = cstr!("order");
}

#[repr(C)]
Expand Down
30 changes: 19 additions & 11 deletions fontconfig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ impl Font {
fn find(fc: &Fontconfig, family: &str, style: Option<&str>) -> Option<Font> {
let mut pat = Pattern::new(fc);
let family = CString::new(family).ok()?;
pat.add_string(FC_FAMILY.as_cstr(), &family);
pat.add_string(FC_FAMILY, &family);

if let Some(style) = style {
let style = CString::new(style).ok()?;
pat.add_string(FC_STYLE.as_cstr(), &style);
pat.add_string(FC_STYLE, &style);
}

let font_match = pat.font_match();
Expand Down Expand Up @@ -298,37 +298,37 @@ impl<'fc> Pattern<'fc> {

/// Get the "fullname" (human-readable name) of this pattern.
pub fn name(&self) -> Option<&str> {
self.get_string(FC_FULLNAME.as_cstr())
self.get_string(FC_FULLNAME)
}

/// Get the "file" (path on the filesystem) of this font pattern.
pub fn filename(&self) -> Option<&str> {
self.get_string(FC_FILE.as_cstr())
self.get_string(FC_FILE)
}

/// Get the "index" (The index of the font within the file) of this pattern.
pub fn face_index(&self) -> Option<i32> {
self.get_int(FC_INDEX.as_cstr())
self.get_int(FC_INDEX)
}

/// Get the "slant" (Italic, oblique or roman) of this pattern.
pub fn slant(&self) -> Option<i32> {
self.get_int(FC_SLANT.as_cstr())
self.get_int(FC_SLANT)
}

/// Get the "weight" (Light, medium, demibold, bold or black) of this pattern.
pub fn weight(&self) -> Option<i32> {
self.get_int(FC_WEIGHT.as_cstr())
self.get_int(FC_WEIGHT)
}

/// Get the "width" (Condensed, normal or expanded) of this pattern.
pub fn width(&self) -> Option<i32> {
self.get_int(FC_WIDTH.as_cstr())
self.get_int(FC_WIDTH)
}

/// Get the "fontformat" ("TrueType" "Type 1" "BDF" "PCF" "Type 42" "CID Type 1" "CFF" "PFR" "Windows FNT") of this pattern.
pub fn format(&self) -> Result<FontFormat, UnknownFontFormat> {
self.get_string(FC_FONTFORMAT.as_cstr())
self.get_string(FC_FONTFORMAT)
.ok_or_else(|| UnknownFontFormat(String::new()))
.and_then(|format| format.parse())
}
Expand Down Expand Up @@ -377,7 +377,15 @@ impl Pattern<'_> {
pub fn lang_set(&self) -> Option<StrList<'_>> {
unsafe {
let mut ret: *mut sys::FcLangSet = ptr::null_mut();
if ffi_dispatch!(LIB, FcPatternGetLangSet, self.pat, FC_LANG.as_ptr(), 0, &mut ret as *mut _) == sys::FcResultMatch {
if ffi_dispatch!(
LIB,
FcPatternGetLangSet,
self.pat,
FC_LANG.as_ptr(),
0,
&mut ret as *mut _
) == sys::FcResultMatch
{
let ss: *mut sys::FcStrSet = ffi_dispatch!(LIB, FcLangSetGetLangs, ret);
let lang_strs: *mut sys::FcStrList = ffi_dispatch!(LIB, FcStrListCreate, ss);
Some(StrList::from_raw(self.fc, lang_strs))
Expand Down Expand Up @@ -593,7 +601,7 @@ mod tests {
let fc = Fontconfig::new().unwrap();
let mut pat = Pattern::new(&fc);
let family = CString::new("dejavu sans").unwrap();
pat.add_string(FC_FAMILY.as_cstr(), &family);
pat.add_string(FC_FAMILY, &family);
let pattern = pat.font_match();
for lang in pattern.lang_set().unwrap() {
println!("{:?}", lang);
Expand Down

0 comments on commit 0d99eeb

Please sign in to comment.