1

我正在使用 PoDoFo 提取字符位移以正确更新文本矩阵。这是我的一个代码片段:

PdfString str, ucode_str;
std::stack<PdfVariant> *stack;
const PdfFontMetrics *f_metrics;
...

/* Convert string to UTF8 */
str = stack->top().GetString();
ucode_str = ts->font->GetEncoding()->ConvertToUnicode(str, ts->font);
stack->pop();
c_str = (char *) ucode_str.GetStringUtf8().c_str();

/* Font metrics to obtain a character displacement */
f_metrics = ts->font->GetFontMetrics();

for (j = 0; j < strlen(c_str); j++) {
    str_w = f_metrics->CharWidth(c_str[j]);

    /* Adjust text matrix using str_w */
    ...
}

它适用于某些 PDF 文件(str_w包含有用的宽度),但不适用于其他文件。在这些情况下str_w包含0.0. 我查看了 PoDoFo0.9.5源代码,发现CharWidth()所有子类都实现了PdfFontMetrics.

在这个字符串转换过程中我是否遗漏了一些重要的东西?

从 04.08.2017 更新

@mkl 在审查 PoDoFo 的代码方面做得非常好。但是,我意识到我必须获得一些不同的参数。准确地说,我需要一个以文本空间单位表示的字形宽度(请参阅PDF 参考1.7、5.1.3 Glyph Positioning and Metrics ) ,但其实现方式如下:CharWidth()PdfFontMetricsObject.cpp

double PdfFontMetricsObject::CharWidth(unsigned char c) const
{
    if (c >= m_nFirst && c <= m_nLast &&
        c - m_nFirst < static_cast<int>(m_width.GetSize())) {
        double dWidth = m_width[c - m_nFirst].GetReal();

        return (dWidth * m_matrix.front().GetReal() * this->GetFontSize() + this->GetFontCharSpace()) * this->GetFontScale() / 100.0;
    }

    if (m_missingWidth != NULL)
        return m_missingWidth->GetReal();
    else
        return m_dDefWidth;
}

宽度是使用附加乘数(如字体大小、字符空间等)计算的。我真正需要的dWidth * m_matrix.front().GetReal()只是。因此,我决定GetGlyphWidth(int c)从同一个文件中实现,例如:

double PdfFontMetricsObject::GetGlyphWidth(int c) const
{
    if (c >= m_nFirst && c <= m_nLast &&
        c - m_nFirst < static_cast<int>(m_width.GetSize())) {
        double dWidth = m_width[c - m_nFirst].GetReal();
        return dWidth * m_matrix.front().GetReal();
    }
    return 0.0;
}

并调用这个而不是CharWidth()从第一个列表中调用。

4

1 回答 1

0

如果我正确理解了 Podofo 代码(我不是真正的 Podofo 专家......),则PdfFontMetricsObject该类用于表示现有 PDF 中包含的字体的度量:

/** Create a font metrics object based on an existing PdfObject
 *
 *  \param pObject an existing font descriptor object
 *  \param pEncoding a PdfEncoding which will NOT be owned by PdfFontMetricsObject
 */
PdfFontMetricsObject( PdfObject* pFont, PdfObject* pDescriptor, const PdfEncoding* const pEncoding );

这里的方法CharWidth是这样实现的:

double PdfFontMetricsObject::CharWidth( unsigned char c ) const
{
    if( c >= m_nFirst && c <= m_nLast
        && c - m_nFirst < static_cast<int>(m_width.GetSize()) )
    {
        double dWidth = m_width[c - m_nFirst].GetReal();

        return (dWidth * m_matrix.front().GetReal() * this->GetFontSize() + this->GetFontCharSpace()) * this->GetFontScale() / 100.0;
    }

    if( m_missingWidth != NULL )
        return m_missingWidth->GetReal ();
    else
        return m_dDefWidth;
}

特别是看到该参数c不是根据字体编码进行编码的,而是保留原样用于在宽度数组中的查找。因此,此方法的预期输入似乎不是 ASCII 或 ANSI 字符代码,而是原始字形 ID。

另一方面,您的代码已经将字形 ID 转换为 UTF-8 中的 Unicode,因此,基本上尝试通过 ANSI 字符代码进行查找。


这将匹配示例文档,处理错误的 PDF 中的典型字体编码如下所示

28 0 obj
<<
  /Differences[0/B/G/W/a/d/e/f/g  9/i/l/n/o/p/r/space/t/w]
  /BaseEncoding/MacRomanEncoding
  /Type/Encoding
>>
endobj

字形代码从 0 ( FirstChar ) 到 17 ( LastChar ),或

12 0 obj
<<
  /Differences[1/A/B/C/D/F/I/L/M/N/O/P/R/T/U/a/c/d
                /degree/e/eight/f/five/four/g/h
               27/i/l/m/n/o/one/p/parenleft/parenright
                /period/r/registered/s/space
                /t/three/two/u/w/zero]
  /BaseEncoding/MacRomanEncoding
  /Type/Encoding
>>
endobj 

字形代码从 1 ( FirstChar ) 到 46 ( LastChar )。

因此,这些编码处理从 0 开始的所有必需字形的字形代码,并没有真正涵盖那么多字形

因此,CharWidth将返回0所有大于 17 或大于 46 的 char 值,这意味着所有(在前一种情况下)或大多数(在后一种情况下)ANSI 非控制字符。

另一方面,正确处理的 PDF 中的典型字体编码如下所示:

1511 0 obj
<<
  /Type/Encoding
  /BaseEncoding/WinAnsiEncoding
  /Differences[
    1/Delta/Theta
    8/Phi
    11/ff/fi/fl/ffi
    39/quoteright
  ]
>>
endobj 

字形代码从 1 ( FirstChar ) 到 122 ( LastChar )。

这些编码基本上是WinAnsiEncoding,在较低的值中添加了少量,特别是控制字符值。


因此,您可以做的是迭代中的字形代码str(允许您调用CharWidth它们)并在需要时将它们单独转换为 Unicode,而不是先转换str为 Unicode ucode_str,然后在ucode_str.

于 2017-08-03T14:46:54.643 回答