• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2006 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#import "config.h"
24#import "Font.h"
25
26#import "GlyphBuffer.h"
27#import "GraphicsContext.h"
28#import "Logging.h"
29#import "SimpleFontData.h"
30#import "WebCoreSystemInterface.h"
31#import <AppKit/AppKit.h>
32
33#define SYNTHETIC_OBLIQUE_ANGLE 14
34
35#ifdef __LP64__
36#define URefCon void*
37#else
38#define URefCon UInt32
39#endif
40
41using namespace std;
42
43namespace WebCore {
44
45bool Font::canReturnFallbackFontsForComplexText()
46{
47    return true;
48}
49
50void Font::drawGlyphs(GraphicsContext* context, const SimpleFontData* font, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) const
51{
52    CGContextRef cgContext = context->platformContext();
53    bool newShouldUseFontSmoothing = shouldUseSmoothing();
54
55    switch(fontDescription().fontSmoothing()) {
56    case Antialiased: {
57        context->setShouldAntialias(true);
58        newShouldUseFontSmoothing = false;
59        break;
60    }
61    case SubpixelAntialiased: {
62        context->setShouldAntialias(true);
63        newShouldUseFontSmoothing = true;
64        break;
65    }
66    case NoSmoothing: {
67        context->setShouldAntialias(false);
68        newShouldUseFontSmoothing = false;
69        break;
70    }
71    case AutoSmoothing: {
72        // For the AutoSmooth case, don't do anything! Keep the default settings.
73        break;
74    }
75    default:
76        ASSERT_NOT_REACHED();
77    }
78
79    bool originalShouldUseFontSmoothing = wkCGContextGetShouldSmoothFonts(cgContext);
80    if (originalShouldUseFontSmoothing != newShouldUseFontSmoothing)
81        CGContextSetShouldSmoothFonts(cgContext, newShouldUseFontSmoothing);
82
83    const FontPlatformData& platformData = font->platformData();
84    NSFont* drawFont;
85    if (!isPrinterFont()) {
86        drawFont = [platformData.font() screenFont];
87        if (drawFont != platformData.font())
88            // We are getting this in too many places (3406411); use ERROR so it only prints on debug versions for now. (We should debug this also, eventually).
89            LOG_ERROR("Attempting to set non-screen font (%@) when drawing to screen.  Using screen font anyway, may result in incorrect metrics.",
90                [[[platformData.font() fontDescriptor] fontAttributes] objectForKey:NSFontNameAttribute]);
91    } else {
92        drawFont = [platformData.font() printerFont];
93        if (drawFont != platformData.font())
94            NSLog(@"Attempting to set non-printer font (%@) when printing.  Using printer font anyway, may result in incorrect metrics.",
95                [[[platformData.font() fontDescriptor] fontAttributes] objectForKey:NSFontNameAttribute]);
96    }
97
98    CGContextSetFont(cgContext, platformData.cgFont());
99
100    CGAffineTransform matrix = CGAffineTransformIdentity;
101    if (drawFont)
102        memcpy(&matrix, [drawFont matrix], sizeof(matrix));
103    matrix.b = -matrix.b;
104    matrix.d = -matrix.d;
105    if (platformData.m_syntheticOblique)
106        matrix = CGAffineTransformConcat(matrix, CGAffineTransformMake(1, 0, -tanf(SYNTHETIC_OBLIQUE_ANGLE * acosf(0) / 90), 1, 0, 0));
107    CGContextSetTextMatrix(cgContext, matrix);
108
109    if (drawFont) {
110        wkSetCGFontRenderingMode(cgContext, drawFont);
111        CGContextSetFontSize(cgContext, 1.0f);
112    } else
113        CGContextSetFontSize(cgContext, platformData.m_size);
114
115    IntSize shadowSize;
116    int shadowBlur;
117    Color shadowColor;
118    ColorSpace fillColorSpace = context->fillColorSpace();
119    context->getShadow(shadowSize, shadowBlur, shadowColor);
120
121    bool hasSimpleShadow = context->textDrawingMode() == cTextFill && shadowColor.isValid() && !shadowBlur;
122    if (hasSimpleShadow) {
123        // Paint simple shadows ourselves instead of relying on CG shadows, to avoid losing subpixel antialiasing.
124        context->clearShadow();
125        Color fillColor = context->fillColor();
126        Color shadowFillColor(shadowColor.red(), shadowColor.green(), shadowColor.blue(), shadowColor.alpha() * fillColor.alpha() / 255);
127        context->setFillColor(shadowFillColor, fillColorSpace);
128        CGContextSetTextPosition(cgContext, point.x() + shadowSize.width(), point.y() + shadowSize.height());
129        CGContextShowGlyphsWithAdvances(cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs);
130        if (font->syntheticBoldOffset()) {
131            CGContextSetTextPosition(cgContext, point.x() + shadowSize.width() + font->syntheticBoldOffset(), point.y() + shadowSize.height());
132            CGContextShowGlyphsWithAdvances(cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs);
133        }
134        context->setFillColor(fillColor, fillColorSpace);
135    }
136
137    CGContextSetTextPosition(cgContext, point.x(), point.y());
138    CGContextShowGlyphsWithAdvances(cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs);
139    if (font->syntheticBoldOffset()) {
140        CGContextSetTextPosition(cgContext, point.x() + font->syntheticBoldOffset(), point.y());
141        CGContextShowGlyphsWithAdvances(cgContext, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs);
142    }
143
144    if (hasSimpleShadow)
145        context->setShadow(shadowSize, shadowBlur, shadowColor, fillColorSpace);
146
147    if (originalShouldUseFontSmoothing != newShouldUseFontSmoothing)
148        CGContextSetShouldSmoothFonts(cgContext, originalShouldUseFontSmoothing);
149}
150
151}
152