1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
* Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
* Copyright (C) 2007-2009 Torch Mobile, Inc.
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
* Copyright (C) 2008 Holger Hans Peter Freyther
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``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 COMPUTER, INC. OR
* 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 "Font.h"
#include "AffineTransform.h"
#include "FloatRect.h"
#include "FontCache.h"
#include "FontData.h"
#include "FontGlyphs.h"
#include "GlyphBuffer.h"
#include "GraphicsContext.h"
#include "IntRect.h"
#include "NotImplemented.h"
#include "TextRun.h"
#include "WidthIterator.h"
#include <wtf/MathExtras.h>
#include <wtf/OwnPtr.h>
#include <wtf/unicode/Unicode.h>
#include <windows.h>
using namespace WTF::Unicode;
namespace WebCore {
HDC g_screenDC = GetDC(0);
class ScreenDcReleaser {
public:
~ScreenDcReleaser()
{
ReleaseDC(0, g_screenDC);
}
};
ScreenDcReleaser releaseScreenDc;
void Font::drawGlyphs(GraphicsContext* graphicsContext, const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer,
int from, int numGlyphs, const FloatPoint& point) const
{
graphicsContext->drawText(fontData, glyphBuffer, from, numGlyphs, point);
}
class TextRunComponent {
public:
TextRunComponent() : m_textRun(0, 0) {}
TextRunComponent(const UChar *start, int length, const TextRun& parentTextRun, const Font &font, int offset);
TextRunComponent(int spaces, const Font &font, int offset);
~TextRunComponent() { m_textRun; }
bool isSpace() const { return m_spaces; }
int textLength() const { return m_spaces ? m_spaces : m_textRun.length(); }
TextRun m_textRun;
float m_width;
int m_offset;
int m_spaces;
};
TextRunComponent::TextRunComponent(const UChar *start, int length, const TextRun& parentTextRun, const Font &font, int o)
: m_textRun(start, length, 0, 0
, parentTextRun.allowsTrailingExpansion() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion
, parentTextRun.direction()
, parentTextRun.directionalOverride())
, m_offset(o)
, m_spaces(0)
{
m_textRun.setTabSize(parentTextRun.allowTabs(), parentTextRun.tabSize());
WidthIterator it(&font, m_textRun);
it.advance(m_textRun.length(), 0);
m_width = it.m_runWidthSoFar;
}
TextRunComponent::TextRunComponent(int s, const Font &font, int o)
: m_textRun(0, 0)
, m_offset(o)
, m_spaces(s)
{
m_width = s * font.primaryFont()->widthForGlyph(' ');
}
typedef Vector<TextRunComponent, 128> TextRunComponents;
static int generateComponents(TextRunComponents* components, const Font &font, const TextRun &run)
{
int letterSpacing = font.letterSpacing();
int wordSpacing = font.wordSpacing();
int padding = run.expansion();
int numSpaces = 0;
if (padding) {
for (int i = 0; i < run.length(); i++)
if (Font::treatAsSpace(run[i]))
++numSpaces;
}
int offset = 0;
if (letterSpacing) {
// need to draw every letter on it's own
int start = 0;
if (Font::treatAsSpace(run[0])) {
int add = 0;
if (numSpaces) {
add = padding/numSpaces;
padding -= add;
--numSpaces;
}
components->append(TextRunComponent(1, font, offset));
offset += add + letterSpacing + components->last().m_width;
start = 1;
}
for (int i = 1; i < run.length(); ++i) {
UChar ch = run[i];
if (U16_IS_LEAD(ch) && U16_IS_TRAIL(run[i-1]))
ch = U16_GET_SUPPLEMENTARY(ch, run[i-1]);
if (U16_IS_TRAIL(ch) || category(ch) == Mark_NonSpacing)
continue;
if (Font::treatAsSpace(run[i])) {
int add = 0;
if (i - start > 0) {
components->append(TextRunComponent(run.characters16() + start, i - start,
run, font, offset));
offset += components->last().m_width + letterSpacing;
}
if (numSpaces) {
add = padding/numSpaces;
padding -= add;
--numSpaces;
}
components->append(TextRunComponent(1, font, offset));
offset += wordSpacing + add + components->last().m_width + letterSpacing;
start = i + 1;
continue;
}
if (i - start > 0) {
components->append(TextRunComponent(run.characters16() + start, i - start,
run,
font, offset));
offset += components->last().m_width + letterSpacing;
}
start = i;
}
if (run.length() - start > 0) {
components->append(TextRunComponent(run.characters16() + start, run.length() - start,
run,
font, offset));
offset += components->last().m_width;
}
offset += letterSpacing;
} else {
int start = 0;
for (int i = 0; i < run.length(); ++i) {
if (Font::treatAsSpace(run[i])) {
if (i - start > 0) {
components->append(TextRunComponent(run.characters16() + start, i - start,
run,
font, offset));
offset += components->last().m_width;
}
int add = 0;
if (numSpaces) {
add = padding/numSpaces;
padding -= add;
--numSpaces;
}
components->append(TextRunComponent(1, font, offset));
offset += add + components->last().m_width;
if (i)
offset += wordSpacing;
start = i + 1;
}
}
if (run.length() - start > 0) {
components->append(TextRunComponent(run.characters16() + start, run.length() - start,
run,
font, offset));
offset += components->last().m_width;
}
}
return offset;
}
void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point,
int from, int to) const
{
if (to < 0)
to = run.length();
if (from < 0)
from = 0;
TextRunComponents components;
int w = generateComponents(&components, *this, run);
int curPos = 0;
for (int i = 0; i < (int)components.size(); ++i) {
const TextRunComponent& comp = components.at(i);
int len = comp.textLength();
int curEnd = curPos + len;
if (curPos < to && from < curEnd && !comp.isSpace()) {
FloatPoint pt = point;
if (run.rtl())
pt.setX(point.x() + w - comp.m_offset - comp.m_width);
else
pt.setX(point.x() + comp.m_offset);
drawSimpleText(context, comp.m_textRun, pt, from - curPos, std::min(to, curEnd) - curPos);
}
curPos += len;
if (from < curPos)
from = curPos;
}
}
void Font::drawEmphasisMarksForComplexText(GraphicsContext* /* context */, const TextRun& /* run */, const AtomicString& /* mark */, const FloatPoint& /* point */, int /* from */, int /* to */) const
{
notImplemented();
}
float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
{
TextRunComponents components;
int w = generateComponents(&components, *this, run);
return w;
}
int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat, bool includePartialGlyphs) const
{
// FIXME: This truncation is not a problem for HTML, but only affects SVG, which passes floating-point numbers
// to Font::offsetForPosition(). Bug http://webkit.org/b/40673 tracks fixing this problem.
int position = static_cast<int>(xFloat);
TextRunComponents components;
int w = generateComponents(&components, *this, run);
if (position >= w)
return run.length();
int offset = 0;
if (run.rtl()) {
for (size_t i = 0; i < components.size(); ++i) {
const TextRunComponent& comp = components.at(i);
int xe = w - comp.m_offset;
int xs = xe - comp.m_width;
if (position >= xs)
return offset + (comp.isSpace()
? static_cast<int>((position - xe) * comp.m_spaces / std::max(1.f, comp.m_width) + 0.5)
: offsetForPositionForSimpleText(comp.m_textRun, position - xs, includePartialGlyphs));
offset += comp.textLength();
}
} else {
for (size_t i = 0; i < components.size(); ++i) {
const TextRunComponent& comp = components.at(i);
int xs = comp.m_offset;
int xe = xs + comp.m_width;
if (position <= xe) {
if (position - xs >= xe)
return offset + comp.textLength();
return offset + (comp.isSpace()
? static_cast<int>((position - xs) * comp.m_spaces / std::max(1.f, comp.m_width) + 0.5)
: offsetForPositionForSimpleText(comp.m_textRun, position - xs, includePartialGlyphs));
}
offset += comp.textLength();
}
}
return run.length();
}
static float cursorToX(const Font* font, const TextRunComponents& components, int width, bool rtl, int cursor)
{
int start = 0;
for (size_t i = 0; i < components.size(); ++i) {
const TextRunComponent& comp = components.at(i);
if (start + comp.textLength() <= cursor) {
start += comp.textLength();
continue;
}
int xs = comp.m_offset;
if (rtl)
xs = width - xs - comp.m_width;
int pos = cursor - start;
if (comp.isSpace()) {
if (rtl)
pos = comp.textLength() - pos;
return xs + pos * comp.m_width / comp.m_spaces;
}
WidthIterator it(font, comp.m_textRun);
GlyphBuffer glyphBuffer;
it.advance(pos, &glyphBuffer);
return xs + it.m_runWidthSoFar;
}
return width;
}
FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& pt,
int h, int from, int to) const
{
TextRunComponents components;
int w = generateComponents(&components, *this, run);
if (!from && to == run.length())
return FloatRect(pt.x(), pt.y(), w, h);
float x1 = cursorToX(this, components, w, run.rtl(), from);
float x2 = cursorToX(this, components, w, run.rtl(), to);
if (x2 < x1)
std::swap(x1, x2);
return FloatRect(pt.x() + x1, pt.y(), x2 - x1, h);
}
bool Font::canReturnFallbackFontsForComplexText()
{
return false;
}
bool Font::canExpandAroundIdeographsInComplexText()
{
return false;
}
} // namespace WebCore
|