彻底解决使用Doxygen 1.5.6自动生成的chm文件导航栏中文乱码问题

最近升级了一下doxygen到最新版1.5.6,结果发现生成的chm文件导航栏中文会出现乱码,折腾了一天,终于查明原因并解决,特此分享给大家。

原因:
doxygen 1.5.6版(不知道从哪个版本开始的)内部一律使用UTF-8编码,生成的index.hhc文件也是使用UTF-8编码,然而微软的hhc编译工具不支持UTF-8编码,结果导致编译出的chm文件正文部分中文一切正常,但导航栏全是乱码。从网上搜了一圈,好像大家基本都遇到了这一情况,解决办法各异。

解决办法:
解决办法的核心就是将index.hhc文件用编码转换工具(例如iconv,ultraedit等)转换成GBK,再用hhc编译即可。

由于doxygen会生成index.hhc之后直接自动调用hhc进行编译,中间并未留有供我进行转换的机会,而我又不想在doxygen运行完毕生成了chm文件之后再重新修改index.hhc文件(自动或手动),然后再次重新生成chm文件,没办法,只有修改doxygen的源码重新编译了。


修改过程:
首先下载源码并编译doxygen,注意一定要先阅读编译指南,我就是没看编译指南导致走了不少弯路,因为doxygen编译过程中要用到UnxUtil工具,需要下载并装好。

编译通过后,找到生成index.hhc文件的源代码文件HtmlHelp.h/HtmlHelp.cpp,进行修改。修改思路为:
在保存index.hhc文件之前先进行编码转换,直接转换到GBK编码在存储,这样就可以直接生成GBK编码的index.hhc文件,后续过程就水到渠成了。

相关代码:
HtmlHelp.h
None.gif class  HtmlHelp  :  public  IndexIntf
ExpandedBlockStart.gif
{
InBlock.gif  dot.gif
InBlock.gif  
private:
InBlock.gif  dot.gif
InBlock.gif    QFile 
*cf,*kf; 
InBlock.gif    QBuffer 
*cb;        // Added by zwjia
InBlock.gif
    QTextStream cts,kts;
InBlock.gif  dot.gif
ExpandedBlockEnd.gif}
;


HtmlHelp.cpp
None.gif void HtmlHelp::initialize()
ExpandedBlockStart.gif {
InBlock.gif  const char *str = Config_getString("CHM_INDEX_ENCODING");
InBlock.gif  if(!str) str = "Windows-1250";
InBlock.gif  m_fromUtf8 = portable_iconv_open(str,"UTF-8"); 
ExpandedSubBlockStart.gif  /* open the contents file&nb
ExpandedSubBlockEnd.gifsp;
*/
InBlock.gif  QCString fName = Config_getString("HTML_OUTPUT") + "/index.hhc";
InBlock.gif  cf = new QFile(fName);
InBlock.gif  if (!cf->open(IO_WriteOnly))
ExpandedSubBlockStart.gif  {
InBlock.gif    err("Could not open file %s for writing\n",fName.data());
InBlock.gif    exit(1);
ExpandedSubBlockEnd.gif  }
InBlock.gif  cb = new QBuffer();       // Added by zwjia
InBlock.gif
  cb->open(IO_WriteOnly);   // Added by zwjia
ExpandedSubBlockStart.gif
  /* Write the header of the contents file */
InBlock.gif  cts.setDevice(cb);
InBlock.gif  cts.setEncoding(QTextStream::Latin1);
InBlock.gif  dot.gif
ExpandedBlockEnd.gif}
None.gif
None.gif void HtmlHelp::finalize()
ExpandedBlockStart.gif {
InBlock.gif  // end the contents file
InBlock.gif
  cts << "</UL>\n";
InBlock.gif  cts << "</BODY>\n";
InBlock.gif  cts << "</HTML>\n";
InBlock.gif  cts.unsetDevice();
ExpandedSubBlockStart.gif  ////
ExpandedSubBlockEnd.gif
////
InBlock.gif  // Modified by zwjia
InBlock.gif
  QByteArray buf = cb->buffer();
InBlock.gif  void* handle = portable_iconv_open("GBK","UTF-8");
InBlock.gif  size_t inputsize = buf.size();
InBlock.gif  size_t outputsize = inputsize*4+1;
InBlock.gif  charin = buf.data();
InBlock.gif  QByteArray outbuf;
InBlock.gif  outbuf.resize(outputsize);
InBlock.gif  charout = outbuf.data();
InBlock.gif  size_t len = portable_iconv(handle,(const char **)&in,&inputsize, &out, &outputsize );
InBlock.gif  outbuf.resize(out-outbuf.data());
InBlock.gif  cf->writeBlock(outbuf);
InBlock.gif  cf->close();
InBlock.gif  delete cf;
InBlock.gif  delete cb;
InBlock.gif  portable_iconv_close(handle);
InBlock.gif  // Modified by zwjia --end
ExpandedSubBlockStart.gif
  //
InBlock.gif  dot.gif
ExpandedBlockEnd.gif}

重新编译后,大功告成!
本想顺便把编译后的exe作为附件贴在这里供大家直接使用,却没找到贴附件的地方...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值