File: gvtextlayout_gdiplus.cpp

package info (click to toggle)
graphviz 2.38.0-17
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 80,692 kB
  • ctags: 27,194
  • sloc: ansic: 1,038,718; sh: 14,765; cpp: 11,377; makefile: 4,480; yacc: 3,339; xml: 2,466; tcl: 1,950; cs: 1,890; objc: 1,157; perl: 422; lex: 375; awk: 241; python: 45; ruby: 41; php: 26
file content (121 lines) | stat: -rwxr-xr-x 3,666 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include "gvplugin_textlayout.h"
#include "gvplugin_gdiplus.h"

static int count = 0;

using namespace Gdiplus;

static int CALLBACK fetch_first_font(
	const LOGFONTA *logFont,
	const TEXTMETRICA *textMetrics,
	DWORD fontType,
	LPARAM lParam)
{
	/* save the first font we see in the font enumeration */
	*((LOGFONTA *)lParam) = *logFont;
	return 0;
}

Layout::Layout(char *fontname, double fontsize, char* string)
{
	/* convert incoming UTF8 string to wide chars */
	/* NOTE: conversion is 1 or more UTF8 chars to 1 wide char */
	int len = strlen(string);
	text.resize(len);
	text.resize(MultiByteToWideChar(CP_UTF8, 0, string, len, &text[0], len));

	/* search for a font with this name. if we can't find it, use the generic serif instead */
	/* NOTE: GDI font search is more comprehensive than GDI+ and will look for variants e.g. Arial Bold */
	DeviceContext reference;
	LOGFONTA font_to_find;
	font_to_find.lfCharSet = ANSI_CHARSET;
	strncpy(font_to_find.lfFaceName, fontname, sizeof(font_to_find.lfFaceName) - 1);
	font_to_find.lfFaceName[sizeof(font_to_find.lfFaceName) - 1] = '\0';
	font_to_find.lfPitchAndFamily = 0;
	LOGFONTA found_font;
	if (EnumFontFamiliesExA(reference.hdc,
		&font_to_find,
		fetch_first_font,
		(LPARAM)&found_font,
		0) == 0) {
		found_font.lfHeight = (LONG)-fontsize;
		found_font.lfWidth = 0;
		font = new Font(reference.hdc, &found_font);
	}
	else
		font = new Font(FontFamily::GenericSerif(), fontsize);
}

Layout::~Layout()
{
	delete font;
}

void gdiplus_free_layout(void *layout)
{
	if (layout)
		delete (Layout*)layout;
};

boolean gdiplus_textlayout(textspan_t *span, 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(span->font->name, span->font->size, span->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();
		
	span->layout = (void*)layout;
	span->free_layout = &gdiplus_free_layout;
	span->size.x = boundingBox.Width;
	span->size.y = layout->font->GetHeight(&measureGraphics);
	span->yoffset_layout = fontFamily.GetCellAscent(style) * span->font->size / fontFamily.GetEmHeight(style); /* convert design units to pixels */
	span->yoffset_centerline = 0;
	return TRUE;
};

static gvtextlayout_engine_t gdiplus_textlayout_engine = {
    gdiplus_textlayout
};

gvplugin_installed_t gvtextlayout_gdiplus_types[] = {
    {0, "textlayout", 8, &gdiplus_textlayout_engine, NULL},
    {0, NULL, 0, NULL, NULL}
};