1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/src/base/StyleInfo.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set expandtab shiftwidth=2 tabstop=2: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "StyleInfo.h" 1.11 + 1.12 +#include "mozilla/dom/Element.h" 1.13 +#include "nsComputedDOMStyle.h" 1.14 +#include "nsCSSProps.h" 1.15 +#include "nsIFrame.h" 1.16 + 1.17 +using namespace mozilla; 1.18 +using namespace mozilla::a11y; 1.19 + 1.20 +StyleInfo::StyleInfo(dom::Element* aElement, nsIPresShell* aPresShell) : 1.21 + mElement(aElement) 1.22 +{ 1.23 + mStyleContext = 1.24 + nsComputedDOMStyle::GetStyleContextForElementNoFlush(aElement, 1.25 + nullptr, 1.26 + aPresShell); 1.27 +} 1.28 + 1.29 +void 1.30 +StyleInfo::Display(nsAString& aValue) 1.31 +{ 1.32 + aValue.Truncate(); 1.33 + AppendASCIItoUTF16( 1.34 + nsCSSProps::ValueToKeyword(mStyleContext->StyleDisplay()->mDisplay, 1.35 + nsCSSProps::kDisplayKTable), aValue); 1.36 +} 1.37 + 1.38 +void 1.39 +StyleInfo::TextAlign(nsAString& aValue) 1.40 +{ 1.41 + aValue.Truncate(); 1.42 + AppendASCIItoUTF16( 1.43 + nsCSSProps::ValueToKeyword(mStyleContext->StyleText()->mTextAlign, 1.44 + nsCSSProps::kTextAlignKTable), aValue); 1.45 +} 1.46 + 1.47 +void 1.48 +StyleInfo::TextIndent(nsAString& aValue) 1.49 +{ 1.50 + aValue.Truncate(); 1.51 + 1.52 + const nsStyleCoord& styleCoord = 1.53 + mStyleContext->StyleText()->mTextIndent; 1.54 + 1.55 + nscoord coordVal = 0; 1.56 + switch (styleCoord.GetUnit()) { 1.57 + case eStyleUnit_Coord: 1.58 + coordVal = styleCoord.GetCoordValue(); 1.59 + break; 1.60 + 1.61 + case eStyleUnit_Percent: 1.62 + { 1.63 + nsIFrame* frame = mElement->GetPrimaryFrame(); 1.64 + nsIFrame* containerFrame = frame->GetContainingBlock(); 1.65 + nscoord percentageBase = containerFrame->GetContentRect().width; 1.66 + coordVal = NSCoordSaturatingMultiply(percentageBase, 1.67 + styleCoord.GetPercentValue()); 1.68 + break; 1.69 + } 1.70 + 1.71 + case eStyleUnit_Null: 1.72 + case eStyleUnit_Normal: 1.73 + case eStyleUnit_Auto: 1.74 + case eStyleUnit_None: 1.75 + case eStyleUnit_Factor: 1.76 + case eStyleUnit_Degree: 1.77 + case eStyleUnit_Grad: 1.78 + case eStyleUnit_Radian: 1.79 + case eStyleUnit_Turn: 1.80 + case eStyleUnit_FlexFraction: 1.81 + case eStyleUnit_Integer: 1.82 + case eStyleUnit_Enumerated: 1.83 + case eStyleUnit_Calc: 1.84 + break; 1.85 + } 1.86 + 1.87 + aValue.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(coordVal)); 1.88 + aValue.AppendLiteral("px"); 1.89 +} 1.90 + 1.91 +void 1.92 +StyleInfo::Margin(css::Side aSide, nsAString& aValue) 1.93 +{ 1.94 + aValue.Truncate(); 1.95 + 1.96 + nscoord coordVal = mElement->GetPrimaryFrame()->GetUsedMargin().Side(aSide); 1.97 + aValue.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(coordVal)); 1.98 + aValue.AppendLiteral("px"); 1.99 +} 1.100 + 1.101 +void 1.102 +StyleInfo::FormatColor(const nscolor& aValue, nsString& aFormattedValue) 1.103 +{ 1.104 + // Combine the string like rgb(R, G, B) from nscolor. 1.105 + aFormattedValue.AppendLiteral("rgb("); 1.106 + aFormattedValue.AppendInt(NS_GET_R(aValue)); 1.107 + aFormattedValue.AppendLiteral(", "); 1.108 + aFormattedValue.AppendInt(NS_GET_G(aValue)); 1.109 + aFormattedValue.AppendLiteral(", "); 1.110 + aFormattedValue.AppendInt(NS_GET_B(aValue)); 1.111 + aFormattedValue.Append(')'); 1.112 +} 1.113 + 1.114 +void 1.115 +StyleInfo::FormatFontStyle(const nscoord& aValue, nsAString& aFormattedValue) 1.116 +{ 1.117 + nsCSSKeyword keyword = 1.118 + nsCSSProps::ValueToKeywordEnum(aValue, nsCSSProps::kFontStyleKTable); 1.119 + AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(keyword), aFormattedValue); 1.120 +} 1.121 + 1.122 +void 1.123 +StyleInfo::FormatTextDecorationStyle(uint8_t aValue, nsAString& aFormattedValue) 1.124 +{ 1.125 + nsCSSKeyword keyword = 1.126 + nsCSSProps::ValueToKeywordEnum(aValue, 1.127 + nsCSSProps::kTextDecorationStyleKTable); 1.128 + AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(keyword), aFormattedValue); 1.129 +}