1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/render_text_mac.h"
7 #include <ApplicationServices/ApplicationServices.h>
13 #include "base/mac/foundation_util.h"
14 #include "base/mac/scoped_cftyperef.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "skia/ext/skia_utils_mac.h"
20 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {
23 RenderTextMac::~RenderTextMac() {
26 Size
RenderTextMac::GetStringSize() {
31 int RenderTextMac::GetBaseline() {
33 return common_baseline_
;
36 SelectionModel
RenderTextMac::FindCursorPosition(const Point
& point
) {
37 // TODO(asvitkine): Implement this. http://crbug.com/131618
38 return SelectionModel();
41 std::vector
<RenderText::FontSpan
> RenderTextMac::GetFontSpansForTesting() {
46 std::vector
<RenderText::FontSpan
> spans
;
47 for (size_t i
= 0; i
< runs_
.size(); ++i
) {
48 gfx::Font
font(runs_
[i
].font_name
, runs_
[i
].text_size
);
49 const CFRange cf_range
= CTRunGetStringRange(runs_
[i
].ct_run
);
50 const ui::Range
range(cf_range
.location
,
51 cf_range
.location
+ cf_range
.length
);
52 spans
.push_back(RenderText::FontSpan(font
, range
));
58 SelectionModel
RenderTextMac::AdjacentCharSelectionModel(
59 const SelectionModel
& selection
,
60 VisualCursorDirection direction
) {
61 // TODO(asvitkine): Implement this. http://crbug.com/131618
62 return SelectionModel();
65 SelectionModel
RenderTextMac::AdjacentWordSelectionModel(
66 const SelectionModel
& selection
,
67 VisualCursorDirection direction
) {
68 // TODO(asvitkine): Implement this. http://crbug.com/131618
69 return SelectionModel();
72 ui::Range
RenderTextMac::GetGlyphBounds(size_t index
) {
73 // TODO(asvitkine): Implement this. http://crbug.com/131618
77 std::vector
<Rect
> RenderTextMac::GetSubstringBounds(const ui::Range
& range
) {
78 // TODO(asvitkine): Implement this. http://crbug.com/131618
79 return std::vector
<Rect
>();
82 size_t RenderTextMac::TextIndexToLayoutIndex(size_t index
) const {
83 // TODO(asvitkine): Implement this. http://crbug.com/131618
87 size_t RenderTextMac::LayoutIndexToTextIndex(size_t index
) const {
88 // TODO(asvitkine): Implement this. http://crbug.com/131618
92 bool RenderTextMac::IsCursorablePosition(size_t position
) {
93 // TODO(asvitkine): Implement this. http://crbug.com/131618
97 void RenderTextMac::ResetLayout() {
104 void RenderTextMac::EnsureLayout() {
110 const Font
& font
= GetPrimaryFont();
111 base::ScopedCFTypeRef
<CFStringRef
> font_name_cf_string(
112 base::SysUTF8ToCFStringRef(font
.GetFontName()));
113 base::ScopedCFTypeRef
<CTFontRef
> ct_font(
114 CTFontCreateWithName(font_name_cf_string
, font
.GetFontSize(), NULL
));
116 const void* keys
[] = { kCTFontAttributeName
};
117 const void* values
[] = { ct_font
};
118 base::ScopedCFTypeRef
<CFDictionaryRef
> attributes(
119 CFDictionaryCreate(NULL
,
124 &kCFTypeDictionaryValueCallBacks
));
126 base::ScopedCFTypeRef
<CFStringRef
> cf_text(
127 base::SysUTF16ToCFStringRef(text()));
128 base::ScopedCFTypeRef
<CFAttributedStringRef
> attr_text(
129 CFAttributedStringCreate(NULL
, cf_text
, attributes
));
130 base::ScopedCFTypeRef
<CFMutableAttributedStringRef
> attr_text_mutable(
131 CFAttributedStringCreateMutableCopy(NULL
, 0, attr_text
));
133 // TODO(asvitkine|msw): Respect GetTextDirection(), which may not match the
134 // natural text direction. See kCTTypesetterOptionForcedEmbeddingLevel, etc.
136 ApplyStyles(attr_text_mutable
, ct_font
);
137 line_
.reset(CTLineCreateWithAttributedString(attr_text_mutable
));
142 // TODO(asvitkine): Consider using CTLineGetBoundsWithOptions() on 10.8+.
143 double width
= CTLineGetTypographicBounds(line_
, &ascent
, &descent
, &leading
);
144 // Ensure ascent and descent are not smaller than ones of the font list.
145 // Keep them tall enough to draw often-used characters.
146 // For example, if a text field contains a Japanese character, which is
147 // smaller than Latin ones, and then later a Latin one is inserted, this
148 // ensures that the text baseline does not shift.
149 CGFloat font_list_height
= font_list().GetHeight();
150 CGFloat font_list_baseline
= font_list().GetBaseline();
151 ascent
= std::max(ascent
, font_list_baseline
);
152 descent
= std::max(descent
, font_list_height
- font_list_baseline
);
153 string_size_
= Size(width
, ascent
+ descent
+ leading
);
154 common_baseline_
= ascent
;
157 void RenderTextMac::DrawVisualText(Canvas
* canvas
) {
162 internal::SkiaTextRenderer
renderer(canvas
);
163 ApplyFadeEffects(&renderer
);
164 ApplyTextShadows(&renderer
);
166 for (size_t i
= 0; i
< runs_
.size(); ++i
) {
167 const TextRun
& run
= runs_
[i
];
168 renderer
.SetForegroundColor(run
.foreground
);
169 renderer
.SetTextSize(run
.text_size
);
170 renderer
.SetFontFamilyWithStyle(run
.font_name
, run
.font_style
);
171 renderer
.DrawPosText(&run
.glyph_positions
[0], &run
.glyphs
[0],
173 renderer
.DrawDecorations(run
.origin
.x(), run
.origin
.y(), run
.width
,
174 run
.underline
, run
.strike
, run
.diagonal_strike
);
178 RenderTextMac::TextRun::TextRun()
180 origin(SkPoint::Make(0, 0)),
182 font_style(Font::NORMAL
),
184 foreground(SK_ColorBLACK
),
187 diagonal_strike(false) {
190 RenderTextMac::TextRun::~TextRun() {
193 void RenderTextMac::ApplyStyles(CFMutableAttributedStringRef attr_string
,
195 // Temporarily apply composition underlines and selection colors.
196 ApplyCompositionAndSelectionStyles();
198 // Note: CFAttributedStringSetAttribute() does not appear to retain the values
199 // passed in, as can be verified via CFGetRetainCount(). To ensure the
200 // attribute objects do not leak, they are saved to |attributes_|.
201 // Clear the attributes storage.
202 attributes_
.reset(CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
));
204 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_StringAttributes_Ref/Reference/reference.html
205 internal::StyleIterator
style(colors(), styles());
206 const size_t layout_text_length
= GetLayoutText().length();
207 for (size_t i
= 0, end
= 0; i
< layout_text_length
; i
= end
) {
208 end
= TextIndexToLayoutIndex(style
.GetRange().end());
209 const CFRange range
= CFRangeMake(i
, end
- i
);
210 base::ScopedCFTypeRef
<CGColorRef
> foreground(
211 gfx::CGColorCreateFromSkColor(style
.color()));
212 CFAttributedStringSetAttribute(attr_string
, range
,
213 kCTForegroundColorAttributeName
, foreground
);
214 CFArrayAppendValue(attributes_
, foreground
);
216 if (style
.style(UNDERLINE
)) {
217 CTUnderlineStyle value
= kCTUnderlineStyleSingle
;
218 base::ScopedCFTypeRef
<CFNumberRef
> underline_value(
219 CFNumberCreate(NULL
, kCFNumberSInt32Type
, &value
));
220 CFAttributedStringSetAttribute(attr_string
, range
,
221 kCTUnderlineStyleAttributeName
,
223 CFArrayAppendValue(attributes_
, underline_value
);
226 const int traits
= (style
.style(BOLD
) ? kCTFontBoldTrait
: 0) |
227 (style
.style(ITALIC
) ? kCTFontItalicTrait
: 0);
229 base::ScopedCFTypeRef
<CTFontRef
> styled_font(
230 CTFontCreateCopyWithSymbolicTraits(font
, 0.0, NULL
, traits
, traits
));
231 // TODO(asvitkine): Handle |styled_font| == NULL case better.
233 CFAttributedStringSetAttribute(attr_string
, range
, kCTFontAttributeName
,
235 CFArrayAppendValue(attributes_
, styled_font
);
239 style
.UpdatePosition(LayoutIndexToTextIndex(end
));
242 // Undo the temporarily applied composition underlines and selection colors.
243 UndoCompositionAndSelectionStyles();
246 void RenderTextMac::ComputeRuns() {
249 CFArrayRef ct_runs
= CTLineGetGlyphRuns(line_
);
250 const CFIndex ct_runs_count
= CFArrayGetCount(ct_runs
);
252 // TODO(asvitkine): Don't use GetTextOffset() until draw time, since it may be
253 // updated based on alignment changes without resetting the layout.
254 gfx::Vector2d text_offset
= GetTextOffset();
255 // Skia will draw glyphs with respect to the baseline.
256 text_offset
+= gfx::Vector2d(0, common_baseline_
);
258 const SkScalar x
= SkIntToScalar(text_offset
.x());
259 const SkScalar y
= SkIntToScalar(text_offset
.y());
260 SkPoint run_origin
= SkPoint::Make(x
, y
);
262 const CFRange empty_cf_range
= CFRangeMake(0, 0);
263 for (CFIndex i
= 0; i
< ct_runs_count
; ++i
) {
265 base::mac::CFCast
<CTRunRef
>(CFArrayGetValueAtIndex(ct_runs
, i
));
266 const size_t glyph_count
= CTRunGetGlyphCount(ct_run
);
267 const double run_width
=
268 CTRunGetTypographicBounds(ct_run
, empty_cf_range
, NULL
, NULL
, NULL
);
269 if (glyph_count
== 0) {
270 run_origin
.offset(run_width
, 0);
274 runs_
.push_back(TextRun());
275 TextRun
* run
= &runs_
.back();
276 run
->ct_run
= ct_run
;
277 run
->origin
= run_origin
;
278 run
->width
= run_width
;
279 run
->glyphs
.resize(glyph_count
);
280 CTRunGetGlyphs(ct_run
, empty_cf_range
, &run
->glyphs
[0]);
281 // CTRunGetGlyphs() sometimes returns glyphs with value 65535 and zero
282 // width (this has been observed at the beginning of a string containing
283 // Arabic content). Passing these to Skia will trigger an assertion;
284 // instead set their values to 0.
285 for (size_t glyph
= 0; glyph
< glyph_count
; glyph
++) {
286 if (run
->glyphs
[glyph
] == 65535)
287 run
->glyphs
[glyph
] = 0;
290 run
->glyph_positions
.resize(glyph_count
);
291 const CGPoint
* positions_ptr
= CTRunGetPositionsPtr(ct_run
);
292 std::vector
<CGPoint
> positions
;
293 if (positions_ptr
== NULL
) {
294 positions
.resize(glyph_count
);
295 CTRunGetPositions(ct_run
, empty_cf_range
, &positions
[0]);
296 positions_ptr
= &positions
[0];
298 for (size_t glyph
= 0; glyph
< glyph_count
; glyph
++) {
299 SkPoint
* point
= &run
->glyph_positions
[glyph
];
300 point
->set(x
+ SkDoubleToScalar(positions_ptr
[glyph
].x
),
301 y
+ SkDoubleToScalar(positions_ptr
[glyph
].y
));
304 // TODO(asvitkine): Style boundaries are not necessarily per-run. Handle
305 // this better. Also, support strike and diagonal_strike.
306 CFDictionaryRef attributes
= CTRunGetAttributes(ct_run
);
308 base::mac::GetValueFromDictionary
<CTFontRef
>(attributes
,
309 kCTFontAttributeName
);
310 base::ScopedCFTypeRef
<CFStringRef
> font_name_ref(
311 CTFontCopyFamilyName(ct_font
));
312 run
->font_name
= base::SysCFStringRefToUTF8(font_name_ref
);
313 run
->text_size
= CTFontGetSize(ct_font
);
315 CTFontSymbolicTraits traits
= CTFontGetSymbolicTraits(ct_font
);
316 if (traits
& kCTFontBoldTrait
)
317 run
->font_style
|= Font::BOLD
;
318 if (traits
& kCTFontItalicTrait
)
319 run
->font_style
|= Font::ITALIC
;
321 const CGColorRef foreground
=
322 base::mac::GetValueFromDictionary
<CGColorRef
>(
323 attributes
, kCTForegroundColorAttributeName
);
325 run
->foreground
= gfx::CGColorRefToSkColor(foreground
);
327 const CFNumberRef underline
=
328 base::mac::GetValueFromDictionary
<CFNumberRef
>(
329 attributes
, kCTUnderlineStyleAttributeName
);
330 CTUnderlineStyle value
= kCTUnderlineStyleNone
;
331 if (underline
&& CFNumberGetValue(underline
, kCFNumberSInt32Type
, &value
))
332 run
->underline
= (value
== kCTUnderlineStyleSingle
);
334 run_origin
.offset(run_width
, 0);
339 RenderText
* RenderText::CreateInstance() {
340 return new RenderTextMac
;