c++ - 使用 PoDoFo 获得的字符位移不正确

标签 c++ pdf fonts podofo

我正在使用 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。我查看了 PoDoFo 0.9.5 源代码,发现 CharWidth() 是为 PdfFontMetrics 的所有子类实现的。

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

2017 年 8 月 4 日更新

@mkl 在审查 PoDoFo 的代码方面做得非常好。但是,我意识到我必须获得一些不同的参数。准确地说,我需要一个以文本空间单位表示的字形宽度(参见PDF Reference 1.75.1.3 字形定位和指标),但是 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;
}

并调用这个而不是第一个 list 中的 CharWidth()

最佳答案

如果我正确理解 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 没有根据字体编码进行编码,而是保留为在 widths 数组中查找。因此,此方法的预期输入似乎不是 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 开始的所有必需字形的字形代码,并没有真正涵盖那么多字形

因此,对于所有大于 17 或大于 46 的字符值,CharWidth 将返回 0,这意味着所有(在前一种情况下)或大部分(在后一种情况下)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 中的 ANSI 字符。

关于c++ - 使用 PoDoFo 获得的字符位移不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45483845/

相关文章:

visual-studio - Visual Studio字体和颜色:需要更多的可自定义性

c++ - Opengl平滑着色生成对象

C++ Lambdas : capture list vs. 参数列表

php - Symfony2 : disable Twig cache

php - 生成带有图像的 PDF 太慢

php - 使用 PHP 代码将 PJL 命令添加到 PDF 文件中

css - 在 OpenStack Horizo​​n 中更改字体

c# - 如何找出窗口标题的字体大小?

c++ - vector 分配崩溃

C++在函数中分配动态内存 - 新手问题