Skip to content

Instantly share code, notes, and snippets.

@Beej126
Last active September 11, 2017 14:51
Show Gist options
  • Save Beej126/7599e8de8b4b04e8f582c073d252dc8d to your computer and use it in GitHub Desktop.
Save Beej126/7599e8de8b4b04e8f582c073d252dc8d to your computer and use it in GitHub Desktop.
# ____
# / __ \________ ________ ____ ______
# / /_/ / ___/ _ \/ ___/ _ \/ __ `/ ___/
# / ____/ / / __/ / / __/ /_/ (__ )
#/_/ /_/ \___/_/ \___/\__, /____/
# /_/
# this script will automatically pull font awesome files from github via .net framework "WebClient"
# install fantastically handy ImageMagick tool from => http://www.imagemagick.org/script/binary-releases.php
# and add it to your path
if (-not [bool](Get-Command "magick.exe" -ErrorAction SilentlyContinue)) { throw "Magick.exe not in path" }
# set the desired foreGround color (background will be set to transparent per magick.exe command line below)
$textColor="white"
cd (Split-Path -parent $PSCommandPath)
erase fa-*.png
#download font-awesome if not present (just delete ttf if you want to pull fresh copy)
if (-not (test-path fontawesome-webfont.ttf) -or -not (test-path font-awesome.css)) {
#ignore SSL
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webClient = New-Object Net.WebClient
$webClient.DownloadFile("https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/fonts/fontawesome-webfont.ttf", "fontawesome-webfont.ttf")
$webClient.DownloadFile("https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/css/font-awesome.css", "font-awesome.css")
$webClient.Dispose()
}
#using readily provided css file as a convenient icon name to unicode lookup
if (-not (test-path fontawesome-webfont.ttf) -or -not (test-path font-awesome.css)) { throw "font-awesome download failed for some reason" }
[string]$faCSS = gc font-awesome.css
function CreateFAImages([string] $faIconName) {
if (-not ($faIconName -like "fa-")) { $faIconName = "fa-" + $faIconName }
if ($Matches) { $Matches.Clear() }
$faCSS -match "\.$($faIconName):before.*?content:\s`"\\(.*?)`""
if (-not $Matches[1]) { throw "$faIconName not found in css unicode lookup" }
$faUnicode = [char][int]"0x$($Matches[1])"
# i found these sizes in another xamarin sponsored project, not sure if they're "standard" yet
CreateFAImage $faIconName $faUnicode 24x24
CreateFAImage $faIconName@2x $faUnicode 48x48
CreateFAImage $faIconName@3x $faUnicode 72x72
}
function CreateFAImage([string]$faIconName, [string]$faUnicode, [string]$size) {
# ***install fantastically handy ImageMagick tool from => http://www.imagemagick.org/script/binary-releases.php
# main options docs: http://www.imagemagick.org/script/command-line-options.php#compose
# ImageMagick basically works left to right applying the various options... parenthesis do work for nested flow... BUT MAKE SURE YOU LEAVE A SPACE INSIDE EACH PAREN OR IT'LL BOMB OUT SYNTAX CHECK
# if you're nesting operations, you'll probably have to "-levels merge" or "-flatten" them back together into single final image or else it'll output a file per each layer
# options used:
# -pointsize - start with a nice big font to avoid pixelation on resize
# -trim - seemed to be required for these icon fonts... imagemagick docs mention automatic text "fit" logic but i always got a big whitespace gap under the icon character
# -resize is obvious but the annoying downside is that it somehow automatically trims in the horizontal direction... not sure what to make of that... so for now applying -extent after
# -gravity is a centering command, we can also do horizontal/vertical separately via "North", etc
# and then the -extent sets the outer "canvas" size
magick -fill $textColor -background transparent -pointsize 500 -font "fontawesome-webfont.ttf" label:$faUnicode -trim -bordercolor none -border 5% -resize $size -gravity center -extent $size "$faIconName.png"
# nugget: using powershell since i couldn't coax cmd.exe to generate the unicode character no matter what i tried from stack-o
}
@("bars", "camera") | % { CreateFAImages $_ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment