lottie_common.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "lottie/lottie_common.h"
  8. #include "base/algorithm.h"
  9. #include <QFile>
  10. namespace Lottie {
  11. namespace {
  12. QByteArray ReadFile(const QString &filepath) {
  13. auto f = QFile(filepath);
  14. return (f.size() <= kMaxFileSize && f.open(QIODevice::ReadOnly))
  15. ? f.readAll()
  16. : QByteArray();
  17. }
  18. } // namespace
  19. QSize FrameRequest::size(
  20. const QSize &original,
  21. int sizeRounding) const {
  22. Expects(!empty());
  23. Expects(sizeRounding != 0);
  24. const auto result = original.scaled(box, Qt::KeepAspectRatio);
  25. const auto skipw = result.width() % sizeRounding;
  26. const auto skiph = result.height() % sizeRounding;
  27. return QSize(
  28. std::max(result.width() - skipw, sizeRounding),
  29. std::max(result.height() - skiph, sizeRounding));
  30. }
  31. QByteArray ReadContent(const QByteArray &data, const QString &filepath) {
  32. return data.isEmpty() ? ReadFile(filepath) : base::duplicate(data);
  33. }
  34. std::string ReadUtf8(const QByteArray &data) {
  35. //00 00 FE FF UTF-32BE
  36. //FF FE 00 00 UTF-32LE
  37. //FE FF UTF-16BE
  38. //FF FE UTF-16LE
  39. //EF BB BF UTF-8
  40. if (data.size() < 4) {
  41. return data.toStdString();
  42. }
  43. const auto bom = uint32(uint8(data[0]))
  44. | (uint32(uint8(data[1])) << 8)
  45. | (uint32(uint8(data[2])) << 16)
  46. | (uint32(uint8(data[3])) << 24);
  47. const auto skip = ((bom == 0xFFFE0000U) || (bom == 0x0000FEFFU))
  48. ? 4
  49. : (((bom & 0xFFFFU) == 0xFFFEU) || ((bom & 0xFFFFU) == 0xFEFFU))
  50. ? 2
  51. : ((bom & 0xFFFFFFU) == 0xBFBBEFU)
  52. ? 3
  53. : 0;
  54. const auto bytes = data.data() + skip;
  55. const auto length = data.size() - skip;
  56. // Old RapidJSON didn't convert encoding, just skipped BOM.
  57. // We emulate old behavior here, so don't convert as well.
  58. return std::string(bytes, length);
  59. }
  60. bool GoodStorageForFrame(const QImage &storage, QSize size) {
  61. return !storage.isNull()
  62. && (storage.format() == kImageFormat)
  63. && (storage.size() == size)
  64. && storage.isDetached();
  65. }
  66. QImage CreateFrameStorage(QSize size) {
  67. return QImage(size, kImageFormat);
  68. }
  69. } // namespace Lottie