Bumping manifests a=b2g-bump
[gecko.git] / gfx / src / nsFontMetrics.cpp
blob4cba05af8fda692539b4b504943327d17b38a375
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsFontMetrics.h"
7 #include <math.h> // for floor, ceil
8 #include <algorithm> // for max
9 #include "gfxFontConstants.h" // for NS_FONT_SYNTHESIS_*
10 #include "gfxPlatform.h" // for gfxPlatform
11 #include "gfxPoint.h" // for gfxPoint
12 #include "gfxRect.h" // for gfxRect
13 #include "gfxTypes.h" // for gfxFloat
14 #include "nsBoundingMetrics.h" // for nsBoundingMetrics
15 #include "nsDebug.h" // for NS_ERROR, NS_ABORT_IF_FALSE
16 #include "nsDeviceContext.h" // for nsDeviceContext
17 #include "nsIAtom.h" // for nsIAtom
18 #include "nsMathUtils.h" // for NS_round
19 #include "nsRenderingContext.h" // for nsRenderingContext
20 #include "nsString.h" // for nsString
21 #include "nsStyleConsts.h" // for NS_STYLE_HYPHENS_NONE
23 class gfxUserFontSet;
25 namespace {
27 class AutoTextRun {
28 public:
29 AutoTextRun(nsFontMetrics* aMetrics, nsRenderingContext* aRC,
30 const char* aString, int32_t aLength)
32 mTextRun = aMetrics->GetThebesFontGroup()->MakeTextRun(
33 reinterpret_cast<const uint8_t*>(aString), aLength,
34 aRC->ThebesContext(),
35 aMetrics->AppUnitsPerDevPixel(),
36 ComputeFlags(aMetrics));
39 AutoTextRun(nsFontMetrics* aMetrics, nsRenderingContext* aRC,
40 const char16_t* aString, int32_t aLength)
42 mTextRun = aMetrics->GetThebesFontGroup()->MakeTextRun(
43 aString, aLength,
44 aRC->ThebesContext(),
45 aMetrics->AppUnitsPerDevPixel(),
46 ComputeFlags(aMetrics));
49 gfxTextRun *get() { return mTextRun; }
50 gfxTextRun *operator->() { return mTextRun; }
52 private:
53 static uint32_t ComputeFlags(nsFontMetrics* aMetrics) {
54 uint32_t flags = 0;
55 if (aMetrics->GetTextRunRTL()) {
56 flags |= gfxTextRunFactory::TEXT_IS_RTL;
58 return flags;
61 nsAutoPtr<gfxTextRun> mTextRun;
64 class StubPropertyProvider : public gfxTextRun::PropertyProvider {
65 public:
66 virtual void GetHyphenationBreaks(uint32_t aStart, uint32_t aLength,
67 bool* aBreakBefore) {
68 NS_ERROR("This shouldn't be called because we never call BreakAndMeasureText");
70 virtual int8_t GetHyphensOption() {
71 NS_ERROR("This shouldn't be called because we never call BreakAndMeasureText");
72 return NS_STYLE_HYPHENS_NONE;
74 virtual gfxFloat GetHyphenWidth() {
75 NS_ERROR("This shouldn't be called because we never enable hyphens");
76 return 0;
78 virtual already_AddRefed<gfxContext> GetContext() {
79 NS_ERROR("This shouldn't be called because we never enable hyphens");
80 return nullptr;
82 virtual uint32_t GetAppUnitsPerDevUnit() {
83 NS_ERROR("This shouldn't be called because we never enable hyphens");
84 return 60;
86 virtual void GetSpacing(uint32_t aStart, uint32_t aLength,
87 Spacing* aSpacing) {
88 NS_ERROR("This shouldn't be called because we never enable spacing");
92 } // anon namespace
94 nsFontMetrics::nsFontMetrics()
95 : mDeviceContext(nullptr), mP2A(0), mTextRunRTL(false)
99 nsFontMetrics::~nsFontMetrics()
101 if (mDeviceContext)
102 mDeviceContext->FontMetricsDeleted(this);
105 nsresult
106 nsFontMetrics::Init(const nsFont& aFont, nsIAtom* aLanguage,
107 nsDeviceContext *aContext,
108 gfxUserFontSet *aUserFontSet,
109 gfxTextPerfMetrics *aTextPerf)
111 NS_ABORT_IF_FALSE(mP2A == 0, "already initialized");
113 mFont = aFont;
114 mLanguage = aLanguage;
115 mDeviceContext = aContext;
116 mP2A = mDeviceContext->AppUnitsPerDevPixel();
118 gfxFontStyle style(aFont.style,
119 aFont.weight,
120 aFont.stretch,
121 gfxFloat(aFont.size) / mP2A,
122 aLanguage,
123 aFont.sizeAdjust,
124 aFont.systemFont,
125 mDeviceContext->IsPrinterSurface(),
126 aFont.synthesis & NS_FONT_SYNTHESIS_WEIGHT,
127 aFont.synthesis & NS_FONT_SYNTHESIS_STYLE,
128 aFont.languageOverride);
130 aFont.AddFontFeaturesToStyle(&style);
132 mFontGroup = gfxPlatform::GetPlatform()->
133 CreateFontGroup(aFont.fontlist, &style, aUserFontSet);
134 mFontGroup->SetTextPerfMetrics(aTextPerf);
135 if (mFontGroup->FontListLength() < 1)
136 return NS_ERROR_UNEXPECTED;
138 return NS_OK;
141 void
142 nsFontMetrics::Destroy()
144 mDeviceContext = nullptr;
147 // XXXTODO get rid of this macro
148 #define ROUND_TO_TWIPS(x) (nscoord)floor(((x) * mP2A) + 0.5)
149 #define CEIL_TO_TWIPS(x) (nscoord)ceil((x) * mP2A)
151 const gfxFont::Metrics& nsFontMetrics::GetMetrics() const
153 return mFontGroup->GetFontAt(0)->GetMetrics();
156 nscoord
157 nsFontMetrics::XHeight()
159 return ROUND_TO_TWIPS(GetMetrics().xHeight);
162 nscoord
163 nsFontMetrics::SuperscriptOffset()
165 return ROUND_TO_TWIPS(GetMetrics().emHeight *
166 NS_FONT_SUPERSCRIPT_OFFSET_RATIO);
169 nscoord
170 nsFontMetrics::SubscriptOffset()
172 return ROUND_TO_TWIPS(GetMetrics().emHeight *
173 NS_FONT_SUBSCRIPT_OFFSET_RATIO);
176 void
177 nsFontMetrics::GetStrikeout(nscoord& aOffset, nscoord& aSize)
179 aOffset = ROUND_TO_TWIPS(GetMetrics().strikeoutOffset);
180 aSize = ROUND_TO_TWIPS(GetMetrics().strikeoutSize);
183 void
184 nsFontMetrics::GetUnderline(nscoord& aOffset, nscoord& aSize)
186 aOffset = ROUND_TO_TWIPS(mFontGroup->GetUnderlineOffset());
187 aSize = ROUND_TO_TWIPS(GetMetrics().underlineSize);
190 // GetMaxAscent/GetMaxDescent/GetMaxHeight must contain the
191 // text-decoration lines drawable area. See bug 421353.
192 // BE CAREFUL for rounding each values. The logic MUST be same as
193 // nsCSSRendering::GetTextDecorationRectInternal's.
195 static gfxFloat ComputeMaxDescent(const gfxFont::Metrics& aMetrics,
196 gfxFontGroup* aFontGroup)
198 gfxFloat offset = floor(-aFontGroup->GetUnderlineOffset() + 0.5);
199 gfxFloat size = NS_round(aMetrics.underlineSize);
200 gfxFloat minDescent = floor(offset + size + 0.5);
201 return std::max(minDescent, aMetrics.maxDescent);
204 static gfxFloat ComputeMaxAscent(const gfxFont::Metrics& aMetrics)
206 return floor(aMetrics.maxAscent + 0.5);
209 nscoord
210 nsFontMetrics::InternalLeading()
212 return ROUND_TO_TWIPS(GetMetrics().internalLeading);
215 nscoord
216 nsFontMetrics::ExternalLeading()
218 return ROUND_TO_TWIPS(GetMetrics().externalLeading);
221 nscoord
222 nsFontMetrics::EmHeight()
224 return ROUND_TO_TWIPS(GetMetrics().emHeight);
227 nscoord
228 nsFontMetrics::EmAscent()
230 return ROUND_TO_TWIPS(GetMetrics().emAscent);
233 nscoord
234 nsFontMetrics::EmDescent()
236 return ROUND_TO_TWIPS(GetMetrics().emDescent);
239 nscoord
240 nsFontMetrics::MaxHeight()
242 return CEIL_TO_TWIPS(ComputeMaxAscent(GetMetrics())) +
243 CEIL_TO_TWIPS(ComputeMaxDescent(GetMetrics(), mFontGroup));
246 nscoord
247 nsFontMetrics::MaxAscent()
249 return CEIL_TO_TWIPS(ComputeMaxAscent(GetMetrics()));
252 nscoord
253 nsFontMetrics::MaxDescent()
255 return CEIL_TO_TWIPS(ComputeMaxDescent(GetMetrics(), mFontGroup));
258 nscoord
259 nsFontMetrics::MaxAdvance()
261 return CEIL_TO_TWIPS(GetMetrics().maxAdvance);
264 nscoord
265 nsFontMetrics::AveCharWidth()
267 // Use CEIL instead of ROUND for consistency with GetMaxAdvance
268 return CEIL_TO_TWIPS(GetMetrics().aveCharWidth);
271 nscoord
272 nsFontMetrics::SpaceWidth()
274 return CEIL_TO_TWIPS(GetMetrics().spaceWidth);
277 int32_t
278 nsFontMetrics::GetMaxStringLength()
280 const gfxFont::Metrics& m = GetMetrics();
281 const double x = 32767.0 / m.maxAdvance;
282 int32_t len = (int32_t)floor(x);
283 return std::max(1, len);
286 nscoord
287 nsFontMetrics::GetWidth(const char* aString, uint32_t aLength,
288 nsRenderingContext *aContext)
290 if (aLength == 0)
291 return 0;
293 if (aLength == 1 && aString[0] == ' ')
294 return SpaceWidth();
296 StubPropertyProvider provider;
297 AutoTextRun textRun(this, aContext, aString, aLength);
298 return textRun.get() ?
299 NSToCoordRound(textRun->GetAdvanceWidth(0, aLength, &provider)) : 0;
302 nscoord
303 nsFontMetrics::GetWidth(const char16_t* aString, uint32_t aLength,
304 nsRenderingContext *aContext)
306 if (aLength == 0)
307 return 0;
309 if (aLength == 1 && aString[0] == ' ')
310 return SpaceWidth();
312 StubPropertyProvider provider;
313 AutoTextRun textRun(this, aContext, aString, aLength);
314 return textRun.get() ?
315 NSToCoordRound(textRun->GetAdvanceWidth(0, aLength, &provider)) : 0;
318 // Draw a string using this font handle on the surface passed in.
319 void
320 nsFontMetrics::DrawString(const char *aString, uint32_t aLength,
321 nscoord aX, nscoord aY,
322 nsRenderingContext *aContext)
324 if (aLength == 0)
325 return;
327 StubPropertyProvider provider;
328 AutoTextRun textRun(this, aContext, aString, aLength);
329 if (!textRun.get()) {
330 return;
332 gfxPoint pt(aX, aY);
333 if (mTextRunRTL) {
334 pt.x += textRun->GetAdvanceWidth(0, aLength, &provider);
336 textRun->Draw(aContext->ThebesContext(), pt, DrawMode::GLYPH_FILL, 0, aLength,
337 &provider, nullptr, nullptr);
340 void
341 nsFontMetrics::DrawString(const char16_t* aString, uint32_t aLength,
342 nscoord aX, nscoord aY,
343 nsRenderingContext *aContext,
344 nsRenderingContext *aTextRunConstructionContext)
346 if (aLength == 0)
347 return;
349 StubPropertyProvider provider;
350 AutoTextRun textRun(this, aTextRunConstructionContext, aString, aLength);
351 if (!textRun.get()) {
352 return;
354 gfxPoint pt(aX, aY);
355 if (mTextRunRTL) {
356 pt.x += textRun->GetAdvanceWidth(0, aLength, &provider);
358 textRun->Draw(aContext->ThebesContext(), pt, DrawMode::GLYPH_FILL, 0, aLength,
359 &provider, nullptr, nullptr);
362 static nsBoundingMetrics
363 GetTextBoundingMetrics(nsFontMetrics* aMetrics, const char16_t *aString, uint32_t aLength,
364 nsRenderingContext *aContext, gfxFont::BoundingBoxType aType)
366 if (aLength == 0)
367 return nsBoundingMetrics();
369 StubPropertyProvider provider;
370 AutoTextRun textRun(aMetrics, aContext, aString, aLength);
371 nsBoundingMetrics m;
372 if (textRun.get()) {
373 gfxTextRun::Metrics theMetrics =
374 textRun->MeasureText(0, aLength,
375 aType,
376 aContext->ThebesContext(), &provider);
378 m.leftBearing = NSToCoordFloor( theMetrics.mBoundingBox.X());
379 m.rightBearing = NSToCoordCeil( theMetrics.mBoundingBox.XMost());
380 m.ascent = NSToCoordCeil( -theMetrics.mBoundingBox.Y());
381 m.descent = NSToCoordCeil( theMetrics.mBoundingBox.YMost());
382 m.width = NSToCoordRound( theMetrics.mAdvanceWidth);
384 return m;
387 nsBoundingMetrics
388 nsFontMetrics::GetBoundingMetrics(const char16_t *aString, uint32_t aLength,
389 nsRenderingContext *aContext)
391 return GetTextBoundingMetrics(this, aString, aLength, aContext, gfxFont::TIGHT_HINTED_OUTLINE_EXTENTS);
395 nsBoundingMetrics
396 nsFontMetrics::GetInkBoundsForVisualOverflow(const char16_t *aString, uint32_t aLength,
397 nsRenderingContext *aContext)
399 return GetTextBoundingMetrics(this, aString, aLength, aContext, gfxFont::LOOSE_INK_EXTENTS);