📄 kwqfontmetrics.cpp
字号:
/*
* Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
* Portions Copyright (c) 2005 Nokia Corporation, 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.
*
* 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 "KWQFontMetrics.h"
#include "KWQFont.h"
#include "KWQLogging.h"
#include "KWQFontFamily.h"
#include "WebCoreTextRenderer.h"
#include "WebCoreTextRendererFactory.h"
#define SCALE(a) ((a)*100/_scalingFactor)
QFontMetrics::QFontMetrics() : _scalingFactor(100)
{
}
QFontMetrics::QFontMetrics(const QFont &font)
: _font(font), _scalingFactor(100)
{
}
QFontMetrics::QFontMetrics(const QFontMetrics &other)
: _font(other._font), _scalingFactor(other._scalingFactor)
{
}
QFontMetrics::~QFontMetrics()
{
}
QFontMetrics &QFontMetrics::operator=(const QFontMetrics &other)
{
_scalingFactor = other._scalingFactor;
_font = other._font;
return *this;
}
void QFontMetrics::setFont(const QFont &font)
{
_font = font;
}
int QFontMetrics::ascent() const
{
return SCALE(_font.Renderer()->Ascent());
}
int QFontMetrics::descent() const
{
return SCALE(_font.Renderer()->Descent());
}
int QFontMetrics::height() const
{
// According to Qt documentation:
// "This is always equal to ascent()+descent()+1 (the 1 is for the base line)."
// We DO NOT match the Qt behavior here. This is intentional.
return ascent() + descent();
}
int QFontMetrics::lineSpacing() const
{
return SCALE(_font.Renderer()->LineSpacing());
}
int QFontMetrics::xHeight() const
{
return SCALE(_font.Renderer()->XHeight());
}
int QFontMetrics::width(QChar qc) const
{
TText c = qc.unicode();
CREATE_FAMILY_ARRAY(_font, families);
TWebCoreTextRun run(&c, 1, 0, 1);
TWebCoreTextStyle style;
style.iFamilies = families;
return SCALE(_font.Renderer()->WidthForRun(run, style));
}
int QFontMetrics::charWidth(const QString &s, int pos) const
{
return width(s[pos]);
}
int QFontMetrics::width(char c) const
{
TText ch = (uchar) c;
CREATE_FAMILY_ARRAY(_font, families);
TWebCoreTextRun run = TWebCoreTextRun(&ch, 1, 0, 1);
TWebCoreTextStyle style;
style.iFamilies = families;
return SCALE(_font.Renderer()->WidthForRun(run, style));
}
int QFontMetrics::width(const QString &qstring, int len) const
{
CREATE_FAMILY_ARRAY(_font, families);
int length = len == -1 ? qstring.length() : len;
TWebCoreTextRun run = TWebCoreTextRun((const TText*)qstring.unicode(), length, 0, length);
TWebCoreTextStyle style;
style.iFamilies = families;
return SCALE(_font.Renderer()->WidthForRun(run, style));
}
int QFontMetrics::width(const QChar *uchars, int len) const
{
CREATE_FAMILY_ARRAY(_font, families);
TWebCoreTextRun run = TWebCoreTextRun((const TText *)uchars, len, 0, len);
TWebCoreTextStyle style;
style.iFamilies = families;
return SCALE(_font.Renderer()->WidthForRun(run, style));
}
float QFontMetrics::floatWidth(const QChar *uchars, int slen, int pos, int len,
int letterSpacing, int wordSpacing, bool smallCaps) const
{
return (float)width(uchars,slen,pos,len,letterSpacing,wordSpacing,smallCaps);
}
int QFontMetrics::width(const QChar *uchars, int slen, int pos, int len,
int letterSpacing, int wordSpacing, bool smallCaps) const
{
CREATE_FAMILY_ARRAY(_font, families);
TWebCoreTextRun run = TWebCoreTextRun((const TText *)uchars, slen, pos, pos+len);
TWebCoreTextStyle style;
style.iLetterSpacing = letterSpacing;
style.iWordSpacing = wordSpacing;
style.iSmallCaps = smallCaps;
style.iFamilies = families;
return SCALE(_font.Renderer()->WidthForRun(run, style));
}
float QFontMetrics::floatCharacterWidths(const QChar *uchars, int slen, int pos, int len, int toAdd, float *buffer, int letterSpacing, int wordSpacing, bool smallCaps) const
{
CREATE_FAMILY_ARRAY(_font, families);
TWebCoreTextRun run = TWebCoreTextRun((const TText *)uchars, slen, pos, pos+len);
TWebCoreTextStyle style;
style.iLetterSpacing = letterSpacing;
style.iWordSpacing = wordSpacing;
style.iSmallCaps = smallCaps;
style.iPadding = toAdd;
style.iFamilies = families;
float res = SCALE((float)_font.Renderer()->WidthForRun(run, style));
if (buffer)
{
*buffer = res;
}
return res;
}
int QFontMetrics::checkSelectionPoint (QChar *s, int slen, int pos, int len, int toAdd, int letterSpacing, int wordSpacing, bool smallCaps, int x, bool reversed, bool includePartialGlyphs) const
{
CREATE_FAMILY_ARRAY(_font, families);
TWebCoreTextRun run = TWebCoreTextRun((const TText *)s, slen, pos, pos+len);
TWebCoreTextStyle style;
style.iLetterSpacing = letterSpacing;
style.iWordSpacing = wordSpacing;
style.iSmallCaps = smallCaps;
style.iFamilies = families;
style.iPadding = toAdd;
return SCALE(_font.Renderer()->PointToOffset(run, style, x, reversed, includePartialGlyphs));
}
QRect QFontMetrics::boundingRect(const QString &qstring, int len) const
{
return QRect(0, 0, width(qstring, len), height());
}
QRect QFontMetrics::boundingRect(int x, int y, int width, int height, int flags, const QString &str) const
{
// FIXME: need to support word wrapping?
return QRect(x, y, width, height).intersect(boundingRect(str));
}
QSize QFontMetrics::size(int, const QString &qstring) const
{
return QSize(width(qstring), height());
}
void QFontMetrics::setScalingFactor(int factor)
{
_scalingFactor = factor;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -