Add new nsCSSValue units for resolutions. draft
authorJonathan Chan <jchan@mozilla.com>
Thu, 21 Jul 2016 12:35:54 -0700
changeset 390813 2a4fb48492433c24787c0dddcf480974eb41cc7a
parent 390812 1166de80e0aba9422f9ca2b3179621dec5b52aa3
child 390814 20b8c6fd7f61e4949861240d5912e007a40f6bb4
push id23755
push userjchan@mozilla.com
push dateThu, 21 Jul 2016 19:51:58 +0000
milestone50.0a1
Add new nsCSSValue units for resolutions. Before, eCSSUnit_Inch, eCSSUnit_Pixel, and eCSSUnit_Centimeter were used for DPI, DPPX, and DPCM, respectively. Now custom properties can have type <resolution>, and it'd be nice to associate proper units with the resulting nsCSSValues. The next patch in this series edits nsCSSParser to parse resolutions using the new units. r?heycam MozReview-Commit-ID: Fnp4ez2VWUp
layout/style/CSSStyleSheet.cpp
layout/style/nsCSSValue.cpp
layout/style/nsCSSValue.h
layout/style/nsMediaFeatures.cpp
layout/style/nsMediaFeatures.h
--- a/layout/style/CSSStyleSheet.cpp
+++ b/layout/style/CSSStyleSheet.cpp
@@ -219,34 +219,34 @@ nsMediaExpression::Matches(nsPresContext
                 actualDen = actual.GetArrayValue()->Item(1).GetIntValue(),
                 requiredNum = required.GetArrayValue()->Item(0).GetIntValue(),
                 requiredDen = required.GetArrayValue()->Item(1).GetIntValue();
         cmp = DoCompare(actualNum * requiredDen, requiredNum * actualDen);
       }
       break;
     case nsMediaFeature::eResolution:
       {
-        NS_ASSERTION(actual.GetUnit() == eCSSUnit_Inch ||
-                     actual.GetUnit() == eCSSUnit_Pixel ||
-                     actual.GetUnit() == eCSSUnit_Centimeter,
+        NS_ASSERTION(actual.GetUnit() == eCSSUnit_DPI ||
+                     actual.GetUnit() == eCSSUnit_DPPX ||
+                     actual.GetUnit() == eCSSUnit_DPCM,
                      "bad actual value");
-        NS_ASSERTION(required.GetUnit() == eCSSUnit_Inch ||
-                     required.GetUnit() == eCSSUnit_Pixel ||
-                     required.GetUnit() == eCSSUnit_Centimeter,
+        NS_ASSERTION(required.GetUnit() == eCSSUnit_DPI ||
+                     required.GetUnit() == eCSSUnit_DPPX ||
+                     required.GetUnit() == eCSSUnit_DPCM,
                      "bad required value");
         float actualDPI = actual.GetFloatValue();
-        if (actual.GetUnit() == eCSSUnit_Centimeter) {
+        if (actual.GetUnit() == eCSSUnit_DPCM) {
           actualDPI = actualDPI * 2.54f;
-        } else if (actual.GetUnit() == eCSSUnit_Pixel) {
+        } else if (actual.GetUnit() == eCSSUnit_DPPX) {
           actualDPI = actualDPI * 96.0f;
         }
         float requiredDPI = required.GetFloatValue();
-        if (required.GetUnit() == eCSSUnit_Centimeter) {
+        if (required.GetUnit() == eCSSUnit_DPCM) {
           requiredDPI = requiredDPI * 2.54f;
-        } else if (required.GetUnit() == eCSSUnit_Pixel) {
+        } else if (required.GetUnit() == eCSSUnit_DPPX) {
           requiredDPI = requiredDPI * 96.0f;
         }
         cmp = DoCompare(actualDPI, requiredDPI);
       }
       break;
     case nsMediaFeature::eEnumerated:
       {
         NS_ASSERTION(actual.GetUnit() == eCSSUnit_Enumerated,
@@ -526,22 +526,22 @@ nsMediaQuery::AppendToString(nsAString& 
             aString.Append('/');
             array->Item(1).AppendToString(eCSSProperty_z_index, aString,
                                           nsCSSValue::eNormalized);
           }
           break;
         case nsMediaFeature::eResolution:
           {
             aString.AppendFloat(expr.mValue.GetFloatValue());
-            if (expr.mValue.GetUnit() == eCSSUnit_Inch) {
+            if (expr.mValue.GetUnit() == eCSSUnit_DPI) {
               aString.AppendLiteral("dpi");
-            } else if (expr.mValue.GetUnit() == eCSSUnit_Pixel) {
+            } else if (expr.mValue.GetUnit() == eCSSUnit_DPPX) {
               aString.AppendLiteral("dppx");
             } else {
-              NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Centimeter,
+              NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_DPCM,
                            "bad unit");
               aString.AppendLiteral("dpcm");
             }
           }
           break;
         case nsMediaFeature::eEnumerated:
           NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Enumerated,
                        "bad unit");
--- a/layout/style/nsCSSValue.cpp
+++ b/layout/style/nsCSSValue.cpp
@@ -1820,16 +1820,20 @@ nsCSSValue::AppendToString(nsCSSProperty
 
     case eCSSUnit_Hertz:        aResult.AppendLiteral("Hz");   break;
     case eCSSUnit_Kilohertz:    aResult.AppendLiteral("kHz");  break;
 
     case eCSSUnit_Seconds:      aResult.Append(char16_t('s'));    break;
     case eCSSUnit_Milliseconds: aResult.AppendLiteral("ms");   break;
 
     case eCSSUnit_FlexFraction: aResult.AppendLiteral("fr");   break;
+
+    case eCSSUnit_DPI:          aResult.AppendLiteral("dpi");  break;
+    case eCSSUnit_DPPX:         aResult.AppendLiteral("dppx"); break;
+    case eCSSUnit_DPCM:         aResult.AppendLiteral("dpcm"); break;
   }
 }
 
 size_t
 nsCSSValue::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
 {
   size_t n = 0;
 
@@ -1989,16 +1993,19 @@ nsCSSValue::SizeOfExcludingThis(mozilla:
     case eCSSUnit_Grad:
     case eCSSUnit_Turn:
     case eCSSUnit_Radian:
     case eCSSUnit_Hertz:
     case eCSSUnit_Kilohertz:
     case eCSSUnit_Seconds:
     case eCSSUnit_Milliseconds:
     case eCSSUnit_FlexFraction:
+    case eCSSUnit_DPI:
+    case eCSSUnit_DPPX:
+    case eCSSUnit_DPCM:
       break;
 
     default:
       MOZ_ASSERT(false, "bad nsCSSUnit");
       break;
   }
 
   return n;
--- a/layout/style/nsCSSValue.h
+++ b/layout/style/nsCSSValue.h
@@ -409,17 +409,22 @@ enum nsCSSUnit {
   eCSSUnit_Hertz        = 2000,    // (float) 1/seconds
   eCSSUnit_Kilohertz    = 2001,    // (float) 1000 Hertz
 
   // Time units
   eCSSUnit_Seconds      = 3000,    // (float) Standard time
   eCSSUnit_Milliseconds = 3001,    // (float) 1/1000 second
 
   // Flexible fraction (CSS Grid)
-  eCSSUnit_FlexFraction = 4000     // (float) Fraction of free space
+  eCSSUnit_FlexFraction = 4000,    // (float) Fraction of free space
+  
+  // Resolution units
+  eCSSUnit_DPI          = 5000,    // (float) dots per inch
+  eCSSUnit_DPPX         = 5001,    // (float) dots per pixel
+  eCSSUnit_DPCM         = 5002,    // (float) dots per centimeter
 };
 
 struct nsCSSValueGradient;
 struct nsCSSValuePair;
 struct nsCSSValuePair_heap;
 struct nsCSSValueTokenStream;
 struct nsCSSRect;
 struct nsCSSRect_heap;
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -294,17 +294,17 @@ GetResolution(nsPresContext* aPresContex
 
   if (!ShouldResistFingerprinting(aPresContext)) {
     // Resolution measures device pixels per CSS (inch/cm/pixel).  We
     // return it in device pixels per CSS inches.
     dpi = float(nsPresContext::AppUnitsPerCSSInch()) /
           float(aPresContext->AppUnitsPerDevPixel());
   }
 
-  aResult.SetFloatValue(dpi, eCSSUnit_Inch);
+  aResult.SetFloatValue(dpi, eCSSUnit_DPI);
   return NS_OK;
 }
 
 static nsresult
 GetScan(nsPresContext* aPresContext, const nsMediaFeature*,
         nsCSSValue& aResult)
 {
   // Since Gecko doesn't support the 'tv' media type, the 'scan'
--- a/layout/style/nsMediaFeatures.h
+++ b/layout/style/nsMediaFeatures.h
@@ -31,19 +31,19 @@ struct nsMediaFeature
   enum ValueType {
     // All value types allow eCSSUnit_Null to indicate that no value
     // was given (in addition to the types listed below).
     eLength,     // values are such that nsCSSValue::IsLengthUnit() is true
     eInteger,    // values are eCSSUnit_Integer
     eFloat,      // values are eCSSUnit_Number
     eBoolInteger,// values are eCSSUnit_Integer (0, -0, or 1 only)
     eIntRatio,   // values are eCSSUnit_Array of two eCSSUnit_Integer
-    eResolution, // values are in eCSSUnit_Inch (for dpi),
-                 //   eCSSUnit_Pixel (for dppx), or
-                 //   eCSSUnit_Centimeter (for dpcm)
+    eResolution, // values are in eCSSUnit_DPI (for dpi),
+                 //   eCSSUnit_DPPX (for dppx), or
+                 //   eCSSUnit_DPCM (for dpcm)
     eEnumerated, // values are eCSSUnit_Enumerated (uses keyword table)
     eIdent       // values are eCSSUnit_Ident
     // Note that a number of pieces of code (both for parsing and
     // for matching of valueless expressions) assume that all numeric
     // value types cannot be negative.  The parsing code also does
     // not allow zeros in eIntRatio types.
   };
   ValueType mValueType;