1 /* 2 * Copyright 2011 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.typography.font.sfntly.table.bitmap; 18 19 import java.util.Comparator; 20 21 /** 22 * An immutable class holding bitmap glyph information. 23 * 24 * @author Stuart Gill 25 */ 26 public final class BitmapGlyphInfo { 27 private final int glyphId; 28 private final boolean relative; 29 private final int blockOffset; 30 private final int startOffset; 31 private final int length; 32 private final int format; 33 34 /** 35 * Constructor for a relative located glyph. The glyph's position in the EBDT 36 * table is a combination of it's block offset and it's own start offset. 37 * 38 * @param glyphId the glyph id 39 * @param blockOffset the offset of the block to which the glyph belongs 40 * @param startOffset the offset of the glyph within the block 41 * @param length the byte length 42 * @param format the glyph image format 43 */ BitmapGlyphInfo(int glyphId, int blockOffset, int startOffset, int length, int format)44 public BitmapGlyphInfo(int glyphId, int blockOffset, int startOffset, int length, int format) { 45 this.glyphId = glyphId; 46 this.relative = true; 47 this.blockOffset = blockOffset; 48 this.startOffset = startOffset; 49 this.length = length; 50 this.format = format; 51 } 52 53 /** 54 * Constructor for an absolute located glyph. The glyph's position in the EBDT 55 * table is only given by it's own start offset. 56 * 57 * @param glyphId the glyph id 58 * @param startOffset the offset of the glyph within the block 59 * @param length the byte length 60 * @param format the glyph image format 61 */ BitmapGlyphInfo(int glyphId, int startOffset, int length, int format)62 public BitmapGlyphInfo(int glyphId, int startOffset, int length, int format) { 63 this.glyphId = glyphId; 64 this.relative = false; 65 this.blockOffset = 0; 66 this.startOffset = startOffset; 67 this.length = length; 68 this.format = format; 69 } 70 glyphId()71 public int glyphId() { 72 return this.glyphId; 73 } 74 relative()75 public boolean relative() { 76 return this.relative; 77 } 78 blockOffset()79 public int blockOffset() { 80 return this.blockOffset; 81 } 82 offset()83 public int offset() { 84 return this.blockOffset() + this.startOffset(); 85 } 86 startOffset()87 public int startOffset() { 88 return this.startOffset; 89 } 90 length()91 public int length() { 92 return this.length; 93 } 94 format()95 public int format() { 96 return this.format; 97 } 98 99 @Override hashCode()100 public int hashCode() { 101 final int prime = 31; 102 int result = 1; 103 result = prime * result + blockOffset; 104 result = prime * result + format; 105 result = prime * result + glyphId; 106 result = prime * result + length; 107 result = prime * result + startOffset; 108 return result; 109 } 110 111 @Override equals(Object obj)112 public boolean equals(Object obj) { 113 if (this == obj) { 114 return true; 115 } 116 if (obj == null) { 117 return false; 118 } 119 if (!(obj instanceof BitmapGlyphInfo)) { 120 return false; 121 } 122 BitmapGlyphInfo other = (BitmapGlyphInfo) obj; 123 if (this.format != other.format) { 124 return false; 125 } 126 if (this.glyphId != other.glyphId) { 127 return false; 128 } 129 if (this.length != other.length) { 130 return false; 131 } 132 if (this.offset() != other.offset()) { 133 return false; 134 } 135 return true; 136 } 137 138 public static final Comparator<BitmapGlyphInfo> StartOffsetComparator = 139 new StartOffsetComparatorClass(); 140 141 private static final class StartOffsetComparatorClass implements Comparator<BitmapGlyphInfo> { 142 @Override compare(BitmapGlyphInfo o1, BitmapGlyphInfo o2)143 public int compare(BitmapGlyphInfo o1, BitmapGlyphInfo o2) { 144 return (o1.startOffset - o2.startOffset); 145 } 146 } 147 } 148