Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Last active December 12, 2015 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlanhaskins/6b96a1e0ec1ef838b5ea to your computer and use it in GitHub Desktop.
Save harlanhaskins/6b96a1e0ec1ef838b5ea to your computer and use it in GitHub Desktop.
import CCairo
class Surface {
private let surface: COpaquePointer
private let cr: COpaquePointer
init(format: cairo_format_t, width: Int, height: Int) {
self.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 240, 80)
self.cr = cairo_create(surface)
}
func writeToPNG(filename: String) {
cairo_surface_write_to_png(surface, filename)
}
func setFontFace(name: String, slant: cairo_font_slant_t = CAIRO_FONT_SLANT_NORMAL, weight: cairo_font_weight_t = CAIRO_FONT_WEIGHT_NORMAL) {
cairo_select_font_face(cr, name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
}
func setFontSize(size: Double) {
cairo_set_font_size(cr, size)
}
func setSourceRGB(r: Double, g: Double, b: Double) {
cairo_set_source_rgb(cr, r, g, b)
}
func moveTo(x x: Double, y: Double) {
cairo_move_to(cr, x, y)
}
func showText(text: String) {
cairo_show_text(cr, text)
}
deinit {
cairo_destroy(cr)
cairo_surface_destroy(surface)
}
}
let surface = Surface(format: CAIRO_FORMAT_ARGB32, width: 240, height: 80)
surface.setFontFace("serif", weight: CAIRO_FONT_WEIGHT_BOLD)
surface.setFontSize(32.0)
surface.setSourceRGB(0.0, g: 0.0, b: 1.0)
surface.moveTo(x: 10, y: 50)
surface.showText("Hello, World!")
surface.writeToPNG("hello.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment