| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 '''Gatherer for <structure type="chrome_scaled_image">. | 6 '''Gatherer for <structure type="chrome_scaled_image">. |
| 7 ''' | 7 ''' |
| 8 | 8 |
| 9 import os | 9 import os |
| 10 import struct | 10 import struct |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 def _FindInputFile(self): | 93 def _FindInputFile(self): |
| 94 output_context = self.grd_node.GetRoot().output_context | 94 output_context = self.grd_node.GetRoot().output_context |
| 95 match = self.split_context_re_.match(output_context) | 95 match = self.split_context_re_.match(output_context) |
| 96 if not match: | 96 if not match: |
| 97 raise exception.MissingMandatoryAttribute( | 97 raise exception.MissingMandatoryAttribute( |
| 98 'All <output> nodes must have an appropriate context attribute' | 98 'All <output> nodes must have an appropriate context attribute' |
| 99 ' (e.g. context="touch_200_percent")') | 99 ' (e.g. context="touch_200_percent")') |
| 100 req_layout, req_scale = match.group(1), int(match.group(2)) | 100 req_layout, req_scale = match.group(1), int(match.group(2)) |
| 101 | 101 |
| 102 layouts = [req_layout] | 102 layouts = [req_layout] |
| 103 if 'default' not in layouts: | 103 try_default_layout = self.grd_node.GetRoot().fallback_to_default_layout |
| 104 if try_default_layout and 'default' not in layouts: |
| 104 layouts.append('default') | 105 layouts.append('default') |
| 106 |
| 107 # TODO(tdanderson): Search in descending order of all image scales |
| 108 # instead of immediately falling back to 100. |
| 109 # See crbug.com/503643. |
| 105 scales = [req_scale] | 110 scales = [req_scale] |
| 106 try_low_res = self.grd_node.FindBooleanAttribute( | 111 try_low_res = self.grd_node.FindBooleanAttribute( |
| 107 'fallback_to_low_resolution', default=False, skip_self=False) | 112 'fallback_to_low_resolution', default=False, skip_self=False) |
| 108 if try_low_res and 100 not in scales: | 113 if try_low_res and 100 not in scales: |
| 109 scales.append(100) | 114 scales.append(100) |
| 115 |
| 110 for layout in layouts: | 116 for layout in layouts: |
| 111 for scale in scales: | 117 for scale in scales: |
| 112 dir = '%s_%s_percent' % (layout, scale) | 118 dir = '%s_%s_percent' % (layout, scale) |
| 113 path = os.path.join(dir, self.rc_file) | 119 path = os.path.join(dir, self.rc_file) |
| 114 if os.path.exists(self.grd_node.ToRealPath(path)): | 120 if os.path.exists(self.grd_node.ToRealPath(path)): |
| 115 return path, scale, req_scale | 121 return path, scale, req_scale |
| 116 # If we get here then the file is missing, so fail. | 122 |
| 123 if not try_default_layout: |
| 124 # The file was not found in the specified output context and it was |
| 125 # explicitly indicated that the default context should not be searched |
| 126 # as a fallback, so return an empty path. |
| 127 return None, 100, req_scale |
| 128 |
| 129 # The file was found in neither the specified context nor the default |
| 130 # context, so raise an exception. |
| 117 dir = "%s_%s_percent" % (_MakeBraceGlob(layouts), | 131 dir = "%s_%s_percent" % (_MakeBraceGlob(layouts), |
| 118 _MakeBraceGlob(map(str, scales))) | 132 _MakeBraceGlob(map(str, scales))) |
| 119 raise exception.FileNotFound( | 133 raise exception.FileNotFound( |
| 120 'Tried ' + self.grd_node.ToRealPath(os.path.join(dir, self.rc_file))) | 134 'Tried ' + self.grd_node.ToRealPath(os.path.join(dir, self.rc_file))) |
| 121 | 135 |
| 122 def GetInputPath(self): | 136 def GetInputPath(self): |
| 123 path, scale, req_scale = self._FindInputFile() | 137 path, scale, req_scale = self._FindInputFile() |
| 124 return path | 138 return path |
| 125 | 139 |
| 126 def Parse(self): | 140 def Parse(self): |
| 127 pass | 141 pass |
| 128 | 142 |
| 129 def GetTextualIds(self): | 143 def GetTextualIds(self): |
| 130 return [self.extkey] | 144 return [self.extkey] |
| 131 | 145 |
| 132 def GetData(self, *args): | 146 def GetData(self, *args): |
| 133 path, scale, req_scale = self._FindInputFile() | 147 path, scale, req_scale = self._FindInputFile() |
| 148 if path is None: |
| 149 return None |
| 150 |
| 134 data = util.ReadFile(self.grd_node.ToRealPath(path), util.BINARY) | 151 data = util.ReadFile(self.grd_node.ToRealPath(path), util.BINARY) |
| 135 data = _RescaleImage(data, scale, req_scale) | 152 data = _RescaleImage(data, scale, req_scale) |
| 136 data = _MoveSpecialChunksToFront(data) | 153 data = _MoveSpecialChunksToFront(data) |
| 137 return data | 154 return data |
| 138 | 155 |
| 139 def Translate(self, *args, **kwargs): | 156 def Translate(self, *args, **kwargs): |
| 140 return self.GetData() | 157 return self.GetData() |
| OLD | NEW |