From 70a9658c3a720fb5d641ba2cd5c596ceaf3a5ab0 Mon Sep 17 00:00:00 2001 From: Jeremy Bullock Date: Aug 23 2017 21:40:45 +0000 Subject: Merge pull request #1428 from shun-iwasawa/qt_typetool_fix Typetool Fix --- diff --git a/toonz/sources/common/tvrender/tfont_qt.cpp b/toonz/sources/common/tvrender/tfont_qt.cpp index 9ab2037..1080fd1 100644 --- a/toonz/sources/common/tvrender/tfont_qt.cpp +++ b/toonz/sources/common/tvrender/tfont_qt.cpp @@ -70,7 +70,7 @@ TFont::Impl::~Impl() {} //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- - +// returns the offset (advance of the cursor) for the current character TPoint TFont::drawChar(TVectorImageP &image, wchar_t charcode, wchar_t nextCharCode) const { QRawFont raw(QRawFont::fromFont(m_pimpl->m_font)); @@ -223,51 +223,49 @@ TPoint TFont::drawChar(TRasterCM32P &outImage, TPoint &unused, int inkId, //----------------------------------------------------------------------------- TPoint TFont::getDistance(wchar_t firstChar, wchar_t secondChar) const { - QRawFont raw(QRawFont::fromFont(m_pimpl->m_font)); - QChar chars[2] = {firstChar, secondChar}; - quint32 indices[2]; - QPointF advances[2]; - int count = 2; - - if (!raw.glyphIndexesForChars(chars, 2, indices, &count) || count != 2) { - return TPoint(0, 0); - } + QFontMetrics metrics(m_pimpl->m_font); + return TPoint(metrics.width(QChar(firstChar)), 0); +} - if (!raw.advancesForGlyphIndexes(indices, advances, 2, - QRawFont::KernedAdvances)) { - return TPoint(0, 0); - } +//----------------------------------------------------------------------------- - int advance = (int)(advances[0].x()); +int TFont::getMaxWidth() const { + QFontMetrics metrics(m_pimpl->m_font); + return metrics.maxWidth(); +} +//----------------------------------------------------------------------------- - return TPoint(advance, 0); +int TFont::getLineAscender() const { + QFontMetrics metrics(m_pimpl->m_font); + return metrics.ascent(); } //----------------------------------------------------------------------------- -int TFont::getMaxHeight() const { +int TFont::getLineDescender() const { QFontMetrics metrics(m_pimpl->m_font); - return metrics.ascent() - metrics.descent(); + return metrics.descent(); } //----------------------------------------------------------------------------- -int TFont::getMaxWidth() const { +int TFont::getLineSpacing() const { QFontMetrics metrics(m_pimpl->m_font); - return metrics.maxWidth(); + return metrics.lineSpacing(); } + //----------------------------------------------------------------------------- -int TFont::getLineAscender() const { +int TFont::getHeight() const { QFontMetrics metrics(m_pimpl->m_font); - return metrics.ascent(); + return metrics.height(); } //----------------------------------------------------------------------------- -int TFont::getLineDescender() const { +int TFont::getAverageCharWidth() const { QFontMetrics metrics(m_pimpl->m_font); - return metrics.descent(); + return metrics.averageCharWidth(); } //----------------------------------------------------------------------------- diff --git a/toonz/sources/include/tfont.h b/toonz/sources/include/tfont.h index 89caed5..1a368f7 100644 --- a/toonz/sources/include/tfont.h +++ b/toonz/sources/include/tfont.h @@ -53,13 +53,15 @@ public: // brief get kerning distance between two characters TPoint getDistance(wchar_t firstChar, wchar_t secondChar) const; - int getMaxHeight() const; int getMaxWidth() const; // void disableKerning(); // void enableKerning(); bool hasKerning() const; int getLineAscender() const; int getLineDescender() const; + int getLineSpacing() const; + int getHeight() const; + int getAverageCharWidth() const; bool hasVertical() const; std::wstring getFamily() const; std::wstring getTypeface() const; @@ -144,11 +146,13 @@ public: return getCurrentFont()->getDistance(firstChar, secondChar); } - int getMaxHeight() { return getCurrentFont()->getMaxHeight(); } int getMaxWidth() { return getCurrentFont()->getMaxWidth(); } bool hasKerning() { return getCurrentFont()->hasKerning(); } int getLineAscender() { return getCurrentFont()->getLineAscender(); } int getLineDescender() { return getCurrentFont()->getLineDescender(); } + int getLineSpacing() { return getCurrentFont()->getLineSpacing(); } + int getHeight() { return getCurrentFont()->getHeight(); } + int getAverageCharWidth() { return getCurrentFont()->getAverageCharWidth(); } bool hasVertical() { return getCurrentFont()->hasVertical(); } }; diff --git a/toonz/sources/tnztools/typetool.cpp b/toonz/sources/tnztools/typetool.cpp index e2f4f66..31f9db4 100644 --- a/toonz/sources/tnztools/typetool.cpp +++ b/toonz/sources/tnztools/typetool.cpp @@ -29,6 +29,7 @@ #include "trop.h" #include "toonz/ttileset.h" #include "toonz/glrasterpainter.h" +#include "toonz/stage.h" #include "tfont.h" #include "tw/keycodes.h" @@ -520,8 +521,10 @@ void TypeTool::loadFonts() { setFont(m_fontFamilyMenu.getValue()); } - m_scale = - TScale(m_dimension / (double)(TFontManager::instance()->getMaxHeight())); + // not used for now + m_scale = TScale(); + // m_scale = TScale(m_dimension / + // (double)(TFontManager::instance()->getHeight())); } //--------------------------------------------------------- @@ -596,19 +599,26 @@ void TypeTool::setTypeface(std::wstring typeface) { void TypeTool::setSize(std::wstring strSize) { // font e tool fields update - double dimension = std::stod(strSize); - if (m_dimension == dimension) return; - TImageP img = getImage(true); - TToonzImageP ti = img; + TImageP img = getImage(true); + TToonzImageP ti = img; + TVectorImageP vi = img; + // for vector levels, adjust size according to the ratio between + // the viewer dpi and the vector level's dpi + if (vi) dimension *= Stage::inch / Stage::standardDpi; + + if (m_dimension == dimension) return; TFontManager::instance()->setSize((int)dimension); assert(m_dimension != 0); double ratio = dimension / m_dimension; m_dimension = dimension; - m_scale = - TScale(m_dimension / (double)(TFontManager::instance()->getMaxHeight())); + + // not used for now + m_scale = TScale(); + // m_scale = TScale(m_dimension / + // (double)(TFontManager::instance()->getHeight())); // text update @@ -664,11 +674,11 @@ void TypeTool::stopEditing() { void TypeTool::updateStrokeChar() { TFontManager *instance = TFontManager::instance(); - double ascend = (double)(instance->getLineAscender()); - double descend = (double)(instance->getLineDescender()); - m_fontYOffset = m_dimension * descend / (fabs(ascend) + fabs(descend)); - m_scale = TScale(m_dimension / (double)(instance->getMaxHeight())); + // not used for now + m_scale = TScale(); + // m_scale = TScale(m_dimension / (double)(instance->getHeight())); + bool hasKerning = instance->hasKerning(); for (UINT i = 0; i < m_string.size(); i++) { if (hasKerning && i + 1 < m_string.size() && !m_string[i + 1].isReturn()) @@ -687,38 +697,53 @@ void TypeTool::updateCharPositions(int updateFrom) { UINT size = m_string.size(); TPointD currentOffset; TFontManager *instance = TFontManager::instance(); + m_fontYOffset = (double)(instance->getLineSpacing()) * m_scale.a11; + double descent = (double)(instance->getLineDescender()) * m_scale.a11; + double height = (double)(instance->getHeight()) * m_scale.a11; + double vLineSpacing = + (double)(instance->getAverageCharWidth()) * 2.0 * m_scale.a11; + + // Update from "updateFrom" if (updateFrom > 0) { if ((int)m_string.size() <= updateFrom - 1) return; - currentOffset = m_string[updateFrom - 1].m_charPosition - m_startPoint; + // Vertical case if (m_isVertical && !instance->hasVertical()) { if (m_string[updateFrom - 1].isReturn()) - currentOffset = TPointD(currentOffset.x - m_dimension, - -m_dimension - m_fontYOffset); + currentOffset = TPointD(currentOffset.x - vLineSpacing, -height); else - currentOffset = currentOffset + TPointD(0, -m_dimension); - } else { + currentOffset = currentOffset + TPointD(0, -height); + } + // Horizontal case + else { if (m_string[updateFrom - 1].isReturn()) - currentOffset = TPointD(0, currentOffset.y - m_dimension); + currentOffset = TPointD(0, currentOffset.y - m_fontYOffset); else currentOffset = currentOffset + TPointD(m_string[updateFrom - 1].m_offset, 0); } - } else if (m_isVertical && !instance->hasVertical()) - currentOffset = currentOffset + TPointD(0, -m_dimension - m_fontYOffset); + } + // Update whole characters + else { + if (m_isVertical && !instance->hasVertical()) + currentOffset = currentOffset + TPointD(0, -height); + else + currentOffset = currentOffset + TPointD(0, -descent); + } for (UINT j = updateFrom; j < size; j++) { m_string[j].m_charPosition = m_startPoint + currentOffset; - + // Vertical case if (m_isVertical && !instance->hasVertical()) { if (m_string[j].isReturn() || m_string[j].m_key == ' ') - currentOffset = TPointD(currentOffset.x - m_dimension, - -m_dimension - m_fontYOffset); + currentOffset = TPointD(currentOffset.x - vLineSpacing, -height); else - currentOffset = currentOffset + TPointD(0, -m_dimension); - } else { + currentOffset = currentOffset + TPointD(0, -height); + } + // Horizontal case + else { if (m_string[j].isReturn()) - currentOffset = TPointD(0, currentOffset.y - m_dimension); + currentOffset = TPointD(0, currentOffset.y - m_fontYOffset); else currentOffset = currentOffset + TPointD(m_string[j].m_offset, 0); } @@ -736,37 +761,45 @@ void TypeTool::updateCharPositions(int updateFrom) { void TypeTool::updateCursorPoint() { assert(0 <= m_cursorIndex && m_cursorIndex <= (int)m_string.size()); TFontManager *instance = TFontManager::instance(); - double ascend = (double)(instance->getLineAscender()); - double descend = (double)(instance->getLineDescender()); - m_fontYOffset = m_dimension * descend / (fabs(ascend) + fabs(descend)); + double descent = (double)(instance->getLineDescender()) * m_scale.a11; + double height = (double)(instance->getHeight()) * m_scale.a11; + double vLineSpacing = + (double)(instance->getAverageCharWidth()) * 2.0 * m_scale.a11; + m_fontYOffset = (double)(instance->getLineSpacing()) * m_scale.a11; + double scaledDimension = m_dimension * m_scale.a11; if (m_string.empty()) { if (!m_isVertical || instance->hasVertical()) - m_cursorPoint = m_startPoint + TPointD(0, m_dimension + m_fontYOffset); + m_cursorPoint = m_startPoint + TPointD(0, scaledDimension); else m_cursorPoint = m_startPoint; } else if (m_cursorIndex == (int)m_string.size()) { + // Horizontal case if (!m_isVertical || instance->hasVertical()) { if (m_string.back().isReturn()) m_cursorPoint = TPointD( - m_startPoint.x, m_string.back().m_charPosition.y + m_fontYOffset); + m_startPoint.x, m_string.back().m_charPosition.y - m_fontYOffset + + scaledDimension + descent); else m_cursorPoint = m_string.back().m_charPosition + TPointD(m_string.back().m_offset, 0) + - TPointD(0, m_dimension + m_fontYOffset); - } else if (m_string.back().isReturn()) - m_cursorPoint = TPointD(m_string.back().m_charPosition.x - m_dimension, - m_startPoint.y); - else - m_cursorPoint = - m_string.back().m_charPosition + TPointD(0, m_fontYOffset); + TPointD(0, scaledDimension + descent); + } + // Vertical case + else { + if (m_string.back().isReturn()) + m_cursorPoint = TPointD(m_string.back().m_charPosition.x - vLineSpacing, + m_startPoint.y); + else + m_cursorPoint = m_string.back().m_charPosition; + } } else { if (!m_isVertical || instance->hasVertical()) m_cursorPoint = m_string[m_cursorIndex].m_charPosition + - TPointD(0, m_dimension + m_fontYOffset); + TPointD(0, scaledDimension + descent); else - m_cursorPoint = m_string[m_cursorIndex].m_charPosition + - TPointD(0, m_dimension + m_fontYOffset); + m_cursorPoint = + m_string[m_cursorIndex].m_charPosition + TPointD(0, height); } } @@ -779,9 +812,11 @@ void TypeTool::updateTextBox() { double maxXLength = 0; TFontManager *instance = TFontManager::instance(); - double ascend = (double)(instance->getLineAscender()); - double descend = (double)(instance->getLineDescender()); - m_fontYOffset = m_dimension * descend / (fabs(ascend) + fabs(descend)); + double descent = (double)(instance->getLineDescender()) * m_scale.a11; + double height = (double)(instance->getHeight()) * m_scale.a11; + double vLineSpacing = + (double)(instance->getAverageCharWidth()) * 2.0 * m_scale.a11; + m_fontYOffset = (double)(instance->getLineSpacing()) * m_scale.a11; for (UINT j = 0; j < size; j++) { if (m_string[j].isReturn()) { @@ -792,7 +827,7 @@ void TypeTool::updateTextBox() { returnNumber++; } else { currentLineLength += (m_isVertical && !instance->hasVertical()) - ? m_dimension + ? height : m_string[j].m_offset; } } @@ -801,16 +836,15 @@ void TypeTool::updateTextBox() { maxXLength = currentLineLength; if (m_isVertical && !instance->hasVertical()) - m_textBox = TRectD(m_startPoint.x - m_dimension * returnNumber, + m_textBox = TRectD(m_startPoint.x - vLineSpacing * returnNumber, m_startPoint.y - maxXLength, - m_startPoint.x + m_dimension, m_startPoint.y) + m_startPoint.x + vLineSpacing, m_startPoint.y) .enlarge(cBorderSize * m_pixelSize); else m_textBox = TRectD(m_startPoint.x, - m_startPoint.y - (m_dimension * returnNumber) + m_fontYOffset, - m_startPoint.x + maxXLength, - m_startPoint.y + m_dimension + m_fontYOffset) + m_startPoint.y - (m_fontYOffset * returnNumber + descent), + m_startPoint.x + maxXLength, m_startPoint.y + height) .enlarge(cBorderSize * m_pixelSize); } @@ -909,9 +943,11 @@ glPushMatrix(); // draw cursor tglColor(TPixel32::Black); if (!m_isVertical || instance->hasVertical()) - tglDrawSegment(m_cursorPoint, m_cursorPoint + TPointD(0, -m_dimension)); + tglDrawSegment(m_cursorPoint, + m_cursorPoint + m_scale * TPointD(0, -m_dimension)); else - tglDrawSegment(m_cursorPoint, m_cursorPoint + TPointD(m_dimension, 0)); + tglDrawSegment(m_cursorPoint, + m_cursorPoint + m_scale * TPointD(m_dimension, 0)); } TPointD drawableCursor = m_cursorPoint;