Browse Source

Improved calculation of text width and height when using the PostScript graphics context

under X11 or Xft.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@8437 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
pull/49/head
Manolo Gouy 15 years ago
parent
commit
3adac027c4
  1. 11
      src/Fl_PostScript.cxx
  2. 29
      src/fl_font_x.cxx
  3. 24
      src/fl_font_xft.cxx

11
src/Fl_PostScript.cxx

@ -272,8 +272,8 @@ static const char * prolog =
// show at position with desired width // show at position with desired width
// usage: // usage:
// width (string) x y show_pos_width // width (string) x y show_pos_width
"/show_pos_width {GS moveto dup dup stringwidth pop exch length 2 div 1 sub dup 0 eq {pop 1} if " "/show_pos_width {GS moveto dup dup stringwidth pop exch length 2 div dup 2 le {pop 9999} if "
"exch 3 index exch sub exch " "1 sub exch 3 index exch sub exch "
"div 0 2 index 1 -1 scale ashow pop pop GR} bind def\n" // spacing altered to match desired width "div 0 2 index 1 -1 scale ashow pop pop GR} bind def\n" // spacing altered to match desired width
//"/show_pos_width {GS moveto dup stringwidth pop 3 2 roll exch div -1 matrix scale concat " //"/show_pos_width {GS moveto dup stringwidth pop 3 2 roll exch div -1 matrix scale concat "
//"show GR } bind def\n" // horizontally scaled text to match desired width //"show GR } bind def\n" // horizontally scaled text to match desired width
@ -931,16 +931,17 @@ static const char *_fontNames[] = {
}; };
void Fl_PostScript_Graphics_Driver::font(int f, int s) { void Fl_PostScript_Graphics_Driver::font(int f, int s) {
Fl_Display_Device::display_device()->driver()->font(f,s); // Use display fonts for font measurement Fl_Graphics_Driver *driver = Fl_Display_Device::display_device()->driver();
driver->font(f,s); // Use display fonts for font measurement
Fl_Graphics_Driver::font(f, s); Fl_Graphics_Driver::font(f, s);
Fl_Font_Descriptor *desc = driver->font_descriptor();
this->font_descriptor(desc);
if (f < FL_FREE_FONT) { if (f < FL_FREE_FONT) {
int ps_size = s; int ps_size = s;
fprintf(output, "/%s SF\n" , _fontNames[f]); fprintf(output, "/%s SF\n" , _fontNames[f]);
#if defined(USE_X11) && !USE_XFT #if defined(USE_X11) && !USE_XFT
// Non-Xft fonts can have a different size from that required. // Non-Xft fonts can have a different size from that required.
// Give to the PostScript font the same size as that used on the display // Give to the PostScript font the same size as that used on the display
Fl_Font_Descriptor *desc = Fl_Display_Device::display_device()->driver()->font_descriptor();
this->font_descriptor(desc);
char *name = desc->font->font_name_list[0]; char *name = desc->font->font_name_list[0];
char *p = strstr(name, "--"); char *p = strstr(name, "--");
if (p) { if (p) {

29
src/fl_font_x.cxx

@ -278,47 +278,36 @@ void Fl_Xlib_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize size) {
} }
} }
#define current_font (fl_graphics_driver->font_descriptor()->font)
int fl_height() { int fl_height() {
Fl_Graphics_Driver *driver = Fl_Display_Device::display_device()->driver(); if (fl_graphics_driver->font_descriptor()) return current_font->ascent + current_font->descent;
if (driver->font_descriptor()) {
XUtf8FontStruct *font = driver->font_descriptor()->font;
return (font->ascent + font->descent);
}
else return -1; else return -1;
} }
int fl_descent() { int fl_descent() {
Fl_Graphics_Driver *driver = Fl_Display_Device::display_device()->driver(); if (fl_graphics_driver->font_descriptor()) return current_font->descent;
if (driver->font_descriptor()) {
return driver->font_descriptor()->font->descent;
}
else return -1; else return -1;
} }
double fl_width(const char* c, int n) { double fl_width(const char* c, int n) {
Fl_Graphics_Driver *driver = Fl_Display_Device::display_device()->driver(); if (fl_graphics_driver->font_descriptor()) return (double) XUtf8TextWidth(current_font, c, n);
if (driver->font_descriptor())
return (double) XUtf8TextWidth(driver->font_descriptor()->font, c, n);
else return -1; else return -1;
} }
double fl_width(unsigned int c) { double fl_width(unsigned int c) {
Fl_Graphics_Driver *driver = Fl_Display_Device::display_device()->driver(); if (fl_graphics_driver->font_descriptor()) return (double) XUtf8UcsWidth(current_font, c);
if (driver->font_descriptor())
return (double) XUtf8UcsWidth(driver->font_descriptor()->font, c);
else return -1; else return -1;
} }
void fl_text_extents(const char *c, int n, int &dx, int &dy, int &W, int &H) { void fl_text_extents(const char *c, int n, int &dx, int &dy, int &W, int &H) {
Fl_Graphics_Driver *driver = Fl_Display_Device::display_device()->driver();
if (font_gc != fl_gc) { if (font_gc != fl_gc) {
if (!driver->font_descriptor()) driver->font(FL_HELVETICA, FL_NORMAL_SIZE); if (!fl_graphics_driver->font_descriptor()) fl_font(FL_HELVETICA, FL_NORMAL_SIZE);
font_gc = fl_gc; font_gc = fl_gc;
XSetFont(fl_display, fl_gc, driver->font_descriptor()->font->fid); XSetFont(fl_display, fl_gc, current_font->fid);
} }
int xx, yy, ww, hh; int xx, yy, ww, hh;
xx = yy = ww = hh = 0; xx = yy = ww = hh = 0;
if (fl_gc) XUtf8_measure_extents(fl_display, fl_window, driver->font_descriptor()->font, fl_gc, &xx, &yy, &ww, &hh, c, n); if (fl_gc) XUtf8_measure_extents(fl_display, fl_window, current_font, fl_gc, &xx, &yy, &ww, &hh, c, n);
W = ww; H = hh; dx = xx; dy = yy; W = ww; H = hh; dx = xx; dy = yy;
// This is the safe but mostly wrong thing we used to do... // This is the safe but mostly wrong thing we used to do...
@ -326,7 +315,7 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &W, int &H) {
// fl_measure(c, W, H, 0); // fl_measure(c, W, H, 0);
// dx = 0; // dx = 0;
// dy = fl_descent() - H; // dy = fl_descent() - H;
} // fl_text_extents }
void Fl_Xlib_Graphics_Driver::draw(const char* c, int n, int x, int y) { void Fl_Xlib_Graphics_Driver::draw(const char* c, int n, int x, int y) {
if (font_gc != fl_gc) { if (font_gc != fl_gc) {

24
src/fl_font_xft.cxx

@ -115,15 +115,15 @@ void *fl_xftfont = 0;
//const char* fl_encoding_ = "iso8859-1"; //const char* fl_encoding_ = "iso8859-1";
const char* fl_encoding_ = "iso10646-1"; const char* fl_encoding_ = "iso10646-1";
static void fl_font(Fl_Font fnum, Fl_Fontsize size, int angle) { static void fl_font(Fl_Xlib_Graphics_Driver *driver, Fl_Font fnum, Fl_Fontsize size, int angle) {
if (fnum==-1) { // special case to stop font caching if (fnum==-1) { // special case to stop font caching
fl_graphics_driver->Fl_Graphics_Driver::font(0, 0); driver->Fl_Graphics_Driver::font(0, 0);
return; return;
} }
Fl_Font_Descriptor* f = fl_graphics_driver->font_descriptor(); Fl_Font_Descriptor* f = driver->font_descriptor();
if (fnum == fl_graphics_driver->font() && size == fl_graphics_driver->size() && f && f->angle == angle) if (fnum == driver->Fl_Graphics_Driver::font() && size == driver->size() && f && f->angle == angle)
return; return;
fl_graphics_driver->Fl_Graphics_Driver::font(fnum, size); driver->Fl_Graphics_Driver::font(fnum, size);
Fl_Fontdesc *font = fl_fonts + fnum; Fl_Fontdesc *font = fl_fonts + fnum;
// search the fontsizes we have generated already // search the fontsizes we have generated already
for (f = font->first; f; f = f->next) { for (f = font->first; f; f = f->next) {
@ -135,7 +135,7 @@ static void fl_font(Fl_Font fnum, Fl_Fontsize size, int angle) {
f->next = font->first; f->next = font->first;
font->first = f; font->first = f;
} }
fl_graphics_driver->font_descriptor(f); driver->font_descriptor(f);
#if XFT_MAJOR < 2 #if XFT_MAJOR < 2
fl_xfont = f->font->u.core.font; fl_xfont = f->font->u.core.font;
#else #else
@ -145,7 +145,7 @@ static void fl_font(Fl_Font fnum, Fl_Fontsize size, int angle) {
} }
void Fl_Xlib_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize size) { void Fl_Xlib_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize size) {
fl_font(fnum,size,0); fl_font(this,fnum,size,0);
} }
static XftFont* fontopen(const char* name, bool core, int angle) { static XftFont* fontopen(const char* name, bool core, int angle) {
@ -580,8 +580,8 @@ void fl_destroy_xft_draw(Window id) {
} }
void Fl_Xlib_Graphics_Driver::draw(const char *str, int n, int x, int y) { void Fl_Xlib_Graphics_Driver::draw(const char *str, int n, int x, int y) {
if ( !fl_graphics_driver->font_descriptor() ) { if ( !this->font_descriptor() ) {
fl_font(FL_HELVETICA, FL_NORMAL_SIZE); this->font(FL_HELVETICA, FL_NORMAL_SIZE);
} }
#if USE_OVERLAY #if USE_OVERLAY
XftDraw*& draw_ = fl_overlay ? draw_overlay : ::draw_; XftDraw*& draw_ = fl_overlay ? draw_overlay : ::draw_;
@ -622,9 +622,9 @@ void Fl_Xlib_Graphics_Driver::draw(const char *str, int n, int x, int y) {
} }
void Fl_Xlib_Graphics_Driver::draw(int angle, const char *str, int n, int x, int y) { void Fl_Xlib_Graphics_Driver::draw(int angle, const char *str, int n, int x, int y) {
fl_font(fl_graphics_driver->font(), fl_graphics_driver->size(), angle); fl_font(this, this->Fl_Graphics_Driver::font(), this->size(), angle);
fl_draw(str, n, (int)x, (int)y); this->draw(str, n, (int)x, (int)y);
fl_font(fl_graphics_driver->font(), fl_graphics_driver->size()); this->font(this->Fl_Graphics_Driver::font(), this->size());
} }
static void fl_drawUCS4(const FcChar32 *str, int n, int x, int y) { static void fl_drawUCS4(const FcChar32 *str, int n, int x, int y) {

Loading…
Cancel
Save