Skip to content

Instantly share code, notes, and snippets.

@raphlinus
raphlinus / subpixel_antialias.py
Created May 1, 2019 13:59
Research code to do RGB subpixel antialiasing using angle info in signed distance fields
import math
# http://vcl.itn.liu.se/publications/2011/GS11/edtaa_preprint.pdf
def tex(df, cos_th):
cos_th = abs(cos_th)
sin_th = math.sqrt(1 - cos_th ** 2)
if cos_th < sin_th:
cos_th, sin_th = sin_th, cos_th
a1 = 0.5 * sin_th / cos_th # note: paper is missing the 0.5!
@raphlinus
raphlinus / str_source.rs
Created April 25, 2019 15:42
Sketch of StrSource trait for access to string data
use std::borrow::Cow;
pub trait StrSource {
fn len(&self) -> usize;
fn str_at(&self, ix: usize) -> &str;
fn str_before(&self, ix: usize) -> &str;
fn to_string(&self) -> String {
@raphlinus
raphlinus / fallback.rs
Created March 29, 2019 20:37
example code for dwrote-rs to exercise fallback
extern crate dwrote;
extern crate winapi;
use std::borrow::Cow;
use winapi::um::dwrite::{DWRITE_NUMBER_SUBSTITUTION_METHOD_NONE, DWRITE_READING_DIRECTION,
DWRITE_READING_DIRECTION_LEFT_TO_RIGHT
};
use dwrote::{FontCollection, FontFallback, FontStretch, FontStyle, FontWeight, NumberSubstitution,
TextAnalysisSource, TextAnalysisSourceImpl,
@raphlinus
raphlinus / harfbuzz-boom.rs
Created March 13, 2019 23:36
Example of unsoundness in harfbuzz crate
use harfbuzz::Blob;
fn create_blob() -> Blob {
let vec = vec![1; 256];
Blob::new_read_only(&vec)
// BAD: vec is dropped here, the blob still holds a reference
}
fn blob_sum(blob: &Blob) -> u32 {
blob.iter().map(|byte| *byte as u32).sum()
@raphlinus
raphlinus / exclusive.rs
Created March 3, 2019 10:51
Sketch of thread-safety idea for VST
struct Key(Arc<()>);
impl Key {
pub fn new() {
Key(Arc::new(()))
}
}
// Note: Key is not Clone, there can be only one.
let menubar = NSMenu::new(nil).autorelease();
let app_menu_item = NSMenuItem::new(nil).autorelease();
menubar.addItem_(app_menu_item);
app.setMainMenu_(menubar);
let app_menu = NSMenu::new(nil).autorelease();
let quit_title = make_nsstring("Quit");
let quit_action = selector("terminate:");
let quit_key = make_nsstring("q");
let quit_item = NSMenuItem::alloc(nil)
#![feature(test)]
extern crate test;
// Note: this dependency is nightly only
extern crate packed_simd;
#[macro_use]
extern crate cfg_if;
@raphlinus
raphlinus / parse.cu
Created April 23, 2018 19:31
sketch of prefix sum to do backslash unescaping in cuda
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@raphlinus
raphlinus / Decodable.swift
Created December 29, 2017 19:10
Simple JSON task using new Decodable protocol
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@raphlinus
raphlinus / ObjCLike.swift
Created December 29, 2017 19:09
Simple JSON task using Objective-C data structures
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,