• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. 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 INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "config.h"
26 #include "platform/fonts/Font.h"
27 
28 #include "platform/fonts/Character.h"
29 #include "platform/fonts/FontFallbackList.h"
30 #include "platform/fonts/GlyphBuffer.h"
31 #include "platform/fonts/SimpleFontData.h"
32 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
33 #include "platform/fonts/mac/ComplexTextController.h"
34 #include "platform/geometry/IntRect.h"
35 #include "platform/graphics/GraphicsContext.h"
36 #include "platform/text/TextRun.h"
37 #include "wtf/MathExtras.h"
38 
39 using namespace std;
40 
41 namespace WebCore {
42 
preferHarfBuzz(const Font * font)43 static bool preferHarfBuzz(const Font* font)
44 {
45     const FontDescription& description = font->fontDescription();
46     return description.featureSettings() && description.featureSettings()->size() > 0;
47 }
48 
selectionRectForComplexText(const TextRun & run,const FloatPoint & point,int h,int from,int to) const49 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& point, int h,
50                                             int from, int to) const
51 {
52     if (preferHarfBuzz(this)) {
53         HarfBuzzShaper shaper(this, run);
54         if (shaper.shape())
55             return shaper.selectionRect(point, h, from, to);
56     }
57     ComplexTextController controller(this, run);
58     controller.advance(from);
59     float beforeWidth = controller.runWidthSoFar();
60     controller.advance(to);
61     float afterWidth = controller.runWidthSoFar();
62 
63     // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning
64     if (run.rtl()) {
65         float totalWidth = controller.totalWidth();
66         return FloatRect(floorf(point.x() + totalWidth - afterWidth), point.y(), roundf(point.x() + totalWidth - beforeWidth) - floorf(point.x() + totalWidth - afterWidth), h);
67     }
68 
69     return FloatRect(floorf(point.x() + beforeWidth), point.y(), roundf(point.x() + afterWidth) - floorf(point.x() + beforeWidth), h);
70 }
71 
getGlyphsAndAdvancesForComplexText(const TextRunPaintInfo & runInfo,GlyphBuffer & glyphBuffer,ForTextEmphasisOrNot forTextEmphasis) const72 float Font::getGlyphsAndAdvancesForComplexText(const TextRunPaintInfo& runInfo, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const
73 {
74     float initialAdvance;
75 
76     ComplexTextController controller(this, runInfo.run, false, 0, forTextEmphasis);
77     controller.advance(runInfo.from);
78     float beforeWidth = controller.runWidthSoFar();
79     controller.advance(runInfo.to, &glyphBuffer);
80 
81     if (glyphBuffer.isEmpty())
82         return 0;
83 
84     float afterWidth = controller.runWidthSoFar();
85 
86     if (runInfo.run.rtl()) {
87         initialAdvance = controller.totalWidth() + controller.finalRoundingWidth() - afterWidth;
88         glyphBuffer.reverse();
89     } else
90         initialAdvance = beforeWidth;
91 
92     return initialAdvance;
93 }
94 
drawComplexText(GraphicsContext * context,const TextRunPaintInfo & runInfo,const FloatPoint & point) const95 void Font::drawComplexText(GraphicsContext* context, const TextRunPaintInfo& runInfo, const FloatPoint& point) const
96 {
97     if (preferHarfBuzz(this)) {
98         GlyphBuffer glyphBuffer;
99         HarfBuzzShaper shaper(this, runInfo.run);
100         shaper.setDrawRange(runInfo.from, runInfo.to);
101         if (shaper.shape(&glyphBuffer)) {
102             drawGlyphBuffer(context, runInfo, glyphBuffer, point);
103             return;
104         }
105     }
106     // This glyph buffer holds our glyphs + advances + font data for each glyph.
107     GlyphBuffer glyphBuffer;
108 
109     float startX = point.x() + getGlyphsAndAdvancesForComplexText(runInfo, glyphBuffer);
110 
111     // We couldn't generate any glyphs for the run.  Give up.
112     if (glyphBuffer.isEmpty())
113         return;
114 
115     // Draw the glyph buffer now at the starting point returned in startX.
116     FloatPoint startPoint(startX, point.y());
117     drawGlyphBuffer(context, runInfo, glyphBuffer, startPoint);
118 }
119 
drawEmphasisMarksForComplexText(GraphicsContext * context,const TextRunPaintInfo & runInfo,const AtomicString & mark,const FloatPoint & point) const120 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextRunPaintInfo& runInfo, const AtomicString& mark, const FloatPoint& point) const
121 {
122     GlyphBuffer glyphBuffer;
123     float initialAdvance = getGlyphsAndAdvancesForComplexText(runInfo, glyphBuffer, ForTextEmphasis);
124 
125     if (glyphBuffer.isEmpty())
126         return;
127 
128     drawEmphasisMarks(context, runInfo, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
129 }
130 
floatWidthForComplexText(const TextRun & run,HashSet<const SimpleFontData * > * fallbackFonts,IntRectExtent * glyphBounds) const131 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, IntRectExtent* glyphBounds) const
132 {
133     if (preferHarfBuzz(this)) {
134         HarfBuzzShaper shaper(this, run);
135         if (shaper.shape())
136             return shaper.totalWidth();
137     }
138     ComplexTextController controller(this, run, true, fallbackFonts);
139     glyphBounds->setTop(floorf(-controller.minGlyphBoundingBoxY()));
140     glyphBounds->setBottom(ceilf(controller.maxGlyphBoundingBoxY()));
141     glyphBounds->setLeft(max<int>(0, floorf(-controller.minGlyphBoundingBoxX())));
142     glyphBounds->setRight(max<int>(0, ceilf(controller.maxGlyphBoundingBoxX() - controller.totalWidth())));
143 
144     return controller.totalWidth();
145 }
146 
offsetForPositionForComplexText(const TextRun & run,float x,bool includePartialGlyphs) const147 int Font::offsetForPositionForComplexText(const TextRun& run, float x, bool includePartialGlyphs) const
148 {
149     if (preferHarfBuzz(this)) {
150         HarfBuzzShaper shaper(this, run);
151         if (shaper.shape())
152             return shaper.offsetForPosition(x);
153     }
154     ComplexTextController controller(this, run);
155     return controller.offsetForPosition(x, includePartialGlyphs);
156 }
157 
fontDataForCombiningCharacterSequence(const UChar * characters,size_t length,FontDataVariant variant) const158 const SimpleFontData* Font::fontDataForCombiningCharacterSequence(const UChar* characters, size_t length, FontDataVariant variant) const
159 {
160     UChar32 baseCharacter;
161     size_t baseCharacterLength = 0;
162     U16_NEXT(characters, baseCharacterLength, length, baseCharacter);
163 
164     GlyphData baseCharacterGlyphData = glyphDataForCharacter(baseCharacter, false, variant);
165 
166     if (!baseCharacterGlyphData.glyph)
167         return 0;
168 
169     if (length == baseCharacterLength)
170         return baseCharacterGlyphData.fontData;
171 
172     bool triedBaseCharacterFontData = false;
173 
174     unsigned i = 0;
175     for (const FontData* fontData = fontDataAt(0); fontData; fontData = fontDataAt(++i)) {
176         const SimpleFontData* simpleFontData = fontData->fontDataForCharacter(baseCharacter);
177         if (variant == NormalVariant) {
178             if (simpleFontData->platformData().orientation() == Vertical) {
179                 if (Character::isCJKIdeographOrSymbol(baseCharacter) && !simpleFontData->hasVerticalGlyphs()) {
180                     variant = BrokenIdeographVariant;
181                     simpleFontData = simpleFontData->brokenIdeographFontData().get();
182                 } else if (m_fontDescription.nonCJKGlyphOrientation() == NonCJKGlyphOrientationVerticalRight) {
183                     SimpleFontData* verticalRightFontData = simpleFontData->verticalRightOrientationFontData().get();
184                     Glyph verticalRightGlyph = verticalRightFontData->glyphForCharacter(baseCharacter);
185                     if (verticalRightGlyph == baseCharacterGlyphData.glyph)
186                         simpleFontData = verticalRightFontData;
187                 } else {
188                     SimpleFontData* uprightFontData = simpleFontData->uprightOrientationFontData().get();
189                     Glyph uprightGlyph = uprightFontData->glyphForCharacter(baseCharacter);
190                     if (uprightGlyph != baseCharacterGlyphData.glyph)
191                         simpleFontData = uprightFontData;
192                 }
193             }
194         } else {
195             if (const SimpleFontData* variantFontData = simpleFontData->variantFontData(m_fontDescription, variant).get())
196                 simpleFontData = variantFontData;
197         }
198 
199         if (simpleFontData == baseCharacterGlyphData.fontData)
200             triedBaseCharacterFontData = true;
201 
202         if (simpleFontData->canRenderCombiningCharacterSequence(characters, length))
203             return simpleFontData;
204     }
205 
206     if (!triedBaseCharacterFontData && baseCharacterGlyphData.fontData && baseCharacterGlyphData.fontData->canRenderCombiningCharacterSequence(characters, length))
207         return baseCharacterGlyphData.fontData;
208 
209     return SimpleFontData::systemFallback();
210 }
211 
212 } // namespace WebCore
213