| 82 | } |
| 83 | |
| 84 | Valdi::Result<snap::drawing::Ref<snap::drawing::Image>> SVGRenderer::render(const Valdi::Byte* data, |
| 85 | size_t length, |
| 86 | int outputWidth, |
| 87 | int outputHeight) { |
| 88 | ReSVG reSVG; |
| 89 | auto result = reSVG.parse(data, length); |
| 90 | if (!result) { |
| 91 | return result.moveError(); |
| 92 | } |
| 93 | |
| 94 | auto svgSize = result.value()->getSize(); |
| 95 | |
| 96 | int surfaceWidth; |
| 97 | if (outputWidth > 0) { |
| 98 | surfaceWidth = outputWidth; |
| 99 | } else { |
| 100 | surfaceWidth = svgSize.first; |
| 101 | } |
| 102 | |
| 103 | int surfaceHeight; |
| 104 | if (outputHeight > 0) { |
| 105 | surfaceHeight = outputHeight; |
| 106 | } else { |
| 107 | surfaceHeight = svgSize.second; |
| 108 | } |
| 109 | |
| 110 | auto outputBitmapResult = snap::drawing::Bitmap::make(Valdi::BitmapInfo( |
| 111 | surfaceWidth, surfaceHeight, Valdi::ColorType::ColorTypeRGBA8888, Valdi::AlphaType::AlphaTypePremul, 0)); |
| 112 | if (!outputBitmapResult) { |
| 113 | return outputBitmapResult.error().rethrow("Failed to create output bitmap: "); |
| 114 | } |
| 115 | |
| 116 | auto outputBitmap = outputBitmapResult.moveValue(); |
| 117 | |
| 118 | auto* outputBytes = outputBitmap->lockBytes(); |
| 119 | |
| 120 | auto transform = resvg_transform_identity(); |
| 121 | auto scale = std::max(static_cast<float>(surfaceWidth) / static_cast<float>(svgSize.first), |
| 122 | static_cast<float>(surfaceHeight) / static_cast<float>(svgSize.second)); |
| 123 | transform.a = scale; |
| 124 | transform.d = scale; |
| 125 | |
| 126 | resvg_render(result.value()->tree(), |
| 127 | transform, |
| 128 | static_cast<uint32_t>(surfaceWidth), |
| 129 | static_cast<uint32_t>(surfaceHeight), |
| 130 | reinterpret_cast<char*>(outputBytes)); |
| 131 | outputBitmap->unlockBytes(); |
| 132 | |
| 133 | return snap::drawing::Image::makeFromBitmap(outputBitmap, false); |
| 134 | } |
| 135 | |
| 136 | } // namespace snap::imagetoolbox |
nothing calls this directly
no test coverage detected