michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set expandtab shiftwidth=2 tabstop=2: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "StyleInfo.h" michael@0: michael@0: #include "mozilla/dom/Element.h" michael@0: #include "nsComputedDOMStyle.h" michael@0: #include "nsCSSProps.h" michael@0: #include "nsIFrame.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::a11y; michael@0: michael@0: StyleInfo::StyleInfo(dom::Element* aElement, nsIPresShell* aPresShell) : michael@0: mElement(aElement) michael@0: { michael@0: mStyleContext = michael@0: nsComputedDOMStyle::GetStyleContextForElementNoFlush(aElement, michael@0: nullptr, michael@0: aPresShell); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::Display(nsAString& aValue) michael@0: { michael@0: aValue.Truncate(); michael@0: AppendASCIItoUTF16( michael@0: nsCSSProps::ValueToKeyword(mStyleContext->StyleDisplay()->mDisplay, michael@0: nsCSSProps::kDisplayKTable), aValue); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::TextAlign(nsAString& aValue) michael@0: { michael@0: aValue.Truncate(); michael@0: AppendASCIItoUTF16( michael@0: nsCSSProps::ValueToKeyword(mStyleContext->StyleText()->mTextAlign, michael@0: nsCSSProps::kTextAlignKTable), aValue); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::TextIndent(nsAString& aValue) michael@0: { michael@0: aValue.Truncate(); michael@0: michael@0: const nsStyleCoord& styleCoord = michael@0: mStyleContext->StyleText()->mTextIndent; michael@0: michael@0: nscoord coordVal = 0; michael@0: switch (styleCoord.GetUnit()) { michael@0: case eStyleUnit_Coord: michael@0: coordVal = styleCoord.GetCoordValue(); michael@0: break; michael@0: michael@0: case eStyleUnit_Percent: michael@0: { michael@0: nsIFrame* frame = mElement->GetPrimaryFrame(); michael@0: nsIFrame* containerFrame = frame->GetContainingBlock(); michael@0: nscoord percentageBase = containerFrame->GetContentRect().width; michael@0: coordVal = NSCoordSaturatingMultiply(percentageBase, michael@0: styleCoord.GetPercentValue()); michael@0: break; michael@0: } michael@0: michael@0: case eStyleUnit_Null: michael@0: case eStyleUnit_Normal: michael@0: case eStyleUnit_Auto: michael@0: case eStyleUnit_None: michael@0: case eStyleUnit_Factor: michael@0: case eStyleUnit_Degree: michael@0: case eStyleUnit_Grad: michael@0: case eStyleUnit_Radian: michael@0: case eStyleUnit_Turn: michael@0: case eStyleUnit_FlexFraction: michael@0: case eStyleUnit_Integer: michael@0: case eStyleUnit_Enumerated: michael@0: case eStyleUnit_Calc: michael@0: break; michael@0: } michael@0: michael@0: aValue.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(coordVal)); michael@0: aValue.AppendLiteral("px"); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::Margin(css::Side aSide, nsAString& aValue) michael@0: { michael@0: aValue.Truncate(); michael@0: michael@0: nscoord coordVal = mElement->GetPrimaryFrame()->GetUsedMargin().Side(aSide); michael@0: aValue.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(coordVal)); michael@0: aValue.AppendLiteral("px"); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::FormatColor(const nscolor& aValue, nsString& aFormattedValue) michael@0: { michael@0: // Combine the string like rgb(R, G, B) from nscolor. michael@0: aFormattedValue.AppendLiteral("rgb("); michael@0: aFormattedValue.AppendInt(NS_GET_R(aValue)); michael@0: aFormattedValue.AppendLiteral(", "); michael@0: aFormattedValue.AppendInt(NS_GET_G(aValue)); michael@0: aFormattedValue.AppendLiteral(", "); michael@0: aFormattedValue.AppendInt(NS_GET_B(aValue)); michael@0: aFormattedValue.Append(')'); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::FormatFontStyle(const nscoord& aValue, nsAString& aFormattedValue) michael@0: { michael@0: nsCSSKeyword keyword = michael@0: nsCSSProps::ValueToKeywordEnum(aValue, nsCSSProps::kFontStyleKTable); michael@0: AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(keyword), aFormattedValue); michael@0: } michael@0: michael@0: void michael@0: StyleInfo::FormatTextDecorationStyle(uint8_t aValue, nsAString& aFormattedValue) michael@0: { michael@0: nsCSSKeyword keyword = michael@0: nsCSSProps::ValueToKeywordEnum(aValue, michael@0: nsCSSProps::kTextDecorationStyleKTable); michael@0: AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(keyword), aFormattedValue); michael@0: }