Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 */ | 23 */ |
| 24 | 24 |
| 25 #include "config.h" | 25 #include "config.h" |
| 26 #include "platform/fonts/Font.h" | 26 #include "platform/fonts/Font.h" |
| 27 | 27 |
| 28 #include "platform/fonts/Character.h" | 28 #include "platform/fonts/Character.h" |
| 29 #include "platform/fonts/FontFallbackList.h" | 29 #include "platform/fonts/FontFallbackList.h" |
| 30 #include "platform/fonts/GlyphBuffer.h" | 30 #include "platform/fonts/GlyphBuffer.h" |
| 31 #include "platform/fonts/SimpleFontData.h" | 31 #include "platform/fonts/SimpleFontData.h" |
| 32 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h" | 32 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h" |
| 33 #include "platform/fonts/mac/ComplexTextController.h" | |
| 34 #include "platform/geometry/IntRect.h" | 33 #include "platform/geometry/IntRect.h" |
| 35 #include "platform/graphics/GraphicsContext.h" | 34 #include "platform/graphics/GraphicsContext.h" |
| 36 #include "platform/text/TextRun.h" | 35 #include "platform/text/TextRun.h" |
| 37 #include "wtf/MathExtras.h" | 36 #include "wtf/MathExtras.h" |
| 38 | 37 |
| 39 using namespace std; | 38 using namespace std; |
| 40 | 39 |
| 41 namespace WebCore { | 40 namespace WebCore { |
| 42 | 41 |
|
eae
2014/03/20 18:20:30
Do we still need this file? It doesn't look much d
| |
| 43 static bool preferHarfBuzz(const Font* font) | |
| 44 { | |
| 45 const FontDescription& description = font->fontDescription(); | |
| 46 return description.featureSettings() && description.featureSettings()->size( ) > 0; | |
| 47 } | |
| 48 | |
| 49 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint & point, int h, | 42 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint & point, int h, |
| 50 int from, int to) const | 43 int from, int to) const |
| 51 { | 44 { |
| 52 if (preferHarfBuzz(this)) { | 45 HarfBuzzShaper shaper(this, run); |
| 53 HarfBuzzShaper shaper(this, run); | 46 return shaper.shape() ? shaper.selectionRect(point, h, from, to) : FloatRect (); |
| 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 } | 47 } |
| 71 | 48 |
| 72 float Font::getGlyphsAndAdvancesForComplexText(const TextRun& run, int from, int to, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const | 49 float Font::getGlyphsAndAdvancesForComplexText(const TextRun& run, int from, int to, GlyphBuffer& glyphBuffer, Font::ForTextEmphasisOrNot forTextEmphasis) const |
| 73 { | 50 { |
| 74 float initialAdvance; | 51 HarfBuzzShaper shaper(this, run, forTextEmphasis == Font::ForTextEmphasis ? HarfBuzzShaper::ForTextEmphasis : HarfBuzzShaper::NotForTextEmphasis); |
| 75 | 52 shaper.setDrawRange(from, to); |
| 76 ComplexTextController controller(this, run, false, 0, forTextEmphasis); | 53 shaper.shape(&glyphBuffer); |
| 77 controller.advance(from); | 54 return 0; |
| 78 float beforeWidth = controller.runWidthSoFar(); | |
| 79 controller.advance(to, &glyphBuffer); | |
| 80 | |
| 81 if (glyphBuffer.isEmpty()) | |
| 82 return 0; | |
| 83 | |
| 84 float afterWidth = controller.runWidthSoFar(); | |
| 85 | |
| 86 if (run.rtl()) { | |
| 87 initialAdvance = controller.totalWidth() + controller.finalRoundingWidth () - afterWidth; | |
| 88 glyphBuffer.reverse(0, glyphBuffer.size()); | |
| 89 } else | |
| 90 initialAdvance = beforeWidth; | |
| 91 | |
| 92 return initialAdvance; | |
| 93 } | 55 } |
| 94 | 56 |
| 95 void Font::drawComplexText(GraphicsContext* context, const TextRunPaintInfo& run Info, const FloatPoint& point) const | 57 void Font::drawComplexText(GraphicsContext* context, const TextRunPaintInfo& run Info, const FloatPoint& point) const |
| 96 { | 58 { |
| 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; | 59 GlyphBuffer glyphBuffer; |
| 108 | 60 HarfBuzzShaper shaper(this, runInfo.run); |
| 109 float startX = point.x() + getGlyphsAndAdvancesForComplexText(runInfo.run, r unInfo.from, runInfo.to, glyphBuffer); | 61 shaper.setDrawRange(runInfo.from, runInfo.to); |
| 110 | 62 if (shaper.shape(&glyphBuffer)) |
| 111 // We couldn't generate any glyphs for the run. Give up. | 63 drawGlyphBuffer(context, runInfo, glyphBuffer, point); |
| 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 } | 64 } |
| 119 | 65 |
| 120 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextR unPaintInfo& runInfo, const AtomicString& mark, const FloatPoint& point) const | 66 void Font::drawEmphasisMarksForComplexText(GraphicsContext* context, const TextR unPaintInfo& runInfo, const AtomicString& mark, const FloatPoint& point) const |
| 121 { | 67 { |
| 122 GlyphBuffer glyphBuffer; | 68 GlyphBuffer glyphBuffer; |
| 123 float initialAdvance = getGlyphsAndAdvancesForComplexText(runInfo.run, runIn fo.from, runInfo.to, glyphBuffer, ForTextEmphasis); | 69 float initialAdvance = getGlyphsAndAdvancesForComplexText(runInfo.run, runIn fo.from, runInfo.to, glyphBuffer, ForTextEmphasis); |
| 124 | 70 |
| 125 if (glyphBuffer.isEmpty()) | 71 if (glyphBuffer.isEmpty()) |
| 126 return; | 72 return; |
| 127 | 73 |
| 128 drawEmphasisMarks(context, runInfo, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y())); | 74 drawEmphasisMarks(context, runInfo, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y())); |
| 129 } | 75 } |
| 130 | 76 |
| 131 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFon tData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const | 77 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFon tData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const |
|
eae
2014/03/20 18:20:30
We really want to use the FontHarfBuzz.cpp impleme
| |
| 132 { | 78 { |
| 133 if (preferHarfBuzz(this)) { | 79 HarfBuzzShaper shaper(this, run); |
| 134 HarfBuzzShaper shaper(this, run); | 80 return shaper.shape() ? shaper.totalWidth() : 0; |
| 135 if (shaper.shape()) | |
| 136 return shaper.totalWidth(); | |
| 137 } | |
| 138 ComplexTextController controller(this, run, true, fallbackFonts); | |
| 139 if (glyphOverflow) { | |
| 140 glyphOverflow->top = max<int>(glyphOverflow->top, ceilf(-controller.minG lyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().ascent() )); | |
| 141 glyphOverflow->bottom = max<int>(glyphOverflow->bottom, ceilf(controller .maxGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().des cent())); | |
| 142 glyphOverflow->left = max<int>(0, ceilf(-controller.minGlyphBoundingBoxX ())); | |
| 143 glyphOverflow->right = max<int>(0, ceilf(controller.maxGlyphBoundingBoxX () - controller.totalWidth())); | |
| 144 } | |
| 145 return controller.totalWidth(); | |
| 146 } | 81 } |
| 147 | 82 |
| 148 int Font::offsetForPositionForComplexText(const TextRun& run, float x, bool incl udePartialGlyphs) const | 83 int Font::offsetForPositionForComplexText(const TextRun& run, float x, bool incl udePartialGlyphs) const |
| 149 { | 84 { |
| 150 if (preferHarfBuzz(this)) { | 85 HarfBuzzShaper shaper(this, run); |
| 151 HarfBuzzShaper shaper(this, run); | 86 return shaper.shape() ? shaper.offsetForPosition(x) : 0; |
| 152 if (shaper.shape()) | |
| 153 return shaper.offsetForPosition(x); | |
| 154 } | |
| 155 ComplexTextController controller(this, run); | |
| 156 return controller.offsetForPosition(x, includePartialGlyphs); | |
| 157 } | |
| 158 | |
| 159 const SimpleFontData* Font::fontDataForCombiningCharacterSequence(const UChar* c haracters, size_t length, FontDataVariant variant) const | |
| 160 { | |
| 161 UChar32 baseCharacter; | |
| 162 size_t baseCharacterLength = 0; | |
| 163 U16_NEXT(characters, baseCharacterLength, length, baseCharacter); | |
| 164 | |
| 165 GlyphData baseCharacterGlyphData = glyphDataForCharacter(baseCharacter, fals e, variant); | |
| 166 | |
| 167 if (!baseCharacterGlyphData.glyph) | |
| 168 return 0; | |
| 169 | |
| 170 if (length == baseCharacterLength) | |
| 171 return baseCharacterGlyphData.fontData; | |
| 172 | |
| 173 bool triedBaseCharacterFontData = false; | |
| 174 | |
| 175 unsigned i = 0; | |
| 176 for (const FontData* fontData = fontDataAt(0); fontData; fontData = fontData At(++i)) { | |
| 177 const SimpleFontData* simpleFontData = fontData->fontDataForCharacter(ba seCharacter); | |
| 178 if (variant == NormalVariant) { | |
| 179 if (simpleFontData->platformData().orientation() == Vertical) { | |
| 180 if (Character::isCJKIdeographOrSymbol(baseCharacter) && !simpleF ontData->hasVerticalGlyphs()) { | |
| 181 variant = BrokenIdeographVariant; | |
| 182 simpleFontData = simpleFontData->brokenIdeographFontData().g et(); | |
| 183 } else if (m_fontDescription.nonCJKGlyphOrientation() == NonCJKG lyphOrientationVerticalRight) { | |
| 184 SimpleFontData* verticalRightFontData = simpleFontData->vert icalRightOrientationFontData().get(); | |
| 185 Glyph verticalRightGlyph = verticalRightFontData->glyphForCh aracter(baseCharacter); | |
| 186 if (verticalRightGlyph == baseCharacterGlyphData.glyph) | |
| 187 simpleFontData = verticalRightFontData; | |
| 188 } else { | |
| 189 SimpleFontData* uprightFontData = simpleFontData->uprightOri entationFontData().get(); | |
| 190 Glyph uprightGlyph = uprightFontData->glyphForCharacter(base Character); | |
| 191 if (uprightGlyph != baseCharacterGlyphData.glyph) | |
| 192 simpleFontData = uprightFontData; | |
| 193 } | |
| 194 } | |
| 195 } else { | |
| 196 if (const SimpleFontData* variantFontData = simpleFontData->variantF ontData(m_fontDescription, variant).get()) | |
| 197 simpleFontData = variantFontData; | |
| 198 } | |
| 199 | |
| 200 if (simpleFontData == baseCharacterGlyphData.fontData) | |
| 201 triedBaseCharacterFontData = true; | |
| 202 | |
| 203 if (simpleFontData->canRenderCombiningCharacterSequence(characters, leng th)) | |
| 204 return simpleFontData; | |
| 205 } | |
| 206 | |
| 207 if (!triedBaseCharacterFontData && baseCharacterGlyphData.fontData && baseCh aracterGlyphData.fontData->canRenderCombiningCharacterSequence(characters, lengt h)) | |
| 208 return baseCharacterGlyphData.fontData; | |
| 209 | |
| 210 return SimpleFontData::systemFallback(); | |
| 211 } | 87 } |
| 212 | 88 |
| 213 } // namespace WebCore | 89 } // namespace WebCore |
| OLD | NEW |