Skip to content

Instantly share code, notes, and snippets.

Rasmus rsms

Block or report user

Report or block rsms

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View ascii-utf8-table.c
static unsigned char map[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0x00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <CTRL> ...
/* 0x10 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <CTRL> ...
/* 0x20 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // ! " # $ % & ' ( ) * + , - . /
/* 0x30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
/* 0x40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // @ A B C D E F G H I J K L M N O
/* 0x50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // P Q R S T U V W X Y Z [ \ ] ^ _
/* 0x60 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // ` a b c d e f g h i j k l m n o
/* 0x70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // p q r s t u v w x y z { | } ~ <DEL>
@rsms
rsms / fig-thumbnails.js
Created Nov 18, 2019
Script to fetch thumbnails of figma files
View fig-thumbnails.js
// 1. Make sure you have imagemagick installed (`brew install imagemagick` on macos)
// 2. Visit https://www.figma.com/developers/api#authentication and click "Get personal access token"
// 3. Add your file links below, replacing the example links
// 4. Run with `FIGMA_API_TOKEN=YOUR_TOKEN_FROM_STEP_2_HERE node fig-thumbnails.js`
// [ host:string, filekey:string ][]
const fileKeys = `
https://www.figma.com/file/FILEKEY/
https://staging.figma.com/file/FILEKEY/
https://www.figma.com/file/FILEKEY/
View chromium-v8-firefox-regexp-bug.md

Subject

RegExp that never finishes; hangs with 100% CPU. Easy repro

Repro steps

  1. Run the attached script in V8/Chrome
  2. Sample the process
  3. Notice how it's stuck in v8::internal::NativeRegExpMacroAssembler::Execute

Expected outcome

The code to not take forever to finish

@rsms
rsms / fps.ts
Created Nov 3, 2019
FPS measurement in web browser
View fps.ts
// measureFPS uses requestAnimationFrame to measure frame times and reports
// the average frames per second.
//
function measureFPS(report :(fps:number)=>void) {
const samplesSize = 120 // total number of frame times look at (window size)
const samples :number[] = [] // ring buffer; sliding window
const reportAt = Math.round(samplesSize / 4)
let samplesIndex = 0 // next index in samples
let prevTime = 0 // last time value; frameTime = prevTime - time
View dominant-color.ts
function dominantColorRGBA(pxv :ArrayLike<int>, pxcount :int, stride :int) :RGBA {
let cm = new Map<int,int[]>()
// average and count RGB values
for (let i = 0; i < pxcount; i += stride) {
let r = pxv[i], g = pxv[i + 1], b = pxv[i + 2], a = pxv[i + 3]
// key = (a << 24) + (r << 16) + (g << 8) + b
let key = (r << 16) + (g << 8) + b
// update or store entry in temporary map.
View event.ts
type EventHandler<T=any> = (data :T)=>void
export class EventEmitter<EventMap = {[k:string]:any}> {
_events = new Map<keyof EventMap,Set<EventHandler>>()
addListener<K extends keyof EventMap>(e :K, handler :EventHandler<EventMap[K]>) {
let s = this._events.get(e)
if (s) {
s.add(handler)
} else {
View 00 xdb.ts
import { EventEmitter } from "./event"
const print = console.log.bind(console)
const _indexedDB = (window.indexedDB
|| window["mozIndexedDB"]
|| window["webkitIndexedDB"]
|| window["msIndexedDB"]) as IDBFactory
View determine-font-style.md

Determining if a font is italic

To determine if a font is italic, you can look at fsSelection of the OS/2 table, checking the 0th and 9th bit:

For instance, here’s fsSelection for "Inter Medium Italic":

node -p '(129).toString(2).split("").reverse().join("")'
=> 10000001 == ITALIC | USE_TYPO_METRICS
View imutil.ts
import { Vec, RGBA } from './util'
interface OffscreenCanvas extends EventTarget {
readonly width: number
readonly height: number
getContext(
contextId: "2d",
contextAttributes?: CanvasRenderingContext2DSettings
) :CanvasRenderingContext2D | null
View fig-fetch-image-plugin.ts
figma.showUI(`<script>
let imurl = 'https://66.media.tumblr.com/7af65560449c91e8cd82a4a3478f5e0b/tumblr_pb7bt5CTrS1qzdllao1_1280.jpg'
fetch(imurl).then(r => r.arrayBuffer()).then(a =>
parent.postMessage({ pluginMessage: ["imdata", new Uint8Array(a)] }, '*'))
</script>`, { visible:false, width:200, height:200 })
figma.ui.onmessage = msg => {
if (msg[0] == "imdata") {
let data = msg[1] as Uint8Array
let imageHash = figma.createImage(new Uint8Array(data)).hash
const rect = figma.createRectangle()
You can’t perform that action at this time.