[cairo] The function cairo_ft_font_face_set_synthesize() in Windows environment can be bold, but not in macOS.

秋源 284679437 at qq.com
Thu Oct 15 06:33:13 UTC 2020


The same code, in Windows environment can be bold, but not in macOS.
The function used to set the bold is:
cairo_ft_font_face_set_synthesize(ct, CAIRO_FT_SYNTHESIZE_BOLD);


The complete test case code is as follows:
void TestFontBold( bool bold)
{
	int bmp_w = 80;	int bmp_h = 32;
	int nSizebuff = bmp_w * bmp_h * 3;
	unsigned char * buffer = NULL;
	buffer = new unsigned char[nSizebuff]; memset(buffer, 0, nSizebuff);


	Oint32 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, bmp_w);
	cairo_surface_t* cs = cairo_image_surface_create_for_data(buffer, CAIRO_FORMAT_ARGB32, bmp_w, bmp_h, stride);
	cairo_t* c = cairo_create(cs);


	//set background	 
	cairo_set_source_rgba(c, 0, 0, 0, 0);
	cairo_paint(c);


	//set font size; color;
	int font_size = 12;
	cairo_set_source_rgb(c, 1, 0, 0);
	cairo_set_font_size(c, font_size);


	FT_Library FTLib = NULL;
	FT_Error status = FT_Init_FreeType(&FTLib);
	if (status != 0) {
		return;
	}
	FT_Face* face = new FT_Face;
	status = FT_New_Face(FTLib, "../allfonts/1.ttf", 0, face);
	if (status != 0) {
		return;
	}
	cairo_t *g_c = (cairo_t *)c;
	cairo_font_face_t *ct = cairo_ft_font_face_create_for_ft_face(*face, 0);
	if (bold) {
		cairo_ft_font_face_set_synthesize(ct, CAIRO_FT_SYNTHESIZE_BOLD);
	}
	cairo_set_font_face(g_c, ct);
	cairo_font_face_destroy(ct);


	//get textextents
	string str = "TestIsBold";
	//GB2312ToUTF8(str);


	//close antialias
	cairo_font_options_t *pOption = cairo_font_options_create();
	cairo_get_font_options(g_c, pOption);
	cairo_font_options_set_antialias(pOption, CAIRO_ANTIALIAS_NONE);
	cairo_set_font_options(g_c, pOption);
	cairo_font_options_destroy(pOption);


	//set pos
	cairo_text_extents_t ttCurStr;
	cairo_text_extents(c, str.c_str(), &ttCurStr);
	cairo_move_to(c, 0, ttCurStr.height + (bmp_h - ttCurStr.height) / 2.0);


	//show text
	cairo_show_text(c, str.c_str());


	//Save as a PNG image to see the effect
	cairo_surface_write_to_png(cs, "123---.png");


	cairo_destroy(c);
	cairo_surface_destroy(cs);


	delete buffer; buffer = NULL;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20201015/407fb146/attachment.htm>


More information about the cairo mailing list