michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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 "TextAttrs.h" michael@0: michael@0: #include "Accessible-inl.h" michael@0: #include "nsAccUtils.h" michael@0: #include "nsCoreUtils.h" michael@0: #include "StyleInfo.h" michael@0: michael@0: #include "gfxFont.h" michael@0: #include "nsFontMetrics.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "HyperTextAccessible.h" michael@0: #include "mozilla/AppUnits.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::a11y; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // TextAttrsMgr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void michael@0: TextAttrsMgr::GetAttributes(nsIPersistentProperties* aAttributes, michael@0: int32_t* aStartHTOffset, michael@0: int32_t* aEndHTOffset) michael@0: { michael@0: // 1. Hyper text accessible must be specified always. michael@0: // 2. Offset accessible and result hyper text offsets must be specified in michael@0: // the case of text attributes. michael@0: // 3. Offset accessible and result hyper text offsets must not be specified michael@0: // but include default text attributes flag and attributes list must be michael@0: // specified in the case of default text attributes. michael@0: NS_PRECONDITION(mHyperTextAcc && michael@0: ((mOffsetAcc && mOffsetAccIdx != -1 && michael@0: aStartHTOffset && aEndHTOffset) || michael@0: (!mOffsetAcc && mOffsetAccIdx == -1 && michael@0: !aStartHTOffset && !aEndHTOffset && michael@0: mIncludeDefAttrs && aAttributes)), michael@0: "Wrong usage of TextAttrsMgr!"); michael@0: michael@0: // Embedded objects are combined into own range with empty attributes set. michael@0: if (mOffsetAcc && nsAccUtils::IsEmbeddedObject(mOffsetAcc)) { michael@0: for (int32_t childIdx = mOffsetAccIdx - 1; childIdx >= 0; childIdx--) { michael@0: Accessible* currAcc = mHyperTextAcc->GetChildAt(childIdx); michael@0: if (!nsAccUtils::IsEmbeddedObject(currAcc)) michael@0: break; michael@0: michael@0: (*aStartHTOffset)--; michael@0: } michael@0: michael@0: uint32_t childCount = mHyperTextAcc->ChildCount(); michael@0: for (uint32_t childIdx = mOffsetAccIdx + 1; childIdx < childCount; michael@0: childIdx++) { michael@0: Accessible* currAcc = mHyperTextAcc->GetChildAt(childIdx); michael@0: if (!nsAccUtils::IsEmbeddedObject(currAcc)) michael@0: break; michael@0: michael@0: (*aEndHTOffset)++; michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: // Get the content and frame of the accessible. In the case of document michael@0: // accessible it's role content and root frame. michael@0: nsIContent *hyperTextElm = mHyperTextAcc->GetContent(); michael@0: nsIFrame *rootFrame = mHyperTextAcc->GetFrame(); michael@0: NS_ASSERTION(rootFrame, "No frame for accessible!"); michael@0: if (!rootFrame) michael@0: return; michael@0: michael@0: nsIContent *offsetNode = nullptr, *offsetElm = nullptr; michael@0: nsIFrame *frame = nullptr; michael@0: if (mOffsetAcc) { michael@0: offsetNode = mOffsetAcc->GetContent(); michael@0: offsetElm = nsCoreUtils::GetDOMElementFor(offsetNode); michael@0: NS_ASSERTION(offsetElm, "No element for offset accessible!"); michael@0: if (!offsetElm) michael@0: return; michael@0: frame = offsetElm->GetPrimaryFrame(); michael@0: } michael@0: michael@0: // "language" text attribute michael@0: LangTextAttr langTextAttr(mHyperTextAcc, hyperTextElm, offsetNode); michael@0: michael@0: // "aria-invalid" text attribute michael@0: InvalidTextAttr invalidTextAttr(hyperTextElm, offsetNode); michael@0: michael@0: // "background-color" text attribute michael@0: BGColorTextAttr bgColorTextAttr(rootFrame, frame); michael@0: michael@0: // "color" text attribute michael@0: ColorTextAttr colorTextAttr(rootFrame, frame); michael@0: michael@0: // "font-family" text attribute michael@0: FontFamilyTextAttr fontFamilyTextAttr(rootFrame, frame); michael@0: michael@0: // "font-size" text attribute michael@0: FontSizeTextAttr fontSizeTextAttr(rootFrame, frame); michael@0: michael@0: // "font-style" text attribute michael@0: FontStyleTextAttr fontStyleTextAttr(rootFrame, frame); michael@0: michael@0: // "font-weight" text attribute michael@0: FontWeightTextAttr fontWeightTextAttr(rootFrame, frame); michael@0: michael@0: // "auto-generated" text attribute michael@0: AutoGeneratedTextAttr autoGenTextAttr(mHyperTextAcc, mOffsetAcc); michael@0: michael@0: // "text-underline(line-through)-style(color)" text attributes michael@0: TextDecorTextAttr textDecorTextAttr(rootFrame, frame); michael@0: michael@0: // "text-position" text attribute michael@0: TextPosTextAttr textPosTextAttr(rootFrame, frame); michael@0: michael@0: TextAttr* attrArray[] = michael@0: { michael@0: &langTextAttr, michael@0: &invalidTextAttr, michael@0: &bgColorTextAttr, michael@0: &colorTextAttr, michael@0: &fontFamilyTextAttr, michael@0: &fontSizeTextAttr, michael@0: &fontStyleTextAttr, michael@0: &fontWeightTextAttr, michael@0: &autoGenTextAttr, michael@0: &textDecorTextAttr, michael@0: &textPosTextAttr michael@0: }; michael@0: michael@0: // Expose text attributes if applicable. michael@0: if (aAttributes) { michael@0: for (uint32_t idx = 0; idx < ArrayLength(attrArray); idx++) michael@0: attrArray[idx]->Expose(aAttributes, mIncludeDefAttrs); michael@0: } michael@0: michael@0: // Expose text attributes range where they are applied if applicable. michael@0: if (mOffsetAcc) michael@0: GetRange(attrArray, ArrayLength(attrArray), aStartHTOffset, aEndHTOffset); michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::GetRange(TextAttr* aAttrArray[], uint32_t aAttrArrayLen, michael@0: int32_t* aStartHTOffset, int32_t* aEndHTOffset) michael@0: { michael@0: // Navigate backward from anchor accessible to find start offset. michael@0: for (int32_t childIdx = mOffsetAccIdx - 1; childIdx >= 0; childIdx--) { michael@0: Accessible* currAcc = mHyperTextAcc->GetChildAt(childIdx); michael@0: michael@0: // Stop on embedded accessible since embedded accessibles are combined into michael@0: // own range. michael@0: if (nsAccUtils::IsEmbeddedObject(currAcc)) michael@0: break; michael@0: michael@0: bool offsetFound = false; michael@0: for (uint32_t attrIdx = 0; attrIdx < aAttrArrayLen; attrIdx++) { michael@0: TextAttr* textAttr = aAttrArray[attrIdx]; michael@0: if (!textAttr->Equal(currAcc)) { michael@0: offsetFound = true; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (offsetFound) michael@0: break; michael@0: michael@0: *(aStartHTOffset) -= nsAccUtils::TextLength(currAcc); michael@0: } michael@0: michael@0: // Navigate forward from anchor accessible to find end offset. michael@0: uint32_t childLen = mHyperTextAcc->ChildCount(); michael@0: for (uint32_t childIdx = mOffsetAccIdx + 1; childIdx < childLen; childIdx++) { michael@0: Accessible* currAcc = mHyperTextAcc->GetChildAt(childIdx); michael@0: if (nsAccUtils::IsEmbeddedObject(currAcc)) michael@0: break; michael@0: michael@0: bool offsetFound = false; michael@0: for (uint32_t attrIdx = 0; attrIdx < aAttrArrayLen; attrIdx++) { michael@0: TextAttr* textAttr = aAttrArray[attrIdx]; michael@0: michael@0: // Alter the end offset when text attribute changes its value and stop michael@0: // the search. michael@0: if (!textAttr->Equal(currAcc)) { michael@0: offsetFound = true; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (offsetFound) michael@0: break; michael@0: michael@0: (*aEndHTOffset) += nsAccUtils::TextLength(currAcc); michael@0: } michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // LangTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::LangTextAttr:: michael@0: LangTextAttr(HyperTextAccessible* aRoot, michael@0: nsIContent* aRootElm, nsIContent* aElm) : michael@0: TTextAttr(!aElm), mRootContent(aRootElm) michael@0: { michael@0: aRoot->Language(mRootNativeValue); michael@0: mIsRootDefined = !mRootNativeValue.IsEmpty(); michael@0: michael@0: if (aElm) { michael@0: nsCoreUtils::GetLanguageFor(aElm, mRootContent, mNativeValue); michael@0: mIsDefined = !mNativeValue.IsEmpty(); michael@0: } michael@0: } michael@0: michael@0: TextAttrsMgr::LangTextAttr:: michael@0: ~LangTextAttr() {} michael@0: michael@0: bool michael@0: TextAttrsMgr::LangTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, nsString* aValue) michael@0: { michael@0: nsCoreUtils::GetLanguageFor(aAccessible->GetContent(), mRootContent, *aValue); michael@0: return !aValue->IsEmpty(); michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::LangTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const nsString& aValue) michael@0: { michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::language, aValue); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // InvalidTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::InvalidTextAttr:: michael@0: InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm) : michael@0: TTextAttr(!aElm), mRootElm(aRootElm) michael@0: { michael@0: mIsRootDefined = GetValue(mRootElm, &mRootNativeValue); michael@0: if (aElm) michael@0: mIsDefined = GetValue(aElm, &mNativeValue); michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::InvalidTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, uint32_t* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: return elm ? GetValue(elm, aValue) : false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::InvalidTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const uint32_t& aValue) michael@0: { michael@0: switch (aValue) { michael@0: case eFalse: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::invalid, michael@0: NS_LITERAL_STRING("false")); michael@0: break; michael@0: michael@0: case eGrammar: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::invalid, michael@0: NS_LITERAL_STRING("grammar")); michael@0: break; michael@0: michael@0: case eSpelling: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::invalid, michael@0: NS_LITERAL_STRING("spelling")); michael@0: break; michael@0: michael@0: case eTrue: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::invalid, michael@0: NS_LITERAL_STRING("true")); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::InvalidTextAttr:: michael@0: GetValue(nsIContent* aElm, uint32_t* aValue) michael@0: { michael@0: nsIContent* elm = aElm; michael@0: do { michael@0: if (nsAccUtils::HasDefinedARIAToken(elm, nsGkAtoms::aria_invalid)) { michael@0: static nsIContent::AttrValuesArray tokens[] = michael@0: { &nsGkAtoms::_false, &nsGkAtoms::grammar, &nsGkAtoms::spelling, michael@0: nullptr }; michael@0: michael@0: int32_t idx = elm->FindAttrValueIn(kNameSpaceID_None, michael@0: nsGkAtoms::aria_invalid, tokens, michael@0: eCaseMatters); michael@0: switch (idx) { michael@0: case 0: michael@0: *aValue = eFalse; michael@0: return true; michael@0: case 1: michael@0: *aValue = eGrammar; michael@0: return true; michael@0: case 2: michael@0: *aValue = eSpelling; michael@0: return true; michael@0: default: michael@0: *aValue = eTrue; michael@0: return true; michael@0: } michael@0: } michael@0: } while ((elm = elm->GetParent()) && elm != mRootElm); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // BGColorTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::BGColorTextAttr:: michael@0: BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame), mRootFrame(aRootFrame) michael@0: { michael@0: mIsRootDefined = GetColor(mRootFrame, &mRootNativeValue); michael@0: if (aFrame) michael@0: mIsDefined = GetColor(aFrame, &mNativeValue); michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::BGColorTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, nscolor* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: return frame ? GetColor(frame, aValue) : false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::BGColorTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const nscolor& aValue) michael@0: { michael@0: nsAutoString formattedValue; michael@0: StyleInfo::FormatColor(aValue, formattedValue); michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::backgroundColor, michael@0: formattedValue); michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::BGColorTextAttr:: michael@0: GetColor(nsIFrame* aFrame, nscolor* aColor) michael@0: { michael@0: const nsStyleBackground* styleBackground = aFrame->StyleBackground(); michael@0: michael@0: if (NS_GET_A(styleBackground->mBackgroundColor) > 0) { michael@0: *aColor = styleBackground->mBackgroundColor; michael@0: return true; michael@0: } michael@0: michael@0: nsIFrame *parentFrame = aFrame->GetParent(); michael@0: if (!parentFrame) { michael@0: *aColor = aFrame->PresContext()->DefaultBackgroundColor(); michael@0: return true; michael@0: } michael@0: michael@0: // Each frame of parents chain for the initially passed 'aFrame' has michael@0: // transparent background color. So background color isn't changed from michael@0: // 'mRootFrame' to initially passed 'aFrame'. michael@0: if (parentFrame == mRootFrame) michael@0: return false; michael@0: michael@0: return GetColor(parentFrame, aColor); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // ColorTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::ColorTextAttr:: michael@0: ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mRootNativeValue = aRootFrame->StyleColor()->mColor; michael@0: mIsRootDefined = true; michael@0: michael@0: if (aFrame) { michael@0: mNativeValue = aFrame->StyleColor()->mColor; michael@0: mIsDefined = true; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::ColorTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, nscolor* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: if (frame) { michael@0: *aValue = frame->StyleColor()->mColor; michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::ColorTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const nscolor& aValue) michael@0: { michael@0: nsAutoString formattedValue; michael@0: StyleInfo::FormatColor(aValue, formattedValue); michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::color, formattedValue); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // FontFamilyTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::FontFamilyTextAttr:: michael@0: FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mIsRootDefined = GetFontFamily(aRootFrame, mRootNativeValue); michael@0: michael@0: if (aFrame) michael@0: mIsDefined = GetFontFamily(aFrame, mNativeValue); michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::FontFamilyTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, nsString* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: return frame ? GetFontFamily(frame, *aValue) : false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::FontFamilyTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const nsString& aValue) michael@0: { michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::font_family, aValue); michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::FontFamilyTextAttr:: michael@0: GetFontFamily(nsIFrame* aFrame, nsString& aFamily) michael@0: { michael@0: nsRefPtr fm; michael@0: nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm)); michael@0: michael@0: gfxFontGroup* fontGroup = fm->GetThebesFontGroup(); michael@0: gfxFont* font = fontGroup->GetFontAt(0); michael@0: gfxFontEntry* fontEntry = font->GetFontEntry(); michael@0: aFamily = fontEntry->FamilyName(); michael@0: return true; michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // FontSizeTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::FontSizeTextAttr:: michael@0: FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mDC = aRootFrame->PresContext()->DeviceContext(); michael@0: michael@0: mRootNativeValue = aRootFrame->StyleFont()->mSize; michael@0: mIsRootDefined = true; michael@0: michael@0: if (aFrame) { michael@0: mNativeValue = aFrame->StyleFont()->mSize; michael@0: mIsDefined = true; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::FontSizeTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, nscoord* aValue) michael@0: { michael@0: nsIContent* content = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = content->GetPrimaryFrame(); michael@0: if (frame) { michael@0: *aValue = frame->StyleFont()->mSize; michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::FontSizeTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const nscoord& aValue) michael@0: { michael@0: // Convert from nscoord to pt. michael@0: // michael@0: // Note: according to IA2, "The conversion doesn't have to be exact. michael@0: // The intent is to give the user a feel for the size of the text." michael@0: // michael@0: // ATK does not specify a unit and will likely follow IA2 here. michael@0: // michael@0: // XXX todo: consider sharing this code with layout module? (bug 474621) michael@0: float px = michael@0: NSAppUnitsToFloatPixels(aValue, mozilla::AppUnitsPerCSSPixel()); michael@0: // Each pt is 4/3 of a CSS pixel. michael@0: int pts = NS_lround(px*3/4); michael@0: michael@0: nsAutoString value; michael@0: value.AppendInt(pts); michael@0: value.Append(NS_LITERAL_STRING("pt")); michael@0: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::font_size, value); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // FontStyleTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::FontStyleTextAttr:: michael@0: FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mRootNativeValue = aRootFrame->StyleFont()->mFont.style; michael@0: mIsRootDefined = true; michael@0: michael@0: if (aFrame) { michael@0: mNativeValue = aFrame->StyleFont()->mFont.style; michael@0: mIsDefined = true; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::FontStyleTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, nscoord* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: if (frame) { michael@0: *aValue = frame->StyleFont()->mFont.style; michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::FontStyleTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const nscoord& aValue) michael@0: { michael@0: nsAutoString formattedValue; michael@0: StyleInfo::FormatFontStyle(aValue, formattedValue); michael@0: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::font_style, formattedValue); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // FontWeightTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::FontWeightTextAttr:: michael@0: FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mRootNativeValue = GetFontWeight(aRootFrame); michael@0: mIsRootDefined = true; michael@0: michael@0: if (aFrame) { michael@0: mNativeValue = GetFontWeight(aFrame); michael@0: mIsDefined = true; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::FontWeightTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, int32_t* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: if (frame) { michael@0: *aValue = GetFontWeight(frame); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::FontWeightTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const int32_t& aValue) michael@0: { michael@0: nsAutoString formattedValue; michael@0: formattedValue.AppendInt(aValue); michael@0: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::fontWeight, formattedValue); michael@0: } michael@0: michael@0: int32_t michael@0: TextAttrsMgr::FontWeightTextAttr:: michael@0: GetFontWeight(nsIFrame* aFrame) michael@0: { michael@0: // nsFont::width isn't suitable here because it's necessary to expose real michael@0: // value of font weight (used font might not have some font weight values). michael@0: nsRefPtr fm; michael@0: nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm)); michael@0: michael@0: gfxFontGroup *fontGroup = fm->GetThebesFontGroup(); michael@0: gfxFont *font = fontGroup->GetFontAt(0); michael@0: michael@0: // When there doesn't exist a bold font in the family and so the rendering of michael@0: // a non-bold font face is changed so that the user sees what looks like a michael@0: // bold font, i.e. synthetic bolding is used. IsSyntheticBold method is only michael@0: // needed on Mac, but it is "safe" to use on all platforms. (For non-Mac michael@0: // platforms it always return false.) michael@0: if (font->IsSyntheticBold()) michael@0: return 700; michael@0: michael@0: #if defined(MOZ_WIDGET_GTK) || defined(MOZ_WIDGET_QT) michael@0: // On Linux, font->GetStyle()->weight will give the absolute weight requested michael@0: // of the font face. The Linux code uses the gfxFontEntry constructor which michael@0: // doesn't initialize the weight field. michael@0: return font->GetStyle()->weight; michael@0: #else michael@0: // On Windows, font->GetStyle()->weight will give the same weight as michael@0: // fontEntry->Weight(), the weight of the first font in the font group, which michael@0: // may not be the weight of the font face used to render the characters. michael@0: // On Mac, font->GetStyle()->weight will just give the same number as michael@0: // getComputedStyle(). fontEntry->Weight() will give the weight of the font michael@0: // face used. michael@0: gfxFontEntry *fontEntry = font->GetFontEntry(); michael@0: return fontEntry->Weight(); michael@0: #endif michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // AutoGeneratedTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: TextAttrsMgr::AutoGeneratedTextAttr:: michael@0: AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc, michael@0: Accessible* aAccessible) : michael@0: TTextAttr(!aAccessible) michael@0: { michael@0: mRootNativeValue = false; michael@0: mIsRootDefined = false; michael@0: michael@0: if (aAccessible) michael@0: mIsDefined = mNativeValue = (aAccessible->NativeRole() == roles::STATICTEXT); michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::AutoGeneratedTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, bool* aValue) michael@0: { michael@0: return *aValue = (aAccessible->NativeRole() == roles::STATICTEXT); michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::AutoGeneratedTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const bool& aValue) michael@0: { michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::auto_generated, michael@0: aValue ? NS_LITERAL_STRING("true") : NS_LITERAL_STRING("false")); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // TextDecorTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::TextDecorValue:: michael@0: TextDecorValue(nsIFrame* aFrame) michael@0: { michael@0: const nsStyleTextReset* textReset = aFrame->StyleTextReset(); michael@0: mStyle = textReset->GetDecorationStyle(); michael@0: michael@0: bool isForegroundColor = false; michael@0: textReset->GetDecorationColor(mColor, isForegroundColor); michael@0: if (isForegroundColor) michael@0: mColor = aFrame->StyleColor()->mColor; michael@0: michael@0: mLine = textReset->mTextDecorationLine & michael@0: (NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE | michael@0: NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH); michael@0: } michael@0: michael@0: TextAttrsMgr::TextDecorTextAttr:: michael@0: TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mRootNativeValue = TextDecorValue(aRootFrame); michael@0: mIsRootDefined = mRootNativeValue.IsDefined(); michael@0: michael@0: if (aFrame) { michael@0: mNativeValue = TextDecorValue(aFrame); michael@0: mIsDefined = mNativeValue.IsDefined(); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::TextDecorTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, TextDecorValue* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: if (frame) { michael@0: *aValue = TextDecorValue(frame); michael@0: return aValue->IsDefined(); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::TextDecorTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const TextDecorValue& aValue) michael@0: { michael@0: if (aValue.IsUnderline()) { michael@0: nsAutoString formattedStyle; michael@0: StyleInfo::FormatTextDecorationStyle(aValue.Style(), formattedStyle); michael@0: nsAccUtils::SetAccAttr(aAttributes, michael@0: nsGkAtoms::textUnderlineStyle, michael@0: formattedStyle); michael@0: michael@0: nsAutoString formattedColor; michael@0: StyleInfo::FormatColor(aValue.Color(), formattedColor); michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::textUnderlineColor, michael@0: formattedColor); michael@0: return; michael@0: } michael@0: michael@0: if (aValue.IsLineThrough()) { michael@0: nsAutoString formattedStyle; michael@0: StyleInfo::FormatTextDecorationStyle(aValue.Style(), formattedStyle); michael@0: nsAccUtils::SetAccAttr(aAttributes, michael@0: nsGkAtoms::textLineThroughStyle, michael@0: formattedStyle); michael@0: michael@0: nsAutoString formattedColor; michael@0: StyleInfo::FormatColor(aValue.Color(), formattedColor); michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::textLineThroughColor, michael@0: formattedColor); michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // TextPosTextAttr michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: TextAttrsMgr::TextPosTextAttr:: michael@0: TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame) : michael@0: TTextAttr(!aFrame) michael@0: { michael@0: mRootNativeValue = GetTextPosValue(aRootFrame); michael@0: mIsRootDefined = mRootNativeValue != eTextPosNone; michael@0: michael@0: if (aFrame) { michael@0: mNativeValue = GetTextPosValue(aFrame); michael@0: mIsDefined = mNativeValue != eTextPosNone; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: TextAttrsMgr::TextPosTextAttr:: michael@0: GetValueFor(Accessible* aAccessible, TextPosValue* aValue) michael@0: { michael@0: nsIContent* elm = nsCoreUtils::GetDOMElementFor(aAccessible->GetContent()); michael@0: nsIFrame* frame = elm->GetPrimaryFrame(); michael@0: if (frame) { michael@0: *aValue = GetTextPosValue(frame); michael@0: return *aValue != eTextPosNone; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: TextAttrsMgr::TextPosTextAttr:: michael@0: ExposeValue(nsIPersistentProperties* aAttributes, const TextPosValue& aValue) michael@0: { michael@0: switch (aValue) { michael@0: case eTextPosBaseline: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::textPosition, michael@0: NS_LITERAL_STRING("baseline")); michael@0: break; michael@0: michael@0: case eTextPosSub: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::textPosition, michael@0: NS_LITERAL_STRING("sub")); michael@0: break; michael@0: michael@0: case eTextPosSuper: michael@0: nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::textPosition, michael@0: NS_LITERAL_STRING("super")); michael@0: break; michael@0: michael@0: case eTextPosNone: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: TextAttrsMgr::TextPosValue michael@0: TextAttrsMgr::TextPosTextAttr:: michael@0: GetTextPosValue(nsIFrame* aFrame) const michael@0: { michael@0: const nsStyleCoord& styleCoord = aFrame->StyleTextReset()->mVerticalAlign; michael@0: switch (styleCoord.GetUnit()) { michael@0: case eStyleUnit_Enumerated: michael@0: switch (styleCoord.GetIntValue()) { michael@0: case NS_STYLE_VERTICAL_ALIGN_BASELINE: michael@0: return eTextPosBaseline; michael@0: case NS_STYLE_VERTICAL_ALIGN_SUB: michael@0: return eTextPosSub; michael@0: case NS_STYLE_VERTICAL_ALIGN_SUPER: michael@0: return eTextPosSuper; michael@0: michael@0: // No good guess for these: michael@0: // NS_STYLE_VERTICAL_ALIGN_TOP michael@0: // NS_STYLE_VERTICAL_ALIGN_TEXT_TOP michael@0: // NS_STYLE_VERTICAL_ALIGN_MIDDLE michael@0: // NS_STYLE_VERTICAL_ALIGN_TEXT_BOTTOM michael@0: // NS_STYLE_VERTICAL_ALIGN_BOTTOM michael@0: // NS_STYLE_VERTICAL_ALIGN_MIDDLE_WITH_BASELINE michael@0: // Do not expose value of text-position attribute. michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: return eTextPosNone; michael@0: michael@0: case eStyleUnit_Percent: michael@0: { michael@0: float percentValue = styleCoord.GetPercentValue(); michael@0: return percentValue > 0 ? michael@0: eTextPosSuper : michael@0: (percentValue < 0 ? eTextPosSub : eTextPosBaseline); michael@0: } michael@0: michael@0: case eStyleUnit_Coord: michael@0: { michael@0: nscoord coordValue = styleCoord.GetCoordValue(); michael@0: return coordValue > 0 ? michael@0: eTextPosSuper : michael@0: (coordValue < 0 ? eTextPosSub : eTextPosBaseline); 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_Calc: michael@0: break; michael@0: } michael@0: michael@0: const nsIContent* content = aFrame->GetContent(); michael@0: if (content && content->IsHTML()) { michael@0: const nsIAtom* tagName = content->Tag(); michael@0: if (tagName == nsGkAtoms::sup) michael@0: return eTextPosSuper; michael@0: if (tagName == nsGkAtoms::sub) michael@0: return eTextPosSub; michael@0: } michael@0: michael@0: return eTextPosNone; michael@0: }