2001-11-27 20:44:08 +03:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
2001-11-27 20:44:08 +03:00
|
|
|
//
|
|
|
|
// MacOS font selection routines for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2009-01-02 00:28:26 +03:00
|
|
|
// Copyright 1998-2009 by Bill Spitzak and others.
|
2001-11-27 20:44:08 +03:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2001-11-27 20:44:08 +03:00
|
|
|
//
|
|
|
|
|
2004-08-25 04:20:27 +04:00
|
|
|
#include <config.h>
|
|
|
|
|
2008-09-11 03:56:49 +04:00
|
|
|
/* from fl_utf.c */
|
|
|
|
extern unsigned fl_utf8toUtf16(const char* src, unsigned srclen, unsigned short* dst, unsigned dstlen);
|
|
|
|
|
2008-10-06 20:40:42 +04:00
|
|
|
// if no font has been selected yet by the user, get one.
|
|
|
|
#define check_default_font() {if (!fl_fontsize) fl_font(0, 12);}
|
|
|
|
|
2009-12-07 01:21:55 +03:00
|
|
|
static const CGAffineTransform font_mx = { 1, 0, 0, -1, 0, 0 };
|
2010-01-24 11:55:41 +03:00
|
|
|
static SInt32 MACsystemVersion = 0;
|
2009-12-07 01:21:55 +03:00
|
|
|
|
2008-08-16 01:18:27 +04:00
|
|
|
Fl_Font_Descriptor::Fl_Font_Descriptor(const char* name, Fl_Fontsize Size) {
|
2004-09-10 01:34:48 +04:00
|
|
|
next = 0;
|
|
|
|
# if HAVE_GL
|
|
|
|
listbase = 0;
|
|
|
|
# endif
|
2008-10-14 03:10:43 +04:00
|
|
|
|
2008-09-11 03:56:49 +04:00
|
|
|
// knowWidths = 0;
|
2006-06-02 18:17:41 +04:00
|
|
|
// OpenGL needs those for its font handling
|
2004-09-09 04:55:41 +04:00
|
|
|
q_name = strdup(name);
|
2004-08-26 04:18:43 +04:00
|
|
|
size = Size;
|
2009-12-07 01:21:55 +03:00
|
|
|
minsize = maxsize = Size;
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2010-01-24 11:55:41 +03:00
|
|
|
if(MACsystemVersion == 0) Gestalt(gestaltSystemVersion, &MACsystemVersion);
|
|
|
|
|
|
|
|
if(MACsystemVersion >= 0x1050) {//unfortunately, CTFontCreateWithName != NULL on 10.4 also!
|
2009-12-07 01:21:55 +03:00
|
|
|
CFStringRef str = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
|
|
|
|
fontref = CTFontCreateWithName(str, size, NULL);
|
|
|
|
CGGlyph glyph[2];
|
|
|
|
const UniChar A[2]={'W','.'};
|
|
|
|
CTFontGetGlyphsForCharacters(fontref, A, glyph, 2);
|
|
|
|
CGSize advances[2];
|
|
|
|
double w;
|
|
|
|
CTFontGetAdvancesForGlyphs(fontref, kCTFontHorizontalOrientation, glyph, advances, 2);
|
|
|
|
w = advances[0].width;
|
|
|
|
if( abs(advances[0].width - advances[1].width) < 1E-2 ) {//this is a fixed-width font
|
2010-01-24 11:55:41 +03:00
|
|
|
//slightly rescale fixed-width fonts so the character width has an integral value
|
|
|
|
CFRelease(fontref);
|
|
|
|
CGFloat fsize = size / ( w/floor(w + 0.5) );
|
|
|
|
fontref = CTFontCreateWithName(str, fsize, NULL);
|
|
|
|
w = CTFontGetAdvancesForGlyphs(fontref, kCTFontHorizontalOrientation, glyph, NULL, 1);
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
|
|
|
CFRelease(str);
|
|
|
|
ascent = (short)(CTFontGetAscent(fontref) + 0.5);
|
|
|
|
descent = (short)(CTFontGetDescent(fontref) + 0.5);
|
|
|
|
q_width = w + 0.5;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif
|
|
|
|
#if ! __LP64__
|
2006-06-02 18:17:41 +04:00
|
|
|
OSStatus err;
|
|
|
|
// fill our structure with a few default values
|
2004-08-26 04:18:43 +04:00
|
|
|
ascent = Size*3/4;
|
|
|
|
descent = Size-ascent;
|
|
|
|
q_width = Size*2/3;
|
2009-12-07 01:21:55 +03:00
|
|
|
// now use ATS to get the actual Glyph size information
|
2008-09-11 03:56:49 +04:00
|
|
|
// say that our passed-in name is encoded as UTF-8, since this works for plain ASCII names too...
|
|
|
|
CFStringRef cfname = CFStringCreateWithCString(0L, name, kCFStringEncodingUTF8);
|
2004-09-09 04:55:41 +04:00
|
|
|
ATSFontRef font = ATSFontFindFromName(cfname, kATSOptionFlagsDefault);
|
|
|
|
if (font) {
|
|
|
|
ATSFontMetrics m = { 0 };
|
|
|
|
ATSFontGetHorizontalMetrics(font, kATSOptionFlagsDefault, &m);
|
2006-06-02 18:17:41 +04:00
|
|
|
if (m.avgAdvanceWidth) q_width = int(m.avgAdvanceWidth*Size);
|
|
|
|
// playing with the offsets a little to make standard sizes fit
|
|
|
|
if (m.ascent) ascent = int(m.ascent*Size-0.5f);
|
|
|
|
if (m.descent) descent = -int(m.descent*Size-1.5f);
|
2004-09-09 04:55:41 +04:00
|
|
|
}
|
|
|
|
CFRelease(cfname);
|
2006-06-02 18:17:41 +04:00
|
|
|
// now we allocate everything needed to render text in this font later
|
|
|
|
// get us the default layout and style
|
|
|
|
err = ATSUCreateTextLayout(&layout);
|
|
|
|
UniChar mTxt[2] = { 65, 0 };
|
|
|
|
err = ATSUSetTextPointerLocation(layout, mTxt, kATSUFromTextBeginning, 1, 1);
|
|
|
|
err = ATSUCreateStyle(&style);
|
|
|
|
err = ATSUSetRunStyle(layout, style, kATSUFromTextBeginning, kATSUToTextEnd);
|
2008-09-11 03:56:49 +04:00
|
|
|
// now set the actual font, size and attributes. We also set the font matrix to
|
2006-06-05 13:01:29 +04:00
|
|
|
// render our font up-side-down, so when rendered through our inverted CGContext,
|
|
|
|
// text will appear normal again.
|
2008-09-11 03:56:49 +04:00
|
|
|
Fixed fsize = IntToFixed(Size);
|
|
|
|
// ATSUFontID fontID = FMGetFontFromATSFontRef(font);
|
|
|
|
ATSUFontID fontID;
|
|
|
|
ATSUFindFontFromName(name, strlen(name), kFontFullName, kFontMacintoshPlatform, kFontRomanScript, kFontEnglishLanguage, &fontID);
|
|
|
|
|
|
|
|
// draw the font upside-down... Compensate for fltk/OSX origin differences
|
2006-06-05 13:01:29 +04:00
|
|
|
static CGAffineTransform font_mx = { 1, 0, 0, -1, 0, 0 };
|
|
|
|
ATSUAttributeTag sTag[] = { kATSUFontTag, kATSUSizeTag, kATSUFontMatrixTag };
|
|
|
|
ByteCount sBytes[] = { sizeof(ATSUFontID), sizeof(Fixed), sizeof(CGAffineTransform) };
|
|
|
|
ATSUAttributeValuePtr sAttr[] = { &fontID, &fsize, &font_mx };
|
|
|
|
err = ATSUSetAttributes(style, 3, sTag, sBytes, sAttr);
|
2006-06-02 18:17:41 +04:00
|
|
|
// next, make sure that Quartz will only render at integer coordinates
|
2008-09-11 03:56:49 +04:00
|
|
|
ATSLineLayoutOptions llo = kATSLineUseDeviceMetrics | kATSLineDisableAllLayoutOperations;
|
2006-06-02 18:17:41 +04:00
|
|
|
ATSUAttributeTag aTag[] = { kATSULineLayoutOptionsTag };
|
|
|
|
ByteCount aBytes[] = { sizeof(ATSLineLayoutOptions) };
|
|
|
|
ATSUAttributeValuePtr aAttr[] = { &llo };
|
|
|
|
err = ATSUSetLineControls (layout, kATSUFromTextBeginning, 1, aTag, aBytes, aAttr);
|
|
|
|
// now we are finally ready to measure some letter to get the bounding box
|
|
|
|
Fixed bBefore, bAfter, bAscent, bDescent;
|
|
|
|
err = ATSUGetUnjustifiedBounds(layout, kATSUFromTextBeginning, 1, &bBefore, &bAfter, &bAscent, &bDescent);
|
2006-06-05 13:01:29 +04:00
|
|
|
// Requesting a certain height font on Mac does not guarantee that ascent+descent
|
2006-08-19 19:24:55 +04:00
|
|
|
// equal the requested height. fl_height will reflect the actual height that we got.
|
2006-06-05 13:01:29 +04:00
|
|
|
// The font "Apple Chancery" is a pretty extreme example of overlapping letters.
|
|
|
|
float fa = -FixedToFloat(bAscent), fd = -FixedToFloat(bDescent);
|
|
|
|
if (fa>0.0f && fd>0.0f) {
|
2006-08-30 14:01:35 +04:00
|
|
|
//float f = Size/(fa+fd);
|
2006-08-19 19:24:55 +04:00
|
|
|
ascent = fa; //int(fa*f+0.5f);
|
|
|
|
descent = fd; //Size - ascent;
|
2006-06-05 13:01:29 +04:00
|
|
|
}
|
|
|
|
int w = FixedToInt(bAfter);
|
2008-09-11 03:56:49 +04:00
|
|
|
if (w)
|
2006-06-05 13:01:29 +04:00
|
|
|
q_width = FixedToInt(bAfter);
|
2008-09-11 03:56:49 +04:00
|
|
|
|
2008-09-30 22:06:44 +04:00
|
|
|
# define ENABLE_TRANSIENT_FONTS 1
|
2008-09-11 03:56:49 +04:00
|
|
|
|
2008-09-30 22:06:44 +04:00
|
|
|
# ifdef ENABLE_TRANSIENT_FONTS
|
2008-09-11 03:56:49 +04:00
|
|
|
// Now, by way of experiment, try enabling Transient Font Matching, this will
|
|
|
|
// cause ATSU to find a suitable font to render any chars the current font can't do...
|
|
|
|
ATSUSetTransientFontMatching (layout, true);
|
2008-09-30 22:06:44 +04:00
|
|
|
# endif
|
2009-12-07 01:21:55 +03:00
|
|
|
#endif//__LP64__
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2001-11-27 20:44:08 +03:00
|
|
|
}
|
|
|
|
|
2008-08-16 01:18:27 +04:00
|
|
|
Fl_Font_Descriptor* fl_fontsize = 0L;
|
2001-11-27 20:44:08 +03:00
|
|
|
|
2008-08-16 01:18:27 +04:00
|
|
|
Fl_Font_Descriptor::~Fl_Font_Descriptor() {
|
2001-11-27 20:44:08 +03:00
|
|
|
/*
|
|
|
|
#if HAVE_GL
|
2002-06-27 08:29:39 +04:00
|
|
|
// ++ todo: remove OpenGL font alocations
|
2001-11-27 20:44:08 +03:00
|
|
|
// Delete list created by gl_draw(). This is not done by this code
|
|
|
|
// as it will link in GL unnecessarily. There should be some kind
|
|
|
|
// of "free" routine pointer, or a subclass?
|
|
|
|
// if (listbase) {
|
|
|
|
// int base = font->min_char_or_byte2;
|
|
|
|
// int size = font->max_char_or_byte2-base+1;
|
|
|
|
// int base = 0; int size = 256;
|
|
|
|
// glDeleteLists(listbase+base,size);
|
|
|
|
// }
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
if (this == fl_fontsize) fl_fontsize = 0;
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2010-01-24 11:55:41 +03:00
|
|
|
if(MACsystemVersion >= 0x1050) CFRelease(fontref);
|
2009-12-07 01:21:55 +03:00
|
|
|
#else
|
|
|
|
/* ATSUDisposeTextLayout(layout);
|
|
|
|
ATSUDisposeStyle(style); */
|
|
|
|
#endif
|
2001-11-27 20:44:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static Fl_Fontdesc built_in_table[] = {
|
2004-09-09 04:55:41 +04:00
|
|
|
{"Arial"},
|
|
|
|
{"Arial Bold"},
|
|
|
|
{"Arial Italic"},
|
|
|
|
{"Arial Bold Italic"},
|
|
|
|
{"Courier New"},
|
|
|
|
{"Courier New Bold"},
|
|
|
|
{"Courier New Italic"},
|
|
|
|
{"Courier New Bold Italic"},
|
|
|
|
{"Times New Roman"},
|
|
|
|
{"Times New Roman Bold"},
|
|
|
|
{"Times New Roman Italic"},
|
|
|
|
{"Times New Roman Bold Italic"},
|
|
|
|
{"Symbol"},
|
|
|
|
{"Monaco"},
|
|
|
|
{"Andale Mono"}, // there is no bold Monaco font on standard Mac
|
|
|
|
{"Webdings"},
|
2001-11-27 20:44:08 +03:00
|
|
|
};
|
|
|
|
|
2008-09-11 03:56:49 +04:00
|
|
|
static UniChar *utfWbuf = 0;
|
|
|
|
static unsigned utfWlen = 0;
|
|
|
|
|
|
|
|
static UniChar *mac_Utf8_to_Utf16(const char *txt, int len, int *new_len)
|
2006-06-02 19:01:32 +04:00
|
|
|
{
|
2008-09-11 03:56:49 +04:00
|
|
|
unsigned wlen = fl_utf8toUtf16(txt, len, (unsigned short*)utfWbuf, utfWlen);
|
|
|
|
if(wlen >= utfWlen)
|
|
|
|
{
|
|
|
|
utfWlen = wlen + 100;
|
|
|
|
if(utfWbuf) free(utfWbuf);
|
|
|
|
utfWbuf = (UniChar*)malloc((utfWlen)*sizeof(UniChar));
|
|
|
|
wlen = fl_utf8toUtf16(txt, len, (unsigned short*)utfWbuf, utfWlen);
|
2006-06-02 19:01:32 +04:00
|
|
|
}
|
2008-09-11 03:56:49 +04:00
|
|
|
*new_len = wlen;
|
|
|
|
return utfWbuf;
|
|
|
|
} // mac_Utf8_to_Utf16
|
2006-06-02 19:01:32 +04:00
|
|
|
|
2001-11-27 20:44:08 +03:00
|
|
|
Fl_Fontdesc* fl_fonts = built_in_table;
|
|
|
|
|
2008-08-16 01:18:27 +04:00
|
|
|
void fl_font(Fl_Font_Descriptor* s) {
|
2001-11-27 20:44:08 +03:00
|
|
|
fl_fontsize = s;
|
2006-06-02 18:17:41 +04:00
|
|
|
// we will use fl_fontsize later to access the required style and layout
|
2001-11-27 20:44:08 +03:00
|
|
|
}
|
|
|
|
|
2008-08-16 01:18:27 +04:00
|
|
|
static Fl_Font_Descriptor* find(Fl_Font fnum, Fl_Fontsize size) {
|
2001-11-27 20:44:08 +03:00
|
|
|
Fl_Fontdesc* s = fl_fonts+fnum;
|
|
|
|
if (!s->name) s = fl_fonts; // use 0 if fnum undefined
|
2008-08-16 01:18:27 +04:00
|
|
|
Fl_Font_Descriptor* f;
|
2001-11-27 20:44:08 +03:00
|
|
|
for (f = s->first; f; f = f->next)
|
|
|
|
if (f->minsize <= size && f->maxsize >= size) return f;
|
2008-08-16 01:18:27 +04:00
|
|
|
f = new Fl_Font_Descriptor(s->name, size);
|
2001-11-27 20:44:08 +03:00
|
|
|
f->next = s->first;
|
|
|
|
s->first = f;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
// Public interface:
|
|
|
|
|
2008-04-23 23:09:28 +04:00
|
|
|
Fl_Font fl_font_ = 0;
|
2008-08-16 01:11:21 +04:00
|
|
|
Fl_Fontsize fl_size_ = 0;
|
2003-04-06 16:54:57 +04:00
|
|
|
|
2008-10-06 20:40:42 +04:00
|
|
|
|
2010-05-27 21:20:18 +04:00
|
|
|
void Fl_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize size) {
|
2006-09-05 15:26:41 +04:00
|
|
|
if (fnum==-1) {
|
|
|
|
fl_font_ = 0;
|
|
|
|
fl_size_ = 0;
|
|
|
|
return;
|
|
|
|
}
|
2003-04-06 16:54:57 +04:00
|
|
|
fl_font_ = fnum;
|
|
|
|
fl_size_ = size;
|
2001-11-27 20:44:08 +03:00
|
|
|
fl_font(find(fnum, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
int fl_height() {
|
2009-01-17 18:58:11 +03:00
|
|
|
check_default_font();
|
2004-09-08 00:59:18 +04:00
|
|
|
if (fl_fontsize) return fl_fontsize->ascent+fl_fontsize->descent;
|
|
|
|
else return -1;
|
2001-11-27 20:44:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int fl_descent() {
|
2009-01-17 18:58:11 +03:00
|
|
|
check_default_font();
|
|
|
|
if (fl_fontsize)
|
|
|
|
return fl_fontsize->descent+1;
|
2004-09-08 00:59:18 +04:00
|
|
|
else return -1;
|
2001-11-27 20:44:08 +03:00
|
|
|
}
|
|
|
|
|
2008-09-11 03:56:49 +04:00
|
|
|
double fl_width(const UniChar* txt, int n) {
|
2009-01-17 18:58:11 +03:00
|
|
|
check_default_font();
|
2006-06-06 19:28:20 +04:00
|
|
|
if (!fl_fontsize) {
|
2008-10-06 20:40:42 +04:00
|
|
|
check_default_font(); // avoid a crash!
|
2006-06-06 19:28:20 +04:00
|
|
|
if (!fl_fontsize)
|
2006-08-19 19:24:55 +04:00
|
|
|
return 8*n; // user must select a font first!
|
2006-06-06 19:28:20 +04:00
|
|
|
}
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2010-01-24 11:55:41 +03:00
|
|
|
if(MACsystemVersion >= 0x1050) {
|
2009-12-07 01:21:55 +03:00
|
|
|
CTFontRef fontref = fl_fontsize->fontref;
|
|
|
|
CFStringRef str = CFStringCreateWithBytes(NULL, (const UInt8*)txt, n * sizeof(UniChar), kCFStringEncodingUTF16, false);
|
|
|
|
CFAttributedStringRef astr = CFAttributedStringCreate(NULL, str, NULL);
|
|
|
|
CFMutableAttributedStringRef mastr = CFAttributedStringCreateMutableCopy(NULL, 0, astr);
|
|
|
|
CFRelease(astr);
|
|
|
|
CFAttributedStringSetAttribute(mastr, CFRangeMake(0, CFStringGetLength(str)), kCTFontAttributeName, fontref);
|
|
|
|
CFRelease(str);
|
|
|
|
CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
|
|
|
|
CFRelease(mastr);
|
|
|
|
double retval = CTLineGetTypographicBounds(ctline, NULL, NULL, NULL);
|
|
|
|
CFRelease(ctline);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif
|
|
|
|
#if ! __LP64__
|
|
|
|
OSStatus err;
|
2008-09-11 03:56:49 +04:00
|
|
|
Fixed bBefore, bAfter, bAscent, bDescent;
|
|
|
|
ATSUTextLayout layout;
|
|
|
|
ByteCount iSize;
|
|
|
|
ATSUAttributeTag iTag;
|
|
|
|
ATSUAttributeValuePtr iValuePtr;
|
|
|
|
|
|
|
|
// Here's my ATSU text measuring attempt... This seems to do the Right Thing
|
|
|
|
// now collect our ATSU resources and measure our text string
|
|
|
|
layout = fl_fontsize->layout;
|
2006-08-19 19:24:55 +04:00
|
|
|
// activate the current GC
|
2008-09-11 03:56:49 +04:00
|
|
|
iSize = sizeof(CGContextRef);
|
|
|
|
iTag = kATSUCGContextTag;
|
|
|
|
iValuePtr = &fl_gc;
|
2006-08-19 19:24:55 +04:00
|
|
|
ATSUSetLayoutControls(layout, 1, &iTag, &iSize, &iValuePtr);
|
|
|
|
// now measure the bounding box
|
2008-09-11 03:56:49 +04:00
|
|
|
err = ATSUSetTextPointerLocation(layout, txt, kATSUFromTextBeginning, n, n);
|
|
|
|
err = ATSUGetUnjustifiedBounds(layout, kATSUFromTextBeginning, n, &bBefore, &bAfter, &bAscent, &bDescent);
|
|
|
|
// If err is OK then return length, else return 0. Or something...
|
|
|
|
int len = FixedToInt(bAfter);
|
2006-08-19 19:24:55 +04:00
|
|
|
return len;
|
2009-12-07 01:21:55 +03:00
|
|
|
#endif
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2010-02-26 02:29:42 +03:00
|
|
|
return 0; // FIXME: I do not understand the shuffeling of the above ifdef's and why they are here!
|
2008-09-11 03:56:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double fl_width(const char* txt, int n) {
|
|
|
|
int wc_len = n;
|
|
|
|
UniChar *uniStr = mac_Utf8_to_Utf16(txt, n, &wc_len);
|
|
|
|
return fl_width(uniStr, wc_len);
|
2004-09-10 01:34:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double fl_width(uchar c) {
|
|
|
|
return fl_width((const char*)(&c), 1);
|
|
|
|
}
|
2003-05-21 17:23:40 +04:00
|
|
|
|
2008-09-11 03:56:49 +04:00
|
|
|
double fl_width(unsigned int wc) {
|
|
|
|
return fl_width((const UniChar*)(&wc), 1);
|
|
|
|
}
|
|
|
|
|
2008-11-19 23:34:27 +03:00
|
|
|
// text extent calculation
|
|
|
|
void fl_text_extents(const UniChar* txt, int n, int &dx, int &dy, int &w, int &h) {
|
|
|
|
if (!fl_fontsize) {
|
|
|
|
check_default_font(); // avoid a crash!
|
|
|
|
if (!fl_fontsize)
|
|
|
|
w = 8.0 * n; // user must select a font first!
|
|
|
|
h = 8.0;
|
|
|
|
return;
|
|
|
|
}
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2010-01-24 11:55:41 +03:00
|
|
|
if(MACsystemVersion >= 0x1050) {
|
2009-12-07 01:21:55 +03:00
|
|
|
CTFontRef fontref = fl_fontsize->fontref;
|
|
|
|
CFStringRef str16 = CFStringCreateWithBytes(NULL, (const UInt8*)txt, n *sizeof(UniChar), kCFStringEncodingUTF16, false);
|
|
|
|
CFAttributedStringRef astr = CFAttributedStringCreate(NULL, str16, NULL);
|
|
|
|
CFMutableAttributedStringRef mastr = CFAttributedStringCreateMutableCopy(NULL, 0, astr);
|
|
|
|
CFRelease(astr);
|
|
|
|
CFAttributedStringSetAttribute(mastr, CFRangeMake(0, CFStringGetLength(str16)), kCTFontAttributeName, fontref);
|
|
|
|
CFRelease(str16);
|
|
|
|
CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
|
|
|
|
CFRelease(mastr);
|
|
|
|
CGContextSetTextPosition(fl_gc, 0, 0);
|
|
|
|
CGContextSetShouldAntialias(fl_gc, true);
|
|
|
|
CGRect rect = CTLineGetImageBounds(ctline, fl_gc);
|
|
|
|
CGContextSetShouldAntialias(fl_gc, false);
|
|
|
|
CFRelease(ctline);
|
|
|
|
dx = floor(rect.origin.x + 0.5);
|
|
|
|
dy = floor(- rect.origin.y - rect.size.height + 0.5);
|
|
|
|
w = rect.size.width + 0.5;
|
|
|
|
h = rect.size.height + 0.5;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif
|
|
|
|
#if ! __LP64__
|
2008-11-19 23:34:27 +03:00
|
|
|
OSStatus err;
|
|
|
|
ATSUTextLayout layout;
|
|
|
|
ByteCount iSize;
|
|
|
|
ATSUAttributeTag iTag;
|
|
|
|
ATSUAttributeValuePtr iValuePtr;
|
|
|
|
|
|
|
|
// Here's my ATSU text measuring attempt... This seems to do the Right Thing
|
|
|
|
// now collect our ATSU resources and measure our text string
|
|
|
|
layout = fl_fontsize->layout;
|
|
|
|
// activate the current GC
|
|
|
|
iSize = sizeof(CGContextRef);
|
|
|
|
iTag = kATSUCGContextTag;
|
|
|
|
iValuePtr = &fl_gc;
|
|
|
|
ATSUSetLayoutControls(layout, 1, &iTag, &iSize, &iValuePtr);
|
|
|
|
// now measure the bounding box
|
|
|
|
err = ATSUSetTextPointerLocation(layout, txt, kATSUFromTextBeginning, n, n);
|
|
|
|
Rect bbox;
|
|
|
|
err = ATSUMeasureTextImage(layout, kATSUFromTextBeginning, n, 0, 0, &bbox);
|
|
|
|
w = bbox.right - bbox.left;
|
|
|
|
h = bbox.bottom - bbox.top;
|
|
|
|
dx = bbox.left;
|
|
|
|
dy = -bbox.bottom;
|
|
|
|
//printf("r: %d l: %d t: %d b: %d w: %d h: %d\n", bbox.right, bbox.left, bbox.top, bbox.bottom, w, h);
|
2009-12-07 01:21:55 +03:00
|
|
|
#endif
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-19 23:34:27 +03:00
|
|
|
return;
|
|
|
|
} // fl_text_extents
|
|
|
|
|
|
|
|
void fl_text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) {
|
|
|
|
int wc_len = n;
|
|
|
|
UniChar *uniStr = mac_Utf8_to_Utf16(c, n, &wc_len);
|
|
|
|
fl_text_extents(uniStr, wc_len, dx, dy, w, h);
|
|
|
|
} // fl_text_extents
|
|
|
|
|
|
|
|
|
2004-09-10 01:34:48 +04:00
|
|
|
void fl_draw(const char *str, int n, float x, float y);
|
2003-05-21 17:23:40 +04:00
|
|
|
|
2010-05-27 21:20:18 +04:00
|
|
|
void Fl_Graphics_Driver::draw(const char* str, int n, int x, int y) {
|
2010-03-29 18:10:54 +04:00
|
|
|
fl_draw(str, n, (float)x-0.0f, (float)y+0.5f);
|
2001-11-27 20:44:08 +03:00
|
|
|
}
|
|
|
|
|
2009-12-07 01:21:55 +03:00
|
|
|
|
2010-01-24 11:55:41 +03:00
|
|
|
static CGColorRef flcolortocgcolor(Fl_Color i)
|
2009-12-07 01:21:55 +03:00
|
|
|
{
|
|
|
|
uchar r, g, b;
|
2010-01-24 11:55:41 +03:00
|
|
|
Fl::get_color(i, r, g, b);
|
2009-12-13 15:03:26 +03:00
|
|
|
CGFloat components[4] = {r/255.0f, g/255.0f, b/255.0f, 1.};
|
2010-01-24 11:55:41 +03:00
|
|
|
static CGColorSpaceRef cspace = NULL;
|
|
|
|
if(cspace == NULL) {
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
|
|
|
|
cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
|
2009-12-07 01:21:55 +03:00
|
|
|
#else
|
2010-01-24 11:55:41 +03:00
|
|
|
cspace = CGColorSpaceCreateWithName(kCGColorSpaceUserRGB);
|
2009-12-07 01:21:55 +03:00
|
|
|
#endif
|
2010-01-24 11:55:41 +03:00
|
|
|
}
|
|
|
|
return CGColorCreate(cspace, components);
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
|
|
|
|
2004-09-10 01:34:48 +04:00
|
|
|
void fl_draw(const char *str, int n, float x, float y) {
|
2008-10-06 20:40:42 +04:00
|
|
|
|
2010-06-25 01:55:20 +04:00
|
|
|
if(fl_device->type() != Fl_Quartz_Graphics_Driver::device_type) {
|
|
|
|
fl_device->draw(str, n, (int)x, (int)y );
|
|
|
|
return;
|
|
|
|
}
|
2008-10-06 20:40:42 +04:00
|
|
|
// avoid a crash if no font has been selected by user yet !
|
|
|
|
check_default_font();
|
2009-12-07 01:21:55 +03:00
|
|
|
// convert to UTF-16 first
|
|
|
|
UniChar *uniStr = mac_Utf8_to_Utf16(str, n, &n);
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2010-01-24 11:55:41 +03:00
|
|
|
if(MACsystemVersion >= 0x1050) {
|
2009-12-13 15:03:26 +03:00
|
|
|
CFStringRef keys[2];
|
|
|
|
CFTypeRef values[2];
|
|
|
|
CFStringRef str16 = CFStringCreateWithBytes(NULL, (const UInt8*)uniStr, n * sizeof(UniChar), kCFStringEncodingUTF16, false);
|
|
|
|
CGColorRef color = flcolortocgcolor(fl_color());
|
|
|
|
keys[0] = kCTFontAttributeName;
|
|
|
|
keys[1] = kCTForegroundColorAttributeName;
|
|
|
|
values[0] = fl_fontsize->fontref;
|
|
|
|
values[1] = color;
|
|
|
|
CFDictionaryRef attributes = CFDictionaryCreate(kCFAllocatorDefault,
|
|
|
|
(const void**)&keys,
|
|
|
|
(const void**)&values,
|
|
|
|
2,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, str16, attributes);
|
|
|
|
CFRelease(str16);
|
|
|
|
CFRelease(attributes);
|
|
|
|
CFRelease(color);
|
|
|
|
CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
|
|
|
|
CFRelease(mastr);
|
|
|
|
CGContextSetTextMatrix(fl_gc, font_mx);
|
|
|
|
CGContextSetTextPosition(fl_gc, x, y);
|
|
|
|
CGContextSetShouldAntialias(fl_gc, true);
|
|
|
|
CTLineDraw(ctline, fl_gc);
|
|
|
|
CGContextSetShouldAntialias(fl_gc, false);
|
|
|
|
CFRelease(ctline);
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
2009-12-13 15:03:26 +03:00
|
|
|
else {
|
2009-12-07 01:21:55 +03:00
|
|
|
#endif
|
|
|
|
#if ! __LP64__
|
|
|
|
OSStatus err;
|
2008-10-06 20:40:42 +04:00
|
|
|
// now collect our ATSU resources
|
2006-06-02 18:17:41 +04:00
|
|
|
ATSUTextLayout layout = fl_fontsize->layout;
|
2006-06-05 13:01:29 +04:00
|
|
|
|
|
|
|
ByteCount iSize = sizeof(CGContextRef);
|
|
|
|
ATSUAttributeTag iTag = kATSUCGContextTag;
|
|
|
|
ATSUAttributeValuePtr iValuePtr=&fl_gc;
|
|
|
|
ATSUSetLayoutControls(layout, 1, &iTag, &iSize, &iValuePtr);
|
2009-12-12 21:26:12 +03:00
|
|
|
|
2006-06-02 18:17:41 +04:00
|
|
|
err = ATSUSetTextPointerLocation(layout, uniStr, kATSUFromTextBeginning, n, n);
|
2009-12-12 21:10:02 +03:00
|
|
|
CGContextSetShouldAntialias(fl_gc, true);
|
2006-06-02 18:17:41 +04:00
|
|
|
err = ATSUDrawText(layout, kATSUFromTextBeginning, n, FloatToFixed(x), FloatToFixed(y));
|
2009-12-12 21:10:02 +03:00
|
|
|
CGContextSetShouldAntialias(fl_gc, false);
|
|
|
|
#endif
|
2010-03-29 14:35:00 +04:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
2009-12-07 01:21:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2004-09-10 01:34:48 +04:00
|
|
|
}
|
2003-05-21 17:23:40 +04:00
|
|
|
|
2010-05-27 21:20:18 +04:00
|
|
|
void Fl_Graphics_Driver::draw(int angle, const char *str, int n, int x, int y) {
|
2009-12-07 01:21:55 +03:00
|
|
|
CGContextSaveGState(fl_gc);
|
|
|
|
CGContextTranslateCTM(fl_gc, x, y);
|
|
|
|
CGContextRotateCTM(fl_gc, - angle*(M_PI/180) );
|
|
|
|
fl_draw(str, n, (float)0., (float)0.);
|
|
|
|
CGContextRestoreGState(fl_gc);
|
2009-04-24 13:28:30 +04:00
|
|
|
}
|
|
|
|
|
2010-06-21 19:49:45 +04:00
|
|
|
void Fl_Graphics_Driver::rtl_draw(const char* c, int n, int x, int y) {
|
|
|
|
draw(c, n, x - fl_width(c, n), y);
|
2008-09-11 03:56:49 +04:00
|
|
|
}
|
2008-09-30 22:06:44 +04:00
|
|
|
|
2001-11-27 20:44:08 +03:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
2001-11-27 20:44:08 +03:00
|
|
|
//
|