前言 ofd作为板式文档规范,相当于国产化的pdf。由于pdf标准制定的较早,相关生态也比较完备,市面上的pdf阅读器种类繁多。国内ofd阅读器寥寥无几,作者此前采用wpf开发了一款阅读器,但该阅读器只能在windows上运行。若实现跨平台运行,采用QT开发应该是首选。笔者并无QT开发经验,但有多年MFC开发经验,又对ofd研究多年;编程到达一定境界考验的就是思维,在学习QT的过程中,感觉都是熟悉的味道的。边学习边开发,终于完成了一款简易的ofd阅读器。简述开发思路,希望对读者有所启发。

程序下载地址:百度网盘: https://pan.baidu.com/s/1_uyTWCP_jFOd0YR1-1Yapw 提取码: cxpv。

功能简述:

阅读器实现了缩放、旋转、选中、复制、单双页显示等功能。

开发思路解析

ofd阅读器显示的内容包括:文字、图形、图等,称之为图元;阅读器可能需要显示成千上万个图元。采用qt完成此功能,有多重方案可供选择,选择方案时必须考虑下列因素:1)显示的性能。2)图元与鼠标键盘的交互。我选择了“Graphics View Framework 图形视图框架”;程序处理的逻辑见下图:

ofd解压:

  ofd本身就是压缩文件,和zip后缀的文件处理完全一样。解压缩采用QuaZip库。作者在此库基础上作了进一步封装,使之更便于使用。

OfdFileReader::OfdFileReader()
{
_pZipInfo = nullptr;
_file = nullptr;
} OfdFileReader::~OfdFileReader()
{
MemManage::instance()->Delete(_pZipInfo);
MemManage::instance()->Delete(_file);
} bool OfdFileReader::Open(QString fileName)
{
MemManage::instance()->Delete( _file); _file =MemManage::instance()->New<QFile,QString>(fileName); if (!_file->open(QIODevice::ReadOnly))
return false; _ofdFileName = fileName;
return Open(_file);
} bool OfdFileReader::Open(QIODevice *ioDevice)
{
MemManage::instance()->Delete(_pZipInfo); _pZipInfo =MemManage::instance()->New<QuaZip,QIODevice*>(ioDevice);
bool isOpen = _pZipInfo->open(QuaZip::mdUnzip);
if(!isOpen)
return false; _listFilePath.clear();
GetAllZipInfo(); return true;
} QString OfdFileReader::GetFileFullName()
{
return _ofdFileName;
} QString OfdFileReader::GetFileShortName()
{
QFileInfo fileInfo(_ofdFileName);
return fileInfo.baseName();
} void OfdFileReader::GetAllZipInfo()
{
for (bool f = _pZipInfo->goToFirstFile(); f;f=_pZipInfo->goToNextFile())
{
QString relativePath = _pZipInfo->getCurrentFileName();
_listFilePath.append(relativePath); //qDebug() << relativePath;
}
} int OfdFileReader::GetFileCount()
{
return _listFilePath.count();
} QString OfdFileReader::GetFilePath(int index)
{
return _listFilePath[index];
} QStringList OfdFileReader::GetFilePathList()
{
return _listFilePath;
} QByteArray OfdFileReader::GetFileContent(const QString& relativePath)
{
if(relativePath.size()==0)
{
QByteArray empty;
return empty;
} _pZipInfo->setCurrentFile(relativePath); QuaZipFile zFile(_pZipInfo,0);
if(!zFile.open(QIODevice::ReadOnly))
{
QByteArray empty;
return empty;
} QByteArray ba = zFile.readAll();
zFile.close();
return ba;
}

xml解析

  ofd主要是由xml文本和资源文件组成。qt解析xml有两个库:DOM解析(QDomDocument)和流式解析(QXmlStreamReader)。DOM解析使用起来简单,但是性能慢;流式解析反之。从性能角度考虑,作者采用了流式解析的方法。

Qt Graphics View Framework 图形视图框架

  绘制大量图元最佳方案就是采用qt提供的“Graphics View Framework”架构。此架构确保高效的绘制大量图元,又能快速的根据区域定位到图元。该架构采用面向对象的方法处理图元,减轻了开发难度。图元的描述称之为scene,图元显示为view。一个scene可以由多个view展示。首先需要将ofd页面中文字、线、图等元素转换成对应的scene。以显示文字为例,定义类型 class OfdVisualItemText : public QGraphicsObject。需要实现两个虚函数:

   QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
paint函数根据scene数据,绘制对应的文字。第一次绘制时,须记录每个文字的区域;鼠标滑动时,根据选择区域与每个文字的关系,确定文字是否被选中。
void OfdVisualItemText::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget); painter->setRenderHint(QPainter::TextAntialiasing);
painter->setBrush(Qt::black);
painter->setPen(Qt::black); SetPen(painter);
SetFont(painter); //SetCTMTransfer(painter);
if(_isFirstPaint)
{
SetCTMTransfer();
} if(_isSelect)
{
QList<QRectData*> selectData = _boundingRectManage.GetSelectData(_selectPolygon);
foreach(QRectData *item,selectData)
{
painter->fillRect(item->rect,*OfdViewParam::TextSelectBrush);
}
} OfdPageItemText *itemText = (OfdPageItemText*)_ofdPageItem; int charCount = itemText->TextCode.GetCharCount();
QChar charItem;
float x;
float y; QRectF textboundingRect;
QRectF textClipRect; float baseline = GetBaseline();
for(int i=0;i<charCount;i++)
{
itemText->TextCode.GetChar(i,charItem,x,y);
double xPixel = OfdConvert::OfdMMToPixel(x);
double yPixel = OfdConvert::OfdMMToPixel(y);
QString textChar(charItem); textClipRect.setRect(xPixel,yPixel-baseline,10000,10000);
painter->drawText(textClipRect,0,textChar,&textboundingRect); AdjustTextRect(textboundingRect); if(_isFirstPaint )
{
TextInfo *textInfo = MemManage::instance()->New<TextInfo>();
textInfo->Text = textChar;
_textCharInfoGroup.append(textInfo);
_boundingRectManage.AddRect(textboundingRect,textInfo);
}
} _isFirstPaint = false;
}

 阅读器操作截图

后记:理清思路,选对框架是成功的第一步。qt作为一款优秀的跨平台框架,为方便我们开发提供了大量的类库。在充分理解ofd的基础上,配合qt的“Graphics View Framework”框架,开发ofd阅读器并非遥不可及。目前该阅读器仅完成了基本的功能,后续会逐步完善,敬请期待。

最新文章

  1. vue+node开发手机端h5页面开发遇到的坑
  2. TensorFlow的开源与Hadoop的开源
  3. IOS - SDWebImage 非ARC 问题
  4. HTML无序列表和有序列表
  5. 标准Web系统的架构分层[转]
  6. CentOS 6.5安装MySQL中文乱码问题解决
  7. HeadFirst设计模式之命令模式
  8. PureMVC(JS版)源码解析(一):观察者模式解析
  9. 常用SQL笔记总结
  10. [知识梳理]课本3&amp;9.1
  11. android 小项目------黑名单app
  12. iOS—使用picker View
  13. 渐变UI
  14. Java编程思想__异常
  15. LeetCode(72):编辑距离
  16. Linux:磁盘配额
  17. Java8 list根据对象某个属性去重
  18. 微信小程序的视频教程
  19. redis之RDB持久化与AOF持久化
  20. php 前台生成多维数组 后台批量添加

热门文章

  1. NGK.IO网络安全大会暨区块链安全与应用创新论坛圆满落幕
  2. 「NGK每日快讯」12.14日NGK公链第41期官方快讯!
  3. linux DRM 之 GEM 笔记
  4. m1款MacBook Air 使用3个月总结及原生运行于apple架构软件推荐
  5. Ctf_show Web[1-8]
  6. EFCodeFirst 数据库连接
  7. 剑指 Offer 58 - I. 翻转单词顺序 + 双指针
  8. C++树——遍历二叉树
  9. 通达OA后台getshell
  10. Java__包机制__用户输入