| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/browser_font_resource_trusted.h" | 5 #include "content/child/browser_font_resource_trusted.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 : bidi_(NULL), | 64 : bidi_(NULL), |
| 65 num_runs_(0) { | 65 num_runs_(0) { |
| 66 StringVar* text_string = StringVar::FromPPVar(run.text); | 66 StringVar* text_string = StringVar::FromPPVar(run.text); |
| 67 if (!text_string) | 67 if (!text_string) |
| 68 return; // Leave num_runs_ = 0 so we'll do nothing. | 68 return; // Leave num_runs_ = 0 so we'll do nothing. |
| 69 text_ = base::UTF8ToUTF16(text_string->value()); | 69 text_ = base::UTF8ToUTF16(text_string->value()); |
| 70 | 70 |
| 71 if (run.override_direction) { | 71 if (run.override_direction) { |
| 72 // Skip autodetection. | 72 // Skip autodetection. |
| 73 num_runs_ = 1; | 73 num_runs_ = 1; |
| 74 override_run_ = WebTextRun(text_, PP_ToBool(run.rtl), true); | 74 override_run_ = WebTextRun(blink::WebString::fromUTF16(text_), |
| 75 PP_ToBool(run.rtl), true); |
| 75 } else { | 76 } else { |
| 76 bidi_ = ubidi_open(); | 77 bidi_ = ubidi_open(); |
| 77 UErrorCode uerror = U_ZERO_ERROR; | 78 UErrorCode uerror = U_ZERO_ERROR; |
| 78 ubidi_setPara(bidi_, text_.data(), text_.size(), run.rtl, NULL, &uerror); | 79 ubidi_setPara(bidi_, text_.data(), text_.size(), run.rtl, NULL, &uerror); |
| 79 if (U_SUCCESS(uerror)) | 80 if (U_SUCCESS(uerror)) |
| 80 num_runs_ = ubidi_countRuns(bidi_, &uerror); | 81 num_runs_ = ubidi_countRuns(bidi_, &uerror); |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 | 84 |
| 84 ~TextRunCollection() { | 85 ~TextRunCollection() { |
| 85 if (bidi_) | 86 if (bidi_) |
| 86 ubidi_close(bidi_); | 87 ubidi_close(bidi_); |
| 87 } | 88 } |
| 88 | 89 |
| 89 const base::string16& text() const { return text_; } | 90 const base::string16& text() const { return text_; } |
| 90 int num_runs() const { return num_runs_; } | 91 int num_runs() const { return num_runs_; } |
| 91 | 92 |
| 92 // Returns a WebTextRun with the info for the run at the given index. | 93 // Returns a WebTextRun with the info for the run at the given index. |
| 93 // The range covered by the run is in the two output params. | 94 // The range covered by the run is in the two output params. |
| 94 WebTextRun GetRunAt(int index, int32_t* run_start, int32_t* run_len) const { | 95 WebTextRun GetRunAt(int index, int32_t* run_start, int32_t* run_len) const { |
| 95 DCHECK(index < num_runs_); | 96 DCHECK(index < num_runs_); |
| 96 if (bidi_) { | 97 if (bidi_) { |
| 97 bool run_rtl = !!ubidi_getVisualRun(bidi_, index, run_start, run_len); | 98 bool run_rtl = !!ubidi_getVisualRun(bidi_, index, run_start, run_len); |
| 98 return WebTextRun(base::string16(&text_[*run_start], *run_len), | 99 return WebTextRun(blink::WebString::fromUTF16( |
| 100 base::string16(&text_[*run_start], *run_len)), |
| 99 run_rtl, true); | 101 run_rtl, true); |
| 100 } | 102 } |
| 101 | 103 |
| 102 // Override run, return the single one. | 104 // Override run, return the single one. |
| 103 DCHECK_EQ(0, index); | 105 DCHECK_EQ(0, index); |
| 104 *run_start = 0; | 106 *run_start = 0; |
| 105 *run_len = static_cast<int32_t>(text_.size()); | 107 *run_len = static_cast<int32_t>(text_.size()); |
| 106 return override_run_; | 108 return override_run_; |
| 107 } | 109 } |
| 108 | 110 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 121 | 123 |
| 122 DISALLOW_COPY_AND_ASSIGN(TextRunCollection); | 124 DISALLOW_COPY_AND_ASSIGN(TextRunCollection); |
| 123 }; | 125 }; |
| 124 | 126 |
| 125 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, | 127 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, |
| 126 WebTextRun* run) { | 128 WebTextRun* run) { |
| 127 StringVar* text_string = StringVar::FromPPVar(text.text); | 129 StringVar* text_string = StringVar::FromPPVar(text.text); |
| 128 if (!text_string) | 130 if (!text_string) |
| 129 return false; | 131 return false; |
| 130 | 132 |
| 131 *run = WebTextRun(base::UTF8ToUTF16(text_string->value()), | 133 *run = WebTextRun(blink::WebString::fromUTF8(text_string->value()), |
| 132 PP_ToBool(text.rtl), | 134 PP_ToBool(text.rtl), PP_ToBool(text.override_direction)); |
| 133 PP_ToBool(text.override_direction)); | |
| 134 return true; | 135 return true; |
| 135 } | 136 } |
| 136 | 137 |
| 137 // The PP_* version lacks "None", so is just one value shifted from the | 138 // The PP_* version lacks "None", so is just one value shifted from the |
| 138 // WebFontDescription version. These values are checked in | 139 // WebFontDescription version. These values are checked in |
| 139 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a | 140 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a |
| 140 // macro so it can also be used in the static_asserts. | 141 // macro so it can also be used in the static_asserts. |
| 141 #define PP_FAMILY_TO_WEB_FAMILY(f) \ | 142 #define PP_FAMILY_TO_WEB_FAMILY(f) \ |
| 142 static_cast<WebFontDescription::GenericFamily>(f + 1) | 143 static_cast<WebFontDescription::GenericFamily>(f + 1) |
| 143 | 144 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 case PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT: | 192 case PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT: |
| 192 default: | 193 default: |
| 193 resolved_family = GetFontFromMap(prefs.standard_font_family_map, | 194 resolved_family = GetFontFromMap(prefs.standard_font_family_map, |
| 194 kCommonScript); | 195 kCommonScript); |
| 195 break; | 196 break; |
| 196 } | 197 } |
| 197 } else { | 198 } else { |
| 198 // Use the exact font. | 199 // Use the exact font. |
| 199 resolved_family = base::UTF8ToUTF16(face_name->value()); | 200 resolved_family = base::UTF8ToUTF16(face_name->value()); |
| 200 } | 201 } |
| 201 result.family = resolved_family; | 202 result.family = blink::WebString::fromUTF16(resolved_family); |
| 202 | 203 |
| 203 result.genericFamily = PP_FAMILY_TO_WEB_FAMILY(font.family); | 204 result.genericFamily = PP_FAMILY_TO_WEB_FAMILY(font.family); |
| 204 | 205 |
| 205 if (font.size == 0) { | 206 if (font.size == 0) { |
| 206 // Resolve the default font size, using the resolved family to see if | 207 // Resolve the default font size, using the resolved family to see if |
| 207 // we should use the fixed or regular font size. It's difficult at this | 208 // we should use the fixed or regular font size. It's difficult at this |
| 208 // level to detect if the requested font is fixed width, so we only apply | 209 // level to detect if the requested font is fixed width, so we only apply |
| 209 // the alternate font size to the default fixed font family. | 210 // the alternate font size to the default fixed font family. |
| 210 if (base::ToLowerASCII(resolved_family) == | 211 if (base::ToLowerASCII(resolved_family) == |
| 211 base::ToLowerASCII(GetFontFromMap(prefs.fixed_font_family_map, | 212 base::ToLowerASCII(GetFontFromMap(prefs.fixed_font_family_map, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 272 |
| 272 PP_Bool BrowserFontResource_Trusted::Describe( | 273 PP_Bool BrowserFontResource_Trusted::Describe( |
| 273 PP_BrowserFont_Trusted_Description* description, | 274 PP_BrowserFont_Trusted_Description* description, |
| 274 PP_BrowserFont_Trusted_Metrics* metrics) { | 275 PP_BrowserFont_Trusted_Metrics* metrics) { |
| 275 if (description->face.type != PP_VARTYPE_UNDEFINED) | 276 if (description->face.type != PP_VARTYPE_UNDEFINED) |
| 276 return PP_FALSE; | 277 return PP_FALSE; |
| 277 | 278 |
| 278 // While converting the other way in PPFontDescToWebFontDesc we validated | 279 // While converting the other way in PPFontDescToWebFontDesc we validated |
| 279 // that the enums can be casted. | 280 // that the enums can be casted. |
| 280 WebFontDescription web_desc = font_->getFontDescription(); | 281 WebFontDescription web_desc = font_->getFontDescription(); |
| 281 description->face = StringVar::StringToPPVar(base::UTF16ToUTF8( | 282 description->face = StringVar::StringToPPVar(web_desc.family.utf8()); |
| 282 base::StringPiece16(web_desc.family))); | |
| 283 description->family = | 283 description->family = |
| 284 static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily); | 284 static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily); |
| 285 description->size = static_cast<uint32_t>(web_desc.size); | 285 description->size = static_cast<uint32_t>(web_desc.size); |
| 286 description->weight = static_cast<PP_BrowserFont_Trusted_Weight>( | 286 description->weight = static_cast<PP_BrowserFont_Trusted_Weight>( |
| 287 web_desc.weight); | 287 web_desc.weight); |
| 288 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; | 288 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; |
| 289 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; | 289 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; |
| 290 description->letter_spacing = static_cast<int32_t>(web_desc.letterSpacing); | 290 description->letter_spacing = static_cast<int32_t>(web_desc.letterSpacing); |
| 291 description->word_spacing = static_cast<int32_t>(web_desc.wordSpacing); | 291 description->word_spacing = static_cast<int32_t>(web_desc.wordSpacing); |
| 292 | 292 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 444 |
| 445 // Advance to the next run. Note that we avoid doing this for the last run | 445 // Advance to the next run. Note that we avoid doing this for the last run |
| 446 // since it's unnecessary, measuring text is slow, and most of the time | 446 // since it's unnecessary, measuring text is slow, and most of the time |
| 447 // there will be only one run anyway. | 447 // there will be only one run anyway. |
| 448 if (i != runs.num_runs() - 1) | 448 if (i != runs.num_runs() - 1) |
| 449 web_position.x += font_->calculateWidth(run); | 449 web_position.x += font_->calculateWidth(run); |
| 450 } | 450 } |
| 451 } | 451 } |
| 452 | 452 |
| 453 } // namespace content | 453 } // namespace content |
| OLD | NEW |