initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / wx / FontWx.cpp
1 /*
2  * Copyright (C) 2007 Kevin Ollivier.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "Font.h"
28
29 #include "FontFallbackList.h"
30 #include "GlyphBuffer.h"
31 #include "GraphicsContext.h"
32 #include "IntRect.h"
33 #include "NotImplemented.h"
34 #include "SimpleFontData.h"
35 #include "TextRun.h"
36
37 #if OS(WINDOWS)
38 #include "UniscribeController.h"
39 typedef WebCore::UniscribeController ComplexTextController;
40 #endif
41
42 #if OS(DARWIN)
43 #include "mac/ComplexTextController.h"
44 #endif
45
46 #include <wx/dcclient.h>
47 #include "fontprops.h"
48 #include "non-kerned-drawing.h"
49
50 namespace WebCore {
51
52 bool Font::canReturnFallbackFontsForComplexText()
53 {
54 #if OS(WINDOWS) || OS(DARWIN)
55     return true;
56 #else
57     return false;
58 #endif
59 }
60
61 bool Font::canExpandAroundIdeographsInComplexText()
62 {
63 #if OS(DARWIN)
64     return true;
65 #else
66     return false;
67 #endif
68 }
69
70 void Font::drawGlyphs(GraphicsContext* graphicsContext, const SimpleFontData* font, const GlyphBuffer& glyphBuffer, 
71                       int from, int numGlyphs, const FloatPoint& point) const
72 {
73     // prepare DC
74     Color color = graphicsContext->fillColor();
75
76     // We can't use wx drawing methods on Win/Linux because they automatically kern text
77     // so we've created a function with platform dependent drawing implementations that
78     // will hopefully be folded into wx once the API has solidified.
79     // see platform/wx/wxcode/<platform> for the implementations.
80     graphicsContext->save();
81     drawTextWithSpacing(graphicsContext, font, color, glyphBuffer, from, numGlyphs, point);
82     graphicsContext->restore();
83 }
84
85 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& point, int h, int from, int to) const
86 {
87 #if OS(WINDOWS) || OS(DARWIN)
88     ComplexTextController it(this, run);
89     it.advance(from);
90     float beforeWidth = it.runWidthSoFar();
91     it.advance(to);
92     float afterWidth = it.runWidthSoFar();
93
94     // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning
95     if (run.rtl()) {
96 #if OS(WINDOWS)
97         it.advance(run.length());
98         float totalWidth = it.runWidthSoFar();
99         return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
100 #else
101         float totalWidth = it.totalWidth();
102         return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
103 #endif
104     } 
105     
106     return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h);
107 #else
108     notImplemented();
109     return FloatRect();
110 #endif
111 }
112
113 float Font::getGlyphsAndAdvancesForComplexText(const TextRun& /* run */, int /* from */, int /* to */, GlyphBuffer& /* glyphBuffer */, ForTextEmphasisOrNot /* forTextEmphasis */) const
114 {
115     // FIXME: Implement this by moving most of the drawComplexText() implementation in here. Set up the
116     // ComplexTextController according to forTextEmphasis.
117     notImplemented();
118     return 0;
119 }
120
121 void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
122 {
123 #if OS(WINDOWS) || OS(DARWIN)
124     // This glyph buffer holds our glyphs + advances + font data for each glyph.
125     GlyphBuffer glyphBuffer;
126
127     float startX = point.x();
128     ComplexTextController controller(this, run);
129     controller.advance(from);
130     float beforeWidth = controller.runWidthSoFar();
131     controller.advance(to, &glyphBuffer);
132     
133     // We couldn't generate any glyphs for the run.  Give up.
134     if (glyphBuffer.isEmpty())
135         return;
136     
137     float afterWidth = controller.runWidthSoFar();
138
139     if (run.rtl()) {
140 #if OS(WINDOWS)
141         controller.advance(run.length());
142         startX += controller.runWidthSoFar() - afterWidth;
143 #else
144         startX += controller.totalWidth() - afterWidth;
145         for (int i = 0, end = glyphBuffer.size() - 1; i < glyphBuffer.size() / 2; ++i, --end)
146             glyphBuffer.swap(i, end);
147 #endif
148     } else
149         startX += beforeWidth;
150
151     // Draw the glyph buffer now at the starting point returned in startX.
152     FloatPoint startPoint(startX, point.y());
153     drawGlyphBuffer(context, run, glyphBuffer, startPoint);
154 #else
155     notImplemented();
156 #endif
157 }
158
159 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to) const
160 {
161     GlyphBuffer glyphBuffer;
162     float initialAdvance = getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer, ForTextEmphasis);
163
164     if (glyphBuffer.isEmpty())
165         return;
166
167     drawEmphasisMarks(context, run, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
168 }
169
170 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const
171 {
172 #if OS(WINDOWS) || OS(DARWIN)
173     ComplexTextController controller(this, run, fallbackFonts);
174 #if OS(WINDOWS)
175     controller.advance(run.length());
176     return controller.runWidthSoFar();
177 #else
178     return controller.totalWidth();
179 #endif
180 #else
181     notImplemented();
182     return 0;
183 #endif
184 }
185
186 int Font::offsetForPositionForComplexText(const TextRun& run, float x, bool includePartialGlyphs) const
187 {
188 #if OS(WINDOWS) || OS(DARWIN)
189     ComplexTextController controller(this, run);
190     return controller.offsetForPosition(x, includePartialGlyphs);
191 #else
192     notImplemented();
193     return 0;
194 #endif
195 }
196 }