Skip to content

Commit 3830f8b

Browse files
committed
fix(ios): may render squashed at non-default aspect ratios
1 parent ee1c999 commit 3830f8b

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

cpp/PdfViewShadowNode.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,25 @@ ShadowNodeTraits PdfViewShadowNode::BaseTraits()
1717
Size PdfViewShadowNode::measureContent(
1818
const LayoutContext& layoutContext,
1919
const LayoutConstraints& layoutConstraints) const {
20-
int pageWidth = getStateData().getPageWidth();
21-
int pageHeight = getStateData().getPageHeight();
22-
Float aspectRatio = static_cast<Float>(pageWidth) / pageHeight;
23-
Float targetWidth = layoutConstraints.maximumSize.height * aspectRatio;
24-
if (std::isinf(layoutConstraints.maximumSize.width) || layoutConstraints.maximumSize.width < 1) {
25-
if (std::isinf(layoutConstraints.maximumSize.height) || layoutConstraints.maximumSize.height < 1) {
26-
// No restrictions on dimensions? Use pdf dimensions.
27-
return {static_cast<Float>(pageWidth), static_cast<Float>(pageHeight)};
28-
}
29-
// No width requirements? Scale page to match requested height.
30-
return {targetWidth, layoutConstraints.maximumSize.height};
20+
Float pageWidth = getStateData().getPageWidth();
21+
Float pageHeight = getStateData().getPageHeight();
22+
Float aspectRatio = pageWidth / pageHeight;
23+
if (std::isfinite(layoutConstraints.maximumSize.width)) {
24+
// Scale page to match requested width.
25+
pageWidth = layoutConstraints.maximumSize.width;
26+
pageHeight = pageWidth / aspectRatio;
3127
}
32-
33-
if (targetWidth <= layoutConstraints.maximumSize.width) {
34-
// When scaled to match requested height, page scaled width is
35-
// within width bounds. Scale page to match requested height.
36-
return {targetWidth, layoutConstraints.maximumSize.height};
28+
if (std::isfinite(layoutConstraints.maximumSize.height)) {
29+
// Scale page to match requested height.
30+
pageHeight = std::isfinite(layoutConstraints.maximumSize.width)
31+
? std::min(pageHeight, layoutConstraints.maximumSize.height)
32+
: layoutConstraints.maximumSize.height;
33+
pageWidth = pageHeight * aspectRatio;
3734
}
38-
// Scale page to match requested width.
39-
return {layoutConstraints.maximumSize.width, layoutConstraints.maximumSize.width / aspectRatio};
35+
return {
36+
std::max(pageWidth, layoutConstraints.minimumSize.width),
37+
std::max(pageHeight, layoutConstraints.minimumSize.height)
38+
};
4039
}
4140

4241
} // namespace facebook::react

ios/PdfView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ - (instancetype)initWithFrame:(CGRect)frame
6565
return self;
6666
}
6767

68+
- (void)prepareForRecycle
69+
{
70+
[super prepareForRecycle];
71+
[_view prepareForRecycle];
72+
}
73+
6874
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
6975
{
7076
const auto &newViewProps = *std::static_pointer_cast<PdfViewProps const>(props);

ios/PdfView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public class PdfView: UIView {
6666
}
6767
}
6868

69+
@objc public func prepareForRecycle() {
70+
source = ""
71+
resizeMode = ResizeMode.CONTAIN
72+
previousBounds = .zero
73+
}
74+
6975
@objc public func updateProps(annot: String, annotStr: String, pg: Int, rsMd: ResizeMode, src: String) {
7076
var isDirty = false
7177
var needsMeasure = false

0 commit comments

Comments
 (0)