📄 uniscribecontroller.cpp
字号:
/* * Copyright (C) 2007 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include "config.h"#include "UniscribeController.h"#include "Font.h"#include "SimpleFontData.h"#include <wtf/MathExtras.h>namespace WebCore {// FIXME: Rearchitect this to be more like WidthIterator in Font.cpp. Have an advance() method// that does stuff in that method instead of doing everything in the constructor. Have advance()// take the GlyphBuffer as an arg so that we don't have to populate the glyph buffer when// measuring.UniscribeController::UniscribeController(const Font* font, const TextRun& run): m_font(*font), m_run(run), m_end(run.length()), m_currentCharacter(0), m_runWidthSoFar(0), m_computingOffsetPosition(false), m_includePartialGlyphs(false), m_offsetX(0), m_offsetPosition(0){ m_padding = m_run.padding(); if (!m_padding) m_padPerSpace = 0; else { float numSpaces = 0; for (int s = 0; s < m_run.length(); s++) if (Font::treatAsSpace(m_run[s])) numSpaces++; if (numSpaces == 0) m_padPerSpace = 0; else m_padPerSpace = ceilf(m_run.padding() / numSpaces); } // Null out our uniscribe structs resetControlAndState();}int UniscribeController::offsetForPosition(int x, bool includePartialGlyphs){ m_computingOffsetPosition = true; m_includePartialGlyphs = includePartialGlyphs; m_offsetX = x; m_offsetPosition = 0; advance(m_run.length()); if (m_computingOffsetPosition) { // The point is to the left or to the right of the entire run. if (m_offsetX >= m_runWidthSoFar && m_run.ltr() || m_offsetX < 0 && m_run.rtl()) m_offsetPosition = m_end; } m_computingOffsetPosition = false; return m_offsetPosition;}void UniscribeController::advance(unsigned offset, GlyphBuffer* glyphBuffer){ // FIXME: We really want to be using a newer version of Uniscribe that supports the new OpenType // functions. Those functions would allow us to turn off kerning and ligatures. Without being able // to do that, we will have buggy line breaking and metrics when simple and complex text are close // together (the complex code path will narrow the text because of kerning and ligatures and then // when bidi processing splits into multiple runs, the simple portions will get wider and cause us to // spill off the edge of a line). if (static_cast<int>(offset) > m_end) offset = m_end; // Itemize the string. const UChar* cp = m_run.data(m_currentCharacter); int length = offset - m_currentCharacter; if (length <= 0) return; unsigned baseCharacter = m_currentCharacter; // We break up itemization of the string by fontData and (if needed) the use of small caps. // FIXME: It's inconsistent that we use logical order when itemizing, since this // does not match normal RTL. // FIXME: This function should decode surrogate pairs. Currently it makes little difference that // it does not because the font cache on Windows does not support non-BMP characters. Vector<UChar, 256> smallCapsBuffer; if (m_font.isSmallCaps()) smallCapsBuffer.resize(length); unsigned indexOfFontTransition = m_run.rtl() ? length - 1 : 0; const UChar* curr = m_run.rtl() ? cp + length - 1 : cp; const UChar* end = m_run.rtl() ? cp - 1 : cp + length; const SimpleFontData* fontData; const SimpleFontData* nextFontData = m_font.glyphDataForCharacter(*curr, false).fontData; UChar newC = 0; bool isSmallCaps; bool nextIsSmallCaps = m_font.isSmallCaps() && !(U_GET_GC_MASK(*curr) & U_GC_M_MASK) && (newC = u_toupper(*curr)) != *curr; if (nextIsSmallCaps) smallCapsBuffer[curr - cp] = newC; while (true) { curr = m_run.rtl() ? curr - 1 : curr + 1; if (curr == end) break; fontData = nextFontData; isSmallCaps = nextIsSmallCaps; int index = curr - cp; UChar c = *curr; bool forceSmallCaps = isSmallCaps && (U_GET_GC_MASK(c) & U_GC_M_MASK); nextFontData = m_font.glyphDataForCharacter(*curr, false, forceSmallCaps).fontData; if (m_font.isSmallCaps()) { nextIsSmallCaps = forceSmallCaps || (newC = u_toupper(c)) != c; if (nextIsSmallCaps) smallCapsBuffer[index] = forceSmallCaps ? c : newC; } if (nextFontData != fontData || nextIsSmallCaps != isSmallCaps) { int itemStart = m_run.rtl() ? index + 1 : indexOfFontTransition; int itemLength = m_run.rtl() ? indexOfFontTransition - index : index - indexOfFontTransition; m_currentCharacter = baseCharacter + itemStart; itemizeShapeAndPlace((isSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, fontData, glyphBuffer); indexOfFontTransition = index; } } int itemLength = m_run.rtl() ? indexOfFontTransition + 1 : length - indexOfFontTransition; if (itemLength) { int itemStart = m_run.rtl() ? 0 : indexOfFontTransition; m_currentCharacter = baseCharacter + itemStart; itemizeShapeAndPlace((nextIsSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, nextFontData, glyphBuffer); } m_currentCharacter = baseCharacter + length;}void UniscribeController::itemizeShapeAndPlace(const UChar* cp, unsigned length, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer){ // ScriptItemize (in Windows XP versions prior to SP2) can overflow by 1. This is why there is an extra empty item // hanging out at the end of the array m_items.resize(6); int numItems = 0; while (ScriptItemize(cp, length, m_items.size() - 1, &m_control, &m_state, m_items.data(), &numItems) == E_OUTOFMEMORY) { m_items.resize(m_items.size() * 2); resetControlAndState(); } m_items.resize(numItems + 1); if (m_run.rtl()) { for (int i = m_items.size() - 2; i >= 0; i--) { if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer)) return; } } else { for (unsigned i = 0; i < m_items.size() - 1; i++) { if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer)) return; } }}void UniscribeController::resetControlAndState(){ memset(&m_control, 0, sizeof(SCRIPT_CONTROL)); memset(&m_state, 0, sizeof(SCRIPT_STATE)); // Set up the correct direction for the run. m_state.uBidiLevel = m_run.rtl(); // Lock the correct directional override. m_state.fOverrideDirection = m_run.directionalOverride();}bool UniscribeController::shapeAndPlaceItem(const UChar* cp, unsigned i, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer){ // Determine the string for this item. const UChar* str = cp + m_items[i].iCharPos; int len = m_items[i+1].iCharPos - m_items[i].iCharPos; SCRIPT_ITEM item = m_items[i]; // Set up buffers to hold the results of shaping the item. Vector<WORD> glyphs; Vector<WORD> clusters; Vector<SCRIPT_VISATTR> visualAttributes; clusters.resize(len);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -