Categories
discuss

downloadable fonts – can’t download some google fonts

I am playing with Downloadable fonts api. I downloaded the Google sample application and also incorporated the code into my project. Both run successfully but some fonts consistently fail to download both from my app and from the sample app.

I use FontsContractCompat.requestFont and gets a callback to onTypefaceRequestFailed(int reason) with reason 1. The documentation says it means “FAIL_REASON_FONT_NOT_FOUND”. I assume those fonts should exist because: 1) They appear in an xml file that comes with the sample app, 2) They appear in the online list of Google Fonts, and 3) They return from the developer web api (https://www.googleapis.com/webfonts/v1/webfonts?key=)

Here is the list of failed fonts: Angkor Archivo Asap Condensed Baloo Bhaijaan Baloo Tammudu Battambang Bayon Bellefair BioRhyme Expanded Bokor Cabin Condensed Chau Philomene One Chenla Content Dangrek Encode Sans Encode Sans Condensed Encode Sans Expanded Encode Sans Semi Condensed Encode Sans Semi Expanded Fasthand Faustina Freehand Hanuman Khmer Koulen Libre Barcode 128 Libre Barcode 128 Text Libre Barcode 39 Libre Barcode 39 Extended Libre Barcode 39 Extended Text Libre Barcode 39 Text Mada Manuale Metal Moul Moulpali Mukta Mukta Mahee Mukta Malar Nokora Open Sans Condensed Preahvihear Roboto Condensed Saira Saira Condensed Saira Extra Condensed Saira Semi Condensed Sedgwick Ave Sedgwick Ave Display Siemreap Suwannaphum Taprom Ubuntu Condensed Zilla Slab Zilla Slab Highlight

Answer

It’s definitely weird. I observed that many (but not all) of those fonts don’t have a “latin” or “latin-ext” subset, so that seemed a way to auto-filter them. I threw together a little python2 script that asks the API for the whole font list, then filters them for “latin” and outputs whats left as a new font-families resource file, which you can redirect to family_names.xml.

Usage: fontlist.py <API_KEY>

#!/usr/bin/python
# fontlist.py by fat-tire
#
# Collects Google provider latin & latin-ext font families and creates a replacement for
# https://github.com/googlesamples/android-DownloadableFonts/blob/master/app/src/main/res/values/family_names.xml
#
# See https://developers.google.com/fonts/docs/developer_api for more info on the Google Fonts API
#
# Usage:     fontlist.py <API_KEY> > family_names.xml

import sys, urllib2, json

if len(sys.argv) != 2:
    print "Usage:"
    print "  fontlist.py <API_KEY> > family_names.xml"
    print "No Google Fonts API key?  Get one at https://developers.google.com/fonts/docs/developer_api#APIKey"
    sys.exit(0)

APIKEY=sys.argv[1]
url="https://www.googleapis.com/webfonts/v1/webfonts?key="

opener = urllib2.build_opener()
try:
    request = urllib2.Request(url + APIKEY)
    conn = opener.open(request)
except Exception, e:
    print "Whoopsie.  Got a " + str(e.code) + " " + str(e.reason) + " error.  You sure that API is legit?"
    sys.exit(1)
data = json.loads(conn.read())

count = 0
items = data["items"]

print "<?xml version="1.0" encoding="utf-8"?>"
print "<!-- Collected from " + url+APIKEY + " -->"
print """<resources>
    <string-array name="family_names">"""
for key in items:
    if "latin" in key["subsets"]:
        print " "*10 + "<item>" + key["family"] + "</item>"
        count = count + 1
print """    <!--Total:  """ + str(count) + """-->
    </array>
</resources>"""
sys.exit(0)

This script outputs a family_names.xml which is interesting. If you compare it to the one provided by google, it does black-out most of the fonts listed in the question. But it doesn’t get all of them, including the “Zilla”, “Ubuntu”, “Barcode” and “Encode” fonts. Maybe there’s something these fonts also have in common that can be used to filter the list further?

Interestingly, the generated list also includes new fonts NOT in the github list, including:

  • VolKorn SC
  • Spectral
  • Spectral SC
  • Sedgewick Ave
  • Sedgewick Ave Display

….”Barlow”, “Bellefair”, and a bunch more. And some of these fonts do seem to work with Android.

So I’m guessing that the list in that demo file is just old. Maybe there were licensing issues or technical issues that made it necessary to switch up the list.

Regardless it might be worth submitting a pull request with a newer and more up-to-date list that removes the no-longer provided fonts and adds in the ones that the API does offer that are tested and known to work with the provider.

Categories
discuss

Is document.documentElement always defined and always the html element?

I’d like to modify the <html> element from a script that’s inside the <head> element of an HTML page. I don’t need to access/modify any of <html>‘s children, just <html> itself.

Would that script in some cases need to wait for DOMContentLoaded, or will document.documentElement always be defined? Will document.documentElement always be the <html> element?

For example:

<html>
  <head>
    <script type="text/javascript">
      document.documentElement.style.foo = 'bar';
    </script>
  </head>
  <body>
    <!-- ... -->
  </body>
</html>

Answer

Yes, it’s always defined.

The documentElement property returns the documentElement of the document, as an Element object. For HTML documents the returned object is the <html> element. This property is read-only.

https://www.w3schools.com/jsref/prop_document_documentelement.asp

While document.body can be null if script is running from <head> tag and page is not fully loaded, <html> element is always present in html document.

Categories
discuss

Android Studio 3.0 Go to File feature does not work

I used to jump to a file using the combination shift + command + o until Android Studio gets upgraded to 3.0. Now the combination does not work, for instance it even can not find string.xml. I know that I might use double shift instead of it but it also can not find the string.xml file. I need to go to the file manually.

you can track the issue via this link https://issuetracker.google.com/issues/68742341

UPDATE: I think there is some problem with the ide for sure but they said that it did not reproduce. However I did what @LazyOne said and it worked.

Answer

The fact that search for files / Navigate to suddenly stopped working suggests that indexes/caches that IDE uses might be somehow outdated/corrupted.

In such case using File | Invalidate Caches... and restarting IDE should help. IDE will mark all caches/indexes as “outdated” and will re-index the project on next launch. P.S. The same can be achieved by manually deleting indexes/caches folders while IDE is closed.

Please note that Local History will also be dropped (in case you are using this functionality) as in current implementation it’s tightly linked to the same subsystem.

Categories
discuss

CQRS and Event Sourcing Difference

What is the difference between CQRS (Command Query Responsibility Segregation) and Event Sourcing?

I believe Event Sourcing is a type of CQRS. What distinguishes each, and what makes Event Sourcing different from other types of CQRS?

Thanks,

Answer

CQRS

CQRS was introduced by Greg Young; his explanation in 2010

CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation, a command is any method that mutates state and a query is any method that returns a value).

Typically, this means that each of the objects will use a different representation of the data, so as to be fit for purpose. Common language here is to refer to a “write model” and a “read model”. It is usually the case that changes will be made to the write model first, and propagate asynchronously to the read model.

As it happens, there’s nothing magic about the number “2”; you could just as easily have 3 different representations as two.

The key benefit here is that you can tune the data structures for your read use cases and your write use cases independently.

Event Sourcing

Event sourcing a pattern for recording of state in a non-destructive way. Each change of state is appended to a log. Because the changes are non-destructive, we preserve the ability to answer queries about the state of the object at any point in its life cycle.

The use of events affords a more efficient use of storage space (relative to storing the complete representation of the state after each change) while retaining the semantic intent of the change (relative to just storing diffs)

Greg described event sourcing this way

storing current state as a series of events and rebuilding state within the system by replaying that series of events

CQRS + Event Sourcing

These techniques are frequently paired together, because the log, as a conceptual data structure, is not particularly efficient at supporting queries. You will normally want to condense the log into some other representation that is more suitable for reads (which are likely to be much more frequent than writes).

Thus, in the CQRS pattern, we typically use a log as the persistence model, and then from that log create query suitable representations of the object, and cache those representations such that queries can be supported quickly (accepting the compromise that the representation used in the query will not always reflect the absolute latest information available).

Categories
discuss

java.lang.UnsupportedOperationException when mocking java.nio.ByteBuffer class

I am trying to mock ByteBuffer class in java.nio with Mockito for testing in JUnit. I get a java.lang.UnsupportedOperationException

My code looks like –

class TestClass {

    @Mock
    private ByteBuffer byteBuffer

     @Before
     public void setup() {
         Mockito.when(byteBuffer.array()).thenReturn("some-string".getBytes()); //this line throws java.lang.UnsupportedOperationException
     }
}

How differently should I mock the array method for this to work? I am using Java 8.

Answer

Like in the comment from Sotirios Delimanolis you don’t need to mock this class or classes that are easily composed from primitives like byte [].

There are a number of different test doubles (fakes, spies etc.) apart from mocks and this is a better case for a fake than a mock.

Just use:

byteBuffer = ByteBuffer.wrap("some-string".getBytes());
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..