| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is a part of the libzmf project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include <cassert> |
| 11 | |
| 12 | #include <libzmf/libzmf.h> |
| 13 | |
| 14 | #include "BMIHeader.h" |
| 15 | #include "BMIParser.h" |
| 16 | #include "ZBRHeader.h" |
| 17 | #include "ZBRParser.h" |
| 18 | #include "ZMF4Header.h" |
| 19 | #include "ZMF4Parser.h" |
| 20 | #include "libzmf_utils.h" |
| 21 | |
| 22 | namespace libzmf |
| 23 | { |
| 24 | |
| 25 | namespace |
| 26 | { |
| 27 | |
| 28 | struct DetectionInfo |
| 29 | { |
| 30 | DetectionInfo(); |
| 31 | |
| 32 | RVNGInputStreamPtr m_content; |
| 33 | RVNGInputStreamPtr m_package; |
| 34 | ZMFDocument::Type m_type; |
| 35 | ZMFDocument::Kind m_kind; |
| 36 | }; |
| 37 | |
| 38 | DetectionInfo::DetectionInfo() |
| 39 | : m_content() |
| 40 | , m_package() |
| 41 | , m_type(ZMFDocument::TYPE_UNKNOWN) |
| 42 | , m_kind(ZMFDocument::KIND_UNKNOWN) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | template<typename THeader> |
| 47 | bool detectFormat(DetectionInfo &info, const ZMFDocument::Type type, const ZMFDocument::Kind kind) try |
| 48 | { |
| 49 | seek(info.m_content, 0); |
| 50 | THeader header; |
| 51 | if (header.load(info.m_content) && header.isSupported()) |
| 52 | { |
| 53 | info.m_type = type; |
| 54 | info.m_kind = kind; |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | catch (...) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool detect(const RVNGInputStreamPtr &input, DetectionInfo &info) |
| 65 | { |
| 66 | if (input->isStructured()) |
| 67 | { |
| 68 | info.m_package = input; |
| 69 | if (input->existsSubStream("content.zmf")) |
| 70 | { |
| 71 | info.m_content.reset(input->getSubStreamByName("content.zmf")); |
| 72 | return detectFormat<ZMF4Header>(info, ZMFDocument::TYPE_DRAW, ZMFDocument::KIND_DRAW); |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | info.m_content = input; |
| 78 | return detectFormat<ZMF4Header>(info, ZMFDocument::TYPE_DRAW, ZMFDocument::KIND_DRAW) |
| 79 | || detectFormat<BMIHeader>(info, ZMFDocument::TYPE_BITMAP, ZMFDocument::KIND_PAINT) |
| 80 | || detectFormat<ZBRHeader>(info, ZMFDocument::TYPE_ZEBRA, ZMFDocument::KIND_DRAW) |
| 81 | ; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | bool ZMFDocument::isSupported(librevenge::RVNGInputStream *const input, Type *const type, Kind *const kind) try |
| 89 | { |
| 90 | DetectionInfo info; |
| 91 | if (detect(RVNGInputStreamPtr(input, ZMFDummyDeleter()), info)) |
| 92 | { |
| 93 | assert(bool(info.m_content)); |
| 94 | if (type) |
| 95 | *type = info.m_type; |
| 96 | if (kind) |
| 97 | *kind = info.m_kind; |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | catch (...) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | bool ZMFDocument::parse(librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter) try |
| 108 | { |
| 109 | DetectionInfo info; |
| 110 | if (!detect(RVNGInputStreamPtr(input, ZMFDummyDeleter()), info)) |
| 111 | return false; |
| 112 | |
| 113 | info.m_content->seek(0, librevenge::RVNG_SEEK_SET); |
| 114 | |
| 115 | switch (info.m_type) |
| 116 | { |
| 117 | case TYPE_DRAW: |
| 118 | { |
| 119 | ZMF4Parser parser(info.m_content, painter); |
| 120 | return parser.parse(); |
| 121 | } |
| 122 | case TYPE_ZEBRA: |
| 123 | { |
| 124 | ZBRParser parser(info.m_content, painter); |
| 125 | return parser.parse(); |
| 126 | } |
| 127 | case TYPE_BITMAP: |
| 128 | { |
| 129 | BMIParser parser(info.m_content, painter); |
| 130 | return parser.parse(); |
| 131 | } |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | catch (...) |
| 139 | { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | |
| 145 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |