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 122 123 124
|
/* $Id: gvtextlayout_gdiplus.cpp,v 1.3 2009/06/03 01:10:56 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2008 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
#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(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;
};
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}
};
|