| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserv
ed. |
| 3 * Copyright (C) 2008 Holger Hans Peter Freyther | 3 * Copyright (C) 2008 Holger Hans Peter Freyther |
| 4 * Copyright (C) 2014 Google Inc. All rights reserved. | 4 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 5 * | 5 * |
| 6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
| 7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
| 8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
| 9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
| 10 * | 10 * |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 namespace blink { | 38 namespace blink { |
| 39 | 39 |
| 40 SimpleShaper::SimpleShaper(const Font* font, const TextRun& run, const GlyphData
* emphasisData, | 40 SimpleShaper::SimpleShaper(const Font* font, const TextRun& run, const GlyphData
* emphasisData, |
| 41 HashSet<const SimpleFontData*>* fallbackFonts, FloatRect* bounds) | 41 HashSet<const SimpleFontData*>* fallbackFonts, FloatRect* bounds) |
| 42 : Shaper(font, run, emphasisData, fallbackFonts, bounds) | 42 : Shaper(font, run, emphasisData, fallbackFonts, bounds) |
| 43 , m_currentCharacter(0) | 43 , m_currentCharacter(0) |
| 44 , m_runWidthSoFar(0) | 44 , m_runWidthSoFar(0) |
| 45 { | 45 { |
| 46 // If the padding is non-zero, count the number of spaces in the run | 46 // If the padding is non-zero, count the number of spaces in the run |
| 47 // and divide that by the padding for per space addition. | 47 // and divide that by the padding for per space addition. |
| 48 m_expansion = m_run.expansion(); | 48 m_expansion = m_textRun.expansion(); |
| 49 if (!m_expansion) { | 49 if (!m_expansion) { |
| 50 m_expansionPerOpportunity = 0; | 50 m_expansionPerOpportunity = 0; |
| 51 } else { | 51 } else { |
| 52 bool isAfterExpansion = m_isAfterExpansion; | 52 bool isAfterExpansion = m_isAfterExpansion; |
| 53 unsigned expansionOpportunityCount = m_run.is8Bit() ? Character::expansi
onOpportunityCount(m_run.characters8(), m_run.length(), m_run.direction(), isAft
erExpansion, m_run.textJustify()) : Character::expansionOpportunityCount(m_run.c
haracters16(), m_run.length(), m_run.direction(), isAfterExpansion, m_run.textJu
stify()); | 53 unsigned expansionOpportunityCount = m_textRun.is8Bit() ? Character::exp
ansionOpportunityCount(m_textRun.characters8(), m_textRun.length(), m_textRun.di
rection(), isAfterExpansion, m_textRun.textJustify()) : Character::expansionOppo
rtunityCount(m_textRun.characters16(), m_textRun.length(), m_textRun.direction()
, isAfterExpansion, m_textRun.textJustify()); |
| 54 if (isAfterExpansion && !m_run.allowsTrailingExpansion()) | 54 if (isAfterExpansion && !m_textRun.allowsTrailingExpansion()) |
| 55 expansionOpportunityCount--; | 55 expansionOpportunityCount--; |
| 56 | 56 |
| 57 if (!expansionOpportunityCount) | 57 if (!expansionOpportunityCount) |
| 58 m_expansionPerOpportunity = 0; | 58 m_expansionPerOpportunity = 0; |
| 59 else | 59 else |
| 60 m_expansionPerOpportunity = m_expansion / expansionOpportunityCount; | 60 m_expansionPerOpportunity = m_expansion / expansionOpportunityCount; |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 GlyphData SimpleShaper::glyphDataForCharacter(CharacterData& charData, bool norm
alizeSpace) | 64 GlyphData SimpleShaper::glyphDataForCharacter(CharacterData& charData, bool norm
alizeSpace) |
| 65 { | 65 { |
| 66 ASSERT(m_font); | 66 ASSERT(m_font); |
| 67 return m_font->glyphDataForCharacter(charData.character, m_run.rtl(), normal
izeSpace); | 67 return m_font->glyphDataForCharacter(charData.character, m_textRun.rtl(), no
rmalizeSpace); |
| 68 } | 68 } |
| 69 | 69 |
| 70 float SimpleShaper::characterWidth(UChar32 character, const GlyphData& glyphData
) const | 70 float SimpleShaper::characterWidth(UChar32 character, const GlyphData& glyphData
) const |
| 71 { | 71 { |
| 72 const SimpleFontData* fontData = glyphData.fontData; | 72 const SimpleFontData* fontData = glyphData.fontData; |
| 73 ASSERT(fontData); | 73 ASSERT(fontData); |
| 74 | 74 |
| 75 if (UNLIKELY(character == tabulationCharacter && m_run.allowTabs())) | 75 if (UNLIKELY(character == tabulationCharacter && m_textRun.allowTabs())) |
| 76 return m_font->tabWidth(*fontData, m_run.tabSize(), m_run.xPos() + m_run
WidthSoFar); | 76 return m_font->tabWidth(*fontData, m_textRun.tabSize(), m_textRun.xPos()
+ m_runWidthSoFar); |
| 77 | 77 |
| 78 float width = fontData->widthForGlyph(glyphData.glyph); | 78 float width = fontData->widthForGlyph(glyphData.glyph); |
| 79 | 79 |
| 80 // SVG uses horizontalGlyphStretch(), when textLength is used to stretch/squ
eeze text. | 80 // SVG uses horizontalGlyphStretch(), when textLength is used to stretch/squ
eeze text. |
| 81 if (UNLIKELY(m_run.horizontalGlyphStretch() != 1)) | 81 if (UNLIKELY(m_textRun.horizontalGlyphStretch() != 1)) |
| 82 width *= m_run.horizontalGlyphStretch(); | 82 width *= m_textRun.horizontalGlyphStretch(); |
| 83 | 83 |
| 84 return width; | 84 return width; |
| 85 } | 85 } |
| 86 | 86 |
| 87 float SimpleShaper::adjustSpacing(float width, const CharacterData& charData) | 87 float SimpleShaper::adjustSpacing(float width, const CharacterData& charData) |
| 88 { | 88 { |
| 89 // Account for letter-spacing. | 89 // Account for letter-spacing. |
| 90 if (width) | 90 if (width) |
| 91 width += m_font->fontDescription().letterSpacing(); | 91 width += m_font->fontDescription().letterSpacing(); |
| 92 | 92 |
| 93 bool isExpansionOpportunity = Character::treatAsSpace(charData.character) ||
(m_run.textJustify() == TextJustifyDistribute); | 93 bool isExpansionOpportunity = Character::treatAsSpace(charData.character) ||
(m_textRun.textJustify() == TextJustifyDistribute); |
| 94 if (isExpansionOpportunity || (m_run.textJustify() == TextJustifyAuto && Cha
racter::isCJKIdeographOrSymbol(charData.character))) { | 94 if (isExpansionOpportunity || (m_textRun.textJustify() == TextJustifyAuto &&
Character::isCJKIdeographOrSymbol(charData.character))) { |
| 95 // Distribute the run's total expansion evenly over all expansion opport
unities in the run. | 95 // Distribute the run's total expansion evenly over all expansion opport
unities in the run. |
| 96 if (m_expansion) { | 96 if (m_expansion) { |
| 97 if (!isExpansionOpportunity && !m_isAfterExpansion) { | 97 if (!isExpansionOpportunity && !m_isAfterExpansion) { |
| 98 // Take the expansion opportunity before this ideograph. | 98 // Take the expansion opportunity before this ideograph. |
| 99 m_expansion -= m_expansionPerOpportunity; | 99 m_expansion -= m_expansionPerOpportunity; |
| 100 m_runWidthSoFar += m_expansionPerOpportunity; | 100 m_runWidthSoFar += m_expansionPerOpportunity; |
| 101 } | 101 } |
| 102 if (m_run.allowsTrailingExpansion() | 102 if (m_textRun.allowsTrailingExpansion() |
| 103 || (m_run.ltr() && charData.characterOffset + charData.clusterLe
ngth < static_cast<size_t>(m_run.length())) | 103 || (m_textRun.ltr() && charData.characterOffset + charData.clust
erLength < static_cast<size_t>(m_textRun.length())) |
| 104 || (m_run.rtl() && charData.characterOffset)) { | 104 || (m_textRun.rtl() && charData.characterOffset)) { |
| 105 m_expansion -= m_expansionPerOpportunity; | 105 m_expansion -= m_expansionPerOpportunity; |
| 106 width += m_expansionPerOpportunity; | 106 width += m_expansionPerOpportunity; |
| 107 m_isAfterExpansion = true; | 107 m_isAfterExpansion = true; |
| 108 } | 108 } |
| 109 } else { | 109 } else { |
| 110 m_isAfterExpansion = false; | 110 m_isAfterExpansion = false; |
| 111 } | 111 } |
| 112 | 112 |
| 113 // Account for word spacing. | 113 // Account for word spacing. |
| 114 // We apply additional space between "words" by adding width to the spac
e character. | 114 // We apply additional space between "words" by adding width to the spac
e character. |
| 115 if (isExpansionOpportunity && (charData.character != tabulationCharacter
|| !m_run.allowTabs()) | 115 if (isExpansionOpportunity && (charData.character != tabulationCharacter
|| !m_textRun.allowTabs()) |
| 116 && (charData.characterOffset || charData.character == noBreakSpaceCh
aracter) | 116 && (charData.characterOffset || charData.character == noBreakSpaceCh
aracter) |
| 117 && m_font->fontDescription().wordSpacing()) { | 117 && m_font->fontDescription().wordSpacing()) { |
| 118 width += m_font->fontDescription().wordSpacing(); | 118 width += m_font->fontDescription().wordSpacing(); |
| 119 } | 119 } |
| 120 } else { | 120 } else { |
| 121 m_isAfterExpansion = false; | 121 m_isAfterExpansion = false; |
| 122 } | 122 } |
| 123 | 123 |
| 124 return width; | 124 return width; |
| 125 } | 125 } |
| 126 | 126 |
| 127 template <typename TextIterator> | 127 template <typename TextIterator> |
| 128 unsigned SimpleShaper::advanceInternal(TextIterator& textIterator, GlyphBuffer*
glyphBuffer) | 128 unsigned SimpleShaper::advanceInternal(TextIterator& textIterator, GlyphBuffer*
glyphBuffer) |
| 129 { | 129 { |
| 130 bool hasExtraSpacing = (m_font->fontDescription().letterSpacing() || m_font-
>fontDescription().wordSpacing() || m_expansion) | 130 bool hasExtraSpacing = (m_font->fontDescription().letterSpacing() || m_font-
>fontDescription().wordSpacing() || m_expansion) |
| 131 && !m_run.spacingDisabled(); | 131 && !m_textRun.spacingDisabled(); |
| 132 | 132 |
| 133 const SimpleFontData* lastFontData = m_font->primaryFont(); | 133 const SimpleFontData* lastFontData = m_font->primaryFont(); |
| 134 bool normalizeSpace = m_run.normalizeSpace(); | 134 bool normalizeSpace = m_textRun.normalizeSpace(); |
| 135 const float initialRunWidth = m_runWidthSoFar; | 135 const float initialRunWidth = m_runWidthSoFar; |
| 136 | 136 |
| 137 CharacterData charData; | 137 CharacterData charData; |
| 138 while (textIterator.consume(charData.character)) { | 138 while (textIterator.consume(charData.character)) { |
| 139 charData.characterOffset = textIterator.offset(); | 139 charData.characterOffset = textIterator.offset(); |
| 140 charData.clusterLength = textIterator.glyphLength(); | 140 charData.clusterLength = textIterator.glyphLength(); |
| 141 GlyphData glyphData = glyphDataForCharacter(charData, normalizeSpace); | 141 GlyphData glyphData = glyphDataForCharacter(charData, normalizeSpace); |
| 142 | 142 |
| 143 // Some fonts do not have a glyph for zero-width-space, | 143 // Some fonts do not have a glyph for zero-width-space, |
| 144 // in that case use the space character and override the width. | 144 // in that case use the space character and override the width. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 190 } |
| 191 | 191 |
| 192 unsigned consumedCharacters = textIterator.offset() - m_currentCharacter; | 192 unsigned consumedCharacters = textIterator.offset() - m_currentCharacter; |
| 193 m_currentCharacter = textIterator.offset(); | 193 m_currentCharacter = textIterator.offset(); |
| 194 | 194 |
| 195 return consumedCharacters; | 195 return consumedCharacters; |
| 196 } | 196 } |
| 197 | 197 |
| 198 unsigned SimpleShaper::advance(unsigned offset, GlyphBuffer* glyphBuffer) | 198 unsigned SimpleShaper::advance(unsigned offset, GlyphBuffer* glyphBuffer) |
| 199 { | 199 { |
| 200 unsigned length = m_run.length(); | 200 unsigned length = m_textRun.length(); |
| 201 | 201 |
| 202 if (offset > length) | 202 if (offset > length) |
| 203 offset = length; | 203 offset = length; |
| 204 | 204 |
| 205 if (m_currentCharacter >= offset) | 205 if (m_currentCharacter >= offset) |
| 206 return 0; | 206 return 0; |
| 207 | 207 |
| 208 if (m_run.is8Bit()) { | 208 if (m_textRun.is8Bit()) { |
| 209 Latin1TextIterator textIterator(m_run.data8(m_currentCharacter), m_curre
ntCharacter, offset); | 209 Latin1TextIterator textIterator(m_textRun.data8(m_currentCharacter), m_c
urrentCharacter, offset); |
| 210 return advanceInternal(textIterator, glyphBuffer); | 210 return advanceInternal(textIterator, glyphBuffer); |
| 211 } | 211 } |
| 212 | 212 |
| 213 UTF16TextIterator textIterator(m_run.data16(m_currentCharacter), m_currentCh
aracter, offset, length); | 213 UTF16TextIterator textIterator(m_textRun.data16(m_currentCharacter), m_curre
ntCharacter, offset, length); |
| 214 return advanceInternal(textIterator, glyphBuffer); | 214 return advanceInternal(textIterator, glyphBuffer); |
| 215 } | 215 } |
| 216 | 216 |
| 217 bool SimpleShaper::advanceOneCharacter(float& width) | 217 bool SimpleShaper::advanceOneCharacter(float& width) |
| 218 { | 218 { |
| 219 float initialWidth = m_runWidthSoFar; | 219 float initialWidth = m_runWidthSoFar; |
| 220 | 220 |
| 221 if (!advance(m_currentCharacter + 1)) | 221 if (!advance(m_currentCharacter + 1)) |
| 222 return false; | 222 return false; |
| 223 | 223 |
| 224 width = m_runWidthSoFar - initialWidth; | 224 width = m_runWidthSoFar - initialWidth; |
| 225 return true; | 225 return true; |
| 226 } | 226 } |
| 227 | 227 |
| 228 } // namespace blink | 228 } // namespace blink |
| OLD | NEW |