Example #1
0
cairo_face::cairo_face(boost::shared_ptr<freetype_engine> const& engine, face_ptr const& face)
    : face_(face)
{
    static cairo_user_data_key_t key;
    c_face_ = cairo_ft_font_face_create_for_ft_face(face->get_face(), FT_LOAD_NO_HINTING);
    cairo_font_face_set_user_data(c_face_, &key, new handle(engine, face), destroy);
}
static cairo_font_face_t *
font_face_create(gchar *fontfile){
    FT_Error error;
    FT_Library library;
    FT_Face face;
    GFile *file;
    gchar *font_file;

    error = FT_Init_FreeType (&library);
    if (error) {
        g_printerr("Could not initialise freetype\n");
        exit(1);
    }

    file = g_file_new_for_commandline_arg (fontfile);
    font_file = g_file_get_uri (file);
    g_object_unref (file);

    if (!font_file) {
        g_printerr("Could not parse argument into a URI\n");
        exit(1);
    }
    error = FT_New_Face_From_URI (library, font_file, 0, &face);
    if (error) {
        g_printerr("Could not load face '%s'\n", font_file);
        exit(1);
    }
    return cairo_ft_font_face_create_for_ft_face (face, 0);
}
Example #3
0
static void
draw (GtkWidget *drawing_area,
      cairo_t *cr,
      FT_Face face)
{
    cairo_font_extents_t font_extents;
    gint *sizes = NULL, n_sizes, alpha_size, pos_y, i;
    const gchar *text;
    cairo_font_face_t *font;

    text = get_sample_string (face);
    sizes = build_sizes_table (face, &n_sizes, &alpha_size);

    font = cairo_ft_font_face_create_for_ft_face (face, 0);
    cairo_set_font_face (cr, font);
    cairo_font_extents (cr, &font_extents);
    cairo_font_face_destroy (font);

    /* draw text */
    pos_y = MAX (font_extents.height, 32) + 4;
    cairo_set_font_size (cr, alpha_size);
    draw_string (cr, lowercase_text, &pos_y);
    draw_string (cr, uppercase_text, &pos_y);
    draw_string (cr, punctuation_text, &pos_y);

    pos_y += 8;
    for (i = 0; i < n_sizes; i++) {
	cairo_set_font_size (cr, sizes[i]);
	draw_string (cr, text, &pos_y);
    }

    g_free (sizes);
}
faceCacheObj *getFontFace(cairoCacheData *cache, const char *font)
{
  faceCacheObj *newface = NULL;
  faceCacheObj *cur=cache->facecache;
  while(cur) {
    if(!strcmp(cur->path,font))
      return cur;
    cur = cur->next;
  }
  newface = malloc(sizeof(faceCacheObj));

  if(FT_New_Face(cache->library, font, 0, &(newface->ftface))) {
    msSetError(MS_RENDERERERR,"Freetype failed to open font %s","getFontFace()",font);
    free(newface);
    return NULL;
  }

  /* Try to select charmap */
  if (!newface->ftface->charmap) {
    if( FT_Select_Charmap(newface->ftface, FT_ENCODING_MS_SYMBOL) )
       FT_Select_Charmap(newface->ftface, FT_ENCODING_APPLE_ROMAN );
  }

  newface->next = cache->facecache;
  cache->facecache = newface;
  newface->face = cairo_ft_font_face_create_for_ft_face(newface->ftface, 0);

  cairo_font_face_set_user_data (newface->face, &newface->facekey,
                                 &(newface->ftface), (cairo_destroy_func_t) NULL); // we call FT_Done_Face ourselves in freeFaceCache

  newface->path = msStrdup(font);
  return newface;
}
Example #5
0
void load_font(caStack* stack)
{
    caValue* filename = circa_input(stack, 0);

    FontFace* font = new FontFace();

    circa_read_file_with_stack(stack, circa_string(filename), &font->rawData);
    if (circa_is_null(&font->rawData))
        return circa_output_error(stack, "Failed to load file");
    
    if (!g_ftLibraryInitialized) {
        FT_Init_FreeType(&g_ftLibrary);
        g_ftLibraryInitialized = true;
    }
    
    int error = FT_New_Memory_Face(g_ftLibrary,
                   (FT_Byte*) circa_blob(&font->rawData),
                   circa_blob_size(&font->rawData),
                   0,
                   &font->ftFace);

    if (error)
        return circa_output_error(stack, "FT_New_Memory_Face failed");

    font->cairoFace = cairo_ft_font_face_create_for_ft_face(font->ftFace, 0);

    caValue* out = circa_set_default_output(stack, 0);
    circa_set_native_ptr(circa_index(out, 0), font, FontFaceRelease);
}
Example #6
0
static VALUE
cr_freetype_font_face_initialize (VALUE self, VALUE path)
{
  FT_Face freetype_face;
  FT_Error error;
  cairo_font_face_t *face;
  cairo_status_t status;

  error = FT_New_Face (cr_freetype_library,
                       StringValueCStr(path),
                       0,
                       &freetype_face);
  cr_freetype_error_check (error, "failed to open FreeType font", path);
  cr_freetype_n_faces++;

  face = cairo_ft_font_face_create_for_ft_face (freetype_face, 0);
  cr_font_face_check_status (face);
  status =
    cairo_font_face_set_user_data (face,
                                   &cr_freetype_face_key,
                                   freetype_face,
                                   (cairo_destroy_func_t) cr_freetype_done_face);
  if (status != CAIRO_STATUS_SUCCESS) {
    cairo_font_face_destroy (face);
    FT_Done_Face (freetype_face);
    rb_cairo_check_status (status);
  }

  DATA_PTR (self) = face;

  return Qnil;
}
Example #7
0
faceCacheObj *getFontFace(cairoCacheData *cache, char *font) {
    faceCacheObj *newface = NULL;
    faceCacheObj *cur=cache->facecache;
    while(cur) {
        if(!strcmp(cur->path,font))
            return cur;
        cur = cur->next;
    }
    newface = malloc(sizeof(faceCacheObj));
    
    if(FT_New_Face(cache->library, font, 0, &(newface->ftface))) {
        msSetError(MS_RENDERERERR,"Freetype failed to open font %s","getFontFace()",font);
        free(newface);
        return NULL;
    }
    newface->next = cache->facecache;
    cache->facecache = newface;
    newface->face = cairo_ft_font_face_create_for_ft_face(newface->ftface, 0);

    cairo_font_face_set_user_data (newface->face, &newface->facekey,
            &(newface->ftface), (cairo_destroy_func_t) FT_Done_Face);

    newface->path = msStrdup(font);
    return newface;
}
Example #8
0
cairo_face::cairo_face(std::shared_ptr<font_library> const& library, face_ptr const& face)
    : face_(face)
{
    static cairo_user_data_key_t key;
    c_face_ = cairo_ft_font_face_create_for_ft_face(face->get_face(), FT_LOAD_NO_HINTING);
    cairo_font_face_set_user_data(c_face_, &key, new handle(library, face), destroy);
}
Example #9
0
void CairoRenderer::SetFontFace(InputStream *pStream)
{
	FT_Face face;
	static cairo_user_data_key_t key;

	if (m_cairo_face)
	{
		cairo_set_font_face(m_pCairo, NULL);
		cairo_font_face_destroy(m_cairo_face);
		m_cairo_face = NULL;
	}

	if (pStream)
	{
		face = FreeType::OpenFace(pStream);  // face will be destroyed by FT_Done_Face
		if (face)
		{
			m_cairo_face = cairo_ft_font_face_create_for_ft_face(face, 0);
			cairo_font_face_set_user_data(m_cairo_face, &key, face, (cairo_destroy_func_t)FT_Done_Face);
		}
	}
	else
		m_cairo_face = cairo_toy_font_face_create("unifont", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);

	if (m_cairo_face)
		cairo_set_font_face(m_pCairo, m_cairo_face);
}
FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
{
    ASSERT_ARG(buffer, buffer);

    int error;

    static FT_Library library = 0;
    if (!library) {
        error = FT_Init_FreeType(&library);
        if (error) {
            library = 0;
            return 0;
        }
    }

    FT_Face face;
    error = FT_New_Memory_Face(library, reinterpret_cast<const FT_Byte*>(buffer->data()), buffer->size(), 0, &face);
    if (error)
        return 0;

    buffer->ref();
    cairo_font_face_t* fontFace = cairo_ft_font_face_create_for_ft_face(face, 0);

    static cairo_user_data_key_t bufferKey;
    cairo_font_face_set_user_data(fontFace, &bufferKey, buffer, releaseData);

    return new FontCustomPlatformData(fontFace);
}
Example #11
0
static int l_font_new(lua_State * L)
{
	const char * family = luaL_checkstring(L, 1);
	struct lfont_t * font = lua_newuserdata(L, sizeof(struct lfont_t));
	if(FT_Init_FreeType(&font->library))
		return 0;
	if(FT_New_Xfs_Face(font->library, family, 0, &font->fface))
	{
		FT_Done_FreeType(font->library);
		return 0;
	}
	font->face = cairo_ft_font_face_create_for_ft_face(font->fface, 0);
	if(font->face->status != CAIRO_STATUS_SUCCESS)
	{
		FT_Done_Face(font->fface);
		FT_Done_FreeType(font->library);
		cairo_font_face_destroy(font->face);
		return 0;
	}
	cairo_font_options_t * options = cairo_font_options_create();
	cairo_matrix_t identity;
	cairo_matrix_init_identity(&identity);
	font->sfont = cairo_scaled_font_create(font->face, &identity, &identity, options);
	cairo_font_options_destroy(options);
	if(cairo_scaled_font_status(font->sfont) != CAIRO_STATUS_SUCCESS)
	{
		FT_Done_Face(font->fface);
		FT_Done_FreeType(font->library);
		cairo_font_face_destroy(font->face);
		cairo_scaled_font_destroy(font->sfont);
		return 0;
	}
	luaL_setmetatable(L, MT_FONT);
	return 1;
}
Example #12
0
File: fonts.c Project: tommed/tuxwm
unsigned int init_font(FontFace *font, char *path)
{
	unsigned int result = FT_New_Face(library, path, 0, &font->ft_font);
	if (0 == result) {
		font->cairo_font = cairo_ft_font_face_create_for_ft_face(font->ft_font, 0);
	}
	return result;
}
Example #13
0
	double lime_cairo_ft_font_face_create (double face, int flags) {
		
		#ifdef LIME_FREETYPE
		Font *font = (Font*)(intptr_t)face;
		return (intptr_t)cairo_ft_font_face_create_for_ft_face ((FT_Face)font->face, flags);
		#else
		return 0;
		#endif
		
	}
Example #14
0
static void
realize_callback (GtkWidget *drawing_area,
		  FT_Face face)
{
    gint i, pixmap_width, pixmap_height;
    const gchar *text;
    cairo_text_extents_t extents;
    cairo_font_face_t *font;
    gint *sizes = NULL, n_sizes, alpha_size;
    cairo_t *cr;

    cr = gdk_cairo_create (gtk_widget_get_window (drawing_area));

    text = get_sample_string (face);
    sizes = build_sizes_table (face, &n_sizes, &alpha_size);

    /* calculate size of pixmap to use (with 4 pixels padding) ... */
    pixmap_width = 8;
    pixmap_height = 8;

    font = cairo_ft_font_face_create_for_ft_face (face, 0);
    cairo_set_font_face (cr, font);
    cairo_set_font_size (cr, alpha_size);
    cairo_font_face_destroy (font);

    cairo_text_extents (cr, lowercase_text, &extents);
    pixmap_height += extents.height + 4;
    pixmap_width = MAX (pixmap_width, 8 + extents.width);

    cairo_text_extents (cr, uppercase_text, &extents);
    pixmap_height += extents.height + 4;
    pixmap_width = MAX (pixmap_width, 8 + extents.width);

    cairo_text_extents (cr, punctuation_text, &extents);
    pixmap_height += extents.height + 4;
    pixmap_width = MAX (pixmap_width, 8 + extents.width);

    pixmap_height += 8;

    for (i = 0; i < n_sizes; i++) {
	cairo_set_font_size (cr, sizes[i]);
	cairo_text_extents (cr, text, &extents);
	pixmap_height += extents.height + 4;
	pixmap_width = MAX (pixmap_width, 8 + extents.width);
    }

    gtk_widget_set_size_request (drawing_area, pixmap_width, pixmap_height);

    cairo_destroy (cr);
    g_free (sizes);
}
Example #15
0
struct GUIConfig *guiConfig()
{
    static struct GUIConfig *gconfig = 0;
    FT_Face face;
    FT_Library library;
    if (!gconfig) {
        gconfig = (struct GUIConfig*)malloc(sizeof(struct GUIConfig));
        gconfig->borderSize = DEFAULT_BORDER_SIZE;
        gconfig->titleHeight = DEFAULT_TITLE_HEIGHT;
        FT_Init_FreeType(&library);
        FT_New_Face(library, "C:/zenhei.ttc", 0, &face);
        gconfig->fontFace = cairo_ft_font_face_create_for_ft_face(face,0);
    }
    return gconfig;
}
Example #16
0
	value lime_cairo_ft_font_face_create (value face, int flags) {
		
		#ifdef LIME_FREETYPE
		Font* font = (Font*)val_data (face);
		cairo_font_face_t* cairoFont = cairo_ft_font_face_create_for_ft_face ((FT_Face)font->face, flags);
		
		AutoGCRoot* fontReference = new AutoGCRoot (face);
		cairo_font_face_set_user_data (cairoFont, &userData, fontReference, gc_user_data);
		
		return CFFIPointer (cairoFont, gc_cairo_font_face);
		#else
		return 0;
		#endif
		
	}
Example #17
0
static cairoFaceCache* getCairoFontFace(cairoCacheData *cache, FT_Face ftface) {
  cairoFaceCache *cur = cache->cairofacecache;
  while(cur) {
    if(cur->ftface == ftface) return cur;
    cur = cur->next;
  }
  cur = msSmallMalloc(sizeof(cairoFaceCache));
  cur->next = cache->cairofacecache;
  cache->cairofacecache = cur;
  cur->ftface = ftface;
  cur->face = cairo_ft_font_face_create_for_ft_face(ftface, 0);
  cur->options = cairo_font_options_create();
  cairo_font_options_set_hint_style(cur->options,CAIRO_HINT_STYLE_NONE);
  return cur;
}
Example #18
0
cairo_font_face_t *Rcairo_set_font_face(int i, const char *file){
	cairo_font_face_t *c_face;
	cairo_status_t status;
	FT_Face face;
	FT_Error er;
	FT_CharMap found = 0; 
	FT_CharMap charmap; 
	int n; 

	/* Ensure that freetype library is ready */
	if (!Rcairo_ft_library){
		if (FT_Init_FreeType(&Rcairo_ft_library)){
			error("Failed to initialize freetype library in Rcairo_set_font_face!\n");
			return FALSE;
		}
	}

	er = FT_New_Face(Rcairo_ft_library, file, 0, &face);
	if ( er == FT_Err_Unknown_File_Format ) { 
		error("Unsupported font file format\n");
		return NULL;
	} else if ( er ) {
		error("Unknown font problem\n");
		return NULL;
	}
	for ( n = 0; n < face->num_charmaps; n++ ) { 
		charmap = face->charmaps[n]; 
		if ( charmap->platform_id == TT_PLATFORM_MACINTOSH) { 
			found = charmap; 
			break; 
		} 
	}

	/* Only do this for symbol font */
	if (found && i == 4){
		er = FT_Set_Charmap( face, found );
	} 

	c_face = cairo_ft_font_face_create_for_ft_face(face,FT_LOAD_DEFAULT);
	status = cairo_font_face_set_user_data (c_face, &key,
		face, (cairo_destroy_func_t) FT_Done_Face);
	if (status) {
	    cairo_font_face_destroy (c_face);
	    FT_Done_Face (face);
	    return NULL;
	}
	return c_face;
}
Example #19
0
cairo_font_face_t *load_font_face() {
  FT_Library lib;
  FT_Face face;
  FT_Error error;
  cairo_font_face_t *cr_face;
  char *path = "/Users/sseefried/code/games/epidemic-game/GoodDog.otf";

  error = FT_Init_FreeType(&lib);
  exitOnError(error);
  error = FT_New_Face(lib, path, 0, &face);
  exitOnError(error);

  cr_face = cairo_ft_font_face_create_for_ft_face(face, 0);


  return cr_face;
}
FontCustomPlatformData::FontCustomPlatformData(FT_Face freeTypeFace, SharedBuffer& buffer)
    : m_freeTypeFace(freeTypeFace)
    , m_fontFace(cairo_ft_font_face_create_for_ft_face(freeTypeFace, 0))
{
    // FIXME Should we be setting some hinting options here?

    buffer.ref(); // This is balanced by the buffer->deref() in releaseCustomFontData.
    static cairo_user_data_key_t bufferKey;
    cairo_font_face_set_user_data(m_fontFace, &bufferKey, &buffer,
                                  static_cast<cairo_destroy_func_t>(releaseCustomFontData));

    // Cairo doesn't do FreeType reference counting, so we need to ensure that when
    // this cairo_font_face_t is destroyed, it cleans up the FreeType face as well.
    static cairo_user_data_key_t freeTypeFaceKey;
    cairo_font_face_set_user_data(m_fontFace, &freeTypeFaceKey, freeTypeFace,
                                  reinterpret_cast<cairo_destroy_func_t>(FT_Done_Face));
}
Example #21
0
ilA_img *ilG_gui_textlayout_render(ilG_gui_textlayout *self, float col[4], enum ilG_gui_textoptions opts)
{
    (void)opts;
    unsigned w = 0, h = 0;
    unsigned x = 0, y = 0;
    int bx = 0, by = 0;
    int scale = 64;
    unsigned i;

    self->cairo_ft_face = cairo_ft_font_face_create_for_ft_face(self->ft_face, 0);
    self->hb_ft_face = hb_ft_face_create(self->ft_face, NULL);

    x *= scale;
    y *= scale;
    self->cairo_glyphs = calloc(self->glyph_count, sizeof(cairo_glyph_t));
    for (i = 0; i < self->glyph_count; i++) {
        self->cairo_glyphs[i].index = self->glyph_info[i].codepoint;
        self->cairo_glyphs[i].x = (x + self->glyph_pos[i].x_offset)/scale;
        self->cairo_glyphs[i].y = (y + self->glyph_pos[i].y_offset)/scale;
        x += self->glyph_pos[i].x_advance;
        y += self->glyph_pos[i].y_advance;
    }

    ilG_gui_textlayout_getExtents(self, &w, &h, &bx, &by, NULL, NULL);
    self->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
    self->cr = cairo_create(self->surface);

    cairo_translate(self->cr, -bx, 1-by);
    cairo_set_source_rgba(self->cr, col[0], col[1], col[2], col[3]);
    cairo_set_font_face(self->cr, self->cairo_ft_face);
    cairo_set_font_size(self->cr, self->pt);
    cairo_show_glyphs(self->cr, self->cairo_glyphs, self->glyph_count);
    cairo_surface_flush(self->surface);

    ilA_img *preswizzle = ilA_img_fromdata(cairo_image_surface_get_data(self->surface), w, h, 8, ILA_IMG_RGBA);
    /*   R G B A | 0x
     * B 0 0 1 0 | 2
     * G 0 1 0 0 | 4
     * R 1 0 0 0 | 8
     * A 0 0 0 1 | 1
    */
    ilA_img *img = ilA_img_bgra_to_rgba(preswizzle);//ilA_img_swizzle(preswizzle, 0x2481);
    ilA_img_free(preswizzle);
    return img;
}
Example #22
0
static int cairo_font_face_create_l( lua_State* L )
{
  const char* filename = luaL_checkstring( L, 1 );

  lua_cairo_font_face_t* lcff = (lua_cairo_font_face_t*)lua_newuserdata( L, sizeof( lua_cairo_font_face_t ) );
  memset( lcff, 0, sizeof( lua_cairo_font_face_t ) );

  luaL_getmetatable( L, "lua_cairo_font_face_t" );
  lua_setmetatable( L, -2 );

  if ( FT_New_Face( m_ftlibrary, filename, 0, &lcff->ftface ) ) {
    luaL_error( L, "Failed to load font from file %s", filename );
  }

  if ( !( lcff->crface = cairo_ft_font_face_create_for_ft_face( lcff->ftface, 0 ) ) ) {
    luaL_error( L, "cairo_ft_font_face_create_for_ft_face() failed for font %s", filename );
  }

  return( 1 );
}
Example #23
0
int addfont(char * filepath, char * key)
{
    fontface * face;
    int error;

    // we'll only accept font keys of a certain size
    if ( (key != NULL) && (strlen(key) > MAX_FONT_NAME_LEN) )  return;
    if ( strlen(strrchr(filepath, '/')+1) > MAX_FONT_NAME_LEN )  return;

    face = malloc(sizeof(fontface));
    if ( face == NULL ) {  return ENOMEM;  }

    face->ftface = malloc(sizeof(FT_Face));
    if ( face->ftface == NULL ) {  return ENOMEM;  }

    error = FT_New_Face(library, filepath, 0, face->ftface);
    if ( error )
    {
        free(face->ftface);
        free(face);
        printf("cannot load font %s : Error code %d\n", filepath, error);
        return error;
    }

    // why does this generate a warning?
    face->cface = cairo_ft_font_face_create_for_ft_face(*(face->ftface), FT_LOAD_FORCE_AUTOHINT);

// Add to error logging when that's ready
//    printf("Added \t%s\t%s\t%s\n", strrchr(filepath, '/')+1, (*(face->ftface))->family_name, (*(face->ftface))->style_name);

    if ( key != NULL )
        hash_set(faces, key, face);
    else
        hash_set(faces, strrchr(filepath, '/')+1, face);

    return 0;
}
Example #24
0
gboolean draw_overview_glyph (cairo_t* context, const char* font_file, gdouble width, gdouble height, gunichar character) {
	FT_Face face;
	int error;
	gdouble units_per_em;
	gdouble units;
	gdouble advance;
	int gid;

	// private use area
	if (0xe000 <= character && character <= 0xf8ff) {
		return FALSE;
	}
	
	// control characters
	if (character <= 0x001f || (0x007f <= character && character <= 0x008d)) {
		return FALSE;
	}

	if (font_file == NULL) {
		g_warning("font_file is null");
		return FALSE;
	}
	
	gchar text[7];
	int length = g_unichar_to_utf8 (character, text);
	text[length] = '\0';
	
	if (freetype_library == NULL) {
		error = FT_Init_FreeType (&freetype_library);
		if (error) {
			g_warning ("Freetype init error %d.\n", error);
			return FALSE;
		}
	}

	error = FT_New_Face (freetype_library, font_file, 0, &face);
	if (error) {
		g_warning ("Freetype font face error %d\n", error);
		return FALSE;
	}
	
	units_per_em = face->units_per_EM;
	units = (height * 0.5) / units_per_em;
	
	error = FT_Select_Charmap (face , FT_ENCODING_UNICODE);
	if (error) {
		g_warning ("Freetype can not use Unicode, error: %d\n", error);
		FT_Done_Face (face);
		return FALSE;
	}

	error = FT_Set_Char_Size (face, 0, 64, (int) height, (int) height);
	if (error) {
		g_warning ("FT_Set_Char_Size, error: %d.\n", error);
		FT_Done_Face (face);
		return FALSE;
	}
	
	error = FT_Set_Pixel_Sizes (face, 0, (int) (height * 0.5));
	if (error) {
		g_warning ("FT_Set_Pixel_Sizes, error: %d.\n", error);
		FT_Done_Face (face);
		return FALSE;
	}

	gid = FT_Get_Char_Index (face, character);
	advance = 0;
	if (gid != 0) {
		FT_Load_Glyph(face, gid, FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE);
		advance = face->glyph->metrics.horiAdvance;
		advance *= units;
	} else {
		FT_Done_Face (face);
		return FALSE;
	}

	static const cairo_user_data_key_t key;

	cairo_save (context);
	
	cairo_font_face_t* cairo_face = cairo_ft_font_face_create_for_ft_face (face, 0);
	
	if (cairo_face == NULL) {
		g_warning("cairo font face is null");
		FT_Done_Face (face);
		return FALSE;
	}
	
	int status = cairo_font_face_set_user_data (cairo_face, &key, face, (cairo_destroy_func_t) FT_Done_Face);
	
	if (status != CAIRO_STATUS_SUCCESS) {		
		cairo_font_face_destroy (cairo_face);
		FT_Done_Face (face);
		return FALSE;
	}

	cairo_set_font_face (context, cairo_face);
	cairo_set_font_size (context, height * 0.5);
	
	gdouble x = (width - advance) / 2;
	
	if (x < 0) {
		x = 0;
	}
	
	cairo_move_to (context, x, height - 30);
	cairo_show_text (context, text);
	
	cairo_font_face_destroy (cairo_face);
	cairo_restore (context);
	
	// cairo closes the font face and the library must be kept open
	
	return TRUE;
}
Example #25
0
/**
 * Initialize a new console
 */
Console* new_Console(cairo_t* context, double font_size)
{
  //printf("Console::Console(cairo_t@%p)\n", context);

  Console* self;
  self = malloc(sizeof(Console));
  self->head = null;
  self->tail = null;
  self->cursor = null;
  self->width = default_width;
  self->height = default_height;
  self->pad = 4;
  self->font_filename = "./Monaco_Linux.ttf"; /* TODO */
  self->font_size = font_size;
  self->transparency = 1.0;
  self->antialias_text = false;
  self->antialias_graphics = true;
  
  /* initialize lithp, cairo - and define callbacks */
  self->lithp = new_LithpInterpreter();
  char* cairo_init [] = {
    //"(set 'libcairo             (xl:dlopen \"libcairo.dylib\"))",
    //"(set 'libcairo             (xl:dlopen \"libcairo.so.2\"))",
    //"(set 'libcairo (xl:dlopen \"/Users/antoine/Projects/phlo/install/lib/libcairo.dylib\"))",
#if defined(__linux__)
    "(set 'libcairo             (xl:dlopen \"libcairo.so.2\"))",
#elif defined(__APPLE__) && defined(__MACH__)
    "(set 'libcairo             (xl:dlopen \"libcairo.dylib\"))",
#endif
    "(set 'cairo:set-source-rgb       (xl:dlsym libcairo \"cairo_set_source_rgb\"))",
    "(set 'cairo:rectangle            (xl:dlsym libcairo \"cairo_rectangle\"))",
    "(set 'cairo:fill                 (xl:dlsym libcairo \"cairo_fill\"))",
    "(set 'cairo:translate            (xl:dlsym libcairo \"cairo_translate\"))",
    "(set 'cairo:rotate               (xl:dlsym libcairo \"cairo_rotate\"))",
    "(set 'cairo:move-to              (xl:dlsym libcairo \"cairo_move_to\"))",
    "(set 'cairo:set-source-rgba      (xl:dlsym libcairo \"cairo_set_source_rgba\"))",
    "(set 'console:width 492)",
    "(set 'console:height 344)",
    "(set 'pi 3.141592)",

    "(set 'pretty '(lambda (cr arc theta)                      "
    "  (cond ((lte arc 0.0) 0.0)                               "
    "        ('t (progn                                        "
    "              (cairo:rotate cr theta)                     "
    "              (cairo:rectangle cr 20.0 20.0 140.0 20.0)    "
    "              (cairo:fill cr)                             "
    "              (pretty cr (sub arc theta) theta))))))      ",

    "(set 'console:expose-event '(lambda (context)             "
    "  (cairo:set-source-rgb  context 0.5 0.5 1.0)         "
    "  (cairo:translate context (div console:width 2.0) (div console:height 2.0))"
    "  (pretty context (mul pi 2.0) (div pi 4.0))))",
    //"(set 'console:expose-event '(lambda (context) ()))",
    null
  };
  for (size_t t = 0; cairo_init[t] != null; t++) {
    Expression* callback = Expression_parse_utf8(cairo_init[t], null, &self->lithp->symbol_list); 
    _gc_protect(callback);
    Expression_eval(callback, self->lithp->environment);
    _gc_unprotect(callback);
  }


  /* initialize and configure console font */
  FT_Library  library;
  int error = FT_Init_FreeType(&library);
  if (error) { perror("Could not open FreeType library"); exit(EXIT_FAILURE); }
  self->font_face = malloc(sizeof(FT_Face*));
  error = FT_New_Face(library, self->font_filename, 0, self->font_face);
  if (error) { perror("Could not open font"); exit(EXIT_FAILURE); }
  cairo_set_font_face(context, cairo_ft_font_face_create_for_ft_face(*self->font_face, 0));
  cairo_set_font_size(context, self->font_size);
  cairo_font_options_t* font_options = cairo_font_options_create();
  cairo_get_font_options(context, font_options);
  if (self->antialias_text) {
    cairo_font_options_set_antialias(font_options, CAIRO_ANTIALIAS_NONE);
  }
  cairo_set_font_options(context, font_options);
  self->font_extents = malloc(sizeof(cairo_font_extents_t));
  cairo_font_extents(context, self->font_extents);

  /* some sample text */
  //wchar_t* test_text = L"ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n1234567890\0";
  wchar_t* test_text = L"  \n  ";
  unsigned int n;
  for (n = 0; n < wcslen(test_text); n++) {
    //  Console_append(self, test_text[n]);
    Console_insert(self, test_text[n]);
  }
  self->cursor = self->head;

  return self;
}
Example #26
0
/**
 * Handle console expose events
 */
void Console_expose(Console* self, cairo_t* context) 
{
  /* a pretty background */
  cairo_save(context);
  cairo_set_source_rgb(context, 1.0, 1.0, 1.0);
  cairo_translate(context, self->width / 2, self->height / 2);
  double t = pi / 4;
  for (double n = 0; n < pi * 2; n += t) {
    cairo_rectangle(context, 20, 20, 140, 40);
    cairo_fill(context);
    cairo_rotate(context, t);
  }
  cairo_restore(context);

  /* set console:width and console:height */
  char buf[256];
  sprintf(buf, "(set 'console:width %d)", (int)self->width);
  Expression* e = Expression_parse_utf8(buf, null, &self->lithp->symbol_list); 
  _gc_protect(e); Expression_eval(e, self->lithp->environment); _gc_unprotect(e);
  sprintf(buf, "(set 'console:height %d)", (int)self->height);
  e = Expression_parse_utf8(buf, null, &self->lithp->symbol_list); 
  _gc_protect(e); Expression_eval(e, self->lithp->environment); _gc_unprotect(e);


  /* invoke callbacks inside lithp */
  cairo_save(context);
  Expression* fn = Expression_intern("console:expose-event", &self->lithp->symbol_list);
  Expression* arg = _new_Expression(pointer);
  arg->car = (void*)context;
  _gc_protect(fn); _gc_protect(arg);
  Expression* callback = Expression_cons(fn, (Expression_cons(arg, nil_)));
  _gc_protect(callback);
  Expression_eval(callback, self->lithp->environment);
  _gc_unprotect(callback);
  _gc_unprotect(arg); _gc_unprotect(fn);
  cairo_restore(context);

  /* force lithp to garbage collect - TODO fix */
  self->lithp->gc->free_list = _gc_collect();

  /* select font */
  cairo_set_font_face(context, cairo_ft_font_face_create_for_ft_face(*self->font_face, 0));
  cairo_set_font_size(context, self->font_size);

  /* layout text */
  Console_layout(self);

  /* render text */  
  cairo_set_source_rgb(context, 0, 0, 0); 
  glyph_list* current = self->head;
  while (current) {
    cairo_show_glyphs(context, current->glyph, 1);
    current = current->next;
  }

  /* render cursor */
  cairo_set_source_rgba(context, 0, 0, 1, 0.5);
  double x = 0;
  double y = 0;
  if (self->cursor) {
    x = self->cursor->glyph->x; // + self->font_extents->max_x_advance;
    y = self->cursor->glyph->y - self->font_extents->ascent;
  }
  cairo_rectangle(context, x, y, self->font_extents->max_x_advance, self->font_extents->height);
  cairo_fill(context);
}
Example #27
0
static gboolean
sushi_font_widget_draw (GtkWidget *drawing_area,
                        cairo_t *cr)
{
  SushiFontWidget *self = SUSHI_FONT_WIDGET (drawing_area);
  SushiFontWidgetPrivate *priv = self->priv;
  gint *sizes = NULL, n_sizes, alpha_size, title_size, pos_y = 0, i;
  cairo_font_face_t *font;
  FT_Face face = priv->face;
  GtkStyleContext *context;
  GdkRGBA color;
  GtkBorder padding;
  GtkStateFlags state;
  gint allocated_width, allocated_height;

  if (face == NULL)
    goto end;

  context = gtk_widget_get_style_context (drawing_area);
  state = gtk_style_context_get_state (context);

  allocated_width = gtk_widget_get_allocated_width (drawing_area);
  allocated_height = gtk_widget_get_allocated_height (drawing_area);

  gtk_render_background (context, cr,
                         0, 0, allocated_width, allocated_height);

  gtk_style_context_get_color (context, state, &color);
  gtk_style_context_get_padding (context, state, &padding);

  gdk_cairo_set_source_rgba (cr, &color);

  sizes = build_sizes_table (face, &n_sizes, &alpha_size, &title_size);

  font = cairo_ft_font_face_create_for_ft_face (face, 0);
  cairo_set_font_face (cr, font);
  cairo_font_face_destroy (font);

  /* draw text */

  if (self->priv->font_name != NULL) {
    cairo_set_font_size (cr, title_size);
    draw_string (self, cr, padding, self->priv->font_name, &pos_y);
  }

  if (pos_y > allocated_height)
    goto end;

  pos_y += SECTION_SPACING / 2;
  cairo_set_font_size (cr, alpha_size);

  if (self->priv->lowercase_text != NULL)
    draw_string (self, cr, padding, self->priv->lowercase_text, &pos_y);
  if (pos_y > allocated_height)
    goto end;

  if (self->priv->uppercase_text != NULL)
    draw_string (self, cr, padding, self->priv->uppercase_text, &pos_y);
  if (pos_y > allocated_height)
    goto end;

  if (self->priv->punctuation_text != NULL)
    draw_string (self, cr, padding, self->priv->punctuation_text, &pos_y);
  if (pos_y > allocated_height)
    goto end;

  pos_y += SECTION_SPACING;

  for (i = 0; i < n_sizes; i++) {
    cairo_set_font_size (cr, sizes[i]);
    if (self->priv->sample_string !=  NULL)
      draw_string (self, cr, padding, self->priv->sample_string, &pos_y);
    if (pos_y > allocated_height)
      break;
  }

 end:
  g_free (sizes);

  return FALSE;
}
Example #28
0
static void
sushi_font_widget_size_request (GtkWidget *drawing_area,
                                gint *width,
                                gint *height,
                                gint *min_height)
{
  SushiFontWidget *self = SUSHI_FONT_WIDGET (drawing_area);
  SushiFontWidgetPrivate *priv = self->priv;
  gint i, pixmap_width, pixmap_height;
  cairo_text_extents_t extents;
  cairo_font_extents_t font_extents;
  cairo_font_face_t *font;
  gint *sizes = NULL, n_sizes, alpha_size, title_size;
  cairo_t *cr;
  cairo_surface_t *surface;
  FT_Face face = priv->face;
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkBorder padding;

  if (face == NULL) {
    if (width != NULL)
      *width = 1;
    if (height != NULL)
      *height = 1;
    if (min_height != NULL)
      *min_height = 1;

    return;
  }

  if (min_height != NULL)
    *min_height = -1;

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                        SURFACE_SIZE, SURFACE_SIZE);
  cr = cairo_create (surface);
  context = gtk_widget_get_style_context (drawing_area);
  state = gtk_style_context_get_state (context);
  gtk_style_context_get_padding (context, state, &padding);

  sizes = build_sizes_table (face, &n_sizes, &alpha_size, &title_size);

  /* calculate size of pixmap to use */
  pixmap_width = padding.left + padding.right;
  pixmap_height = padding.top + padding.bottom;

  font = cairo_ft_font_face_create_for_ft_face (face, 0);
  cairo_set_font_face (cr, font);
  cairo_font_face_destroy (font);

  if (self->priv->font_name != NULL) {
      cairo_set_font_size (cr, title_size);
      cairo_font_extents (cr, &font_extents);
      text_extents (cr, self->priv->font_name, &extents);
      pixmap_height += font_extents.ascent + font_extents.descent +
        extents.y_advance + LINE_SPACING;
      pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
  }

  pixmap_height += SECTION_SPACING / 2;
  cairo_set_font_size (cr, alpha_size);
  cairo_font_extents (cr, &font_extents);

  if (self->priv->lowercase_text != NULL) {
    text_extents (cr, self->priv->lowercase_text, &extents);
    pixmap_height += font_extents.ascent + font_extents.descent + 
      extents.y_advance + LINE_SPACING;
    pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
  }

  if (self->priv->uppercase_text != NULL) {
    text_extents (cr, self->priv->uppercase_text, &extents);
    pixmap_height += font_extents.ascent + font_extents.descent +
      extents.y_advance + LINE_SPACING;
    pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
  }

  if (self->priv->punctuation_text != NULL) {
    text_extents (cr, self->priv->punctuation_text, &extents);
    pixmap_height += font_extents.ascent + font_extents.descent +
      extents.y_advance + LINE_SPACING;
    pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
  }

  if (self->priv->sample_string != NULL) {
    pixmap_height += SECTION_SPACING;

    for (i = 0; i < n_sizes; i++) {
      cairo_set_font_size (cr, sizes[i]);
      cairo_font_extents (cr, &font_extents);
      text_extents (cr, self->priv->sample_string, &extents);
      pixmap_height += font_extents.ascent + font_extents.descent +
        extents.y_advance + LINE_SPACING;
      pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);

      if ((i == 7) && (min_height != NULL))
        *min_height = pixmap_height;
    }
  }

  pixmap_height += padding.bottom + SECTION_SPACING;

  if (min_height != NULL && *min_height == -1)
    *min_height = pixmap_height;

  if (width != NULL)
    *width = pixmap_width;

  if (height != NULL)
    *height = pixmap_height;

  cairo_destroy (cr);
  cairo_surface_destroy (surface);
  g_free (sizes);
}
Example #29
0
static zend_bool php_cairo_create_ft_font_face(pecl_ft_container *ft_container, cairo_ft_font_face_object *font_face_object, php_stream *stream, zend_bool owned_stream, int load_flags, zend_bool throw_exceptions TSRMLS_DC) {
	FT_Stream ft_stream;
	stream_closure *closure;
	php_stream_statbuf ssbuf;
	FT_Open_Args open_args;
	int error;

	if (php_stream_stat(stream,&ssbuf) < 0) {
		return 1;
	}
	
    ft_container->ft_face = NULL; 
    ft_container->ft_stream = NULL;

    font_face_object->closure = NULL;
    
	closure = ecalloc(1, sizeof(stream_closure));
	closure->stream = stream;
	closure->owned_stream = owned_stream;
#ifdef ZTS
	closure->TSRMLS_C = TSRMLS_C;
#endif

	ft_stream = pecalloc(1, sizeof(*ft_stream), TRUE);
	ft_stream->descriptor.pointer = (void *)closure;
	ft_stream->pos = php_stream_tell(stream);
	ft_stream->size = ssbuf.sb.st_size;
	ft_stream->read = php_cairo_ft_read_func;
	open_args.flags = FT_OPEN_STREAM;
	open_args.stream = ft_stream;

	error = FT_Open_Face(ft_container->ft_lib, &open_args, 0, &ft_container->ft_face);
	
	if (error) {
		if (owned_stream) {
			php_stream_close(stream);
		}
		efree(closure);
		pefree(ft_stream, TRUE);
        
		return error;
	} 

    font_face_object->closure = closure;
    
    ft_container->ft_stream = ft_stream;
	font_face_object->font_face = (cairo_font_face_t *)cairo_ft_font_face_create_for_ft_face(ft_container->ft_face, (int)load_flags);

	/* Set Cairo to automatically destroy the FT_Face when the cairo_font_face_t is destroyed */
	error = cairo_font_face_set_user_data (
			font_face_object->font_face, 
			&font_face_object->key,
			ft_container, 
			(cairo_destroy_func_t) cairo_user_data_callback_ft_free);

            
	if (error) {
		cairo_font_face_destroy (font_face_object->font_face);
		FT_Done_Face(ft_container->ft_face);
        pefree(ft_stream, TRUE);
		return error;
	}

	return 0;
}
Example #30
0
void Drawer::DrawText(std::string& aTexte)
{
    Point Point1      (100,100);

    FacesMap::iterator IterFont;

    cairo_text_extents_t te;
    cairo_font_face_t* Cairo_font_face = NULL;

    for (IterFont = mFacesMap.begin(); IterFont != mFacesMap.end(); ++IterFont)
    {
        cairo_user_data_key_t Key;
        FT_Face FaceFind = IterFont->second;

        if (FaceFind)
        {
            Cairo_font_face = cairo_ft_font_face_create_for_ft_face (FaceFind,0);
            cairo_font_face_set_user_data (Cairo_font_face, &Key, FaceFind, NULL);

            cairo_set_font_size (mCairoDC, 75);
            cairo_set_source_rgb (mCairoDC, 0.0, 0.0, 0.0);

            MoveTo(Point1);

            std::string StrTmp = IterFont->first;
            StrTmp += "||";

            cairo_ft_font_face_set_synthesize   (Cairo_font_face, CAIRO_FT_SYNTHESIZE_BOLD);
            cairo_ft_font_face_set_synthesize (Cairo_font_face, CAIRO_FT_SYNTHESIZE_OBLIQUE);

            cairo_set_font_face(mCairoDC, Cairo_font_face);
            cairo_text_path (mCairoDC, StrTmp.c_str());
            cairo_fill(mCairoDC);

            cairo_text_extents(mCairoDC,StrTmp.c_str(),&te);
            Point1.X += (te.width + 10);
            MoveTo(Point1);

            cairo_text_path (mCairoDC, IterFont->first.c_str());
            cairo_fill(mCairoDC);

            Point1.X -= (te.width + 10);
            Point1.Y += 100;

            cairo_font_face_set_user_data (Cairo_font_face, &Key, NULL, NULL);
            cairo_font_face_destroy(Cairo_font_face);
        }
    }

    cairo_user_data_key_t KeyFontSystem;
    if (mFontSystem)
    {
        Cairo_font_face = cairo_ft_font_face_create_for_ft_face (mFontSystem,0);
        cairo_font_face_set_user_data (Cairo_font_face, &KeyFontSystem, mFontSystem, NULL);

        cairo_set_font_size (mCairoDC, 75);
        cairo_set_source_rgb (mCairoDC, 0.0, 0.0, 0.0);

        MoveTo(Point1);

        cairo_set_font_face(mCairoDC, Cairo_font_face);
        cairo_text_path (mCairoDC, "FontSystem");
        cairo_fill(mCairoDC);

        cairo_font_face_set_user_data (Cairo_font_face, &KeyFontSystem, NULL, NULL);
        cairo_font_face_destroy(Cairo_font_face);
    }

    Point1.X -= (100);
    Point1.Y += 100;

    cairo_user_data_key_t KeyFontTest;
    if (mFontTest)
    {
        Cairo_font_face = cairo_ft_font_face_create_for_ft_face (mFontTest,0);
        cairo_font_face_set_user_data (Cairo_font_face, &KeyFontTest, mFontTest, NULL);

        cairo_set_font_size (mCairoDC, 75);
        cairo_set_source_rgb (mCairoDC, 0.0, 0.0, 0.0);

        MoveTo(Point1);

        cairo_set_font_face(mCairoDC, Cairo_font_face);
        cairo_text_path (mCairoDC, "FontTest");
        cairo_fill(mCairoDC);

        cairo_font_face_set_user_data (Cairo_font_face, &KeyFontTest, NULL, NULL);
        cairo_font_face_destroy(Cairo_font_face);
    }
}