Beispiel #1
0
// --------------------------------------------------------------
GFontWin32GDIPlus::GFontWin32GDIPlus( Font* nativeFont, const char * faceName, 
									  int size, int properties) 
						: mNativeFont(nativeFont), mName(faceName),
						  mSize(size), mFontProp(properties)
{
	DWORD count = (DWORD)mbstowcs(NULL, faceName, strlen(faceName));
	WCHAR * wstr = (WCHAR*) malloc ((count + 1) * sizeof(WCHAR));
	mbstowcs(wstr, faceName, strlen(faceName));
	FontCollection fc;
	FontFamily ff (wstr, &fc);
	int style;
	switch (properties) {
		case kFontBold:
			style = FontStyleBold;
			break;
		case kFontItalic:
			style = FontStyleItalic;
			break;
		case kFontBold+kFontItalic:
			style = FontStyleBoldItalic;
			break;
		case kFontUnderline:
			style = FontStyleUnderline;
			break;
		default:
			style = FontStyleRegular;
	}
	mEmHeight = ff.GetEmHeight(style);
	float ratio = mEmHeight ? (float)mEmHeight / size : 1.f;
	mAscent = ff.GetCellAscent(style) / ratio;
	mDescent = ff.GetCellDescent(style) / ratio;
	free (wstr);
}
boolean gdiplus_textlayout(textpara_t *para, char **fontpath)
{
	/* ensure GDI+ is started up: since we get called outside of a job, we can't rely on GDI+ startup then */
	UseGdiplus();
	
	Layout* layout = new Layout(para->fontname, para->fontsize, para->str);
	
	/* measure the text */
	/* NOTE: use TextRenderingHintAntiAlias + GetGenericTypographic to get a layout without extra space at beginning and end */
	RectF boundingBox;
	DeviceContext deviceContext;
	Graphics measureGraphics(deviceContext.hdc);
	measureGraphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
	measureGraphics.MeasureString(
		&layout->text[0],
		layout->text.size(),
		layout->font,
		PointF(0.0f, 0.0f),
		GetGenericTypographic(),
		&boundingBox);
		
	FontFamily fontFamily;
	layout->font->GetFamily(&fontFamily);
	int style = layout->font->GetStyle();
		
	para->layout = (void*)layout;
	para->free_layout = &gdiplus_free_layout;
	para->width = boundingBox.Width;
	para->height = layout->font->GetHeight(&measureGraphics);
	para->yoffset_layout = fontFamily.GetCellAscent(style) * para->fontsize / fontFamily.GetEmHeight(style); /* convert design units to pixels */
	para->yoffset_centerline = 0;
	return TRUE;
};