Possible fix for Automation text_extents function being inaccurate, yet untested

Originally committed to SVN as r798.
This commit is contained in:
Niels Martin Hansen 2007-01-15 17:02:39 +00:00
parent 818b31edc3
commit c3995de991
3 changed files with 18 additions and 17 deletions

View File

@ -57,11 +57,12 @@
namespace Automation4 { namespace Automation4 {
bool CalculateTextExtents(AssStyle *style, wxString &text, int &width, int &height, int &descent, int &extlead) bool CalculateTextExtents(AssStyle *style, wxString &text, double &width, double &height, double &descent, double &extlead)
{ {
width = height = descent = extlead = 0; width = height = descent = extlead = 0;
double fontsize = style->fontsize; double fontsize = style->fontsize * 64;
double spacing = style->spacing * 64;
#ifdef WIN32 #ifdef WIN32
// This is almost copypasta from TextSub // This is almost copypasta from TextSub
@ -70,7 +71,7 @@ namespace Automation4 {
SetMapMode(thedc, MM_TEXT); SetMapMode(thedc, MM_TEXT);
HDC dczero = GetDC(0); HDC dczero = GetDC(0);
fontsize = -MulDiv((int)(fontsize+0.5), GetDeviceCaps(dczero, LOGPIXELSY), 72); //fontsize = -MulDiv((int)(fontsize+0.5), GetDeviceCaps(dczero, LOGPIXELSY), 72);
ReleaseDC(0, dczero); ReleaseDC(0, dczero);
LOGFONTW lf; LOGFONTW lf;
@ -94,11 +95,11 @@ namespace Automation4 {
SIZE sz; SIZE sz;
size_t thetextlen = text.length(); size_t thetextlen = text.length();
const TCHAR *thetext = text.wc_str(); const TCHAR *thetext = text.wc_str();
if (style->spacing) { if (spacing != 0 ) {
width = 0; width = 0;
for (unsigned int i = 0; i < thetextlen; i++) { for (unsigned int i = 0; i < thetextlen; i++) {
GetTextExtentPoint32(thedc, &thetext[i], 1, &sz); GetTextExtentPoint32(thedc, &thetext[i], 1, &sz);
width += sz.cx + (int)style->spacing; width += sz.cx + spacing;
height = sz.cy; height = sz.cy;
} }
} else { } else {
@ -108,9 +109,9 @@ namespace Automation4 {
} }
// HACKISH FIX! This seems to work, but why? It shouldn't be needed?!? // HACKISH FIX! This seems to work, but why? It shouldn't be needed?!?
fontsize = style->fontsize; //fontsize = style->fontsize;
width = (int)(width * fontsize/height + 0.5); //width = (int)(width * fontsize/height + 0.5);
height = (int)(fontsize + 0.5); //height = (int)(fontsize + 0.5);
TEXTMETRIC tm; TEXTMETRIC tm;
GetTextMetrics(thedc, &tm); GetTextMetrics(thedc, &tm);
@ -124,7 +125,7 @@ namespace Automation4 {
wxMemoryDC thedc; wxMemoryDC thedc;
// fix fontsize to be 72 DPI // fix fontsize to be 72 DPI
fontsize = -FT_MulDiv((int)(fontsize+0.5), 72, thedc.GetPPI().y); //fontsize = -FT_MulDiv((int)(fontsize+0.5), 72, thedc.GetPPI().y);
// now try to get a font! // now try to get a font!
// use the font list to get some caching... (chance is the script will need the same font very often) // use the font list to get some caching... (chance is the script will need the same font very often)
@ -140,13 +141,13 @@ namespace Automation4 {
wxFONTENCODING_SYSTEM); // FIXME! make sure to get the right encoding here, make some translation table between windows and wx encodings wxFONTENCODING_SYSTEM); // FIXME! make sure to get the right encoding here, make some translation table between windows and wx encodings
thedc.SetFont(thefont); thedc.SetFont(thefont);
if (style->spacing) { if (spacing) {
// If there's inter-character spacing, kerning info must not be used, so calculate width per character // If there's inter-character spacing, kerning info must not be used, so calculate width per character
// NOTE: Is kerning actually done either way?! // NOTE: Is kerning actually done either way?!
for (unsigned int i = 0; i < text.length(); i++) { for (unsigned int i = 0; i < text.length(); i++) {
int a, b, c, d; int a, b, c, d;
thedc.GetTextExtent(text[i], &a, &b, &c, &d); thedc.GetTextExtent(text[i], &a, &b, &c, &d);
width += a + (int)style->spacing; width += a + >spacing;
height = b > height ? b : height; height = b > height ? b : height;
descent = c > descent ? c : descent; descent = c > descent ? c : descent;
extlead= d > extlead ? d : extlead; extlead= d > extlead ? d : extlead;
@ -158,10 +159,10 @@ namespace Automation4 {
#endif #endif
// Compensate for scaling // Compensate for scaling
width = (int)(style->scalex / 100 * width + 0.5); width = style->scalex / 100 * width / 64;
height = (int)(style->scaley / 100 * height + 0.5); height = style->scaley / 100 * height /64;
descent = (int)(style->scaley / 100 * descent + 0.5); descent = style->scaley / 100 * descent /64;
extlead = (int)(style->scaley / 100 * extlead + 0.5); extlead = style->scaley / 100 * extlead /64;
return true; return true;
} }

View File

@ -58,7 +58,7 @@ DECLARE_EVENT_TYPE(wxEVT_AUTOMATION_SCRIPT_COMPLETED, -1)
namespace Automation4 { namespace Automation4 {
// Calculate the extents of a text string given a style // Calculate the extents of a text string given a style
bool CalculateTextExtents(AssStyle *style, wxString &text, int &width, int &height, int &descent, int &extlead); bool CalculateTextExtents(AssStyle *style, wxString &text, double &width, double &height, double &descent, double &extlead);
// The class of a Feature... // The class of a Feature...

View File

@ -353,7 +353,7 @@ namespace Automation4 {
wxString text(lua_tostring(L, 2), wxConvUTF8); wxString text(lua_tostring(L, 2), wxConvUTF8);
int width, height, descent, extlead; double width, height, descent, extlead;
if (!CalculateTextExtents(st, text, width, height, descent, extlead)) { if (!CalculateTextExtents(st, text, width, height, descent, extlead)) {
delete st; delete st;
lua_pushstring(L, "Some internal error occurred calculating text_extents"); lua_pushstring(L, "Some internal error occurred calculating text_extents");