Changeset 44387 in webkit


Ignore:
Timestamp:
Jun 3, 2009 10:07:34 AM (15 years ago)
Author:
kevino@webkit.org
Message:

Reviewed by Kevin Ollivier.

Use CGContextShowGlyphsWithAdvances to get more accurate text rendering on Mac.

https://bugs.webkit.org/show_bug.cgi?id=26161

Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r44386 r44387  
     12009-06-03  Kevin Watters  <kevinwatters@gmail.com>
     2
     3        Reviewed by Kevin Ollivier.
     4
     5        Use CGContextShowGlyphsWithAdvances to get more accurate text rendering on Mac.
     6       
     7        https://bugs.webkit.org/show_bug.cgi?id=26161
     8
     9        * platform/wx/wxcode/mac/carbon/non-kerned-drawing.cpp:
     10        (WebCore::drawTextWithSpacing):
     11
    1122009-06-03  Pavel Feldman  <pfeldman@chromium.org>
    213
  • trunk/WebCore/platform/wx/wxcode/mac/carbon/non-kerned-drawing.cpp

    r40615 r44387  
    2525
    2626#include "config.h"
     27#include "FloatSize.h"
    2728#include "GlyphBuffer.h"
    2829#include "GraphicsContext.h"
    2930#include "SimpleFontData.h"
     31#include <wtf/Vector.h>
     32
     33#include <ApplicationServices/ApplicationServices.h>
     34
     35#include <dlfcn.h>
    3036
    3137#include <wx/defs.h>
     
    3339#include <wx/dcgraph.h>
    3440#include <wx/gdicmn.h>
    35 #include <vector>
     41
     42
     43// Unfortunately we need access to a private function to get the character -> glyph conversion needed to
     44// allow us to use CGContextShowGlyphsWithAdvances
     45// Note that on < 10.5, the function is called CGFontGetGlyphsForUnicodes, so we need to detect and deal
     46// with this.
     47typedef void (*CGFontGetGlyphsForUnicharsPtr)(CGFontRef, const UniChar[], const CGGlyph[], size_t);
     48static CGFontGetGlyphsForUnicharsPtr CGFontGetGlyphsForUnichars = (CGFontGetGlyphsForUnicharsPtr)dlsym(RTLD_DEFAULT, "CGFontGetGlyphsForUnichars");
    3649
    3750namespace WebCore {
     
    3952void drawTextWithSpacing(GraphicsContext* graphicsContext, const SimpleFontData* font, const wxColour& color, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point)
    4053{
    41 #if USE(WXGC)
     54    graphicsContext->save();
     55   
    4256    wxGCDC* dc = static_cast<wxGCDC*>(graphicsContext->platformContext());
    43 #else
    44     wxDC* dc = graphicsContext->platformContext();
    45 #endif
    4657
    4758    wxFont* wxfont = font->getWxFont();
    48     if (wxfont->IsOk())
    49         dc->SetFont(*wxfont);
    50     dc->SetTextForeground(color);
     59    graphicsContext->setFillColor(graphicsContext->fillColor());
    5160
    52     // convert glyphs to wxString
    53     GlyphBufferGlyph* glyphs = const_cast<GlyphBufferGlyph*>(glyphBuffer.glyphs(from));
    54     int offset = point.x();
    55     wxString text = wxEmptyString;
    56     for (unsigned i = 0; i < numGlyphs; i++) {
    57         text = text.Append((wxChar)glyphs[i]);
    58         offset += glyphBuffer.advanceAt(from + i);
     61    CGContextRef cgContext = static_cast<CGContextRef>(dc->GetGraphicsContext()->GetNativeContext());
     62
     63    CGFontRef cgFont;
     64
     65#ifdef wxOSX_USE_CORE_TEXT && wxOSX_USE_CORE_TEXT
     66    cgFont = CTFontCopyGraphicsFont((CTFontRef)font->OSXGetCTFont(), NULL);
     67#else
     68    ATSFontRef fontRef;
     69   
     70    fontRef = FMGetATSFontRefFromFont(wxfont->MacGetATSUFontID());
     71   
     72    if (fontRef)
     73        cgFont = CGFontCreateWithPlatformFont((void*)&fontRef);
     74#endif
     75   
     76    CGContextSetFont(cgContext, cgFont);
     77
     78    CGContextSetFontSize(cgContext, wxfont->GetPointSize());
     79
     80    CGFloat red, green, blue, alpha;
     81    graphicsContext->fillColor().getRGBA(red, green, blue, alpha);
     82    CGContextSetRGBFillColor(cgContext, red, green, blue, alpha);
     83
     84    CGAffineTransform matrix = CGAffineTransformIdentity;
     85    matrix.b = -matrix.b;
     86    matrix.d = -matrix.d;
     87   
     88    CGContextSetTextMatrix(cgContext, matrix);
     89
     90    CGContextSetTextPosition(cgContext, point.x(), point.y());
     91   
     92    const FloatSize* advanceSizes = static_cast<const FloatSize*>(glyphBuffer.advances(from));
     93    int size = glyphBuffer.size() - from;
     94    CGSize sizes[size];
     95    CGGlyph glyphs[numGlyphs];
     96   
     97    // if the function doesn't exist, we're probably on tiger and need to grab the
     98    // function under its old name, CGFontGetGlyphsForUnicodes
     99    if (!CGFontGetGlyphsForUnichars)
     100        CGFontGetGlyphsForUnichars = (CGFontGetGlyphsForUnicharsPtr)dlsym(RTLD_DEFAULT, "CGFontGetGlyphsForUnicodes");
     101   
     102    // Let's make sure we got the function under one name or another!
     103    ASSERT(CGFontGetGlyphsForUnichars);
     104    CGFontGetGlyphsForUnichars(cgFont, glyphBuffer.glyphs(from), glyphs, numGlyphs);
     105   
     106    for (int i = 0; i < size; i++) {
     107        FloatSize fsize = advanceSizes[i];
     108        sizes[i] = CGSizeMake(fsize.width(), fsize.height());
    59109    }
    60110   
    61     // NOTE: The wx API actually adds the ascent to the y value internally,
    62     // so we have to subtract it from the y point here so that the ascent
    63     // isn't added twice.
    64     dc->DrawText(text, (wxCoord)point.x(), int(point.y() - font->ascent()));
     111    CGContextShowGlyphsWithAdvances(cgContext, glyphs, sizes, numGlyphs);
     112   
     113    if (cgFont)
     114        CGFontRelease(cgFont);
     115    graphicsContext->restore();
    65116}
    66117
Note: See TracChangeset for help on using the changeset viewer.