|
1 |
| -import 'package:flutter_html/src/style/length.dart'; |
| 1 | +import 'dart:math'; |
2 | 2 |
|
3 |
| -class DimensionComputeContext { |
4 |
| - const DimensionComputeContext({ |
5 |
| - required this.emValue, |
6 |
| - required this.autoValue, |
7 |
| - }); |
8 |
| - |
9 |
| - final double emValue; |
10 |
| - final double autoValue; |
11 |
| -} |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_html/style.dart'; |
12 | 5 |
|
13 | 6 | /// [computeDimensionUnit] takes a [Dimension] and some information about the
|
14 | 7 | /// context where the Dimension is being used, and returns a "used" value to
|
15 | 8 | /// use in a rendering.
|
16 |
| -double computeDimensionValue(Dimension dimension, DimensionComputeContext computeContext) { |
| 9 | +double _computeDimensionValue(Dimension dimension, double emValue, double autoValue) { |
17 | 10 | switch (dimension.unit) {
|
18 |
| - case Unit.em: return computeContext.emValue * dimension.value; |
| 11 | + case Unit.em: return emValue * dimension.value; |
19 | 12 | case Unit.px: return dimension.value;
|
20 |
| - case Unit.auto: return computeContext.autoValue; |
| 13 | + case Unit.auto: return autoValue; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +double _calculateEmValue(Style style, BuildContext buildContext) { |
| 18 | + //TODO is there a better value for this? |
| 19 | + return (style.fontSize?.size ?? 16) * |
| 20 | + MediaQuery.textScaleFactorOf(buildContext) * |
| 21 | + MediaQuery.of(buildContext).devicePixelRatio; |
| 22 | +} |
| 23 | + |
| 24 | +/// This class handles the calculation of widths and margins during parsing: |
| 25 | +/// See [calculate] within. |
| 26 | +class WidthAndMargins { |
| 27 | + final double? width; |
| 28 | + |
| 29 | + final EdgeInsets margins; |
| 30 | + |
| 31 | + const WidthAndMargins({required this.width, required this.margins}); |
| 32 | + |
| 33 | + /// [WidthsAndMargins.calculate] calculates any auto values ans resolves any |
| 34 | + /// overconstraint for various elements.. |
| 35 | + /// See https://drafts.csswg.org/css2/#Computing_widths_and_margins |
| 36 | + static WidthAndMargins calculate(Style style, Size containingBlockSize, BuildContext buildContext) { |
| 37 | + |
| 38 | + final emValue = _calculateEmValue(style, buildContext); |
| 39 | + |
| 40 | + double? width; |
| 41 | + double marginLeft = _computeDimensionValue(style.margin?.left ?? Margin.zero(), emValue, 0); |
| 42 | + double marginRight = _computeDimensionValue(style.margin?.right ?? Margin.zero(), emValue, 0); |
| 43 | + |
| 44 | + switch(style.display ?? Display.BLOCK) { |
| 45 | + case Display.BLOCK: |
| 46 | + |
| 47 | + // TODO: Handle the case of determining the width of replaced block elements in normal flow |
| 48 | + // See https://drafts.csswg.org/css2/#block-replaced-width |
| 49 | + |
| 50 | + bool autoWidth = style.width == null; |
| 51 | + bool autoMarginLeft = style.margin?.left?.unit == Unit.auto; |
| 52 | + bool autoMarginRight = style.margin?.right?.unit == Unit.auto; |
| 53 | + |
| 54 | + double? overrideMarginLeft; |
| 55 | + double? overrideMarginRight; |
| 56 | + double? autoLeftMarginValue; |
| 57 | + double? autoRightMarginValue; |
| 58 | + final borderWidth = (style.border?.left.width ?? 0) + (style.border?.right.width ?? 0); |
| 59 | + final paddingWidth = (style.padding?.left ?? 0) + (style.padding?.right ?? 0); |
| 60 | + final nonAutoWidths = borderWidth + paddingWidth; |
| 61 | + final nonAutoMarginWidth = marginLeft + marginRight; |
| 62 | + |
| 63 | + //If width is not auto, check the total width of the containing block: |
| 64 | + if(!autoWidth) { |
| 65 | + if(nonAutoWidths + style.width! + nonAutoMarginWidth > containingBlockSize.width) { |
| 66 | + autoLeftMarginValue = 0; |
| 67 | + autoRightMarginValue = 0; |
| 68 | + autoMarginLeft = false; |
| 69 | + autoMarginRight = false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + //If all values are explicit, the box is over-constrained, and we must |
| 74 | + //override one of the given margin values (left if the overconstrained |
| 75 | + //element has a rtl directionality, and right if the overconstrained |
| 76 | + //element has a ltr directionality). Margins must be non-negative in |
| 77 | + //Flutter, so we set them to 0 if they go below that. |
| 78 | + if(!autoWidth && !autoMarginLeft && !autoMarginRight) { |
| 79 | + final difference = containingBlockSize.width - (nonAutoWidths + style.width! + nonAutoMarginWidth); |
| 80 | + switch(style.direction) { |
| 81 | + case TextDirection.rtl: |
| 82 | + overrideMarginLeft = max(marginLeft + difference, 0); |
| 83 | + break; |
| 84 | + case TextDirection.ltr: |
| 85 | + overrideMarginRight = max(marginRight + difference, 0); |
| 86 | + break; |
| 87 | + case null: |
| 88 | + final directionality = Directionality.maybeOf(buildContext); |
| 89 | + if(directionality != null) { |
| 90 | + switch(directionality) { |
| 91 | + case TextDirection.rtl: |
| 92 | + overrideMarginLeft = max(marginLeft + difference, 0); |
| 93 | + break; |
| 94 | + case TextDirection.ltr: |
| 95 | + overrideMarginRight = max(marginRight + difference, 0); |
| 96 | + break; |
| 97 | + } |
| 98 | + } else { |
| 99 | + overrideMarginRight = max(marginRight + difference, 0); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //If exactly one unit is auto, calculate it from the equality. |
| 105 | + if(autoWidth && !autoMarginLeft && !autoMarginRight) { |
| 106 | + width = containingBlockSize.width - (nonAutoWidths + nonAutoMarginWidth); |
| 107 | + } else if(!autoWidth && autoMarginLeft && !autoMarginRight) { |
| 108 | + overrideMarginLeft = containingBlockSize.width - (nonAutoWidths + style.width! + marginRight); |
| 109 | + } else if(!autoWidth && !autoMarginLeft && autoMarginRight) { |
| 110 | + overrideMarginRight = containingBlockSize.width - (nonAutoWidths + style.width! + marginLeft); |
| 111 | + } |
| 112 | + |
| 113 | + //If width is auto, set all other auto values to 0, and the width is |
| 114 | + //calculated from the equality |
| 115 | + if(style.width == null) { |
| 116 | + autoLeftMarginValue = 0; |
| 117 | + autoRightMarginValue = 0; |
| 118 | + autoMarginLeft = false; |
| 119 | + autoMarginRight = false; |
| 120 | + width = containingBlockSize.width - (nonAutoMarginWidth + nonAutoWidths); |
| 121 | + } |
| 122 | + |
| 123 | + //If margin-left and margin-right are both auto, their values are equal, |
| 124 | + // and the element is centered. |
| 125 | + if(autoMarginLeft && autoMarginRight) { |
| 126 | + final marginWidth = containingBlockSize.width - (nonAutoWidths + style.width!); |
| 127 | + overrideMarginLeft = marginWidth / 2; |
| 128 | + overrideMarginRight = marginWidth / 2; |
| 129 | + } |
| 130 | + |
| 131 | + marginLeft = overrideMarginLeft ?? _computeDimensionValue(style.margin?.left ?? Margin.zero(), emValue, autoLeftMarginValue ?? 0); |
| 132 | + marginRight = overrideMarginRight ?? _computeDimensionValue(style.margin?.right ?? Margin.zero(), emValue, autoRightMarginValue ?? 0); |
| 133 | + break; |
| 134 | + case Display.INLINE: |
| 135 | + case Display.INLINE_BLOCK: |
| 136 | + |
| 137 | + //All inline elements have a computed auto value for margin-left and right of 0. |
| 138 | + marginLeft = _computeDimensionValue(style.margin?.left ?? Margin.zero(), emValue, 0); |
| 139 | + marginRight = _computeDimensionValue(style.margin?.right ?? Margin.zero(), emValue, 0); |
| 140 | + |
| 141 | + // TODO: Handle the case of replaced inline elements and intrinsic ratio |
| 142 | + // (See https://drafts.csswg.org/css2/#inline-replaced-width) |
| 143 | + break; |
| 144 | + case Display.LIST_ITEM: |
| 145 | + // TODO: Any handling for this case? |
| 146 | + break; |
| 147 | + case Display.NONE: |
| 148 | + // Do nothing |
| 149 | + break; |
| 150 | + } |
| 151 | + |
| 152 | + return WidthAndMargins( |
| 153 | + width: width, |
| 154 | + margins: EdgeInsets.only( |
| 155 | + left: marginLeft, |
| 156 | + right: marginRight, |
| 157 | + top: _computeDimensionValue(style.margin?.top ?? Margin.zero(), emValue, 0), |
| 158 | + bottom: _computeDimensionValue(style.margin?.bottom ?? Margin.zero(), emValue, 0), |
| 159 | + ), |
| 160 | + ); |
21 | 161 | }
|
| 162 | + |
22 | 163 | }
|
0 commit comments